chan_zap.c 311 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * Zaptel Pseudo TDM interface
  5. *
  6. * Copyright (C) 2003-2004, Digium, Inc.
  7. *
  8. * Mark Spencer <markster@digium.com>
  9. *
  10. * This program is free software, distributed under the terms of
  11. * the GNU General Public License
  12. */
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <asterisk/lock.h>
  16. #include <asterisk/channel.h>
  17. #include <asterisk/channel_pvt.h>
  18. #include <asterisk/config.h>
  19. #include <asterisk/logger.h>
  20. #include <asterisk/module.h>
  21. #include <asterisk/pbx.h>
  22. #include <asterisk/options.h>
  23. #include <asterisk/file.h>
  24. #include <asterisk/ulaw.h>
  25. #include <asterisk/alaw.h>
  26. #include <asterisk/callerid.h>
  27. #include <asterisk/adsi.h>
  28. #include <asterisk/cli.h>
  29. #include <asterisk/cdr.h>
  30. #include <asterisk/features.h>
  31. #include <asterisk/musiconhold.h>
  32. #include <asterisk/say.h>
  33. #include <asterisk/tdd.h>
  34. #include <asterisk/app.h>
  35. #include <asterisk/dsp.h>
  36. #include <asterisk/astdb.h>
  37. #include <asterisk/manager.h>
  38. #include <asterisk/causes.h>
  39. #include <asterisk/term.h>
  40. #include <asterisk/utils.h>
  41. #include <sys/signal.h>
  42. #include <errno.h>
  43. #include <stdlib.h>
  44. #include <stdint.h>
  45. #include <unistd.h>
  46. #include <sys/ioctl.h>
  47. #ifdef __linux__
  48. #include <linux/zaptel.h>
  49. #else
  50. #include <zaptel.h>
  51. #endif /* __linux__ */
  52. #include <math.h>
  53. #include <tonezone.h>
  54. #include <ctype.h>
  55. #ifdef ZAPATA_PRI
  56. #include <libpri.h>
  57. #ifndef PRI_NSF_NONE
  58. #error "You need newer libpri"
  59. #endif
  60. #endif
  61. #ifdef ZAPATA_R2
  62. #include <libmfcr2.h>
  63. #endif
  64. #include "../asterisk.h"
  65. #ifndef ZT_SIG_EM_E1
  66. #error "Your zaptel is too old. please cvs update"
  67. #endif
  68. #ifndef ZT_TONEDETECT
  69. /* Work around older code with no tone detect */
  70. #define ZT_EVENT_DTMFDOWN 0
  71. #define ZT_EVENT_DTMFUP 0
  72. #endif
  73. /*
  74. * Define ZHONE_HACK to cause us to go off hook and then back on hook when
  75. * the user hangs up to reset the state machine so ring works properly.
  76. * This is used to be able to support kewlstart by putting the zhone in
  77. * groundstart mode since their forward disconnect supervision is entirely
  78. * broken even though their documentation says it isn't and their support
  79. * is entirely unwilling to provide any assistance with their channel banks
  80. * even though their web site says they support their products for life.
  81. */
  82. /* #define ZHONE_HACK */
  83. /*
  84. * Define if you want to check the hook state for an FXO (FXS signalled) interface
  85. * before dialing on it. Certain FXO interfaces always think they're out of
  86. * service with this method however.
  87. */
  88. /* #define ZAP_CHECK_HOOKSTATE */
  89. /* Typically, how many rings before we should send Caller*ID */
  90. #define DEFAULT_CIDRINGS 1
  91. #define CHANNEL_PSEUDO -12
  92. #define AST_LAW(p) (((p)->law == ZT_LAW_ALAW) ? AST_FORMAT_ALAW : AST_FORMAT_ULAW)
  93. static char *desc = "Zapata Telephony"
  94. #ifdef ZAPATA_PRI
  95. " w/PRI"
  96. #endif
  97. #ifdef ZAPATA_R2
  98. " w/R2"
  99. #endif
  100. ;
  101. static char *tdesc = "Zapata Telephony Driver"
  102. #ifdef ZAPATA_PRI
  103. " w/PRI"
  104. #endif
  105. #ifdef ZAPATA_R2
  106. " w/R2"
  107. #endif
  108. ;
  109. static char *type = "Zap";
  110. static char *typecompat = "Tor"; /* Retain compatibility with chan_tor */
  111. static char *config = "zapata.conf";
  112. #define SIG_EM ZT_SIG_EM
  113. #define SIG_EMWINK (0x100000 | ZT_SIG_EM)
  114. #define SIG_FEATD (0x200000 | ZT_SIG_EM)
  115. #define SIG_FEATDMF (0x400000 | ZT_SIG_EM)
  116. #define SIG_FEATB (0x800000 | ZT_SIG_EM)
  117. #define SIG_E911 (0x1000000 | ZT_SIG_EM)
  118. #define SIG_FXSLS ZT_SIG_FXSLS
  119. #define SIG_FXSGS ZT_SIG_FXSGS
  120. #define SIG_FXSKS ZT_SIG_FXSKS
  121. #define SIG_FXOLS ZT_SIG_FXOLS
  122. #define SIG_FXOGS ZT_SIG_FXOGS
  123. #define SIG_FXOKS ZT_SIG_FXOKS
  124. #define SIG_PRI ZT_SIG_CLEAR
  125. #define SIG_R2 ZT_SIG_CAS
  126. #define SIG_SF ZT_SIG_SF
  127. #define SIG_SFWINK (0x100000 | ZT_SIG_SF)
  128. #define SIG_SF_FEATD (0x200000 | ZT_SIG_SF)
  129. #define SIG_SF_FEATDMF (0x400000 | ZT_SIG_SF)
  130. #define SIG_SF_FEATB (0x800000 | ZT_SIG_SF)
  131. #define SIG_EM_E1 ZT_SIG_EM_E1
  132. #define SIG_GR303FXOKS (0x100000 | ZT_SIG_FXOKS)
  133. #define SIG_GR303FXSKS (0x200000 | ZT_SIG_FXSKS)
  134. #define NUM_SPANS 32
  135. #define NUM_DCHANS 4 /* No more than 4 d-channels */
  136. #define MAX_CHANNELS 672 /* No more than a DS3 per trunk group */
  137. #define RESET_INTERVAL 3600 /* How often (in seconds) to reset unused channels */
  138. #define CHAN_PSEUDO -2
  139. #define DCHAN_PROVISIONED (1 << 0)
  140. #define DCHAN_NOTINALARM (1 << 1)
  141. #define DCHAN_UP (1 << 2)
  142. #define DCHAN_AVAILABLE (DCHAN_PROVISIONED | DCHAN_NOTINALARM | DCHAN_UP)
  143. #define zt_close(fd) if(fd > 0) close(fd);
  144. static char context[AST_MAX_EXTENSION] = "default";
  145. static char callerid[256] = "";
  146. static char language[MAX_LANGUAGE] = "";
  147. static char musicclass[MAX_LANGUAGE] = "";
  148. static char progzone[10]= "";
  149. static int usedistinctiveringdetection = 0;
  150. static int use_callerid = 1;
  151. static int cid_signalling = CID_SIG_BELL;
  152. static int cid_start = CID_START_RING;
  153. static int zaptrcallerid = 0;
  154. static int cur_signalling = -1;
  155. static unsigned int cur_group = 0;
  156. static unsigned int cur_callergroup = 0;
  157. static unsigned int cur_pickupgroup = 0;
  158. static int relaxdtmf = 0;
  159. static int immediate = 0;
  160. static int stripmsd = 0;
  161. static int callwaiting = 0;
  162. static int callwaitingcallerid = 0;
  163. static int hidecallerid = 0;
  164. static int restrictcid = 0;
  165. static int use_callingpres = 0;
  166. static int callreturn = 0;
  167. static int threewaycalling = 0;
  168. static int transfer = 0;
  169. static int cancallforward = 0;
  170. static float rxgain = 0.0;
  171. static float txgain = 0.0;
  172. static int tonezone = -1;
  173. static int echocancel;
  174. static int echotraining;
  175. static int pulse;
  176. static int echocanbridged = 0;
  177. static int busydetect = 0;
  178. static int busycount = 3;
  179. static int callprogress = 0;
  180. static char accountcode[20] = "";
  181. static char mailbox[AST_MAX_EXTENSION];
  182. static int amaflags = 0;
  183. static int adsi = 0;
  184. static int numbufs = 4;
  185. static int cur_prewink = -1;
  186. static int cur_preflash = -1;
  187. static int cur_wink = -1;
  188. static int cur_flash = -1;
  189. static int cur_start = -1;
  190. static int cur_rxwink = -1;
  191. static int cur_rxflash = -1;
  192. static int cur_debounce = -1;
  193. static int priindication_oob = 0;
  194. #ifdef ZAPATA_PRI
  195. static int minunused = 2;
  196. static int minidle = 0;
  197. static char idleext[AST_MAX_EXTENSION];
  198. static char idledial[AST_MAX_EXTENSION];
  199. static int overlapdial = 0;
  200. static struct ast_channel inuse = { "GR-303InUse" };
  201. #endif
  202. /* Wait up to 16 seconds for first digit (FXO logic) */
  203. static int firstdigittimeout = 16000;
  204. /* How long to wait for following digits (FXO logic) */
  205. static int gendigittimeout = 8000;
  206. /* How long to wait for an extra digit, if there is an ambiguous match */
  207. static int matchdigittimeout = 3000;
  208. static int usecnt =0;
  209. AST_MUTEX_DEFINE_STATIC(usecnt_lock);
  210. /* Protect the interface list (of zt_pvt's) */
  211. AST_MUTEX_DEFINE_STATIC(iflock);
  212. static int ifcount = 0;
  213. /* When to send the CallerID signals (rings) */
  214. static int sendcalleridafter = DEFAULT_CIDRINGS;
  215. /* Protect the monitoring thread, so only one process can kill or start it, and not
  216. when it's doing something critical. */
  217. AST_MUTEX_DEFINE_STATIC(monlock);
  218. /* This is the thread for the monitor which checks for input on the channels
  219. which are not currently in use. */
  220. static pthread_t monitor_thread = AST_PTHREADT_NULL;
  221. static int restart_monitor(void);
  222. static int zt_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc);
  223. static int zt_sendtext(struct ast_channel *c, char *text);
  224. static inline int zt_get_event(int fd)
  225. {
  226. /* Avoid the silly zt_getevent which ignores a bunch of events */
  227. int j;
  228. if (ioctl(fd, ZT_GETEVENT, &j) == -1) return -1;
  229. return j;
  230. }
  231. static inline int zt_wait_event(int fd)
  232. {
  233. /* Avoid the silly zt_waitevent which ignores a bunch of events */
  234. int i,j=0;
  235. i = ZT_IOMUX_SIGEVENT;
  236. if (ioctl(fd, ZT_IOMUX, &i) == -1) return -1;
  237. if (ioctl(fd, ZT_GETEVENT, &j) == -1) return -1;
  238. return j;
  239. }
  240. /* Chunk size to read -- we use 20ms chunks to make things happy. */
  241. #define READ_SIZE 160
  242. #define MASK_AVAIL (1 << 0) /* Channel available for PRI use */
  243. #define MASK_INUSE (1 << 1) /* Channel currently in use */
  244. #define CALLWAITING_SILENT_SAMPLES ( (300 * 8) / READ_SIZE) /* 300 ms */
  245. #define CALLWAITING_REPEAT_SAMPLES ( (10000 * 8) / READ_SIZE) /* 300 ms */
  246. #define CIDCW_EXPIRE_SAMPLES ( (500 * 8) / READ_SIZE) /* 500 ms */
  247. #define MIN_MS_SINCE_FLASH ( (2000) ) /* 2000 ms */
  248. #define RINGT ( (8000 * 8) / READ_SIZE)
  249. struct zt_pvt;
  250. #ifdef ZAPATA_R2
  251. static int r2prot = -1;
  252. #endif
  253. #ifdef ZAPATA_PRI
  254. #define PVT_TO_CHANNEL(p) (((p)->prioffset) | ((p)->logicalspan << 8))
  255. #define PRI_CHANNEL(p) ((p) & 0xff)
  256. #define PRI_SPAN(p) (((p) >> 8) & 0xff)
  257. struct zt_pri {
  258. pthread_t master; /* Thread of master */
  259. ast_mutex_t lock; /* Mutex */
  260. char idleext[AST_MAX_EXTENSION]; /* Where to idle extra calls */
  261. char idlecontext[AST_MAX_EXTENSION]; /* What context to use for idle */
  262. char idledial[AST_MAX_EXTENSION]; /* What to dial before dumping */
  263. int minunused; /* Min # of channels to keep empty */
  264. int minidle; /* Min # of "idling" calls to keep active */
  265. int nodetype; /* Node type */
  266. int switchtype; /* Type of switch to emulate */
  267. int nsf; /* Network-Specific Facilities */
  268. int dialplan; /* Dialing plan */
  269. int localdialplan; /* Local dialing plan */
  270. int dchannels[NUM_DCHANS]; /* What channel are the dchannels on */
  271. int trunkgroup; /* What our trunkgroup is */
  272. int mastertrunkgroup; /* What trunk group is our master */
  273. int prilogicalspan; /* Logical span number within trunk group */
  274. int numchans; /* Num of channels we represent */
  275. int overlapdial; /* In overlap dialing mode */
  276. struct pri *dchans[NUM_DCHANS]; /* Actual d-channels */
  277. int dchanavail[NUM_DCHANS]; /* Whether each channel is available */
  278. struct pri *pri; /* Currently active D-channel */
  279. int debug;
  280. int fds[NUM_DCHANS]; /* FD's for d-channels */
  281. int offset;
  282. int span;
  283. int resetting;
  284. int resetpos;
  285. time_t lastreset;
  286. struct zt_pvt *pvts[MAX_CHANNELS]; /* Member channel pvt structs */
  287. struct zt_pvt *crvs; /* Member CRV structs */
  288. struct zt_pvt *crvend; /* Pointer to end of CRV structs */
  289. };
  290. static struct zt_pri pris[NUM_SPANS];
  291. static int pritype = PRI_CPE;
  292. #if 0
  293. #define DEFAULT_PRI_DEBUG (PRI_DEBUG_Q931_DUMP | PRI_DEBUG_Q921_DUMP | PRI_DEBUG_Q921_RAW | PRI_DEBUG_Q921_STATE)
  294. #else
  295. #define DEFAULT_PRI_DEBUG 0
  296. #endif
  297. static inline void pri_rel(struct zt_pri *pri)
  298. {
  299. ast_mutex_unlock(&pri->lock);
  300. }
  301. static int switchtype = PRI_SWITCH_NI2;
  302. static int nsf = PRI_NSF_NONE;
  303. static int dialplan = PRI_NATIONAL_ISDN + 1;
  304. static int localdialplan = PRI_NATIONAL_ISDN + 1;
  305. #else
  306. /* Shut up the compiler */
  307. struct zt_pri;
  308. #endif
  309. #define SUB_REAL 0 /* Active call */
  310. #define SUB_CALLWAIT 1 /* Call-Waiting call on hold */
  311. #define SUB_THREEWAY 2 /* Three-way call */
  312. static struct zt_distRings drings;
  313. struct distRingData {
  314. int ring[3];
  315. };
  316. struct ringContextData {
  317. char contextData[AST_MAX_EXTENSION];
  318. };
  319. struct zt_distRings {
  320. struct distRingData ringnum[3];
  321. struct ringContextData ringContext[3];
  322. };
  323. static char *subnames[] = {
  324. "Real",
  325. "Callwait",
  326. "Threeway"
  327. };
  328. struct zt_subchannel {
  329. int zfd;
  330. struct ast_channel *owner;
  331. int chan;
  332. short buffer[AST_FRIENDLY_OFFSET/2 + READ_SIZE];
  333. struct ast_frame f; /* One frame for each channel. How did this ever work before? */
  334. int needringing;
  335. int needbusy;
  336. int needcongestion;
  337. int needcallerid;
  338. int needanswer;
  339. int linear;
  340. int inthreeway;
  341. ZT_CONFINFO curconf;
  342. };
  343. #define CONF_USER_REAL (1 << 0)
  344. #define CONF_USER_THIRDCALL (1 << 1)
  345. #define MAX_SLAVES 4
  346. static struct zt_pvt {
  347. ast_mutex_t lock;
  348. struct ast_channel *owner; /* Our current active owner (if applicable) */
  349. /* Up to three channels can be associated with this call */
  350. struct zt_subchannel sub_unused; /* Just a safety precaution */
  351. struct zt_subchannel subs[3]; /* Sub-channels */
  352. struct zt_confinfo saveconf; /* Saved conference info */
  353. struct zt_pvt *slaves[MAX_SLAVES]; /* Slave to us (follows our conferencing) */
  354. struct zt_pvt *master; /* Master to us (we follow their conferencing) */
  355. int inconference; /* If our real should be in the conference */
  356. int sig; /* Signalling style */
  357. int radio; /* radio type */
  358. int firstradio; /* first radio flag */
  359. float rxgain;
  360. float txgain;
  361. int tonezone; /* tone zone for this chan, or -1 for default */
  362. struct zt_pvt *next; /* Next channel in list */
  363. struct zt_pvt *prev; /* Prev channel in list */
  364. struct zt_distRings drings;
  365. int usedistinctiveringdetection;
  366. char context[AST_MAX_EXTENSION];
  367. char defcontext[AST_MAX_EXTENSION];
  368. char exten[AST_MAX_EXTENSION];
  369. char language[MAX_LANGUAGE];
  370. char musicclass[MAX_LANGUAGE];
  371. char callerid[AST_MAX_EXTENSION];
  372. char lastcallerid[AST_MAX_EXTENSION];
  373. char *origcallerid; /* malloced original callerid */
  374. char callwaitcid[AST_MAX_EXTENSION];
  375. char rdnis[AST_MAX_EXTENSION];
  376. char dnid[AST_MAX_EXTENSION];
  377. unsigned int group;
  378. int law;
  379. int confno; /* Our conference */
  380. int confusers; /* Who is using our conference */
  381. int propconfno; /* Propagated conference number */
  382. unsigned int callgroup;
  383. unsigned int pickupgroup;
  384. int immediate; /* Answer before getting digits? */
  385. int channel; /* Channel Number or CRV */
  386. int span; /* Span number */
  387. int dialing;
  388. time_t guardtime; /* Must wait this much time before using for new call */
  389. int dialednone;
  390. int use_callerid; /* Whether or not to use caller id on this channel */
  391. int cid_signalling; /* CID signalling type bell202 or v23 */
  392. int cid_start; /* CID start indicator, polarity or ring */
  393. int hidecallerid;
  394. int callreturn;
  395. int permhidecallerid; /* Whether to hide our outgoing caller ID or not */
  396. int restrictcid; /* Whether restrict the callerid -> only send ANI */
  397. int use_callingpres; /* Whether to use the callingpres the calling switch sends */
  398. int callingpres; /* The value of callling presentation that we're going to use when placing a PRI call */
  399. int callwaitingrepeat; /* How many samples to wait before repeating call waiting */
  400. int cidcwexpire; /* When to expire our muting for CID/CW */
  401. unsigned char *cidspill;
  402. int cidpos;
  403. int cidlen;
  404. int ringt;
  405. int stripmsd;
  406. int callwaiting;
  407. int callwaitcas;
  408. int callwaitrings;
  409. int echocancel;
  410. int echotraining;
  411. int pulse;
  412. int echocanbridged;
  413. int echocanon;
  414. int echobreak;
  415. char echorest[20];
  416. int permcallwaiting;
  417. int callwaitingcallerid;
  418. int threewaycalling;
  419. int transfer;
  420. int digital;
  421. int outgoing;
  422. int dnd;
  423. int busydetect;
  424. int busycount;
  425. int callprogress;
  426. int priindication_oob;
  427. struct timeval flashtime; /* Last flash-hook time */
  428. struct ast_dsp *dsp;
  429. int cref; /* Call reference number */
  430. ZT_DIAL_OPERATION dop;
  431. int destroy;
  432. int ignoredtmf;
  433. int inalarm;
  434. char accountcode[20]; /* Account code */
  435. int amaflags; /* AMA Flags */
  436. char didtdd; /* flag to say its done it once */
  437. struct tdd_state *tdd; /* TDD flag */
  438. int adsi;
  439. int cancallforward;
  440. char call_forward[AST_MAX_EXTENSION];
  441. char mailbox[AST_MAX_EXTENSION];
  442. char dialdest[256];
  443. int onhooktime;
  444. int msgstate;
  445. int confirmanswer; /* Wait for '#' to confirm answer */
  446. int distinctivering; /* Which distinctivering to use */
  447. int cidrings; /* Which ring to deliver CID on */
  448. int faxhandled; /* Has a fax tone already been handled? */
  449. char mate; /* flag to say its in MATE mode */
  450. int pulsedial; /* whether a pulse dial phone is detected */
  451. int dtmfrelax; /* whether to run in relaxed DTMF mode */
  452. int fake_event;
  453. int zaptrcallerid; /* should we use the callerid from incoming call on zap transfer or not */
  454. int sendcalleridafter;
  455. #ifdef ZAPATA_PRI
  456. struct zt_pri *pri;
  457. struct zt_pvt *bearer;
  458. struct zt_pvt *realcall;
  459. q931_call *call;
  460. int isidlecall;
  461. int resetting;
  462. int prioffset;
  463. int logicalspan;
  464. int alreadyhungup;
  465. int proceeding;
  466. int alerting;
  467. int setup_ack; /* wheter we received SETUP_ACKNOWLEDGE or not */
  468. #endif
  469. #ifdef ZAPATA_R2
  470. int r2prot;
  471. mfcr2_t *r2;
  472. int hasr2call;
  473. int r2blocked;
  474. int sigchecked;
  475. #endif
  476. } *iflist = NULL, *ifend = NULL;
  477. #ifdef ZAPATA_PRI
  478. #define GET_CHANNEL(p) ((p)->bearer ? (p)->bearer->channel : p->channel)
  479. #else
  480. #define GET_CHANNEL(p) ((p)->channel)
  481. #endif
  482. struct zt_pvt *round_robin[32];
  483. #ifdef ZAPATA_PRI
  484. static inline int pri_grab(struct zt_pvt *pvt, struct zt_pri *pri)
  485. {
  486. int res;
  487. /* Grab the lock first */
  488. do {
  489. res = ast_mutex_trylock(&pri->lock);
  490. if (res) {
  491. ast_mutex_unlock(&pvt->lock);
  492. /* Release the lock and try again */
  493. usleep(1);
  494. ast_mutex_lock(&pvt->lock);
  495. }
  496. } while(res);
  497. /* Then break the poll */
  498. pthread_kill(pri->master, SIGURG);
  499. return 0;
  500. }
  501. #endif
  502. #define NUM_CADENCE_MAX 25
  503. static int num_cadence = 4;
  504. static int user_has_defined_cadences = 0;
  505. static struct zt_ring_cadence cadences[NUM_CADENCE_MAX] = {
  506. { { 125, 125, 2000, 4000 } }, /* Quick chirp followed by normal ring */
  507. { { 250, 250, 500, 1000, 250, 250, 500, 4000 } }, /* British style ring */
  508. { { 125, 125, 125, 125, 125, 4000 } }, /* Three short bursts */
  509. { { 1000, 500, 2500, 5000 } }, /* Long ring */
  510. };
  511. int receivedRingT; /* Used to find out what ringtone we are on */
  512. /* cidrings says in which pause to transmit the cid information, where the first pause
  513. * is 1, the second pause is 2 and so on.
  514. */
  515. static int cidrings[NUM_CADENCE_MAX] = {
  516. 2, /* Right after first long ring */
  517. 4, /* Right after long part */
  518. 3, /* After third chirp */
  519. 2, /* Second spell */
  520. };
  521. #define ISTRUNK(p) ((p->sig == SIG_FXSLS) || (p->sig == SIG_FXSKS) || \
  522. (p->sig == SIG_FXSGS) || (p->sig == SIG_PRI))
  523. #define CANBUSYDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) /* || (p->sig & __ZT_SIG_FXO) */)
  524. #define CANPROGRESSDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) /* || (p->sig & __ZT_SIG_FXO) */)
  525. static int zt_get_index(struct ast_channel *ast, struct zt_pvt *p, int nullok)
  526. {
  527. int res;
  528. if (p->subs[0].owner == ast)
  529. res = 0;
  530. else if (p->subs[1].owner == ast)
  531. res = 1;
  532. else if (p->subs[2].owner == ast)
  533. res = 2;
  534. else {
  535. res = -1;
  536. if (!nullok)
  537. ast_log(LOG_WARNING, "Unable to get index, and nullok is not asserted\n");
  538. }
  539. return res;
  540. }
  541. #ifdef ZAPATA_PRI
  542. static void wakeup_sub(struct zt_pvt *p, int a, struct zt_pri *pri)
  543. #else
  544. static void wakeup_sub(struct zt_pvt *p, int a, void *pri)
  545. #endif
  546. {
  547. struct ast_frame null = { AST_FRAME_NULL, };
  548. #ifdef ZAPATA_PRI
  549. if (pri)
  550. ast_mutex_unlock(&pri->lock);
  551. #endif
  552. for (;;) {
  553. if (p->subs[a].owner) {
  554. if (ast_mutex_trylock(&p->subs[a].owner->lock)) {
  555. ast_mutex_unlock(&p->lock);
  556. usleep(1);
  557. ast_mutex_lock(&p->lock);
  558. } else {
  559. ast_queue_frame(p->subs[a].owner, &null);
  560. ast_mutex_unlock(&p->subs[a].owner->lock);
  561. break;
  562. }
  563. } else
  564. break;
  565. }
  566. #ifdef ZAPATA_PRI
  567. if (pri)
  568. ast_mutex_lock(&pri->lock);
  569. #endif
  570. }
  571. #ifdef ZAPATA_PRI
  572. static void zap_queue_frame(struct zt_pvt *p, struct ast_frame *f, struct zt_pri *pri)
  573. #else
  574. static void zap_queue_frame(struct zt_pvt *p, struct ast_frame *f, void *pri)
  575. #endif
  576. {
  577. /* We must unlock the PRI to avoid the possibility of a deadlock */
  578. #ifdef ZAPATA_PRI
  579. if (pri)
  580. ast_mutex_unlock(&pri->lock);
  581. #endif
  582. for (;;) {
  583. if (p->owner) {
  584. if (ast_mutex_trylock(&p->owner->lock)) {
  585. ast_mutex_unlock(&p->lock);
  586. usleep(1);
  587. ast_mutex_lock(&p->lock);
  588. } else {
  589. ast_queue_frame(p->owner, f);
  590. ast_mutex_unlock(&p->owner->lock);
  591. break;
  592. }
  593. } else
  594. break;
  595. }
  596. #ifdef ZAPATA_PRI
  597. if (pri)
  598. ast_mutex_lock(&pri->lock);
  599. #endif
  600. }
  601. static int restore_gains(struct zt_pvt *p);
  602. static void swap_subs(struct zt_pvt *p, int a, int b)
  603. {
  604. int tchan;
  605. int tinthreeway;
  606. struct ast_channel *towner;
  607. ast_log(LOG_DEBUG, "Swapping %d and %d\n", a, b);
  608. tchan = p->subs[a].chan;
  609. towner = p->subs[a].owner;
  610. tinthreeway = p->subs[a].inthreeway;
  611. p->subs[a].chan = p->subs[b].chan;
  612. p->subs[a].owner = p->subs[b].owner;
  613. p->subs[a].inthreeway = p->subs[b].inthreeway;
  614. p->subs[b].chan = tchan;
  615. p->subs[b].owner = towner;
  616. p->subs[b].inthreeway = tinthreeway;
  617. if (p->subs[a].owner)
  618. p->subs[a].owner->fds[0] = p->subs[a].zfd;
  619. if (p->subs[b].owner)
  620. p->subs[b].owner->fds[0] = p->subs[b].zfd;
  621. wakeup_sub(p, a, NULL);
  622. wakeup_sub(p, b, NULL);
  623. }
  624. static int zt_open(char *fn)
  625. {
  626. int fd;
  627. int isnum;
  628. int chan = 0;
  629. int bs;
  630. int x;
  631. isnum = 1;
  632. for (x=0;x<strlen(fn);x++) {
  633. if (!isdigit(fn[x])) {
  634. isnum = 0;
  635. break;
  636. }
  637. }
  638. if (isnum) {
  639. chan = atoi(fn);
  640. if (chan < 1) {
  641. ast_log(LOG_WARNING, "Invalid channel number '%s'\n", fn);
  642. return -1;
  643. }
  644. fn = "/dev/zap/channel";
  645. }
  646. fd = open(fn, O_RDWR | O_NONBLOCK);
  647. if (fd < 0) {
  648. ast_log(LOG_WARNING, "Unable to open '%s': %s\n", fn, strerror(errno));
  649. return -1;
  650. }
  651. if (chan) {
  652. if (ioctl(fd, ZT_SPECIFY, &chan)) {
  653. x = errno;
  654. zt_close(fd);
  655. errno = x;
  656. ast_log(LOG_WARNING, "Unable to specify channel %d: %s\n", chan, strerror(errno));
  657. return -1;
  658. }
  659. }
  660. bs = READ_SIZE;
  661. if (ioctl(fd, ZT_SET_BLOCKSIZE, &bs) == -1) return -1;
  662. return fd;
  663. }
  664. int zt_setlinear(int zfd, int linear)
  665. {
  666. int res;
  667. res = ioctl(zfd, ZT_SETLINEAR, &linear);
  668. if (res)
  669. return res;
  670. return 0;
  671. }
  672. int zt_setlaw(int zfd, int law)
  673. {
  674. int res;
  675. res = ioctl(zfd, ZT_SETLAW, &law);
  676. if (res)
  677. return res;
  678. return 0;
  679. }
  680. static int alloc_sub(struct zt_pvt *p, int x)
  681. {
  682. ZT_BUFFERINFO bi;
  683. int res;
  684. if (p->subs[x].zfd < 0) {
  685. p->subs[x].zfd = zt_open("/dev/zap/pseudo");
  686. if (p->subs[x].zfd > -1) {
  687. res = ioctl(p->subs[x].zfd, ZT_GET_BUFINFO, &bi);
  688. if (!res) {
  689. bi.txbufpolicy = ZT_POLICY_IMMEDIATE;
  690. bi.rxbufpolicy = ZT_POLICY_IMMEDIATE;
  691. bi.numbufs = numbufs;
  692. res = ioctl(p->subs[x].zfd, ZT_SET_BUFINFO, &bi);
  693. if (res < 0) {
  694. ast_log(LOG_WARNING, "Unable to set buffer policy on channel %d\n", x);
  695. }
  696. } else
  697. ast_log(LOG_WARNING, "Unable to check buffer policy on channel %d\n", x);
  698. if (ioctl(p->subs[x].zfd, ZT_CHANNO, &p->subs[x].chan) == 1) {
  699. ast_log(LOG_WARNING,"Unable to get channel number for pseudo channel on FD %d\n",p->subs[x].zfd);
  700. zt_close(p->subs[x].zfd);
  701. p->subs[x].zfd = -1;
  702. return -1;
  703. }
  704. if (option_debug)
  705. ast_log(LOG_DEBUG, "Allocated %s subchannel on FD %d channel %d\n", subnames[x], p->subs[x].zfd, p->subs[x].chan);
  706. return 0;
  707. } else
  708. ast_log(LOG_WARNING, "Unable to open pseudo channel: %s\n", strerror(errno));
  709. return -1;
  710. }
  711. ast_log(LOG_WARNING, "%s subchannel of %d already in use\n", subnames[x], p->channel);
  712. return -1;
  713. }
  714. static int unalloc_sub(struct zt_pvt *p, int x)
  715. {
  716. if (!x) {
  717. ast_log(LOG_WARNING, "Trying to unalloc the real channel %d?!?\n", p->channel);
  718. return -1;
  719. }
  720. ast_log(LOG_DEBUG, "Released sub %d of channel %d\n", x, p->channel);
  721. if (p->subs[x].zfd > -1) {
  722. zt_close(p->subs[x].zfd);
  723. }
  724. p->subs[x].zfd = -1;
  725. p->subs[x].linear = 0;
  726. p->subs[x].chan = 0;
  727. p->subs[x].owner = NULL;
  728. p->subs[x].inthreeway = 0;
  729. memset(&p->subs[x].curconf, 0, sizeof(p->subs[x].curconf));
  730. return 0;
  731. }
  732. static int zt_digit(struct ast_channel *ast, char digit)
  733. {
  734. ZT_DIAL_OPERATION zo;
  735. struct zt_pvt *p;
  736. int res = 0;
  737. int index;
  738. p = ast->pvt->pvt;
  739. ast_mutex_lock(&p->lock);
  740. index = zt_get_index(ast, p, 0);
  741. if (index == SUB_REAL) {
  742. #ifdef ZAPATA_PRI
  743. if (p->sig == SIG_PRI && ast->_state == AST_STATE_DIALING && (p->proceeding < 2)) {
  744. if (p->setup_ack) {
  745. if (!pri_grab(p, p->pri)) {
  746. pri_information(p->pri->pri,p->call,digit);
  747. pri_rel(p->pri);
  748. } else
  749. ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
  750. } else if (strlen(p->dialdest) < sizeof(p->dialdest) - 1) {
  751. ast_log(LOG_DEBUG, "Queueing digit '%c' since setup_ack not yet received\n", digit);
  752. res = strlen(p->dialdest);
  753. p->dialdest[res++] = digit;
  754. p->dialdest[res] = '\0';
  755. }
  756. } else {
  757. #else
  758. {
  759. #endif
  760. zo.op = ZT_DIAL_OP_APPEND;
  761. zo.dialstr[0] = 'T';
  762. zo.dialstr[1] = digit;
  763. zo.dialstr[2] = 0;
  764. if ((res = ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &zo)))
  765. ast_log(LOG_WARNING, "Couldn't dial digit %c\n", digit);
  766. else
  767. p->dialing = 1;
  768. }
  769. }
  770. ast_mutex_unlock(&p->lock);
  771. return res;
  772. }
  773. static char *events[] = {
  774. "No event",
  775. "On hook",
  776. "Ring/Answered",
  777. "Wink/Flash",
  778. "Alarm",
  779. "No more alarm",
  780. "HDLC Abort",
  781. "HDLC Overrun",
  782. "HDLC Bad FCS",
  783. "Dial Complete",
  784. "Ringer On",
  785. "Ringer Off",
  786. "Hook Transition Complete",
  787. "Bits Changed",
  788. "Pulse Start",
  789. "Timer Expired",
  790. "Timer Ping",
  791. "Polarity Reversal"
  792. };
  793. static struct {
  794. int alarm;
  795. char *name;
  796. } alarms[] = {
  797. { ZT_ALARM_RED, "Red Alarm" },
  798. { ZT_ALARM_YELLOW, "Yellow Alarm" },
  799. { ZT_ALARM_BLUE, "Blue Alarm" },
  800. { ZT_ALARM_RECOVER, "Recovering" },
  801. { ZT_ALARM_LOOPBACK, "Loopback" },
  802. { ZT_ALARM_NOTOPEN, "Not Open" },
  803. { ZT_ALARM_NONE, "None" },
  804. };
  805. static char *alarm2str(int alarm)
  806. {
  807. int x;
  808. for (x=0;x<sizeof(alarms) / sizeof(alarms[0]); x++) {
  809. if (alarms[x].alarm & alarm)
  810. return alarms[x].name;
  811. }
  812. return alarm ? "Unknown Alarm" : "No Alarm";
  813. }
  814. static char *event2str(int event)
  815. {
  816. static char buf[256];
  817. if ((event < 18) && (event > -1))
  818. return events[event];
  819. sprintf(buf, "Event %d", event); /* safe */
  820. return buf;
  821. }
  822. #ifdef ZAPATA_R2
  823. static int str2r2prot(char *swtype)
  824. {
  825. if (!strcasecmp(swtype, "ar"))
  826. return MFCR2_PROT_ARGENTINA;
  827. /*endif*/
  828. if (!strcasecmp(swtype, "cn"))
  829. return MFCR2_PROT_CHINA;
  830. /*endif*/
  831. if (!strcasecmp(swtype, "kr"))
  832. return MFCR2_PROT_KOREA;
  833. /*endif*/
  834. return -1;
  835. }
  836. #endif
  837. static char *sig2str(int sig)
  838. {
  839. static char buf[256];
  840. switch(sig) {
  841. case SIG_EM:
  842. return "E & M Immediate";
  843. case SIG_EMWINK:
  844. return "E & M Wink";
  845. case SIG_EM_E1:
  846. return "E & M E1";
  847. case SIG_FEATD:
  848. return "Feature Group D (DTMF)";
  849. case SIG_FEATDMF:
  850. return "Feature Group D (MF)";
  851. case SIG_FEATB:
  852. return "Feature Group B (MF)";
  853. case SIG_E911:
  854. return "E911 (MF)";
  855. case SIG_FXSLS:
  856. return "FXS Loopstart";
  857. case SIG_FXSGS:
  858. return "FXS Groundstart";
  859. case SIG_FXSKS:
  860. return "FXS Kewlstart";
  861. case SIG_FXOLS:
  862. return "FXO Loopstart";
  863. case SIG_FXOGS:
  864. return "FXO Groundstart";
  865. case SIG_FXOKS:
  866. return "FXO Kewlstart";
  867. case SIG_PRI:
  868. return "PRI Signalling";
  869. case SIG_R2:
  870. return "R2 Signalling";
  871. case SIG_SF:
  872. return "SF (Tone) Signalling Immediate";
  873. case SIG_SFWINK:
  874. return "SF (Tone) Signalling Wink";
  875. case SIG_SF_FEATD:
  876. return "SF (Tone) Signalling with Feature Group D (DTMF)";
  877. case SIG_SF_FEATDMF:
  878. return "SF (Tone) Signalling with Feature Group D (MF)";
  879. case SIG_SF_FEATB:
  880. return "SF (Tone) Signalling with Feature Group B (MF)";
  881. case SIG_GR303FXOKS:
  882. return "GR-303 Signalling with FXOKS";
  883. case SIG_GR303FXSKS:
  884. return "GR-303 Signalling with FXSKS";
  885. case 0:
  886. return "Pseudo Signalling";
  887. default:
  888. snprintf(buf, sizeof(buf), "Unknown signalling %d", sig);
  889. return buf;
  890. }
  891. }
  892. static int conf_add(struct zt_pvt *p, struct zt_subchannel *c, int index, int slavechannel)
  893. {
  894. /* If the conference already exists, and we're already in it
  895. don't bother doing anything */
  896. ZT_CONFINFO zi;
  897. memset(&zi, 0, sizeof(zi));
  898. zi.chan = 0;
  899. if (slavechannel > 0) {
  900. /* If we have only one slave, do a digital mon */
  901. zi.confmode = ZT_CONF_DIGITALMON;
  902. zi.confno = slavechannel;
  903. } else {
  904. if (!index) {
  905. /* Real-side and pseudo-side both participate in conference */
  906. zi.confmode = ZT_CONF_REALANDPSEUDO | ZT_CONF_TALKER | ZT_CONF_LISTENER |
  907. ZT_CONF_PSEUDO_TALKER | ZT_CONF_PSEUDO_LISTENER;
  908. } else
  909. zi.confmode = ZT_CONF_CONF | ZT_CONF_TALKER | ZT_CONF_LISTENER;
  910. zi.confno = p->confno;
  911. }
  912. if ((zi.confno == c->curconf.confno) && (zi.confmode == c->curconf.confmode))
  913. return 0;
  914. if (c->zfd < 0)
  915. return 0;
  916. if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
  917. ast_log(LOG_WARNING, "Failed to add %d to conference %d/%d\n", c->zfd, zi.confmode, zi.confno);
  918. return -1;
  919. }
  920. if (slavechannel < 1) {
  921. p->confno = zi.confno;
  922. }
  923. memcpy(&c->curconf, &zi, sizeof(c->curconf));
  924. ast_log(LOG_DEBUG, "Added %d to conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
  925. return 0;
  926. }
  927. static int isourconf(struct zt_pvt *p, struct zt_subchannel *c)
  928. {
  929. /* If they're listening to our channel, they're ours */
  930. if ((p->channel == c->curconf.confno) && (c->curconf.confmode == ZT_CONF_DIGITALMON))
  931. return 1;
  932. /* If they're a talker on our (allocated) conference, they're ours */
  933. if ((p->confno > 0) && (p->confno == c->curconf.confno) && (c->curconf.confmode & ZT_CONF_TALKER))
  934. return 1;
  935. return 0;
  936. }
  937. static int conf_del(struct zt_pvt *p, struct zt_subchannel *c, int index)
  938. {
  939. ZT_CONFINFO zi;
  940. if (/* Can't delete if there's no zfd */
  941. (c->zfd < 0) ||
  942. /* Don't delete from the conference if it's not our conference */
  943. !isourconf(p, c)
  944. /* Don't delete if we don't think it's conferenced at all (implied) */
  945. ) return 0;
  946. memset(&zi, 0, sizeof(zi));
  947. zi.chan = 0;
  948. zi.confno = 0;
  949. zi.confmode = 0;
  950. if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
  951. ast_log(LOG_WARNING, "Failed to drop %d from conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
  952. return -1;
  953. }
  954. ast_log(LOG_DEBUG, "Removed %d from conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
  955. memcpy(&c->curconf, &zi, sizeof(c->curconf));
  956. return 0;
  957. }
  958. static int isslavenative(struct zt_pvt *p, struct zt_pvt **out)
  959. {
  960. int x;
  961. int useslavenative;
  962. struct zt_pvt *slave = NULL;
  963. /* Start out optimistic */
  964. useslavenative = 1;
  965. /* Update conference state in a stateless fashion */
  966. for (x=0;x<3;x++) {
  967. /* Any three-way calling makes slave native mode *definitely* out
  968. of the question */
  969. if ((p->subs[x].zfd > -1) && p->subs[x].inthreeway)
  970. useslavenative = 0;
  971. }
  972. /* If we don't have any 3-way calls, check to see if we have
  973. precisely one slave */
  974. if (useslavenative) {
  975. for (x=0;x<MAX_SLAVES;x++) {
  976. if (p->slaves[x]) {
  977. if (slave) {
  978. /* Whoops already have a slave! No
  979. slave native and stop right away */
  980. slave = NULL;
  981. useslavenative = 0;
  982. break;
  983. } else {
  984. /* We have one slave so far */
  985. slave = p->slaves[x];
  986. }
  987. }
  988. }
  989. }
  990. /* If no slave, slave native definitely out */
  991. if (!slave)
  992. useslavenative = 0;
  993. else if (slave->law != p->law) {
  994. useslavenative = 0;
  995. slave = NULL;
  996. }
  997. if (out)
  998. *out = slave;
  999. return useslavenative;
  1000. }
  1001. static int reset_conf(struct zt_pvt *p)
  1002. {
  1003. ZT_CONFINFO zi;
  1004. memset(&zi, 0, sizeof(zi));
  1005. p->confno = -1;
  1006. if (p->subs[SUB_REAL].zfd > -1) {
  1007. if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &zi))
  1008. ast_log(LOG_WARNING, "Failed to reset conferencing on channel %d!\n", p->channel);
  1009. }
  1010. return 0;
  1011. }
  1012. static int update_conf(struct zt_pvt *p)
  1013. {
  1014. int needconf = 0;
  1015. int x;
  1016. int useslavenative;
  1017. struct zt_pvt *slave = NULL;
  1018. useslavenative = isslavenative(p, &slave);
  1019. /* Start with the obvious, general stuff */
  1020. for (x=0;x<3;x++) {
  1021. /* Look for three way calls */
  1022. if ((p->subs[x].zfd > -1) && p->subs[x].inthreeway) {
  1023. conf_add(p, &p->subs[x], x, 0);
  1024. needconf++;
  1025. } else {
  1026. conf_del(p, &p->subs[x], x);
  1027. }
  1028. }
  1029. /* If we have a slave, add him to our conference now. or DAX
  1030. if this is slave native */
  1031. for (x=0;x<MAX_SLAVES;x++) {
  1032. if (p->slaves[x]) {
  1033. if (useslavenative)
  1034. conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p));
  1035. else {
  1036. conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, 0);
  1037. needconf++;
  1038. }
  1039. }
  1040. }
  1041. /* If we're supposed to be in there, do so now */
  1042. if (p->inconference && !p->subs[SUB_REAL].inthreeway) {
  1043. if (useslavenative)
  1044. conf_add(p, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(slave));
  1045. else {
  1046. conf_add(p, &p->subs[SUB_REAL], SUB_REAL, 0);
  1047. needconf++;
  1048. }
  1049. }
  1050. /* If we have a master, add ourselves to his conference */
  1051. if (p->master) {
  1052. if (isslavenative(p->master, NULL)) {
  1053. conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p->master));
  1054. } else {
  1055. conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, 0);
  1056. }
  1057. }
  1058. if (!needconf) {
  1059. /* Nobody is left (or should be left) in our conference.
  1060. Kill it. */
  1061. p->confno = -1;
  1062. }
  1063. ast_log(LOG_DEBUG, "Updated conferencing on %d, with %d conference users\n", p->channel, needconf);
  1064. return 0;
  1065. }
  1066. static void zt_enable_ec(struct zt_pvt *p)
  1067. {
  1068. int x;
  1069. int res;
  1070. if (p->echocanon) {
  1071. ast_log(LOG_DEBUG, "Echo cancellation already on\n");
  1072. return;
  1073. }
  1074. if (p && p->echocancel) {
  1075. if (p->sig == SIG_PRI) {
  1076. x = 1;
  1077. res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &x);
  1078. if (res)
  1079. ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d\n", p->channel);
  1080. }
  1081. x = p->echocancel;
  1082. res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
  1083. if (res)
  1084. ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d\n", p->channel);
  1085. else {
  1086. p->echocanon = 1;
  1087. ast_log(LOG_DEBUG, "Enabled echo cancellation on channel %d\n", p->channel);
  1088. }
  1089. } else
  1090. ast_log(LOG_DEBUG, "No echocancellation requested\n");
  1091. }
  1092. static void zt_train_ec(struct zt_pvt *p)
  1093. {
  1094. int x;
  1095. int res;
  1096. if (p && p->echocancel && p->echotraining) {
  1097. x = p->echotraining;
  1098. res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOTRAIN, &x);
  1099. if (res)
  1100. ast_log(LOG_WARNING, "Unable to request echo training on channel %d\n", p->channel);
  1101. else {
  1102. ast_log(LOG_DEBUG, "Engaged echo training on channel %d\n", p->channel);
  1103. }
  1104. } else
  1105. ast_log(LOG_DEBUG, "No echo training requested\n");
  1106. }
  1107. static void zt_disable_ec(struct zt_pvt *p)
  1108. {
  1109. int x;
  1110. int res;
  1111. if (p->echocancel) {
  1112. x = 0;
  1113. res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
  1114. if (res)
  1115. ast_log(LOG_WARNING, "Unable to disable echo cancellation on channel %d\n", p->channel);
  1116. else
  1117. ast_log(LOG_DEBUG, "disabled echo cancellation on channel %d\n", p->channel);
  1118. }
  1119. p->echocanon = 0;
  1120. }
  1121. int set_actual_gain(int fd, int chan, float rxgain, float txgain, int law)
  1122. {
  1123. struct zt_gains g;
  1124. float ltxgain;
  1125. float lrxgain;
  1126. int j,k;
  1127. g.chan = chan;
  1128. if ((rxgain != 0.0) || (txgain != 0.0)) {
  1129. /* caluculate linear value of tx gain */
  1130. ltxgain = pow(10.0,txgain / 20.0);
  1131. /* caluculate linear value of rx gain */
  1132. lrxgain = pow(10.0,rxgain / 20.0);
  1133. if (law == ZT_LAW_ALAW) {
  1134. for (j=0;j<256;j++) {
  1135. k = (int)(((float)AST_ALAW(j)) * lrxgain);
  1136. if (k > 32767) k = 32767;
  1137. if (k < -32767) k = -32767;
  1138. g.rxgain[j] = AST_LIN2A(k);
  1139. k = (int)(((float)AST_ALAW(j)) * ltxgain);
  1140. if (k > 32767) k = 32767;
  1141. if (k < -32767) k = -32767;
  1142. g.txgain[j] = AST_LIN2A(k);
  1143. }
  1144. } else {
  1145. for (j=0;j<256;j++) {
  1146. k = (int)(((float)AST_MULAW(j)) * lrxgain);
  1147. if (k > 32767) k = 32767;
  1148. if (k < -32767) k = -32767;
  1149. g.rxgain[j] = AST_LIN2MU(k);
  1150. k = (int)(((float)AST_MULAW(j)) * ltxgain);
  1151. if (k > 32767) k = 32767;
  1152. if (k < -32767) k = -32767;
  1153. g.txgain[j] = AST_LIN2MU(k);
  1154. }
  1155. }
  1156. } else {
  1157. for (j=0;j<256;j++) {
  1158. g.rxgain[j] = j;
  1159. g.txgain[j] = j;
  1160. }
  1161. }
  1162. /* set 'em */
  1163. return(ioctl(fd,ZT_SETGAINS,&g));
  1164. }
  1165. static inline int zt_set_hook(int fd, int hs)
  1166. {
  1167. int x, res;
  1168. x = hs;
  1169. res = ioctl(fd, ZT_HOOK, &x);
  1170. if (res < 0)
  1171. {
  1172. if (errno == EINPROGRESS) return 0;
  1173. ast_log(LOG_WARNING, "zt hook failed: %s\n", strerror(errno));
  1174. }
  1175. return res;
  1176. }
  1177. static inline int zt_confmute(struct zt_pvt *p, int muted)
  1178. {
  1179. int x, y, res;
  1180. x = muted;
  1181. if (p->sig == SIG_PRI) {
  1182. y = 1;
  1183. res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &y);
  1184. if (res)
  1185. ast_log(LOG_WARNING, "Unable to set audio mode on '%d'\n", p->channel);
  1186. }
  1187. res = ioctl(p->subs[SUB_REAL].zfd, ZT_CONFMUTE, &x);
  1188. if (res < 0)
  1189. ast_log(LOG_WARNING, "zt confmute(%d) failed on channel %d: %s\n", muted, p->channel, strerror(errno));
  1190. return res;
  1191. }
  1192. static int save_conference(struct zt_pvt *p)
  1193. {
  1194. struct zt_confinfo c;
  1195. int res;
  1196. if (p->saveconf.confmode) {
  1197. ast_log(LOG_WARNING, "Can't save conference -- already in use\n");
  1198. return -1;
  1199. }
  1200. p->saveconf.chan = 0;
  1201. res = ioctl(p->subs[SUB_REAL].zfd, ZT_GETCONF, &p->saveconf);
  1202. if (res) {
  1203. ast_log(LOG_WARNING, "Unable to get conference info: %s\n", strerror(errno));
  1204. p->saveconf.confmode = 0;
  1205. return -1;
  1206. }
  1207. c.chan = 0;
  1208. c.confno = 0;
  1209. c.confmode = ZT_CONF_NORMAL;
  1210. res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &c);
  1211. if (res) {
  1212. ast_log(LOG_WARNING, "Unable to set conference info: %s\n", strerror(errno));
  1213. return -1;
  1214. }
  1215. if (option_debug)
  1216. ast_log(LOG_DEBUG, "Disabled conferencing\n");
  1217. return 0;
  1218. }
  1219. static int restore_conference(struct zt_pvt *p)
  1220. {
  1221. int res;
  1222. if (p->saveconf.confmode) {
  1223. res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &p->saveconf);
  1224. p->saveconf.confmode = 0;
  1225. if (res) {
  1226. ast_log(LOG_WARNING, "Unable to restore conference info: %s\n", strerror(errno));
  1227. return -1;
  1228. }
  1229. }
  1230. if (option_debug)
  1231. ast_log(LOG_DEBUG, "Restored conferencing\n");
  1232. return 0;
  1233. }
  1234. static int send_callerid(struct zt_pvt *p);
  1235. int send_cwcidspill(struct zt_pvt *p)
  1236. {
  1237. p->callwaitcas = 0;
  1238. p->cidcwexpire = 0;
  1239. p->cidspill = malloc(MAX_CALLERID_SIZE);
  1240. if (p->cidspill) {
  1241. memset(p->cidspill, 0x7f, MAX_CALLERID_SIZE);
  1242. p->cidlen = ast_callerid_callwaiting_generate(p->cidspill, p->callwaitcid, AST_LAW(p));
  1243. /* Make sure we account for the end */
  1244. p->cidlen += READ_SIZE * 4;
  1245. p->cidpos = 0;
  1246. send_callerid(p);
  1247. if (option_verbose > 2)
  1248. ast_verbose(VERBOSE_PREFIX_3 "CPE supports Call Waiting Caller*ID. Sending '%s'\n", p->callwaitcid);
  1249. } else return -1;
  1250. return 0;
  1251. }
  1252. static int has_voicemail(struct zt_pvt *p)
  1253. {
  1254. return ast_app_has_voicemail(p->mailbox);
  1255. }
  1256. static int send_callerid(struct zt_pvt *p)
  1257. {
  1258. /* Assumes spill in p->cidspill, p->cidlen in length and we're p->cidpos into it */
  1259. int res;
  1260. /* Take out of linear mode if necessary */
  1261. if (p->subs[SUB_REAL].linear) {
  1262. p->subs[SUB_REAL].linear = 0;
  1263. zt_setlinear(p->subs[SUB_REAL].zfd, 0);
  1264. }
  1265. while(p->cidpos < p->cidlen) {
  1266. res = write(p->subs[SUB_REAL].zfd, p->cidspill + p->cidpos, p->cidlen - p->cidpos);
  1267. if (res < 0) {
  1268. if (errno == EAGAIN)
  1269. return 0;
  1270. else {
  1271. ast_log(LOG_WARNING, "write failed: %s\n", strerror(errno));
  1272. return -1;
  1273. }
  1274. }
  1275. if (!res)
  1276. return 0;
  1277. p->cidpos += res;
  1278. }
  1279. free(p->cidspill);
  1280. p->cidspill = NULL;
  1281. if (p->callwaitcas) {
  1282. /* Wait for CID/CW to expire */
  1283. p->cidcwexpire = CIDCW_EXPIRE_SAMPLES;
  1284. } else
  1285. restore_conference(p);
  1286. return 0;
  1287. }
  1288. static int zt_callwait(struct ast_channel *ast)
  1289. {
  1290. struct zt_pvt *p = ast->pvt->pvt;
  1291. p->callwaitingrepeat = CALLWAITING_REPEAT_SAMPLES;
  1292. if (p->cidspill) {
  1293. ast_log(LOG_WARNING, "Spill already exists?!?\n");
  1294. free(p->cidspill);
  1295. }
  1296. p->cidspill = malloc(2400 /* SAS */ + 680 /* CAS */ + READ_SIZE * 4);
  1297. if (p->cidspill) {
  1298. save_conference(p);
  1299. /* Silence */
  1300. memset(p->cidspill, 0x7f, 2400 + 600 + READ_SIZE * 4);
  1301. if (!p->callwaitrings && p->callwaitingcallerid) {
  1302. ast_gen_cas(p->cidspill, 1, 2400 + 680, AST_LAW(p));
  1303. p->callwaitcas = 1;
  1304. p->cidlen = 2400 + 680 + READ_SIZE * 4;
  1305. } else {
  1306. ast_gen_cas(p->cidspill, 1, 2400, AST_LAW(p));
  1307. p->callwaitcas = 0;
  1308. p->cidlen = 2400 + READ_SIZE * 4;
  1309. }
  1310. p->cidpos = 0;
  1311. send_callerid(p);
  1312. } else {
  1313. ast_log(LOG_WARNING, "Unable to create SAS/CAS spill\n");
  1314. return -1;
  1315. }
  1316. return 0;
  1317. }
  1318. static int zt_call(struct ast_channel *ast, char *rdest, int timeout)
  1319. {
  1320. struct zt_pvt *p = ast->pvt->pvt;
  1321. int x, res, index;
  1322. char *c, *n, *l;
  1323. #ifdef ZAPATA_PRI
  1324. char *s=NULL;
  1325. #endif
  1326. char callerid[256];
  1327. char dest[256]; /* must be same length as p->dialdest */
  1328. ast_mutex_lock(&p->lock);
  1329. strncpy(dest, rdest, sizeof(dest) - 1);
  1330. strncpy(p->dialdest, rdest, sizeof(dest) - 1);
  1331. if ((ast->_state == AST_STATE_BUSY)) {
  1332. p->subs[SUB_REAL].needbusy = 1;
  1333. ast_mutex_unlock(&p->lock);
  1334. return 0;
  1335. }
  1336. if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
  1337. ast_log(LOG_WARNING, "zt_call called on %s, neither down nor reserved\n", ast->name);
  1338. ast_mutex_unlock(&p->lock);
  1339. return -1;
  1340. }
  1341. p->dialednone = 0;
  1342. if (p->radio) /* if a radio channel, up immediately */
  1343. {
  1344. /* Special pseudo -- automatically up */
  1345. ast_setstate(ast, AST_STATE_UP);
  1346. ast_mutex_unlock(&p->lock);
  1347. return 0;
  1348. }
  1349. x = ZT_FLUSH_READ | ZT_FLUSH_WRITE;
  1350. res = ioctl(p->subs[SUB_REAL].zfd, ZT_FLUSH, &x);
  1351. if (res)
  1352. ast_log(LOG_WARNING, "Unable to flush input on channel %d\n", p->channel);
  1353. p->outgoing = 1;
  1354. set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain, p->txgain, p->law);
  1355. switch(p->sig) {
  1356. case SIG_FXOLS:
  1357. case SIG_FXOGS:
  1358. case SIG_FXOKS:
  1359. if (p->owner == ast) {
  1360. /* Normal ring, on hook */
  1361. /* Don't send audio while on hook, until the call is answered */
  1362. p->dialing = 1;
  1363. if (p->use_callerid) {
  1364. /* Generate the Caller-ID spill if desired */
  1365. if (p->cidspill) {
  1366. ast_log(LOG_WARNING, "cidspill already exists??\n");
  1367. free(p->cidspill);
  1368. }
  1369. p->cidspill = malloc(MAX_CALLERID_SIZE);
  1370. p->callwaitcas = 0;
  1371. if (p->cidspill) {
  1372. p->cidlen = ast_callerid_generate(p->cidspill, ast->callerid, AST_LAW(p));
  1373. p->cidpos = 0;
  1374. send_callerid(p);
  1375. } else
  1376. ast_log(LOG_WARNING, "Unable to generate CallerID spill\n");
  1377. }
  1378. /* Choose proper cadence */
  1379. if ((p->distinctivering > 0) && (p->distinctivering <= num_cadence)) {
  1380. if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, &cadences[p->distinctivering-1]))
  1381. ast_log(LOG_WARNING, "Unable to set distinctive ring cadence %d on '%s'\n", p->distinctivering, ast->name);
  1382. p->cidrings = cidrings[p->distinctivering - 1];
  1383. } else {
  1384. if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, NULL))
  1385. ast_log(LOG_WARNING, "Unable to reset default ring on '%s'\n", ast->name);
  1386. p->cidrings = p->sendcalleridafter;
  1387. }
  1388. /* nick@dccinc.com 4/3/03 mods to allow for deferred dialing */
  1389. c = strchr(dest, '/');
  1390. if (c)
  1391. c++;
  1392. if (c && (strlen(c) < p->stripmsd)) {
  1393. ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
  1394. c = NULL;
  1395. }
  1396. if (c) {
  1397. p->dop.op = ZT_DIAL_OP_REPLACE;
  1398. snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "Tw%s", c);
  1399. ast_log(LOG_DEBUG, "FXO: setup deferred dialstring: %s\n", c);
  1400. } else {
  1401. p->dop.dialstr[0] = '\0';
  1402. }
  1403. x = ZT_RING;
  1404. if (ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x) && (errno != EINPROGRESS)) {
  1405. ast_log(LOG_WARNING, "Unable to ring phone: %s\n", strerror(errno));
  1406. ast_mutex_unlock(&p->lock);
  1407. return -1;
  1408. }
  1409. p->dialing = 1;
  1410. } else {
  1411. /* Call waiting call */
  1412. p->callwaitrings = 0;
  1413. if (ast->callerid)
  1414. strncpy(p->callwaitcid, ast->callerid, sizeof(p->callwaitcid)-1);
  1415. else
  1416. p->callwaitcid[0] = '\0';
  1417. /* Call waiting tone instead */
  1418. if (zt_callwait(ast)) {
  1419. ast_mutex_unlock(&p->lock);
  1420. return -1;
  1421. }
  1422. /* Make ring-back */
  1423. if (tone_zone_play_tone(p->subs[SUB_CALLWAIT].zfd, ZT_TONE_RINGTONE))
  1424. ast_log(LOG_WARNING, "Unable to generate call-wait ring-back on channel %s\n", ast->name);
  1425. }
  1426. if (ast->callerid)
  1427. strncpy(callerid, ast->callerid, sizeof(callerid)-1);
  1428. else
  1429. callerid[0] = '\0';
  1430. ast_callerid_parse(callerid, &n, &l);
  1431. if (l) {
  1432. ast_shrink_phone_number(l);
  1433. if (!ast_isphonenumber(l))
  1434. l = NULL;
  1435. }
  1436. if (l)
  1437. strncpy(p->lastcallerid, l, sizeof(p->lastcallerid) - 1);
  1438. else
  1439. p->lastcallerid[0] = '\0';
  1440. ast_setstate(ast, AST_STATE_RINGING);
  1441. index = zt_get_index(ast, p, 0);
  1442. if (index > -1) {
  1443. p->subs[index].needringing = 1;
  1444. }
  1445. break;
  1446. case SIG_FXSLS:
  1447. case SIG_FXSGS:
  1448. case SIG_FXSKS:
  1449. case SIG_EMWINK:
  1450. case SIG_EM:
  1451. case SIG_EM_E1:
  1452. case SIG_FEATD:
  1453. case SIG_FEATDMF:
  1454. case SIG_E911:
  1455. case SIG_FEATB:
  1456. case SIG_SFWINK:
  1457. case SIG_SF:
  1458. case SIG_SF_FEATD:
  1459. case SIG_SF_FEATDMF:
  1460. case SIG_SF_FEATB:
  1461. c = strchr(dest, '/');
  1462. if (c)
  1463. c++;
  1464. else
  1465. c = "";
  1466. if (strlen(c) < p->stripmsd) {
  1467. ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
  1468. ast_mutex_unlock(&p->lock);
  1469. return -1;
  1470. }
  1471. #ifdef ZAPATA_PRI
  1472. /* Start the trunk, if not GR-303 */
  1473. if (!p->pri) {
  1474. #endif
  1475. x = ZT_START;
  1476. res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
  1477. if (res < 0) {
  1478. if (errno != EINPROGRESS) {
  1479. ast_log(LOG_WARNING, "Unable to start channel: %s\n", strerror(errno));
  1480. ast_mutex_unlock(&p->lock);
  1481. return -1;
  1482. }
  1483. }
  1484. #ifdef ZAPATA_PRI
  1485. }
  1486. #endif
  1487. ast_log(LOG_DEBUG, "Dialing '%s'\n", c);
  1488. p->dop.op = ZT_DIAL_OP_REPLACE;
  1489. if (p->sig == SIG_FEATD) {
  1490. if (ast->callerid) {
  1491. strncpy(callerid, ast->callerid, sizeof(callerid)-1);
  1492. ast_callerid_parse(callerid, &n, &l);
  1493. if (l) {
  1494. ast_shrink_phone_number(l);
  1495. if (!ast_isphonenumber(l))
  1496. l = NULL;
  1497. }
  1498. } else
  1499. l = NULL;
  1500. if (l)
  1501. snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T*%s*%s*", l, c + p->stripmsd);
  1502. else
  1503. snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T**%s*", c + p->stripmsd);
  1504. } else
  1505. if (p->sig == SIG_FEATDMF) {
  1506. if (ast->callerid) {
  1507. strncpy(callerid, ast->callerid, sizeof(callerid)-1);
  1508. ast_callerid_parse(callerid, &n, &l);
  1509. if (l) {
  1510. ast_shrink_phone_number(l);
  1511. if (!ast_isphonenumber(l))
  1512. l = NULL;
  1513. }
  1514. } else
  1515. l = NULL;
  1516. if (l)
  1517. snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*00%s#*%s#", l, c + p->stripmsd);
  1518. else
  1519. snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*02#*%s#", c + p->stripmsd);
  1520. } else
  1521. if (p->sig == SIG_E911) {
  1522. strncpy(p->dop.dialstr, "M*911#", sizeof(p->dop.dialstr) - 1);
  1523. } else
  1524. if (p->sig == SIG_FEATB) {
  1525. snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s#", c + p->stripmsd);
  1526. } else
  1527. if(p->pulse)
  1528. snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%sw", c + p->stripmsd);
  1529. else
  1530. snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%sw", c + p->stripmsd);
  1531. if (p->echotraining && (strlen(p->dop.dialstr) > 4)) {
  1532. memset(p->echorest, 'w', sizeof(p->echorest) - 1);
  1533. strcpy(p->echorest + (p->echotraining / 400) + 1, p->dop.dialstr + strlen(p->dop.dialstr) - 2);
  1534. p->echorest[sizeof(p->echorest) - 1] = '\0';
  1535. p->echobreak = 1;
  1536. p->dop.dialstr[strlen(p->dop.dialstr)-2] = '\0';
  1537. } else
  1538. p->echobreak = 0;
  1539. if (!res) {
  1540. if (ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop)) {
  1541. x = ZT_ONHOOK;
  1542. ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
  1543. ast_log(LOG_WARNING, "Dialing failed on channel %d: %s\n", p->channel, strerror(errno));
  1544. ast_mutex_unlock(&p->lock);
  1545. return -1;
  1546. }
  1547. } else
  1548. ast_log(LOG_DEBUG, "Deferring dialing...\n");
  1549. p->dialing = 1;
  1550. if (strlen(c + p->stripmsd) < 1) p->dialednone = 1;
  1551. ast_setstate(ast, AST_STATE_DIALING);
  1552. break;
  1553. case 0:
  1554. /* Special pseudo -- automatically up*/
  1555. ast_setstate(ast, AST_STATE_UP);
  1556. break;
  1557. case SIG_PRI:
  1558. /* We'll get it in a moment -- but use dialdest to store pre-setup_ack digits */
  1559. p->dialdest[0] = '\0';
  1560. break;
  1561. default:
  1562. ast_log(LOG_DEBUG, "not yet implemented\n");
  1563. ast_mutex_unlock(&p->lock);
  1564. return -1;
  1565. }
  1566. #ifdef ZAPATA_PRI
  1567. if (p->pri) {
  1568. struct pri_sr *sr;
  1569. c = strchr(dest, '/');
  1570. if (c)
  1571. c++;
  1572. else
  1573. c = dest;
  1574. if (ast->callerid && !p->hidecallerid) {
  1575. strncpy(callerid, ast->callerid, sizeof(callerid)-1);
  1576. ast_callerid_parse(callerid, &n, &l);
  1577. if (l) {
  1578. ast_shrink_phone_number(l);
  1579. if (!ast_isphonenumber(l))
  1580. l = NULL;
  1581. }
  1582. } else
  1583. l = NULL;
  1584. if (strlen(c) < p->stripmsd) {
  1585. ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
  1586. ast_mutex_unlock(&p->lock);
  1587. return -1;
  1588. }
  1589. if (p->sig != SIG_FXSKS) {
  1590. p->dop.op = ZT_DIAL_OP_REPLACE;
  1591. s = strchr(c + p->stripmsd, 'w');
  1592. if (s) {
  1593. if (strlen(s))
  1594. snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%s", s);
  1595. else
  1596. p->dop.dialstr[0] = '\0';
  1597. *s = '\0';
  1598. } else {
  1599. p->dop.dialstr[0] = '\0';
  1600. }
  1601. }
  1602. if (pri_grab(p, p->pri)) {
  1603. ast_log(LOG_WARNING, "Failed to grab PRI!\n");
  1604. ast_mutex_unlock(&p->lock);
  1605. return -1;
  1606. }
  1607. if (!(p->call = pri_new_call(p->pri->pri))) {
  1608. ast_log(LOG_WARNING, "Unable to create call on channel %d\n", p->channel);
  1609. pri_rel(p->pri);
  1610. ast_mutex_unlock(&p->lock);
  1611. return -1;
  1612. }
  1613. if (!(sr = pri_sr_new())) {
  1614. ast_log(LOG_WARNING, "Failed to allocate setup request channel %d\n", p->channel);
  1615. pri_rel(p->pri);
  1616. ast_mutex_unlock(&p->lock);
  1617. }
  1618. if (p->bearer || (p->sig == SIG_FXSKS)) {
  1619. if (p->bearer) {
  1620. ast_log(LOG_DEBUG, "Oooh, I have a bearer on %d (%d:%d)\n", PVT_TO_CHANNEL(p->bearer), p->bearer->logicalspan, p->bearer->channel);
  1621. p->bearer->call = p->call;
  1622. } else
  1623. ast_log(LOG_DEBUG, "I'm being setup with no bearer right now...\n");
  1624. pri_set_crv(p->pri->pri, p->call, p->channel, 0);
  1625. }
  1626. p->digital = ast_test_flag(ast,AST_FLAG_DIGITAL);
  1627. pri_sr_set_channel(sr, p->bearer ? PVT_TO_CHANNEL(p->bearer) : PVT_TO_CHANNEL(p),
  1628. p->pri->nodetype == PRI_NETWORK ? 0 : 1, 1);
  1629. pri_sr_set_bearer(sr, p->digital ? PRI_TRANS_CAP_DIGITAL : PRI_TRANS_CAP_SPEECH,
  1630. (p->digital ? -1 :
  1631. ((p->law == ZT_LAW_ALAW) ? PRI_LAYER_1_ALAW : PRI_LAYER_1_ULAW)));
  1632. pri_sr_set_called(sr, c + p->stripmsd, p->pri->dialplan - 1, s ? 1 : 0);
  1633. pri_sr_set_caller(sr, l, n, p->pri->localdialplan - 1,
  1634. l ? (ast->restrictcid ? PRES_PROHIB_USER_NUMBER_PASSED_SCREEN :
  1635. (p->use_callingpres ? ast->callingpres : PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN)) :
  1636. PRES_NUMBER_NOT_AVAILABLE);
  1637. if (pri_setup(p->pri->pri, p->call, sr)) {
  1638. ast_log(LOG_WARNING, "Unable to setup call to %s\n", c + p->stripmsd);
  1639. pri_rel(p->pri);
  1640. ast_mutex_unlock(&p->lock);
  1641. pri_sr_free(sr);
  1642. return -1;
  1643. }
  1644. pri_sr_free(sr);
  1645. ast_setstate(ast, AST_STATE_DIALING);
  1646. pri_rel(p->pri);
  1647. }
  1648. #endif
  1649. ast_mutex_unlock(&p->lock);
  1650. return 0;
  1651. }
  1652. static void destroy_zt_pvt(struct zt_pvt **pvt)
  1653. {
  1654. struct zt_pvt *p = *pvt;
  1655. ast_mutex_destroy(&p->lock);
  1656. free(p);
  1657. *pvt = NULL;
  1658. }
  1659. static int destroy_channel(struct zt_pvt *prev, struct zt_pvt *cur, int now)
  1660. {
  1661. int owned = 0;
  1662. int i = 0;
  1663. if (!now) {
  1664. if (cur->owner) {
  1665. owned = 1;
  1666. }
  1667. for (i = 0; i < 3; i++) {
  1668. if (cur->subs[i].owner) {
  1669. owned = 1;
  1670. }
  1671. }
  1672. if (!owned) {
  1673. if (prev) {
  1674. prev->next = cur->next;
  1675. if (prev->next)
  1676. prev->next->prev = prev;
  1677. else
  1678. ifend = prev;
  1679. } else {
  1680. iflist = cur->next;
  1681. if (iflist)
  1682. iflist->prev = NULL;
  1683. else
  1684. ifend = NULL;
  1685. }
  1686. if (cur->subs[SUB_REAL].zfd > -1) {
  1687. zt_close(cur->subs[SUB_REAL].zfd);
  1688. }
  1689. destroy_zt_pvt(&cur);
  1690. }
  1691. } else {
  1692. if (prev) {
  1693. prev->next = cur->next;
  1694. if (prev->next)
  1695. prev->next->prev = prev;
  1696. else
  1697. ifend = prev;
  1698. } else {
  1699. iflist = cur->next;
  1700. if (iflist)
  1701. iflist->prev = NULL;
  1702. else
  1703. ifend = NULL;
  1704. }
  1705. if (cur->subs[SUB_REAL].zfd > -1) {
  1706. zt_close(cur->subs[SUB_REAL].zfd);
  1707. }
  1708. destroy_zt_pvt(&cur);
  1709. }
  1710. return 0;
  1711. }
  1712. #ifdef ZAPATA_PRI
  1713. int pri_is_up(struct zt_pri *pri)
  1714. {
  1715. int x;
  1716. for (x=0;x<NUM_DCHANS;x++) {
  1717. if (pri->dchanavail[x] == DCHAN_AVAILABLE)
  1718. return 1;
  1719. }
  1720. return 0;
  1721. }
  1722. int pri_assign_bearer(struct zt_pvt *crv, struct zt_pri *pri, struct zt_pvt *bearer)
  1723. {
  1724. bearer->owner = &inuse;
  1725. bearer->master = crv;
  1726. crv->subs[SUB_REAL].zfd = bearer->subs[SUB_REAL].zfd;
  1727. if (crv->subs[SUB_REAL].owner)
  1728. crv->subs[SUB_REAL].owner->fds[0] = crv->subs[SUB_REAL].zfd;
  1729. crv->bearer = bearer;
  1730. crv->call = bearer->call;
  1731. crv->pri = pri;
  1732. return 0;
  1733. }
  1734. static char *pri_order(int level)
  1735. {
  1736. switch(level) {
  1737. case 0:
  1738. return "Primary";
  1739. case 1:
  1740. return "Secondary";
  1741. case 2:
  1742. return "Tertiary";
  1743. case 3:
  1744. return "Quaternary";
  1745. default:
  1746. return "<Unknown>";
  1747. }
  1748. }
  1749. int pri_find_dchan(struct zt_pri *pri)
  1750. {
  1751. int oldslot = -1;
  1752. struct pri *old;
  1753. int newslot = -1;
  1754. int x;
  1755. old = pri->pri;
  1756. for(x=0;x<NUM_DCHANS;x++) {
  1757. if ((pri->dchanavail[x] == DCHAN_AVAILABLE) && (newslot < 0))
  1758. newslot = x;
  1759. if (pri->dchans[x] == old) {
  1760. oldslot = x;
  1761. }
  1762. }
  1763. if (newslot < 0) {
  1764. newslot = 0;
  1765. ast_log(LOG_WARNING, "No D-channels available! Using Primary channel %d as D-channel anyway!\n",
  1766. pri->dchannels[newslot]);
  1767. }
  1768. if (old && (oldslot != newslot))
  1769. ast_log(LOG_NOTICE, "Switching from from d-channel %d to channel %d!\n",
  1770. pri->dchannels[oldslot], pri->dchannels[newslot]);
  1771. pri->pri = pri->dchans[newslot];
  1772. return 0;
  1773. }
  1774. #endif
  1775. static int zt_hangup(struct ast_channel *ast)
  1776. {
  1777. int res;
  1778. int index,x, law;
  1779. struct zt_pvt *p = ast->pvt->pvt;
  1780. struct zt_pvt *tmp = NULL;
  1781. struct zt_pvt *prev = NULL;
  1782. ZT_PARAMS par;
  1783. if (option_debug)
  1784. ast_log(LOG_DEBUG, "zt_hangup(%s)\n", ast->name);
  1785. if (!ast->pvt->pvt) {
  1786. ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
  1787. return 0;
  1788. }
  1789. ast_mutex_lock(&p->lock);
  1790. index = zt_get_index(ast, p, 1);
  1791. if (p->sig == SIG_PRI) {
  1792. x = 1;
  1793. ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
  1794. }
  1795. x = 0;
  1796. zt_confmute(p, 0);
  1797. restore_gains(p);
  1798. if (p->origcallerid) {
  1799. strncpy(p->callerid, p->origcallerid, sizeof(p->callerid) - 1);
  1800. free(p->origcallerid);
  1801. p->origcallerid = NULL;
  1802. }
  1803. if (p->dsp)
  1804. ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax);
  1805. if (p->exten)
  1806. p->exten[0] = '\0';
  1807. ast_log(LOG_DEBUG, "Hangup: channel: %d index = %d, normal = %d, callwait = %d, thirdcall = %d\n",
  1808. p->channel, index, p->subs[SUB_REAL].zfd, p->subs[SUB_CALLWAIT].zfd, p->subs[SUB_THREEWAY].zfd);
  1809. p->ignoredtmf = 0;
  1810. if (index > -1) {
  1811. /* Real channel, do some fixup */
  1812. p->subs[index].owner = NULL;
  1813. p->subs[index].needanswer = 0;
  1814. p->subs[index].needringing = 0;
  1815. p->subs[index].needbusy = 0;
  1816. p->subs[index].needcongestion = 0;
  1817. p->subs[index].linear = 0;
  1818. p->subs[index].needcallerid = 0;
  1819. zt_setlinear(p->subs[index].zfd, 0);
  1820. if (index == SUB_REAL) {
  1821. if ((p->subs[SUB_CALLWAIT].zfd > -1) && (p->subs[SUB_THREEWAY].zfd > -1)) {
  1822. ast_log(LOG_DEBUG, "Normal call hung up with both three way call and a call waiting call in place?\n");
  1823. if (p->subs[SUB_CALLWAIT].inthreeway) {
  1824. /* We had flipped over to answer a callwait and now it's gone */
  1825. ast_log(LOG_DEBUG, "We were flipped over to the callwait, moving back and unowning.\n");
  1826. /* Move to the call-wait, but un-own us until they flip back. */
  1827. swap_subs(p, SUB_CALLWAIT, SUB_REAL);
  1828. unalloc_sub(p, SUB_CALLWAIT);
  1829. p->owner = NULL;
  1830. } else {
  1831. /* The three way hung up, but we still have a call wait */
  1832. ast_log(LOG_DEBUG, "We were in the threeway and have a callwait still. Ditching the threeway.\n");
  1833. swap_subs(p, SUB_THREEWAY, SUB_REAL);
  1834. unalloc_sub(p, SUB_THREEWAY);
  1835. if (p->subs[SUB_REAL].inthreeway) {
  1836. /* This was part of a three way call. Immediately make way for
  1837. another call */
  1838. ast_log(LOG_DEBUG, "Call was complete, setting owner to former third call\n");
  1839. p->owner = p->subs[SUB_REAL].owner;
  1840. } else {
  1841. /* This call hasn't been completed yet... Set owner to NULL */
  1842. ast_log(LOG_DEBUG, "Call was incomplete, setting owner to NULL\n");
  1843. p->owner = NULL;
  1844. }
  1845. p->subs[SUB_REAL].inthreeway = 0;
  1846. }
  1847. } else if (p->subs[SUB_CALLWAIT].zfd > -1) {
  1848. /* Move to the call-wait and switch back to them. */
  1849. swap_subs(p, SUB_CALLWAIT, SUB_REAL);
  1850. unalloc_sub(p, SUB_CALLWAIT);
  1851. p->owner = p->subs[SUB_REAL].owner;
  1852. if (p->owner->_state != AST_STATE_UP)
  1853. p->subs[SUB_REAL].needanswer = 1;
  1854. if (p->subs[SUB_REAL].owner->bridge)
  1855. ast_moh_stop(p->subs[SUB_REAL].owner->bridge);
  1856. } else if (p->subs[SUB_THREEWAY].zfd > -1) {
  1857. swap_subs(p, SUB_THREEWAY, SUB_REAL);
  1858. unalloc_sub(p, SUB_THREEWAY);
  1859. if (p->subs[SUB_REAL].inthreeway) {
  1860. /* This was part of a three way call. Immediately make way for
  1861. another call */
  1862. ast_log(LOG_DEBUG, "Call was complete, setting owner to former third call\n");
  1863. p->owner = p->subs[SUB_REAL].owner;
  1864. } else {
  1865. /* This call hasn't been completed yet... Set owner to NULL */
  1866. ast_log(LOG_DEBUG, "Call was incomplete, setting owner to NULL\n");
  1867. p->owner = NULL;
  1868. }
  1869. p->subs[SUB_REAL].inthreeway = 0;
  1870. }
  1871. } else if (index == SUB_CALLWAIT) {
  1872. /* Ditch the holding callwait call, and immediately make it availabe */
  1873. if (p->subs[SUB_CALLWAIT].inthreeway) {
  1874. /* This is actually part of a three way, placed on hold. Place the third part
  1875. on music on hold now */
  1876. if (p->subs[SUB_THREEWAY].owner && p->subs[SUB_THREEWAY].owner->bridge)
  1877. ast_moh_start(p->subs[SUB_THREEWAY].owner->bridge, NULL);
  1878. p->subs[SUB_THREEWAY].inthreeway = 0;
  1879. /* Make it the call wait now */
  1880. swap_subs(p, SUB_CALLWAIT, SUB_THREEWAY);
  1881. unalloc_sub(p, SUB_THREEWAY);
  1882. } else
  1883. unalloc_sub(p, SUB_CALLWAIT);
  1884. } else if (index == SUB_THREEWAY) {
  1885. if (p->subs[SUB_CALLWAIT].inthreeway) {
  1886. /* The other party of the three way call is currently in a call-wait state.
  1887. Start music on hold for them, and take the main guy out of the third call */
  1888. if (p->subs[SUB_CALLWAIT].owner && p->subs[SUB_CALLWAIT].owner->bridge)
  1889. ast_moh_start(p->subs[SUB_CALLWAIT].owner->bridge, NULL);
  1890. p->subs[SUB_CALLWAIT].inthreeway = 0;
  1891. }
  1892. p->subs[SUB_REAL].inthreeway = 0;
  1893. /* If this was part of a three way call index, let us make
  1894. another three way call */
  1895. unalloc_sub(p, SUB_THREEWAY);
  1896. } else {
  1897. /* This wasn't any sort of call, but how are we an index? */
  1898. ast_log(LOG_WARNING, "Index found but not any type of call?\n");
  1899. }
  1900. }
  1901. if (!p->subs[SUB_REAL].owner && !p->subs[SUB_CALLWAIT].owner && !p->subs[SUB_THREEWAY].owner) {
  1902. p->owner = NULL;
  1903. p->ringt = 0;
  1904. p->distinctivering = 0;
  1905. p->confirmanswer = 0;
  1906. p->cidrings = 1;
  1907. p->outgoing = 0;
  1908. p->digital = 0;
  1909. p->faxhandled = 0;
  1910. p->pulsedial = 0;
  1911. p->onhooktime = time(NULL);
  1912. #ifdef ZAPATA_PRI
  1913. p->proceeding = 0;
  1914. p->alerting = 0;
  1915. p->setup_ack = 0;
  1916. #endif
  1917. if (p->dsp) {
  1918. ast_dsp_free(p->dsp);
  1919. p->dsp = NULL;
  1920. }
  1921. law = ZT_LAW_DEFAULT;
  1922. res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETLAW, &law);
  1923. if (res < 0)
  1924. ast_log(LOG_WARNING, "Unable to set law on channel %d to default\n", p->channel);
  1925. /* Perform low level hangup if no owner left */
  1926. #ifdef ZAPATA_PRI
  1927. if (p->pri) {
  1928. /* Make sure we have a call (or REALLY have a call in the case of a PRI) */
  1929. if (p->call && (!p->bearer || (p->bearer->call == p->call))) {
  1930. if (!pri_grab(p, p->pri)) {
  1931. if (p->alreadyhungup) {
  1932. ast_log(LOG_DEBUG, "Already hungup... Calling hangup once, and clearing call\n");
  1933. pri_hangup(p->pri->pri, p->call, -1);
  1934. p->call = NULL;
  1935. if (p->bearer)
  1936. p->bearer->call = NULL;
  1937. } else {
  1938. char *cause = pbx_builtin_getvar_helper(ast,"PRI_CAUSE");
  1939. int icause = ast->hangupcause ? ast->hangupcause : -1;
  1940. ast_log(LOG_DEBUG, "Not yet hungup... Calling hangup once with icause, and clearing call\n");
  1941. p->alreadyhungup = 1;
  1942. if (p->bearer)
  1943. p->bearer->alreadyhungup = 1;
  1944. if (cause) {
  1945. if (atoi(cause))
  1946. icause = atoi(cause);
  1947. }
  1948. pri_hangup(p->pri->pri, p->call, icause);
  1949. }
  1950. if (res < 0)
  1951. ast_log(LOG_WARNING, "pri_disconnect failed\n");
  1952. pri_rel(p->pri);
  1953. } else {
  1954. ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
  1955. res = -1;
  1956. }
  1957. } else {
  1958. if (p->bearer)
  1959. ast_log(LOG_DEBUG, "Bearer call is %p, while ours is still %p\n", p->bearer->call, p->call);
  1960. p->call = NULL;
  1961. res = 0;
  1962. }
  1963. }
  1964. #endif
  1965. #ifdef ZAPATA_R2
  1966. if (p->sig == SIG_R2) {
  1967. if (p->hasr2call) {
  1968. mfcr2_DropCall(p->r2, NULL, UC_NORMAL_CLEARING);
  1969. p->hasr2call = 0;
  1970. res = 0;
  1971. } else
  1972. res = 0;
  1973. }
  1974. #endif
  1975. if (p->sig && (p->sig != SIG_PRI) && (p->sig != SIG_R2))
  1976. res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_ONHOOK);
  1977. if (res < 0) {
  1978. ast_log(LOG_WARNING, "Unable to hangup line %s\n", ast->name);
  1979. }
  1980. switch(p->sig) {
  1981. case SIG_FXOGS:
  1982. case SIG_FXOLS:
  1983. case SIG_FXOKS:
  1984. res = ioctl(p->subs[SUB_REAL].zfd, ZT_GET_PARAMS, &par);
  1985. if (!res) {
  1986. #if 0
  1987. ast_log(LOG_DEBUG, "Hanging up channel %d, offhook = %d\n", p->channel, par.rxisoffhook);
  1988. #endif
  1989. /* If they're off hook, try playing congestion */
  1990. if ((par.rxisoffhook) && (!p->radio))
  1991. tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION);
  1992. else
  1993. tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
  1994. }
  1995. break;
  1996. case SIG_FXSGS:
  1997. case SIG_FXSLS:
  1998. case SIG_FXSKS:
  1999. /* Make sure we're not made available for at least two seconds assuming
  2000. we were actually used for an inbound or outbound call. */
  2001. if (ast->_state != AST_STATE_RESERVED) {
  2002. time(&p->guardtime);
  2003. p->guardtime += 2;
  2004. }
  2005. break;
  2006. default:
  2007. tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
  2008. }
  2009. if (p->cidspill)
  2010. free(p->cidspill);
  2011. if (p->sig)
  2012. zt_disable_ec(p);
  2013. x = 0;
  2014. ast_channel_setoption(ast,AST_OPTION_TONE_VERIFY,&x,sizeof(char),0);
  2015. ast_channel_setoption(ast,AST_OPTION_TDD,&x,sizeof(char),0);
  2016. p->didtdd = 0;
  2017. p->cidspill = NULL;
  2018. p->callwaitcas = 0;
  2019. p->callwaiting = p->permcallwaiting;
  2020. p->hidecallerid = p->permhidecallerid;
  2021. p->dialing = 0;
  2022. p->rdnis[0] = '\0';
  2023. update_conf(p);
  2024. reset_conf(p);
  2025. /* Restore data mode */
  2026. if (p->sig == SIG_PRI) {
  2027. x = 0;
  2028. ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
  2029. }
  2030. #ifdef ZAPATA_PRI
  2031. if (p->bearer) {
  2032. ast_log(LOG_DEBUG, "Freeing up bearer channel %d\n", p->bearer->channel);
  2033. /* Free up the bearer channel as well, and
  2034. don't use its file descriptor anymore */
  2035. update_conf(p->bearer);
  2036. reset_conf(p->bearer);
  2037. p->bearer->owner = NULL;
  2038. p->bearer = NULL;
  2039. p->subs[SUB_REAL].zfd = -1;
  2040. p->pri = NULL;
  2041. }
  2042. #endif
  2043. restart_monitor();
  2044. }
  2045. p->callwaitingrepeat = 0;
  2046. p->cidcwexpire = 0;
  2047. ast->pvt->pvt = NULL;
  2048. ast_mutex_unlock(&p->lock);
  2049. ast_mutex_lock(&usecnt_lock);
  2050. usecnt--;
  2051. if (usecnt < 0)
  2052. ast_log(LOG_WARNING, "Usecnt < 0???\n");
  2053. ast_mutex_unlock(&usecnt_lock);
  2054. ast_update_use_count();
  2055. if (option_verbose > 2)
  2056. ast_verbose( VERBOSE_PREFIX_3 "Hungup '%s'\n", ast->name);
  2057. ast_mutex_lock(&iflock);
  2058. tmp = iflist;
  2059. prev = NULL;
  2060. if (p->destroy) {
  2061. while (tmp) {
  2062. if (tmp == p) {
  2063. destroy_channel(prev, tmp, 0);
  2064. break;
  2065. } else {
  2066. prev = tmp;
  2067. tmp = tmp->next;
  2068. }
  2069. }
  2070. }
  2071. ast_mutex_unlock(&iflock);
  2072. return 0;
  2073. }
  2074. static int zt_answer(struct ast_channel *ast)
  2075. {
  2076. struct zt_pvt *p = ast->pvt->pvt;
  2077. int res=0;
  2078. int index;
  2079. int oldstate = ast->_state;
  2080. ast_setstate(ast, AST_STATE_UP);
  2081. ast_mutex_lock(&p->lock);
  2082. index = zt_get_index(ast, p, 0);
  2083. if (index < 0)
  2084. index = SUB_REAL;
  2085. /* nothing to do if a radio channel */
  2086. if (p->radio) {
  2087. ast_mutex_unlock(&p->lock);
  2088. return 0;
  2089. }
  2090. switch(p->sig) {
  2091. case SIG_FXSLS:
  2092. case SIG_FXSGS:
  2093. case SIG_FXSKS:
  2094. p->ringt = 0;
  2095. /* Fall through */
  2096. case SIG_EM:
  2097. case SIG_EM_E1:
  2098. case SIG_EMWINK:
  2099. case SIG_FEATD:
  2100. case SIG_FEATDMF:
  2101. case SIG_E911:
  2102. case SIG_FEATB:
  2103. case SIG_SF:
  2104. case SIG_SFWINK:
  2105. case SIG_SF_FEATD:
  2106. case SIG_SF_FEATDMF:
  2107. case SIG_SF_FEATB:
  2108. case SIG_FXOLS:
  2109. case SIG_FXOGS:
  2110. case SIG_FXOKS:
  2111. /* Pick up the line */
  2112. ast_log(LOG_DEBUG, "Took %s off hook\n", ast->name);
  2113. res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_OFFHOOK);
  2114. tone_zone_play_tone(p->subs[index].zfd, -1);
  2115. p->dialing = 0;
  2116. if ((index == SUB_REAL) && p->subs[SUB_THREEWAY].inthreeway) {
  2117. if (oldstate == AST_STATE_RINGING) {
  2118. ast_log(LOG_DEBUG, "Finally swapping real and threeway\n");
  2119. tone_zone_play_tone(p->subs[SUB_THREEWAY].zfd, -1);
  2120. swap_subs(p, SUB_THREEWAY, SUB_REAL);
  2121. p->owner = p->subs[SUB_REAL].owner;
  2122. }
  2123. }
  2124. if (p->sig & __ZT_SIG_FXS) {
  2125. zt_enable_ec(p);
  2126. zt_train_ec(p);
  2127. }
  2128. break;
  2129. #ifdef ZAPATA_PRI
  2130. case SIG_PRI:
  2131. /* Send a pri acknowledge */
  2132. if (!pri_grab(p, p->pri)) {
  2133. p->proceeding = 2;
  2134. res = pri_answer(p->pri->pri, p->call, 0, 1);
  2135. pri_rel(p->pri);
  2136. } else {
  2137. ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
  2138. res= -1;
  2139. }
  2140. break;
  2141. #endif
  2142. #ifdef ZAPATA_R2
  2143. case SIG_R2:
  2144. res = mfcr2_AnswerCall(p->r2, NULL);
  2145. if (res)
  2146. ast_log(LOG_WARNING, "R2 Answer call failed :( on %s\n", ast->name);
  2147. break;
  2148. #endif
  2149. case 0:
  2150. ast_mutex_unlock(&p->lock);
  2151. return 0;
  2152. default:
  2153. ast_log(LOG_WARNING, "Don't know how to answer signalling %d (channel %d)\n", p->sig, p->channel);
  2154. res = -1;
  2155. }
  2156. ast_mutex_unlock(&p->lock);
  2157. return res;
  2158. }
  2159. static int zt_setoption(struct ast_channel *chan, int option, void *data, int datalen)
  2160. {
  2161. char *cp;
  2162. int x;
  2163. struct zt_pvt *p = chan->pvt->pvt;
  2164. if ((option != AST_OPTION_TONE_VERIFY) && (option != AST_OPTION_AUDIO_MODE) &&
  2165. (option != AST_OPTION_TDD) && (option != AST_OPTION_RELAXDTMF))
  2166. {
  2167. errno = ENOSYS;
  2168. return -1;
  2169. }
  2170. cp = (char *)data;
  2171. if ((!cp) || (datalen < 1))
  2172. {
  2173. errno = EINVAL;
  2174. return -1;
  2175. }
  2176. switch(option) {
  2177. case AST_OPTION_TONE_VERIFY:
  2178. if (!p->dsp)
  2179. break;
  2180. switch(*cp) {
  2181. case 1:
  2182. ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: MUTECONF(1) on %s\n",chan->name);
  2183. ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MUTECONF | p->dtmfrelax); /* set mute mode if desired */
  2184. break;
  2185. case 2:
  2186. ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: MUTECONF/MAX(2) on %s\n",chan->name);
  2187. ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MUTECONF | DSP_DIGITMODE_MUTEMAX | p->dtmfrelax); /* set mute mode if desired */
  2188. break;
  2189. default:
  2190. ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: OFF(0) on %s\n",chan->name);
  2191. ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax); /* set mute mode if desired */
  2192. break;
  2193. }
  2194. break;
  2195. case AST_OPTION_TDD: /* turn on or off TDD */
  2196. if (!*cp) { /* turn it off */
  2197. ast_log(LOG_DEBUG, "Set option TDD MODE, value: OFF(0) on %s\n",chan->name);
  2198. if (p->tdd) tdd_free(p->tdd);
  2199. p->tdd = 0;
  2200. p->mate = 0;
  2201. break;
  2202. }
  2203. if (*cp == 2)
  2204. ast_log(LOG_DEBUG, "Set option TDD MODE, value: MATE(2) on %s\n",chan->name);
  2205. else ast_log(LOG_DEBUG, "Set option TDD MODE, value: ON(1) on %s\n",chan->name);
  2206. p->mate = 0;
  2207. zt_disable_ec(p);
  2208. /* otherwise, turn it on */
  2209. if (!p->didtdd) { /* if havent done it yet */
  2210. unsigned char mybuf[41000],*buf;
  2211. int size,res,fd,len;
  2212. int index;
  2213. struct pollfd fds[1];
  2214. buf = mybuf;
  2215. memset(buf,0x7f,sizeof(mybuf)); /* set to silence */
  2216. ast_tdd_gen_ecdisa(buf + 16000,16000); /* put in tone */
  2217. len = 40000;
  2218. index = zt_get_index(chan, p, 0);
  2219. if (index < 0) {
  2220. ast_log(LOG_WARNING, "No index in TDD?\n");
  2221. return -1;
  2222. }
  2223. fd = p->subs[index].zfd;
  2224. while(len) {
  2225. if (ast_check_hangup(chan)) return -1;
  2226. size = len;
  2227. if (size > READ_SIZE)
  2228. size = READ_SIZE;
  2229. fds[0].fd = fd;
  2230. fds[0].events = POLLPRI | POLLOUT;
  2231. res = poll(fds, 1, -1);
  2232. if (!res) {
  2233. ast_log(LOG_DEBUG, "poll (for write) ret. 0 on channel %d\n", p->channel);
  2234. continue;
  2235. }
  2236. /* if got exception */
  2237. if (fds[0].revents & POLLPRI) return -1;
  2238. if (!(fds[0].revents & POLLOUT)) {
  2239. ast_log(LOG_DEBUG, "write fd not ready on channel %d\n", p->channel);
  2240. continue;
  2241. }
  2242. res = write(fd, buf, size);
  2243. if (res != size) {
  2244. if (res == -1) return -1;
  2245. ast_log(LOG_DEBUG, "Write returned %d (%s) on channel %d\n", res, strerror(errno), p->channel);
  2246. break;
  2247. }
  2248. len -= size;
  2249. buf += size;
  2250. }
  2251. p->didtdd = 1; /* set to have done it now */
  2252. }
  2253. if (*cp == 2) { /* Mate mode */
  2254. if (p->tdd) tdd_free(p->tdd);
  2255. p->tdd = 0;
  2256. p->mate = 1;
  2257. break;
  2258. }
  2259. if (!p->tdd) { /* if we dont have one yet */
  2260. p->tdd = tdd_new(); /* allocate one */
  2261. }
  2262. break;
  2263. case AST_OPTION_RELAXDTMF: /* Relax DTMF decoding (or not) */
  2264. if (!*cp)
  2265. {
  2266. ast_log(LOG_DEBUG, "Set option RELAX DTMF, value: OFF(0) on %s\n",chan->name);
  2267. x = 0;
  2268. }
  2269. else
  2270. {
  2271. ast_log(LOG_DEBUG, "Set option RELAX DTMF, value: ON(1) on %s\n",chan->name);
  2272. x = 1;
  2273. }
  2274. ast_dsp_digitmode(p->dsp,x ? DSP_DIGITMODE_RELAXDTMF : DSP_DIGITMODE_DTMF | p->dtmfrelax);
  2275. break;
  2276. case AST_OPTION_AUDIO_MODE: /* Set AUDIO mode (or not) */
  2277. if (!*cp)
  2278. {
  2279. ast_log(LOG_DEBUG, "Set option AUDIO MODE, value: OFF(0) on %s\n",chan->name);
  2280. x = 0;
  2281. zt_disable_ec(p);
  2282. }
  2283. else
  2284. {
  2285. ast_log(LOG_DEBUG, "Set option AUDIO MODE, value: ON(1) on %s\n",chan->name);
  2286. x = 1;
  2287. }
  2288. if (ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &x) == -1)
  2289. ast_log(LOG_WARNING, "Unable to set audio mode on channel %d to %d\n", p->channel, x);
  2290. break;
  2291. }
  2292. errno = 0;
  2293. return 0;
  2294. }
  2295. static void zt_unlink(struct zt_pvt *slave, struct zt_pvt *master, int needlock)
  2296. {
  2297. /* Unlink a specific slave or all slaves/masters from a given master */
  2298. int x;
  2299. int hasslaves;
  2300. if (!master)
  2301. return;
  2302. if (needlock) {
  2303. ast_mutex_lock(&master->lock);
  2304. if (slave) {
  2305. while(ast_mutex_trylock(&slave->lock)) {
  2306. ast_mutex_unlock(&master->lock);
  2307. usleep(1);
  2308. ast_mutex_lock(&master->lock);
  2309. }
  2310. }
  2311. }
  2312. hasslaves = 0;
  2313. for (x=0;x<MAX_SLAVES;x++) {
  2314. if (master->slaves[x]) {
  2315. if (!slave || (master->slaves[x] == slave)) {
  2316. /* Take slave out of the conference */
  2317. ast_log(LOG_DEBUG, "Unlinking slave %d from %d\n", master->slaves[x]->channel, master->channel);
  2318. conf_del(master, &master->slaves[x]->subs[SUB_REAL], SUB_REAL);
  2319. conf_del(master->slaves[x], &master->subs[SUB_REAL], SUB_REAL);
  2320. master->slaves[x]->master = NULL;
  2321. master->slaves[x] = NULL;
  2322. } else
  2323. hasslaves = 1;
  2324. }
  2325. if (!hasslaves)
  2326. master->inconference = 0;
  2327. }
  2328. if (!slave) {
  2329. if (master->master) {
  2330. /* Take master out of the conference */
  2331. conf_del(master->master, &master->subs[SUB_REAL], SUB_REAL);
  2332. conf_del(master, &master->master->subs[SUB_REAL], SUB_REAL);
  2333. hasslaves = 0;
  2334. for (x=0;x<MAX_SLAVES;x++) {
  2335. if (master->master->slaves[x] == master)
  2336. master->master->slaves[x] = NULL;
  2337. else if (master->master->slaves[x])
  2338. hasslaves = 1;
  2339. }
  2340. if (!hasslaves)
  2341. master->master->inconference = 0;
  2342. }
  2343. master->master = NULL;
  2344. }
  2345. update_conf(master);
  2346. if (needlock) {
  2347. if (slave)
  2348. ast_mutex_unlock(&slave->lock);
  2349. ast_mutex_unlock(&master->lock);
  2350. }
  2351. }
  2352. static void zt_link(struct zt_pvt *slave, struct zt_pvt *master) {
  2353. int x;
  2354. if (!slave || !master) {
  2355. ast_log(LOG_WARNING, "Tried to link to/from NULL??\n");
  2356. return;
  2357. }
  2358. for (x=0;x<MAX_SLAVES;x++) {
  2359. if (!master->slaves[x]) {
  2360. master->slaves[x] = slave;
  2361. break;
  2362. }
  2363. }
  2364. if (x >= MAX_SLAVES) {
  2365. ast_log(LOG_WARNING, "Replacing slave %d with new slave, %d\n", master->slaves[MAX_SLAVES - 1]->channel, slave->channel);
  2366. master->slaves[MAX_SLAVES - 1] = slave;
  2367. }
  2368. if (slave->master)
  2369. ast_log(LOG_WARNING, "Replacing master %d with new master, %d\n", slave->master->channel, master->channel);
  2370. slave->master = master;
  2371. ast_log(LOG_DEBUG, "Making %d slave to master %d at %d\n", slave->channel, master->channel, x);
  2372. }
  2373. static int zt_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc)
  2374. {
  2375. struct ast_channel *who = NULL, *cs[3];
  2376. struct zt_pvt *p0, *p1, *op0, *op1;
  2377. struct zt_pvt *master=NULL, *slave=NULL;
  2378. struct ast_frame *f;
  2379. int to;
  2380. int inconf = 0;
  2381. int nothingok = 0;
  2382. int ofd1, ofd2;
  2383. int oi1, oi2, i1 = -1, i2 = -1, t1, t2;
  2384. int os1 = -1, os2 = -1;
  2385. struct ast_channel *oc1, *oc2;
  2386. /* if need DTMF, cant native bridge */
  2387. if (flags & (AST_BRIDGE_DTMF_CHANNEL_0 | AST_BRIDGE_DTMF_CHANNEL_1))
  2388. return -2;
  2389. ast_mutex_lock(&c0->lock);
  2390. ast_mutex_lock(&c1->lock);
  2391. p0 = c0->pvt->pvt;
  2392. p1 = c1->pvt->pvt;
  2393. /* cant do pseudo-channels here */
  2394. if (!p0 || (!p0->sig) || !p1 || (!p1->sig)) {
  2395. ast_mutex_unlock(&c0->lock);
  2396. ast_mutex_unlock(&c1->lock);
  2397. return -2;
  2398. }
  2399. op0 = p0 = c0->pvt->pvt;
  2400. op1 = p1 = c1->pvt->pvt;
  2401. ofd1 = c0->fds[0];
  2402. ofd2 = c1->fds[0];
  2403. oi1 = zt_get_index(c0, p0, 0);
  2404. oi2 = zt_get_index(c1, p1, 0);
  2405. oc1 = p0->owner;
  2406. oc2 = p1->owner;
  2407. if ((oi1 < 0) || (oi2 < 0)) {
  2408. ast_mutex_unlock(&c0->lock);
  2409. ast_mutex_unlock(&c1->lock);
  2410. return -1;
  2411. }
  2412. ast_mutex_lock(&p0->lock);
  2413. if (ast_mutex_trylock(&p1->lock)) {
  2414. /* Don't block, due to potential for deadlock */
  2415. ast_mutex_unlock(&p0->lock);
  2416. ast_mutex_unlock(&c0->lock);
  2417. ast_mutex_unlock(&c1->lock);
  2418. ast_log(LOG_NOTICE, "Avoiding deadlock...\n");
  2419. return -3;
  2420. }
  2421. if ((oi1 == SUB_REAL) && (oi2 == SUB_REAL)) {
  2422. if (!p0->owner || !p1->owner) {
  2423. /* Currently unowned -- Do nothing. */
  2424. nothingok = 1;
  2425. } else {
  2426. /* If we don't have a call-wait in a 3-way, and we aren't in a 3-way, we can be master */
  2427. if (!p0->subs[SUB_CALLWAIT].inthreeway && !p1->subs[SUB_REAL].inthreeway) {
  2428. master = p0;
  2429. slave = p1;
  2430. inconf = 1;
  2431. } else if (!p1->subs[SUB_CALLWAIT].inthreeway && !p0->subs[SUB_REAL].inthreeway) {
  2432. master = p1;
  2433. slave = p0;
  2434. inconf = 1;
  2435. } else {
  2436. ast_log(LOG_WARNING, "Huh? Both calls are callwaits or 3-ways? That's clever...?\n");
  2437. ast_log(LOG_WARNING, "p0: chan %d/%d/CW%d/3W%d, p1: chan %d/%d/CW%d/3W%d\n", p0->channel, oi1, (p0->subs[SUB_CALLWAIT].zfd > -1) ? 1 : 0, p0->subs[SUB_REAL].inthreeway,
  2438. p0->channel, oi1, (p1->subs[SUB_CALLWAIT].zfd > -1) ? 1 : 0, p1->subs[SUB_REAL].inthreeway);
  2439. }
  2440. }
  2441. } else if ((oi1 == SUB_REAL) && (oi2 == SUB_THREEWAY)) {
  2442. if (p1->subs[SUB_THREEWAY].inthreeway) {
  2443. master = p1;
  2444. slave = p0;
  2445. } else {
  2446. nothingok = 1;
  2447. }
  2448. } else if ((oi1 == SUB_THREEWAY) && (oi2 == SUB_REAL)) {
  2449. if (p0->subs[SUB_THREEWAY].inthreeway) {
  2450. master = p0;
  2451. slave = p1;
  2452. } else {
  2453. nothingok = 1;
  2454. }
  2455. } else if ((oi1 == SUB_REAL) && (oi2 == SUB_CALLWAIT)) {
  2456. /* We have a real and a call wait. If we're in a three way call, put us in it, otherwise,
  2457. don't put us in anything */
  2458. if (p1->subs[SUB_CALLWAIT].inthreeway) {
  2459. master = p1;
  2460. slave = p0;
  2461. } else {
  2462. nothingok = 1;
  2463. }
  2464. } else if ((oi1 == SUB_CALLWAIT) && (oi2 == SUB_REAL)) {
  2465. /* Same as previous */
  2466. if (p0->subs[SUB_CALLWAIT].inthreeway) {
  2467. master = p0;
  2468. slave = p1;
  2469. } else {
  2470. nothingok = 1;
  2471. }
  2472. }
  2473. ast_log(LOG_DEBUG, "master: %d, slave: %d, nothingok: %d\n",
  2474. master ? master->channel : 0, slave ? slave->channel : 0, nothingok);
  2475. if (master && slave) {
  2476. /* Stop any tones, or play ringtone as appropriate. If they're bridged
  2477. in an active threeway call with a channel that is ringing, we should
  2478. indicate ringing. */
  2479. if ((oi2 == SUB_THREEWAY) &&
  2480. p1->subs[SUB_THREEWAY].inthreeway &&
  2481. p1->subs[SUB_REAL].owner &&
  2482. p1->subs[SUB_REAL].inthreeway &&
  2483. (p1->subs[SUB_REAL].owner->_state == AST_STATE_RINGING)) {
  2484. ast_log(LOG_DEBUG, "Playing ringback on %s since %s is in a ringing three-way\n", c0->name, c1->name);
  2485. tone_zone_play_tone(p0->subs[oi1].zfd, ZT_TONE_RINGTONE);
  2486. os2 = p1->subs[SUB_REAL].owner->_state;
  2487. } else {
  2488. ast_log(LOG_DEBUG, "Stoping tones on %d/%d talking to %d/%d\n", p0->channel, oi1, p1->channel, oi2);
  2489. tone_zone_play_tone(p0->subs[oi1].zfd, -1);
  2490. }
  2491. if ((oi1 == SUB_THREEWAY) &&
  2492. p0->subs[SUB_THREEWAY].inthreeway &&
  2493. p0->subs[SUB_REAL].owner &&
  2494. p0->subs[SUB_REAL].inthreeway &&
  2495. (p0->subs[SUB_REAL].owner->_state == AST_STATE_RINGING)) {
  2496. ast_log(LOG_DEBUG, "Playing ringback on %s since %s is in a ringing three-way\n", c1->name, c0->name);
  2497. tone_zone_play_tone(p1->subs[oi2].zfd, ZT_TONE_RINGTONE);
  2498. os1 = p0->subs[SUB_REAL].owner->_state;
  2499. } else {
  2500. ast_log(LOG_DEBUG, "Stoping tones on %d/%d talking to %d/%d\n", p1->channel, oi2, p0->channel, oi1);
  2501. tone_zone_play_tone(p1->subs[oi1].zfd, -1);
  2502. }
  2503. if ((oi1 == SUB_REAL) && (oi2 == SUB_REAL)) {
  2504. if (!p0->echocanbridged || !p1->echocanbridged) {
  2505. /* Disable echo cancellation if appropriate */
  2506. zt_disable_ec(p0);
  2507. zt_disable_ec(p1);
  2508. }
  2509. }
  2510. zt_link(slave, master);
  2511. master->inconference = inconf;
  2512. } else if (!nothingok)
  2513. ast_log(LOG_WARNING, "Can't link %d/%s with %d/%s\n", p0->channel, subnames[oi1], p1->channel, subnames[oi2]);
  2514. update_conf(p0);
  2515. update_conf(p1);
  2516. t1 = p0->subs[SUB_REAL].inthreeway;
  2517. t2 = p1->subs[SUB_REAL].inthreeway;
  2518. ast_mutex_unlock(&p0->lock);
  2519. ast_mutex_unlock(&p1->lock);
  2520. ast_mutex_unlock(&c0->lock);
  2521. ast_mutex_unlock(&c1->lock);
  2522. /* Native bridge failed */
  2523. if ((!master || !slave) && !nothingok) {
  2524. if (op0 == p0)
  2525. zt_enable_ec(p0);
  2526. if (op1 == p1)
  2527. zt_enable_ec(p1);
  2528. return -1;
  2529. }
  2530. cs[0] = c0;
  2531. cs[1] = c1;
  2532. cs[2] = NULL;
  2533. for (;;) {
  2534. /* Here's our main loop... Start by locking things, looking for private parts,
  2535. and then balking if anything is wrong */
  2536. ast_mutex_lock(&c0->lock);
  2537. ast_mutex_lock(&c1->lock);
  2538. p0 = c0->pvt->pvt;
  2539. p1 = c1->pvt->pvt;
  2540. if (op0 == p0)
  2541. i1 = zt_get_index(c0, p0, 1);
  2542. if (op1 == p1)
  2543. i2 = zt_get_index(c1, p1, 1);
  2544. ast_mutex_unlock(&c0->lock);
  2545. ast_mutex_unlock(&c1->lock);
  2546. if ((op0 != p0) || (op1 != p1) ||
  2547. (ofd1 != c0->fds[0]) ||
  2548. (ofd2 != c1->fds[0]) ||
  2549. (p0->subs[SUB_REAL].owner && (os1 > -1) && (os1 != p0->subs[SUB_REAL].owner->_state)) ||
  2550. (p1->subs[SUB_REAL].owner && (os2 > -1) && (os2 != p1->subs[SUB_REAL].owner->_state)) ||
  2551. (oc1 != p0->owner) ||
  2552. (oc2 != p1->owner) ||
  2553. (t1 != p0->subs[SUB_REAL].inthreeway) ||
  2554. (t2 != p1->subs[SUB_REAL].inthreeway) ||
  2555. (oi1 != i1) ||
  2556. (oi2 != i2)) {
  2557. if (slave && master)
  2558. zt_unlink(slave, master, 1);
  2559. ast_log(LOG_DEBUG, "Something changed out on %d/%d to %d/%d, returning -3 to restart\n",
  2560. op0->channel, oi1, op1->channel, oi2);
  2561. if (op0 == p0)
  2562. zt_enable_ec(p0);
  2563. if (op1 == p1)
  2564. zt_enable_ec(p1);
  2565. return -3;
  2566. }
  2567. to = -1;
  2568. who = ast_waitfor_n(cs, 2, &to);
  2569. if (!who) {
  2570. ast_log(LOG_DEBUG, "Ooh, empty read...\n");
  2571. continue;
  2572. }
  2573. if (who->pvt->pvt == op0)
  2574. op0->ignoredtmf = 1;
  2575. else if (who->pvt->pvt == op1)
  2576. op1->ignoredtmf = 1;
  2577. f = ast_read(who);
  2578. if (who->pvt->pvt == op0)
  2579. op0->ignoredtmf = 0;
  2580. else if (who->pvt->pvt == op1)
  2581. op1->ignoredtmf = 0;
  2582. if (!f) {
  2583. *fo = NULL;
  2584. *rc = who;
  2585. if (slave && master)
  2586. zt_unlink(slave, master, 1);
  2587. if (op0 == p0)
  2588. zt_enable_ec(p0);
  2589. if (op1 == p1)
  2590. zt_enable_ec(p1);
  2591. return 0;
  2592. }
  2593. if (f->frametype == AST_FRAME_DTMF) {
  2594. if (((who == c0) && (flags & AST_BRIDGE_DTMF_CHANNEL_0)) ||
  2595. ((who == c1) && (flags & AST_BRIDGE_DTMF_CHANNEL_1))) {
  2596. *fo = f;
  2597. *rc = who;
  2598. if (slave && master)
  2599. zt_unlink(slave, master, 1);
  2600. return 0;
  2601. } else if ((who == c0) && p0->pulsedial) {
  2602. ast_write(c1, f);
  2603. } else if ((who == c1) && p1->pulsedial) {
  2604. ast_write(c0, f);
  2605. }
  2606. }
  2607. ast_frfree(f);
  2608. /* Swap who gets priority */
  2609. cs[2] = cs[0];
  2610. cs[0] = cs[1];
  2611. cs[1] = cs[2];
  2612. }
  2613. }
  2614. static int zt_indicate(struct ast_channel *chan, int condition);
  2615. static int zt_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
  2616. {
  2617. struct zt_pvt *p = newchan->pvt->pvt;
  2618. int x;
  2619. ast_mutex_lock(&p->lock);
  2620. ast_log(LOG_DEBUG, "New owner for channel %d is %s\n", p->channel, newchan->name);
  2621. if (p->owner == oldchan) {
  2622. p->owner = newchan;
  2623. }
  2624. for (x=0;x<3;x++)
  2625. if (p->subs[x].owner == oldchan) {
  2626. if (!x)
  2627. zt_unlink(NULL, p, 0);
  2628. p->subs[x].owner = newchan;
  2629. }
  2630. if (newchan->_state == AST_STATE_RINGING)
  2631. zt_indicate(newchan, AST_CONTROL_RINGING);
  2632. update_conf(p);
  2633. ast_mutex_unlock(&p->lock);
  2634. return 0;
  2635. }
  2636. static int zt_ring_phone(struct zt_pvt *p)
  2637. {
  2638. int x;
  2639. int res;
  2640. /* Make sure our transmit state is on hook */
  2641. x = 0;
  2642. x = ZT_ONHOOK;
  2643. res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
  2644. do {
  2645. x = ZT_RING;
  2646. res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
  2647. #if 0
  2648. printf("Res: %d, error: %s\n", res, strerror(errno));
  2649. #endif
  2650. if (res) {
  2651. switch(errno) {
  2652. case EBUSY:
  2653. case EINTR:
  2654. /* Wait just in case */
  2655. usleep(10000);
  2656. continue;
  2657. case EINPROGRESS:
  2658. res = 0;
  2659. break;
  2660. default:
  2661. ast_log(LOG_WARNING, "Couldn't ring the phone: %s\n", strerror(errno));
  2662. res = 0;
  2663. }
  2664. }
  2665. } while (res);
  2666. return res;
  2667. }
  2668. static void *ss_thread(void *data);
  2669. static struct ast_channel *zt_new(struct zt_pvt *, int, int, int, int, int);
  2670. static int attempt_transfer(struct zt_pvt *p)
  2671. {
  2672. /* In order to transfer, we need at least one of the channels to
  2673. actually be in a call bridge. We can't conference two applications
  2674. together (but then, why would we want to?) */
  2675. if (p->subs[SUB_REAL].owner->bridge) {
  2676. /* The three-way person we're about to transfer to could still be in MOH, so
  2677. stop if now if appropriate */
  2678. if (p->subs[SUB_THREEWAY].owner->bridge)
  2679. ast_moh_stop(p->subs[SUB_THREEWAY].owner->bridge);
  2680. if (p->subs[SUB_THREEWAY].owner->_state == AST_STATE_RINGING) {
  2681. ast_indicate(p->subs[SUB_REAL].owner->bridge, AST_CONTROL_RINGING);
  2682. }
  2683. if (p->subs[SUB_REAL].owner->cdr) {
  2684. /* Move CDR from second channel to current one */
  2685. p->subs[SUB_THREEWAY].owner->cdr =
  2686. ast_cdr_append(p->subs[SUB_THREEWAY].owner->cdr, p->subs[SUB_REAL].owner->cdr);
  2687. p->subs[SUB_REAL].owner->cdr = NULL;
  2688. }
  2689. if (p->subs[SUB_REAL].owner->bridge->cdr) {
  2690. /* Move CDR from second channel's bridge to current one */
  2691. p->subs[SUB_THREEWAY].owner->cdr =
  2692. ast_cdr_append(p->subs[SUB_THREEWAY].owner->cdr, p->subs[SUB_REAL].owner->bridge->cdr);
  2693. p->subs[SUB_REAL].owner->bridge->cdr = NULL;
  2694. }
  2695. if (ast_channel_masquerade(p->subs[SUB_THREEWAY].owner, p->subs[SUB_REAL].owner->bridge)) {
  2696. ast_log(LOG_WARNING, "Unable to masquerade %s as %s\n",
  2697. p->subs[SUB_REAL].owner->bridge->name, p->subs[SUB_THREEWAY].owner->name);
  2698. return -1;
  2699. }
  2700. /* Orphan the channel after releasing the lock */
  2701. ast_mutex_unlock(&p->subs[SUB_THREEWAY].owner->lock);
  2702. unalloc_sub(p, SUB_THREEWAY);
  2703. } else if (p->subs[SUB_THREEWAY].owner->bridge) {
  2704. if (p->subs[SUB_REAL].owner->_state == AST_STATE_RINGING) {
  2705. ast_indicate(p->subs[SUB_THREEWAY].owner->bridge, AST_CONTROL_RINGING);
  2706. }
  2707. ast_moh_stop(p->subs[SUB_THREEWAY].owner->bridge);
  2708. if (p->subs[SUB_THREEWAY].owner->cdr) {
  2709. /* Move CDR from second channel to current one */
  2710. p->subs[SUB_REAL].owner->cdr =
  2711. ast_cdr_append(p->subs[SUB_REAL].owner->cdr, p->subs[SUB_THREEWAY].owner->cdr);
  2712. p->subs[SUB_THREEWAY].owner->cdr = NULL;
  2713. }
  2714. if (p->subs[SUB_THREEWAY].owner->bridge->cdr) {
  2715. /* Move CDR from second channel's bridge to current one */
  2716. p->subs[SUB_REAL].owner->cdr =
  2717. ast_cdr_append(p->subs[SUB_REAL].owner->cdr, p->subs[SUB_THREEWAY].owner->bridge->cdr);
  2718. p->subs[SUB_THREEWAY].owner->bridge->cdr = NULL;
  2719. }
  2720. if (ast_channel_masquerade(p->subs[SUB_REAL].owner, p->subs[SUB_THREEWAY].owner->bridge)) {
  2721. ast_log(LOG_WARNING, "Unable to masquerade %s as %s\n",
  2722. p->subs[SUB_THREEWAY].owner->bridge->name, p->subs[SUB_REAL].owner->name);
  2723. return -1;
  2724. }
  2725. /* Three-way is now the REAL */
  2726. swap_subs(p, SUB_THREEWAY, SUB_REAL);
  2727. ast_mutex_unlock(&p->subs[SUB_REAL].owner->lock);
  2728. unalloc_sub(p, SUB_THREEWAY);
  2729. /* Tell the caller not to hangup */
  2730. return 1;
  2731. } else {
  2732. ast_log(LOG_DEBUG, "Neither %s nor %s are in a bridge, nothing to transfer\n",
  2733. p->subs[SUB_REAL].owner->name, p->subs[SUB_THREEWAY].owner->name);
  2734. p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
  2735. }
  2736. return 0;
  2737. }
  2738. #ifdef ZAPATA_R2
  2739. static struct ast_frame *handle_r2_event(struct zt_pvt *p, mfcr2_event_t *e, int index)
  2740. {
  2741. struct ast_frame *f;
  2742. f = &p->subs[index].f;
  2743. if (!p->r2) {
  2744. ast_log(LOG_WARNING, "Huh? No R2 structure :(\n");
  2745. return NULL;
  2746. }
  2747. switch(e->e) {
  2748. case MFCR2_EVENT_BLOCKED:
  2749. if (option_verbose > 2)
  2750. ast_verbose(VERBOSE_PREFIX_3 "Channel %d blocked\n", p->channel);
  2751. break;
  2752. case MFCR2_EVENT_UNBLOCKED:
  2753. if (option_verbose > 2)
  2754. ast_verbose(VERBOSE_PREFIX_3 "Channel %d unblocked\n", p->channel);
  2755. break;
  2756. case MFCR2_EVENT_CONFIG_ERR:
  2757. if (option_verbose > 2)
  2758. ast_verbose(VERBOSE_PREFIX_3 "Config error on channel %d\n", p->channel);
  2759. break;
  2760. case MFCR2_EVENT_RING:
  2761. if (option_verbose > 2)
  2762. ast_verbose(VERBOSE_PREFIX_3 "Ring on channel %d\n", p->channel);
  2763. break;
  2764. case MFCR2_EVENT_HANGUP:
  2765. if (option_verbose > 2)
  2766. ast_verbose(VERBOSE_PREFIX_3 "Hangup on channel %d\n", p->channel);
  2767. break;
  2768. case MFCR2_EVENT_RINGING:
  2769. if (option_verbose > 2)
  2770. ast_verbose(VERBOSE_PREFIX_3 "Ringing on channel %d\n", p->channel);
  2771. break;
  2772. case MFCR2_EVENT_ANSWER:
  2773. if (option_verbose > 2)
  2774. ast_verbose(VERBOSE_PREFIX_3 "Answer on channel %d\n", p->channel);
  2775. break;
  2776. case MFCR2_EVENT_HANGUP_ACK:
  2777. if (option_verbose > 2)
  2778. ast_verbose(VERBOSE_PREFIX_3 "Hangup ACK on channel %d\n", p->channel);
  2779. break;
  2780. case MFCR2_EVENT_IDLE:
  2781. if (option_verbose > 2)
  2782. ast_verbose(VERBOSE_PREFIX_3 "Idle on channel %d\n", p->channel);
  2783. break;
  2784. default:
  2785. ast_log(LOG_WARNING, "Unknown MFC/R2 event %d\n", e->e);
  2786. break;
  2787. }
  2788. return f;
  2789. }
  2790. static mfcr2_event_t *r2_get_event_bits(struct zt_pvt *p)
  2791. {
  2792. int x;
  2793. int res;
  2794. mfcr2_event_t *e;
  2795. res = ioctl(p->subs[SUB_REAL].zfd, ZT_GETRXBITS, &x);
  2796. if (res) {
  2797. ast_log(LOG_WARNING, "Unable to check received bits\n");
  2798. return NULL;
  2799. }
  2800. if (!p->r2) {
  2801. ast_log(LOG_WARNING, "Odd, no R2 structure on channel %d\n", p->channel);
  2802. return NULL;
  2803. }
  2804. e = mfcr2_cas_signaling_event(p->r2, x);
  2805. return e;
  2806. }
  2807. #endif
  2808. static int check_for_conference(struct zt_pvt *p)
  2809. {
  2810. ZT_CONFINFO ci;
  2811. /* Fine if we already have a master, etc */
  2812. if (p->master || (p->confno > -1))
  2813. return 0;
  2814. memset(&ci, 0, sizeof(ci));
  2815. if (ioctl(p->subs[SUB_REAL].zfd, ZT_GETCONF, &ci)) {
  2816. ast_log(LOG_WARNING, "Failed to get conference info on channel %d\n", p->channel);
  2817. return 0;
  2818. }
  2819. /* If we have no master and don't have a confno, then
  2820. if we're in a conference, it's probably a MeetMe room or
  2821. some such, so don't let us 3-way out! */
  2822. if ((p->subs[SUB_REAL].curconf.confno != ci.confno) || (p->subs[SUB_REAL].curconf.confmode != ci.confmode)) {
  2823. if (option_verbose > 2)
  2824. ast_verbose(VERBOSE_PREFIX_3 "Avoiding 3-way call when in an external conference\n");
  2825. return 1;
  2826. }
  2827. return 0;
  2828. }
  2829. static int get_alarms(struct zt_pvt *p)
  2830. {
  2831. int res;
  2832. ZT_SPANINFO zi;
  2833. memset(&zi, 0, sizeof(zi));
  2834. zi.spanno = p->span;
  2835. res = ioctl(p->subs[SUB_REAL].zfd, ZT_SPANSTAT, &zi);
  2836. if (res < 0) {
  2837. ast_log(LOG_WARNING, "Unable to determine alarm on channel %d\n", p->channel);
  2838. return 0;
  2839. }
  2840. return zi.alarms;
  2841. }
  2842. static struct ast_frame *zt_handle_event(struct ast_channel *ast)
  2843. {
  2844. int res,x;
  2845. int index;
  2846. char *c;
  2847. struct zt_pvt *p = ast->pvt->pvt;
  2848. pthread_t threadid;
  2849. pthread_attr_t attr;
  2850. struct ast_channel *chan;
  2851. pthread_attr_init(&attr);
  2852. pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  2853. index = zt_get_index(ast, p, 0);
  2854. p->subs[index].f.frametype = AST_FRAME_NULL;
  2855. p->subs[index].f.datalen = 0;
  2856. p->subs[index].f.samples = 0;
  2857. p->subs[index].f.mallocd = 0;
  2858. p->subs[index].f.offset = 0;
  2859. p->subs[index].f.src = "zt_handle_event";
  2860. p->subs[index].f.data = NULL;
  2861. if (index < 0)
  2862. return &p->subs[index].f;
  2863. if (p->fake_event) {
  2864. res = p->fake_event;
  2865. p->fake_event = 0;
  2866. } else
  2867. res = zt_get_event(p->subs[index].zfd);
  2868. ast_log(LOG_DEBUG, "Got event %s(%d) on channel %d (index %d)\n", event2str(res), res, p->channel, index);
  2869. if (res & (ZT_EVENT_PULSEDIGIT | ZT_EVENT_DTMFUP)) {
  2870. if (res & ZT_EVENT_PULSEDIGIT)
  2871. p->pulsedial = 1;
  2872. else
  2873. p->pulsedial = 0;
  2874. ast_log(LOG_DEBUG, "Detected %sdigit '%c'\n", p->pulsedial ? "pulse " : "", res & 0xff);
  2875. #ifdef ZAPATA_PRI
  2876. if ((p->proceeding < 2) && p->sig==SIG_PRI && p->pri && p->pri->overlapdial) {
  2877. p->subs[index].f.frametype = AST_FRAME_NULL;
  2878. p->subs[index].f.subclass = 0;
  2879. }
  2880. #endif
  2881. p->subs[index].f.frametype = AST_FRAME_DTMF;
  2882. p->subs[index].f.subclass = res & 0xff;
  2883. /* Unmute conference, return the captured digit */
  2884. zt_confmute(p, 0);
  2885. return &p->subs[index].f;
  2886. }
  2887. if (res & ZT_EVENT_DTMFDOWN) {
  2888. ast_log(LOG_DEBUG, "DTMF Down '%c'\n", res & 0xff);
  2889. p->subs[index].f.frametype = AST_FRAME_NULL;
  2890. p->subs[index].f.subclass = 0;
  2891. zt_confmute(p, 1);
  2892. /* Mute conference, return null frame */
  2893. return &p->subs[index].f;
  2894. }
  2895. switch(res) {
  2896. case ZT_EVENT_BITSCHANGED:
  2897. if (p->sig == SIG_R2) {
  2898. #ifdef ZAPATA_R2
  2899. struct ast_frame *f = &p->subs[index].f;
  2900. mfcr2_event_t *e;
  2901. e = r2_get_event_bits(p);
  2902. if (e)
  2903. f = handle_r2_event(p, e, index);
  2904. return f;
  2905. #else
  2906. break;
  2907. #endif
  2908. }
  2909. ast_log(LOG_WARNING, "Recieved bits changed on %s signalling?\n", sig2str(p->sig));
  2910. case ZT_EVENT_PULSE_START:
  2911. /* Stop tone if there's a pulse start and the PBX isn't started */
  2912. if (!ast->pbx)
  2913. tone_zone_play_tone(p->subs[index].zfd, -1);
  2914. break;
  2915. case ZT_EVENT_DIALCOMPLETE:
  2916. if (p->inalarm) break;
  2917. if (p->radio) break;
  2918. if (ioctl(p->subs[index].zfd,ZT_DIALING,&x) == -1) {
  2919. ast_log(LOG_DEBUG, "ZT_DIALING ioctl failed on %s\n",ast->name);
  2920. return NULL;
  2921. }
  2922. if (!x) { /* if not still dialing in driver */
  2923. zt_enable_ec(p);
  2924. if (p->echobreak) {
  2925. zt_train_ec(p);
  2926. strncpy(p->dop.dialstr, p->echorest, sizeof(p->dop.dialstr) - 1);
  2927. p->dop.op = ZT_DIAL_OP_REPLACE;
  2928. res = ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop);
  2929. p->echobreak = 0;
  2930. } else {
  2931. p->dialing = 0;
  2932. if (p->sig == SIG_E911) {
  2933. /* if thru with dialing after offhook */
  2934. if (ast->_state == AST_STATE_DIALING_OFFHOOK) {
  2935. ast_setstate(ast, AST_STATE_UP);
  2936. p->subs[index].f.frametype = AST_FRAME_CONTROL;
  2937. p->subs[index].f.subclass = AST_CONTROL_ANSWER;
  2938. break;
  2939. } else { /* if to state wait for offhook to dial rest */
  2940. /* we now wait for off hook */
  2941. ast_setstate(ast,AST_STATE_DIALING_OFFHOOK);
  2942. }
  2943. }
  2944. if (ast->_state == AST_STATE_DIALING) {
  2945. if ((p->callprogress & 1) && CANPROGRESSDETECT(p) && p->dsp && p->outgoing) {
  2946. ast_log(LOG_DEBUG, "Done dialing, but waiting for progress detection before doing more...\n");
  2947. } else if (p->confirmanswer || (!p->dialednone && ((p->sig == SIG_EM) || (p->sig == SIG_EM_E1) || (p->sig == SIG_EMWINK) || (p->sig == SIG_FEATD) || (p->sig == SIG_FEATDMF) || (p->sig == SIG_E911) || (p->sig == SIG_FEATB) || (p->sig == SIG_SF) || (p->sig == SIG_SFWINK) || (p->sig == SIG_SF_FEATD) || (p->sig == SIG_SF_FEATDMF) || (p->sig == SIG_SF_FEATB)))) {
  2948. ast_setstate(ast, AST_STATE_RINGING);
  2949. } else {
  2950. ast_setstate(ast, AST_STATE_UP);
  2951. p->subs[index].f.frametype = AST_FRAME_CONTROL;
  2952. p->subs[index].f.subclass = AST_CONTROL_ANSWER;
  2953. }
  2954. }
  2955. }
  2956. }
  2957. break;
  2958. case ZT_EVENT_ALARM:
  2959. #ifdef ZAPATA_PRI
  2960. if (p->call) {
  2961. if (p->pri && p->pri->pri) {
  2962. if (!pri_grab(p, p->pri)) {
  2963. pri_hangup(p->pri->pri, p->call, -1);
  2964. pri_destroycall(p->pri->pri, p->call);
  2965. pri_rel(p->pri);
  2966. } else
  2967. ast_log(LOG_WARNING, "Failed to grab PRI!\n");
  2968. } else
  2969. ast_log(LOG_WARNING, "The PRI Call have not been destroyed\n");
  2970. }
  2971. if (p->owner)
  2972. p->owner->_softhangup |= AST_SOFTHANGUP_DEV;
  2973. p->call = NULL;
  2974. if (p->bearer)
  2975. p->bearer->inalarm = 1;
  2976. else
  2977. #endif
  2978. p->inalarm = 1;
  2979. res = get_alarms(p);
  2980. ast_log(LOG_WARNING, "Detected alarm on channel %d: %s\n", p->channel, alarm2str(res));
  2981. manager_event(EVENT_FLAG_SYSTEM, "Alarm",
  2982. "Alarm: %s\r\n"
  2983. "Channel: %d\r\n",
  2984. alarm2str(res), p->channel);
  2985. /* fall through intentionally */
  2986. case ZT_EVENT_ONHOOK:
  2987. if (p->radio)
  2988. {
  2989. p->subs[index].f.frametype = AST_FRAME_CONTROL;
  2990. p->subs[index].f.subclass = AST_CONTROL_RADIO_UNKEY;
  2991. break;
  2992. }
  2993. switch(p->sig) {
  2994. case SIG_FXOLS:
  2995. case SIG_FXOGS:
  2996. case SIG_FXOKS:
  2997. p->onhooktime = time(NULL);
  2998. p->msgstate = -1;
  2999. /* Check for some special conditions regarding call waiting */
  3000. if (index == SUB_REAL) {
  3001. /* The normal line was hung up */
  3002. if (p->subs[SUB_CALLWAIT].owner) {
  3003. /* There's a call waiting call, so ring the phone, but make it unowned in the mean time */
  3004. swap_subs(p, SUB_CALLWAIT, SUB_REAL);
  3005. if (option_verbose > 2)
  3006. ast_verbose(VERBOSE_PREFIX_3 "Channel %d still has (callwait) call, ringing phone\n", p->channel);
  3007. unalloc_sub(p, SUB_CALLWAIT);
  3008. #if 0
  3009. p->subs[index].needanswer = 0;
  3010. p->subs[index].needringing = 0;
  3011. #endif
  3012. p->callwaitingrepeat = 0;
  3013. p->cidcwexpire = 0;
  3014. p->owner = NULL;
  3015. /* Don't start streaming audio yet if the incoming call isn't up yet */
  3016. if (p->subs[SUB_REAL].owner->_state != AST_STATE_UP)
  3017. p->dialing = 1;
  3018. zt_ring_phone(p);
  3019. } else if (p->subs[SUB_THREEWAY].owner) {
  3020. struct timeval tv;
  3021. unsigned int mssinceflash;
  3022. /* Here we have to retain the lock on both the main channel, the 3-way channel, and
  3023. the private structure -- not especially easy or clean */
  3024. while(p->subs[SUB_THREEWAY].owner && ast_mutex_trylock(&p->subs[SUB_THREEWAY].owner->lock)) {
  3025. /* Yuck, didn't get the lock on the 3-way, gotta release everything and re-grab! */
  3026. ast_mutex_unlock(&p->lock);
  3027. ast_mutex_unlock(&ast->lock);
  3028. usleep(1);
  3029. /* We can grab ast and p in that order, without worry. We should make sure
  3030. nothing seriously bad has happened though like some sort of bizarre double
  3031. masquerade! */
  3032. ast_mutex_lock(&ast->lock);
  3033. ast_mutex_lock(&p->lock);
  3034. if (p->owner != ast) {
  3035. ast_log(LOG_WARNING, "This isn't good...\n");
  3036. return NULL;
  3037. }
  3038. }
  3039. if (!p->subs[SUB_THREEWAY].owner) {
  3040. ast_log(LOG_NOTICE, "Whoa, threeway disappeared kinda randomly.\n");
  3041. return NULL;
  3042. }
  3043. gettimeofday(&tv, NULL);
  3044. mssinceflash = (tv.tv_sec - p->flashtime.tv_sec) * 1000 + (tv.tv_usec - p->flashtime.tv_usec) / 1000;
  3045. ast_log(LOG_DEBUG, "Last flash was %d ms ago\n", mssinceflash);
  3046. if (mssinceflash < MIN_MS_SINCE_FLASH) {
  3047. /* It hasn't been long enough since the last flashook. This is probably a bounce on
  3048. hanging up. Hangup both channels now */
  3049. if (p->subs[SUB_THREEWAY].owner)
  3050. ast_queue_hangup(p->subs[SUB_THREEWAY].owner);
  3051. p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
  3052. ast_log(LOG_DEBUG, "Looks like a bounced flash, hanging up both calls on %d\n", p->channel);
  3053. ast_mutex_unlock(&p->subs[SUB_THREEWAY].owner->lock);
  3054. } else if ((ast->pbx) ||
  3055. (ast->_state == AST_STATE_UP)) {
  3056. if (p->transfer) {
  3057. /* In any case this isn't a threeway call anymore */
  3058. p->subs[SUB_REAL].inthreeway = 0;
  3059. p->subs[SUB_THREEWAY].inthreeway = 0;
  3060. if ((res = attempt_transfer(p)) < 0)
  3061. p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
  3062. else if (res) {
  3063. /* Don't actually hang up at this point */
  3064. if (p->subs[SUB_THREEWAY].owner)
  3065. ast_mutex_unlock(&p->subs[SUB_THREEWAY].owner->lock);
  3066. break;
  3067. }
  3068. } else
  3069. p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
  3070. if (p->subs[SUB_THREEWAY].owner)
  3071. ast_mutex_unlock(&p->subs[SUB_THREEWAY].owner->lock);
  3072. } else {
  3073. ast_mutex_unlock(&p->subs[SUB_THREEWAY].owner->lock);
  3074. /* Swap subs and dis-own channel */
  3075. swap_subs(p, SUB_THREEWAY, SUB_REAL);
  3076. p->owner = NULL;
  3077. /* Ring the phone */
  3078. zt_ring_phone(p);
  3079. }
  3080. }
  3081. } else {
  3082. ast_log(LOG_WARNING, "Got a hangup and my index is %d?\n", index);
  3083. }
  3084. /* Fall through */
  3085. default:
  3086. zt_disable_ec(p);
  3087. return NULL;
  3088. }
  3089. break;
  3090. case ZT_EVENT_RINGOFFHOOK:
  3091. if (p->inalarm) break;
  3092. if (p->radio)
  3093. {
  3094. p->subs[index].f.frametype = AST_FRAME_CONTROL;
  3095. p->subs[index].f.subclass = AST_CONTROL_RADIO_KEY;
  3096. break;
  3097. }
  3098. /* for E911, its supposed to wait for offhook then dial
  3099. the second half of the dial string */
  3100. if ((p->sig == SIG_E911) && (ast->_state == AST_STATE_DIALING_OFFHOOK)) {
  3101. c = strchr(p->dialdest, '/');
  3102. if (c)
  3103. c++;
  3104. else
  3105. c = p->dialdest;
  3106. if (*c) snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*0%s#", c);
  3107. else strncpy(p->dop.dialstr,"M*2#", sizeof(p->dop.dialstr) - 1);
  3108. if (strlen(p->dop.dialstr) > 4) {
  3109. memset(p->echorest, 'w', sizeof(p->echorest) - 1);
  3110. strcpy(p->echorest + (p->echotraining / 401) + 1, p->dop.dialstr + strlen(p->dop.dialstr) - 2);
  3111. p->echorest[sizeof(p->echorest) - 1] = '\0';
  3112. p->echobreak = 1;
  3113. p->dop.dialstr[strlen(p->dop.dialstr)-2] = '\0';
  3114. } else
  3115. p->echobreak = 0;
  3116. if (ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop)) {
  3117. x = ZT_ONHOOK;
  3118. ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
  3119. ast_log(LOG_WARNING, "Dialing failed on channel %d: %s\n", p->channel, strerror(errno));
  3120. return NULL;
  3121. }
  3122. p->dialing = 1;
  3123. return &p->subs[index].f;
  3124. }
  3125. switch(p->sig) {
  3126. case SIG_FXOLS:
  3127. case SIG_FXOGS:
  3128. case SIG_FXOKS:
  3129. switch(ast->_state) {
  3130. case AST_STATE_RINGING:
  3131. zt_enable_ec(p);
  3132. zt_train_ec(p);
  3133. p->subs[index].f.frametype = AST_FRAME_CONTROL;
  3134. p->subs[index].f.subclass = AST_CONTROL_ANSWER;
  3135. /* Make sure it stops ringing */
  3136. zt_set_hook(p->subs[index].zfd, ZT_OFFHOOK);
  3137. ast_log(LOG_DEBUG, "channel %d answered\n", p->channel);
  3138. if (p->cidspill) {
  3139. /* Cancel any running CallerID spill */
  3140. free(p->cidspill);
  3141. p->cidspill = NULL;
  3142. }
  3143. p->dialing = 0;
  3144. p->callwaitcas = 0;
  3145. if (p->confirmanswer) {
  3146. /* Ignore answer if "confirm answer" is enabled */
  3147. p->subs[index].f.frametype = AST_FRAME_NULL;
  3148. p->subs[index].f.subclass = 0;
  3149. } else if (!ast_strlen_zero(p->dop.dialstr)) {
  3150. /* nick@dccinc.com 4/3/03 - fxo should be able to do deferred dialing */
  3151. res = ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop);
  3152. if (res < 0) {
  3153. ast_log(LOG_WARNING, "Unable to initiate dialing on trunk channel %d\n", p->channel);
  3154. p->dop.dialstr[0] = '\0';
  3155. return NULL;
  3156. } else {
  3157. ast_log(LOG_DEBUG, "Sent FXO deferred digit string: %s\n", p->dop.dialstr);
  3158. p->subs[index].f.frametype = AST_FRAME_NULL;
  3159. p->subs[index].f.subclass = 0;
  3160. p->dialing = 1;
  3161. }
  3162. p->dop.dialstr[0] = '\0';
  3163. ast_setstate(ast, AST_STATE_DIALING);
  3164. } else
  3165. ast_setstate(ast, AST_STATE_UP);
  3166. return &p->subs[index].f;
  3167. case AST_STATE_DOWN:
  3168. ast_setstate(ast, AST_STATE_RING);
  3169. ast->rings = 1;
  3170. p->subs[index].f.frametype = AST_FRAME_CONTROL;
  3171. p->subs[index].f.subclass = AST_CONTROL_OFFHOOK;
  3172. ast_log(LOG_DEBUG, "channel %d picked up\n", p->channel);
  3173. return &p->subs[index].f;
  3174. case AST_STATE_UP:
  3175. /* Make sure it stops ringing */
  3176. zt_set_hook(p->subs[index].zfd, ZT_OFFHOOK);
  3177. /* Okay -- probably call waiting*/
  3178. if (p->owner->bridge)
  3179. ast_moh_stop(p->owner->bridge);
  3180. break;
  3181. case AST_STATE_RESERVED:
  3182. /* Start up dialtone */
  3183. if (has_voicemail(p))
  3184. res = tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_STUTTER);
  3185. else
  3186. res = tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_DIALTONE);
  3187. break;
  3188. default:
  3189. ast_log(LOG_WARNING, "FXO phone off hook in weird state %d??\n", ast->_state);
  3190. }
  3191. break;
  3192. case SIG_FXSLS:
  3193. case SIG_FXSGS:
  3194. case SIG_FXSKS:
  3195. if (ast->_state == AST_STATE_RING) {
  3196. p->ringt = RINGT;
  3197. }
  3198. /* Fall through */
  3199. case SIG_EM:
  3200. case SIG_EM_E1:
  3201. case SIG_EMWINK:
  3202. case SIG_FEATD:
  3203. case SIG_FEATDMF:
  3204. case SIG_E911:
  3205. case SIG_FEATB:
  3206. case SIG_SF:
  3207. case SIG_SFWINK:
  3208. case SIG_SF_FEATD:
  3209. case SIG_SF_FEATDMF:
  3210. case SIG_SF_FEATB:
  3211. if (ast->_state == AST_STATE_PRERING)
  3212. ast_setstate(ast, AST_STATE_RING);
  3213. if ((ast->_state == AST_STATE_DOWN) || (ast->_state == AST_STATE_RING)) {
  3214. if (option_debug)
  3215. ast_log(LOG_DEBUG, "Ring detected\n");
  3216. p->subs[index].f.frametype = AST_FRAME_CONTROL;
  3217. p->subs[index].f.subclass = AST_CONTROL_RING;
  3218. } else if (p->outgoing && ((ast->_state == AST_STATE_RINGING) || (ast->_state == AST_STATE_DIALING))) {
  3219. if (option_debug)
  3220. ast_log(LOG_DEBUG, "Line answered\n");
  3221. if (p->confirmanswer) {
  3222. p->subs[index].f.frametype = AST_FRAME_NULL;
  3223. p->subs[index].f.subclass = 0;
  3224. } else {
  3225. p->subs[index].f.frametype = AST_FRAME_CONTROL;
  3226. p->subs[index].f.subclass = AST_CONTROL_ANSWER;
  3227. ast_setstate(ast, AST_STATE_UP);
  3228. }
  3229. } else if (ast->_state != AST_STATE_RING)
  3230. ast_log(LOG_WARNING, "Ring/Off-hook in strange state %d on channel %d\n", ast->_state, p->channel);
  3231. break;
  3232. default:
  3233. ast_log(LOG_WARNING, "Don't know how to handle ring/off hoook for signalling %d\n", p->sig);
  3234. }
  3235. break;
  3236. case ZT_EVENT_RINGEROFF:
  3237. if (p->inalarm) break;
  3238. if (p->radio) break;
  3239. ast->rings++;
  3240. if ((ast->rings > p->cidrings) && (p->cidspill)) {
  3241. ast_log(LOG_WARNING, "Didn't finish Caller-ID spill. Cancelling.\n");
  3242. free(p->cidspill);
  3243. p->cidspill = NULL;
  3244. p->callwaitcas = 0;
  3245. }
  3246. p->subs[index].f.frametype = AST_FRAME_CONTROL;
  3247. p->subs[index].f.subclass = AST_CONTROL_RINGING;
  3248. break;
  3249. case ZT_EVENT_RINGERON:
  3250. break;
  3251. case ZT_EVENT_NOALARM:
  3252. p->inalarm = 0;
  3253. #ifdef ZAPATA_PRI
  3254. /* Extremely unlikely but just in case */
  3255. if (p->bearer)
  3256. p->bearer->inalarm = 0;
  3257. #endif
  3258. ast_log(LOG_NOTICE, "Alarm cleared on channel %d\n", p->channel);
  3259. manager_event(EVENT_FLAG_SYSTEM, "AlarmClear",
  3260. "Channel: %d\r\n", p->channel);
  3261. break;
  3262. case ZT_EVENT_WINKFLASH:
  3263. if (p->inalarm) break;
  3264. if (p->radio) break;
  3265. /* Remember last time we got a flash-hook */
  3266. gettimeofday(&p->flashtime, NULL);
  3267. switch(p->sig) {
  3268. case SIG_FXOLS:
  3269. case SIG_FXOGS:
  3270. case SIG_FXOKS:
  3271. ast_log(LOG_DEBUG, "Winkflash, index: %d, normal: %d, callwait: %d, thirdcall: %d\n",
  3272. index, p->subs[SUB_REAL].zfd, p->subs[SUB_CALLWAIT].zfd, p->subs[SUB_THREEWAY].zfd);
  3273. p->callwaitcas = 0;
  3274. if (index == SUB_REAL) {
  3275. if (p->subs[SUB_CALLWAIT].owner) {
  3276. /* Swap to call-wait */
  3277. swap_subs(p, SUB_REAL, SUB_CALLWAIT);
  3278. tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
  3279. p->owner = p->subs[SUB_REAL].owner;
  3280. ast_log(LOG_DEBUG, "Making %s the new owner\n", p->owner->name);
  3281. if (p->owner->_state == AST_STATE_RINGING) {
  3282. ast_setstate(p->owner, AST_STATE_UP);
  3283. p->subs[SUB_REAL].needanswer = 1;
  3284. }
  3285. p->callwaitingrepeat = 0;
  3286. p->cidcwexpire = 0;
  3287. /* Start music on hold if appropriate */
  3288. if (!p->subs[SUB_CALLWAIT].inthreeway && p->subs[SUB_CALLWAIT].owner->bridge)
  3289. ast_moh_start(p->subs[SUB_CALLWAIT].owner->bridge, NULL);
  3290. if (p->subs[SUB_REAL].owner->bridge)
  3291. ast_moh_stop(p->subs[SUB_REAL].owner->bridge);
  3292. } else if (!p->subs[SUB_THREEWAY].owner) {
  3293. char callerid[256];
  3294. if (p->threewaycalling && !check_for_conference(p)) {
  3295. if (p->zaptrcallerid && p->owner && p->owner->callerid)
  3296. strncpy(callerid, p->owner->callerid, sizeof(callerid) - 1);
  3297. /* XXX This section needs much more error checking!!! XXX */
  3298. /* Start a 3-way call if feasible */
  3299. if ((ast->pbx) ||
  3300. (ast->_state == AST_STATE_UP) ||
  3301. (ast->_state == AST_STATE_RING)) {
  3302. if (!alloc_sub(p, SUB_THREEWAY)) {
  3303. /* Make new channel */
  3304. chan = zt_new(p, AST_STATE_RESERVED, 0, SUB_THREEWAY, 0, 0);
  3305. if (p->zaptrcallerid) {
  3306. if (!p->origcallerid) {
  3307. p->origcallerid = malloc(strlen(p->callerid) + 1);
  3308. strncpy(p->origcallerid, p->callerid, strlen(p->callerid)); /* safe */
  3309. /* make sure p->origcallerid is terminated */
  3310. p->origcallerid[strlen(p->callerid)] = '\0';
  3311. }
  3312. strncpy(p->callerid, callerid, sizeof(p->callerid) -1);
  3313. }
  3314. /* Swap things around between the three-way and real call */
  3315. swap_subs(p, SUB_THREEWAY, SUB_REAL);
  3316. /* Disable echo canceller for better dialing */
  3317. zt_disable_ec(p);
  3318. res = tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_DIALRECALL);
  3319. if (res)
  3320. ast_log(LOG_WARNING, "Unable to start dial recall tone on channel %d\n", p->channel);
  3321. p->owner = chan;
  3322. if (chan && ast_pthread_create(&threadid, &attr, ss_thread, chan)) {
  3323. ast_log(LOG_WARNING, "Unable to start simple switch on channel %d\n", p->channel);
  3324. res = tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION);
  3325. zt_enable_ec(p);
  3326. ast_hangup(chan);
  3327. } else if (!chan) {
  3328. ast_log(LOG_WARNING, "Cannot allocate new structure on channel %d\n", p->channel);
  3329. } else {
  3330. if (option_verbose > 2)
  3331. ast_verbose(VERBOSE_PREFIX_3 "Started three way call on channel %d\n", p->channel);
  3332. /* Start music on hold if appropriate */
  3333. if (p->subs[SUB_THREEWAY].owner->bridge)
  3334. ast_moh_start(p->subs[SUB_THREEWAY].owner->bridge, NULL);
  3335. }
  3336. } else
  3337. ast_log(LOG_WARNING, "Unable to allocate three-way subchannel\n");
  3338. } else
  3339. ast_log(LOG_DEBUG, "Flash when call not up or ringing\n");
  3340. }
  3341. } else {
  3342. /* Already have a 3 way call */
  3343. if (p->subs[SUB_THREEWAY].inthreeway) {
  3344. /* Call is already up, drop the last person */
  3345. if (option_debug)
  3346. ast_log(LOG_DEBUG, "Got flash with three way call up, dropping last call on %d\n", p->channel);
  3347. /* If the primary call isn't answered yet, use it */
  3348. if ((p->subs[SUB_REAL].owner->_state != AST_STATE_UP) && (p->subs[SUB_THREEWAY].owner->_state == AST_STATE_UP)) {
  3349. /* Swap back -- we're droppign the real 3-way that isn't finished yet*/
  3350. swap_subs(p, SUB_THREEWAY, SUB_REAL);
  3351. p->owner = p->subs[SUB_REAL].owner;
  3352. }
  3353. /* Drop the last call and stop the conference */
  3354. if (option_verbose > 2)
  3355. ast_verbose(VERBOSE_PREFIX_3 "Dropping three-way call on %s\n", p->subs[SUB_THREEWAY].owner->name);
  3356. p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
  3357. p->subs[SUB_REAL].inthreeway = 0;
  3358. p->subs[SUB_THREEWAY].inthreeway = 0;
  3359. } else {
  3360. /* Lets see what we're up to */
  3361. if ((ast->pbx) ||
  3362. (ast->_state == AST_STATE_UP)) {
  3363. int otherindex = SUB_THREEWAY;
  3364. if (option_verbose > 2)
  3365. ast_verbose(VERBOSE_PREFIX_3 "Building conference on call on %s and %s\n", p->subs[SUB_THREEWAY].owner->name, p->subs[SUB_REAL].owner->name);
  3366. /* Put them in the threeway, and flip */
  3367. p->subs[SUB_THREEWAY].inthreeway = 1;
  3368. p->subs[SUB_REAL].inthreeway = 1;
  3369. if (ast->_state == AST_STATE_UP) {
  3370. swap_subs(p, SUB_THREEWAY, SUB_REAL);
  3371. otherindex = SUB_REAL;
  3372. }
  3373. if (p->subs[otherindex].owner && p->subs[otherindex].owner->bridge)
  3374. ast_moh_stop(p->subs[otherindex].owner->bridge);
  3375. p->owner = p->subs[SUB_REAL].owner;
  3376. if (ast->_state == AST_STATE_RINGING) {
  3377. ast_log(LOG_DEBUG, "Enabling ringtone on real and threeway\n");
  3378. res = tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_RINGTONE);
  3379. res = tone_zone_play_tone(p->subs[SUB_THREEWAY].zfd, ZT_TONE_RINGTONE);
  3380. }
  3381. } else {
  3382. if (option_verbose > 2)
  3383. ast_verbose(VERBOSE_PREFIX_3 "Dumping incomplete call on on %s\n", p->subs[SUB_THREEWAY].owner->name);
  3384. swap_subs(p, SUB_THREEWAY, SUB_REAL);
  3385. p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
  3386. p->owner = p->subs[SUB_REAL].owner;
  3387. if (p->subs[SUB_REAL].owner && p->subs[SUB_REAL].owner->bridge)
  3388. ast_moh_stop(p->subs[SUB_REAL].owner->bridge);
  3389. zt_enable_ec(p);
  3390. }
  3391. }
  3392. }
  3393. } else {
  3394. ast_log(LOG_WARNING, "Got flash hook with index %d on channel %d?!?\n", index, p->channel);
  3395. }
  3396. update_conf(p);
  3397. break;
  3398. case SIG_EM:
  3399. case SIG_EM_E1:
  3400. case SIG_EMWINK:
  3401. case SIG_FEATD:
  3402. case SIG_SF:
  3403. case SIG_SFWINK:
  3404. case SIG_SF_FEATD:
  3405. case SIG_FXSLS:
  3406. case SIG_FXSGS:
  3407. if (p->dialing)
  3408. ast_log(LOG_DEBUG, "Ignoring wink on channel %d\n", p->channel);
  3409. else
  3410. ast_log(LOG_DEBUG, "Got wink in weird state %d on channel %d\n", ast->_state, p->channel);
  3411. break;
  3412. case SIG_FEATDMF:
  3413. case SIG_E911:
  3414. case SIG_FEATB:
  3415. case SIG_SF_FEATDMF:
  3416. case SIG_SF_FEATB:
  3417. /* FGD MF *Must* wait for wink */
  3418. if (!ast_strlen_zero(p->dop.dialstr))
  3419. res = ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop);
  3420. else if (res < 0) {
  3421. ast_log(LOG_WARNING, "Unable to initiate dialing on trunk channel %d\n", p->channel);
  3422. p->dop.dialstr[0] = '\0';
  3423. return NULL;
  3424. } else
  3425. ast_log(LOG_DEBUG, "Sent deferred digit string: %s\n", p->dop.dialstr);
  3426. p->dop.dialstr[0] = '\0';
  3427. break;
  3428. default:
  3429. ast_log(LOG_WARNING, "Don't know how to handle ring/off hoook for signalling %d\n", p->sig);
  3430. }
  3431. break;
  3432. case ZT_EVENT_HOOKCOMPLETE:
  3433. if (p->inalarm) break;
  3434. if (p->radio) break;
  3435. switch(p->sig) {
  3436. case SIG_FXSLS: /* only interesting for FXS */
  3437. case SIG_FXSGS:
  3438. case SIG_FXSKS:
  3439. case SIG_EM:
  3440. case SIG_EM_E1:
  3441. case SIG_EMWINK:
  3442. case SIG_FEATD:
  3443. case SIG_SF:
  3444. case SIG_SFWINK:
  3445. case SIG_SF_FEATD:
  3446. if (!ast_strlen_zero(p->dop.dialstr))
  3447. res = ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop);
  3448. else if (res < 0) {
  3449. ast_log(LOG_WARNING, "Unable to initiate dialing on trunk channel %d\n", p->channel);
  3450. p->dop.dialstr[0] = '\0';
  3451. return NULL;
  3452. } else
  3453. ast_log(LOG_DEBUG, "Sent deferred digit string: %s\n", p->dop.dialstr);
  3454. p->dop.dialstr[0] = '\0';
  3455. p->dop.op = ZT_DIAL_OP_REPLACE;
  3456. break;
  3457. case SIG_FEATDMF:
  3458. case SIG_E911:
  3459. case SIG_FEATB:
  3460. case SIG_SF_FEATDMF:
  3461. case SIG_SF_FEATB:
  3462. ast_log(LOG_DEBUG, "Got hook complete in MF FGD, waiting for wink now on channel %d\n",p->channel);
  3463. break;
  3464. default:
  3465. break;
  3466. }
  3467. break;
  3468. default:
  3469. ast_log(LOG_DEBUG, "Dunno what to do with event %d on channel %d\n", res, p->channel);
  3470. }
  3471. return &p->subs[index].f;
  3472. }
  3473. static struct ast_frame *__zt_exception(struct ast_channel *ast)
  3474. {
  3475. struct zt_pvt *p = ast->pvt->pvt;
  3476. int res;
  3477. int usedindex=-1;
  3478. int index;
  3479. struct ast_frame *f;
  3480. index = zt_get_index(ast, p, 1);
  3481. p->subs[index].f.frametype = AST_FRAME_NULL;
  3482. p->subs[index].f.datalen = 0;
  3483. p->subs[index].f.samples = 0;
  3484. p->subs[index].f.mallocd = 0;
  3485. p->subs[index].f.offset = 0;
  3486. p->subs[index].f.subclass = 0;
  3487. p->subs[index].f.delivery.tv_sec = 0;
  3488. p->subs[index].f.delivery.tv_usec = 0;
  3489. p->subs[index].f.src = "zt_exception";
  3490. p->subs[index].f.data = NULL;
  3491. if ((!p->owner) && (!p->radio)) {
  3492. /* If nobody owns us, absorb the event appropriately, otherwise
  3493. we loop indefinitely. This occurs when, during call waiting, the
  3494. other end hangs up our channel so that it no longer exists, but we
  3495. have neither FLASH'd nor ONHOOK'd to signify our desire to
  3496. change to the other channel. */
  3497. if (p->fake_event) {
  3498. res = p->fake_event;
  3499. p->fake_event = 0;
  3500. } else
  3501. res = zt_get_event(p->subs[SUB_REAL].zfd);
  3502. /* Switch to real if there is one and this isn't something really silly... */
  3503. if ((res != ZT_EVENT_RINGEROFF) && (res != ZT_EVENT_RINGERON) &&
  3504. (res != ZT_EVENT_HOOKCOMPLETE)) {
  3505. ast_log(LOG_DEBUG, "Restoring owner of channel %d on event %d\n", p->channel, res);
  3506. p->owner = p->subs[SUB_REAL].owner;
  3507. if (p->owner && p->owner->bridge)
  3508. ast_moh_stop(p->owner->bridge);
  3509. }
  3510. switch(res) {
  3511. case ZT_EVENT_ONHOOK:
  3512. zt_disable_ec(p);
  3513. if (p->owner) {
  3514. if (option_verbose > 2)
  3515. ast_verbose(VERBOSE_PREFIX_3 "Channel %s still has call, ringing phone\n", p->owner->name);
  3516. zt_ring_phone(p);
  3517. p->callwaitingrepeat = 0;
  3518. p->cidcwexpire = 0;
  3519. } else
  3520. ast_log(LOG_WARNING, "Absorbed on hook, but nobody is left!?!?\n");
  3521. update_conf(p);
  3522. break;
  3523. case ZT_EVENT_RINGOFFHOOK:
  3524. zt_set_hook(p->subs[SUB_REAL].zfd, ZT_OFFHOOK);
  3525. if (p->owner && (p->owner->_state == AST_STATE_RINGING)) {
  3526. p->subs[SUB_REAL].needanswer = 1;
  3527. p->dialing = 0;
  3528. }
  3529. break;
  3530. case ZT_EVENT_HOOKCOMPLETE:
  3531. case ZT_EVENT_RINGERON:
  3532. case ZT_EVENT_RINGEROFF:
  3533. /* Do nothing */
  3534. break;
  3535. case ZT_EVENT_WINKFLASH:
  3536. gettimeofday(&p->flashtime, NULL);
  3537. if (p->owner) {
  3538. if (option_verbose > 2)
  3539. ast_verbose(VERBOSE_PREFIX_3 "Channel %d flashed to other channel %s\n", p->channel, p->owner->name);
  3540. if (p->owner->_state != AST_STATE_UP) {
  3541. /* Answer if necessary */
  3542. usedindex = zt_get_index(p->owner, p, 0);
  3543. if (usedindex > -1) {
  3544. p->subs[usedindex].needanswer = 1;
  3545. }
  3546. ast_setstate(p->owner, AST_STATE_UP);
  3547. }
  3548. p->callwaitingrepeat = 0;
  3549. p->cidcwexpire = 0;
  3550. if (p->owner->bridge)
  3551. ast_moh_stop(p->owner->bridge);
  3552. } else
  3553. ast_log(LOG_WARNING, "Absorbed on hook, but nobody is left!?!?\n");
  3554. update_conf(p);
  3555. break;
  3556. default:
  3557. ast_log(LOG_WARNING, "Don't know how to absorb event %s\n", event2str(res));
  3558. }
  3559. f = &p->subs[index].f;
  3560. return f;
  3561. }
  3562. if (!p->radio) ast_log(LOG_DEBUG, "Exception on %d, channel %d\n", ast->fds[0],p->channel);
  3563. /* If it's not us, return NULL immediately */
  3564. if (ast != p->owner) {
  3565. ast_log(LOG_WARNING, "We're %s, not %s\n", ast->name, p->owner->name);
  3566. f = &p->subs[index].f;
  3567. return f;
  3568. }
  3569. f = zt_handle_event(ast);
  3570. return f;
  3571. }
  3572. struct ast_frame *zt_exception(struct ast_channel *ast)
  3573. {
  3574. struct zt_pvt *p = ast->pvt->pvt;
  3575. struct ast_frame *f;
  3576. ast_mutex_lock(&p->lock);
  3577. f = __zt_exception(ast);
  3578. ast_mutex_unlock(&p->lock);
  3579. return f;
  3580. }
  3581. struct ast_frame *zt_read(struct ast_channel *ast)
  3582. {
  3583. struct zt_pvt *p = ast->pvt->pvt;
  3584. int res;
  3585. int index;
  3586. void *readbuf;
  3587. struct ast_frame *f;
  3588. ast_mutex_lock(&p->lock);
  3589. index = zt_get_index(ast, p, 0);
  3590. /* Hang up if we don't really exist */
  3591. if (index < 0) {
  3592. ast_log(LOG_WARNING, "We dont exist?\n");
  3593. ast_mutex_unlock(&p->lock);
  3594. return NULL;
  3595. }
  3596. if (p->radio && p->inalarm) return NULL;
  3597. p->subs[index].f.frametype = AST_FRAME_NULL;
  3598. p->subs[index].f.datalen = 0;
  3599. p->subs[index].f.samples = 0;
  3600. p->subs[index].f.mallocd = 0;
  3601. p->subs[index].f.offset = 0;
  3602. p->subs[index].f.subclass = 0;
  3603. p->subs[index].f.delivery.tv_sec = 0;
  3604. p->subs[index].f.delivery.tv_usec = 0;
  3605. p->subs[index].f.src = "zt_read";
  3606. p->subs[index].f.data = NULL;
  3607. /* make sure it sends initial key state as first frame */
  3608. if (p->radio && (!p->firstradio))
  3609. {
  3610. ZT_PARAMS ps;
  3611. ps.channo = p->channel;
  3612. if (ioctl(p->subs[SUB_REAL].zfd, ZT_GET_PARAMS, &ps) < 0) {
  3613. ast_mutex_unlock(&p->lock);
  3614. return NULL;
  3615. }
  3616. p->firstradio = 1;
  3617. p->subs[index].f.frametype = AST_FRAME_CONTROL;
  3618. if (ps.rxisoffhook)
  3619. {
  3620. p->subs[index].f.subclass = AST_CONTROL_RADIO_KEY;
  3621. }
  3622. else
  3623. {
  3624. p->subs[index].f.subclass = AST_CONTROL_RADIO_UNKEY;
  3625. }
  3626. ast_mutex_unlock(&p->lock);
  3627. return &p->subs[index].f;
  3628. }
  3629. if (p->ringt == 1) {
  3630. ast_mutex_unlock(&p->lock);
  3631. return NULL;
  3632. }
  3633. else if (p->ringt > 0)
  3634. p->ringt--;
  3635. if (p->subs[index].needringing) {
  3636. /* Send ringing frame if requested */
  3637. p->subs[index].needringing = 0;
  3638. p->subs[index].f.frametype = AST_FRAME_CONTROL;
  3639. p->subs[index].f.subclass = AST_CONTROL_RINGING;
  3640. ast_setstate(ast, AST_STATE_RINGING);
  3641. ast_mutex_unlock(&p->lock);
  3642. return &p->subs[index].f;
  3643. }
  3644. if (p->subs[index].needbusy) {
  3645. /* Send busy frame if requested */
  3646. p->subs[index].needbusy = 0;
  3647. p->subs[index].f.frametype = AST_FRAME_CONTROL;
  3648. p->subs[index].f.subclass = AST_CONTROL_BUSY;
  3649. ast_mutex_unlock(&p->lock);
  3650. return &p->subs[index].f;
  3651. }
  3652. if (p->subs[index].needcongestion) {
  3653. /* Send congestion frame if requested */
  3654. p->subs[index].needcongestion = 0;
  3655. p->subs[index].f.frametype = AST_FRAME_CONTROL;
  3656. p->subs[index].f.subclass = AST_CONTROL_CONGESTION;
  3657. ast_mutex_unlock(&p->lock);
  3658. return &p->subs[index].f;
  3659. }
  3660. if (p->subs[index].needcallerid) {
  3661. ast_set_callerid(ast, !ast_strlen_zero(p->lastcallerid) ? p->lastcallerid : NULL, 1);
  3662. p->subs[index].needcallerid = 0;
  3663. }
  3664. if (p->subs[index].needanswer) {
  3665. /* Send answer frame if requested */
  3666. p->subs[index].needanswer = 0;
  3667. p->subs[index].f.frametype = AST_FRAME_CONTROL;
  3668. p->subs[index].f.subclass = AST_CONTROL_ANSWER;
  3669. ast_setstate(ast, AST_STATE_UP);
  3670. ast_mutex_unlock(&p->lock);
  3671. return &p->subs[index].f;
  3672. }
  3673. if (ast->pvt->rawreadformat == AST_FORMAT_SLINEAR) {
  3674. if (!p->subs[index].linear) {
  3675. p->subs[index].linear = 1;
  3676. res = zt_setlinear(p->subs[index].zfd, p->subs[index].linear);
  3677. if (res)
  3678. ast_log(LOG_WARNING, "Unable to set channel %d (index %d) to linear mode.\n", p->channel, index);
  3679. }
  3680. } else if ((ast->pvt->rawreadformat == AST_FORMAT_ULAW) ||
  3681. (ast->pvt->rawreadformat == AST_FORMAT_ALAW)) {
  3682. if (p->subs[index].linear) {
  3683. p->subs[index].linear = 0;
  3684. res = zt_setlinear(p->subs[index].zfd, p->subs[index].linear);
  3685. if (res)
  3686. ast_log(LOG_WARNING, "Unable to set channel %d (index %d) to campanded mode.\n", p->channel, index);
  3687. }
  3688. } else {
  3689. ast_log(LOG_WARNING, "Don't know how to read frames in format %s\n", ast_getformatname(ast->pvt->rawreadformat));
  3690. ast_mutex_unlock(&p->lock);
  3691. return NULL;
  3692. }
  3693. readbuf = ((unsigned char *)p->subs[index].buffer) + AST_FRIENDLY_OFFSET;
  3694. CHECK_BLOCKING(ast);
  3695. res = read(p->subs[index].zfd, readbuf, p->subs[index].linear ? READ_SIZE * 2 : READ_SIZE);
  3696. ast->blocking = 0;
  3697. /* Check for hangup */
  3698. if (res < 0) {
  3699. f = NULL;
  3700. if (res == -1) {
  3701. if (errno == EAGAIN) {
  3702. /* Return "NULL" frame if there is nobody there */
  3703. ast_mutex_unlock(&p->lock);
  3704. return &p->subs[index].f;
  3705. } else if (errno == ELAST) {
  3706. f = __zt_exception(ast);
  3707. } else
  3708. ast_log(LOG_WARNING, "zt_rec: %s\n", strerror(errno));
  3709. }
  3710. ast_mutex_unlock(&p->lock);
  3711. return f;
  3712. }
  3713. if (res != (p->subs[index].linear ? READ_SIZE * 2 : READ_SIZE)) {
  3714. ast_log(LOG_DEBUG, "Short read (%d/%d), must be an event...\n", res, p->subs[index].linear ? READ_SIZE * 2 : READ_SIZE);
  3715. f = __zt_exception(ast);
  3716. ast_mutex_unlock(&p->lock);
  3717. return f;
  3718. }
  3719. if (p->tdd) { /* if in TDD mode, see if we receive that */
  3720. int c;
  3721. c = tdd_feed(p->tdd,readbuf,READ_SIZE);
  3722. if (c < 0) {
  3723. ast_log(LOG_DEBUG,"tdd_feed failed\n");
  3724. ast_mutex_unlock(&p->lock);
  3725. return NULL;
  3726. }
  3727. if (c) { /* if a char to return */
  3728. p->subs[index].f.subclass = 0;
  3729. p->subs[index].f.frametype = AST_FRAME_TEXT;
  3730. p->subs[index].f.mallocd = 0;
  3731. p->subs[index].f.offset = AST_FRIENDLY_OFFSET;
  3732. p->subs[index].f.data = p->subs[index].buffer + AST_FRIENDLY_OFFSET;
  3733. p->subs[index].f.datalen = 1;
  3734. *((char *) p->subs[index].f.data) = c;
  3735. ast_mutex_unlock(&p->lock);
  3736. return &p->subs[index].f;
  3737. }
  3738. }
  3739. if (p->callwaitingrepeat)
  3740. p->callwaitingrepeat--;
  3741. if (p->cidcwexpire)
  3742. p->cidcwexpire--;
  3743. /* Repeat callwaiting */
  3744. if (p->callwaitingrepeat == 1) {
  3745. p->callwaitrings++;
  3746. zt_callwait(ast);
  3747. }
  3748. /* Expire CID/CW */
  3749. if (p->cidcwexpire == 1) {
  3750. if (option_verbose > 2)
  3751. ast_verbose(VERBOSE_PREFIX_3 "CPE does not support Call Waiting Caller*ID.\n");
  3752. restore_conference(p);
  3753. }
  3754. if (p->subs[index].linear) {
  3755. p->subs[index].f.datalen = READ_SIZE * 2;
  3756. } else
  3757. p->subs[index].f.datalen = READ_SIZE;
  3758. /* Handle CallerID Transmission */
  3759. if ((p->owner == ast) && p->cidspill &&((ast->_state == AST_STATE_UP) || (ast->rings == p->cidrings))) {
  3760. send_callerid(p);
  3761. }
  3762. p->subs[index].f.frametype = AST_FRAME_VOICE;
  3763. p->subs[index].f.subclass = ast->pvt->rawreadformat;
  3764. p->subs[index].f.samples = READ_SIZE;
  3765. p->subs[index].f.mallocd = 0;
  3766. p->subs[index].f.offset = AST_FRIENDLY_OFFSET;
  3767. p->subs[index].f.data = p->subs[index].buffer + AST_FRIENDLY_OFFSET/2;
  3768. #if 0
  3769. ast_log(LOG_DEBUG, "Read %d of voice on %s\n", p->subs[index].f.datalen, ast->name);
  3770. #endif
  3771. if (p->dialing || /* Transmitting something */
  3772. (index && (ast->_state != AST_STATE_UP)) || /* Three-way or callwait that isn't up */
  3773. ((index == SUB_CALLWAIT) && !p->subs[SUB_CALLWAIT].inthreeway) /* Inactive and non-confed call-wait */
  3774. ) {
  3775. /* Whoops, we're still dialing, or in a state where we shouldn't transmit....
  3776. don't send anything */
  3777. p->subs[index].f.frametype = AST_FRAME_NULL;
  3778. p->subs[index].f.subclass = 0;
  3779. p->subs[index].f.samples = 0;
  3780. p->subs[index].f.mallocd = 0;
  3781. p->subs[index].f.offset = 0;
  3782. p->subs[index].f.data = NULL;
  3783. p->subs[index].f.datalen= 0;
  3784. }
  3785. if (p->dsp && (!p->ignoredtmf || p->callwaitcas || p->busydetect || p->callprogress) && !index) {
  3786. /* Perform busy detection. etc on the zap line */
  3787. f = ast_dsp_process(ast, p->dsp, &p->subs[index].f);
  3788. if (f) {
  3789. if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_BUSY)) {
  3790. if ((ast->_state == AST_STATE_UP) && !p->outgoing) {
  3791. /* Treat this as a "hangup" instead of a "busy" on the assumption that
  3792. a busy */
  3793. f = NULL;
  3794. }
  3795. } else if (f->frametype == AST_FRAME_DTMF) {
  3796. #ifdef ZAPATA_PRI
  3797. if ((p->proceeding < 2) && p->sig==SIG_PRI && p->pri && p->pri->overlapdial) {
  3798. /* Don't accept in-band DTMF when in overlap dial mode */
  3799. f->frametype = AST_FRAME_NULL;
  3800. f->subclass = 0;
  3801. }
  3802. #endif
  3803. /* DSP clears us of being pulse */
  3804. p->pulsedial = 0;
  3805. }
  3806. }
  3807. } else
  3808. f = &p->subs[index].f;
  3809. if (f && (f->frametype == AST_FRAME_DTMF)) {
  3810. ast_log(LOG_DEBUG, "DTMF digit: %c on %s\n", f->subclass, ast->name);
  3811. if (p->confirmanswer) {
  3812. ast_log(LOG_DEBUG, "Confirm answer on %s!\n", ast->name);
  3813. /* Upon receiving a DTMF digit, consider this an answer confirmation instead
  3814. of a DTMF digit */
  3815. p->subs[index].f.frametype = AST_FRAME_CONTROL;
  3816. p->subs[index].f.subclass = AST_CONTROL_ANSWER;
  3817. ast_setstate(ast, AST_STATE_UP);
  3818. f = &p->subs[index].f;
  3819. /* Reset confirmanswer so DTMF's will behave properly for the duration of the call */
  3820. p->confirmanswer = 0;
  3821. } else if (p->callwaitcas) {
  3822. if ((f->subclass == 'A') || (f->subclass == 'D')) {
  3823. ast_log(LOG_DEBUG, "Got some DTMF, but it's for the CAS\n");
  3824. if (p->cidspill)
  3825. free(p->cidspill);
  3826. send_cwcidspill(p);
  3827. }
  3828. if ((f->subclass != 'm') && (f->subclass != 'u'))
  3829. p->callwaitcas = 0;
  3830. p->subs[index].f.frametype = AST_FRAME_NULL;
  3831. p->subs[index].f.subclass = 0;
  3832. f = &p->subs[index].f;
  3833. } else if (f->subclass == 'f') {
  3834. /* Fax tone -- Handle and return NULL */
  3835. if (!p->faxhandled) {
  3836. p->faxhandled++;
  3837. if (strcmp(ast->exten, "fax")) {
  3838. char *target_context = ast_strlen_zero(ast->macrocontext) ? ast->context : ast->macrocontext;
  3839. if (ast_exists_extension(ast, target_context, "fax", 1, ast->callerid)) {
  3840. if (option_verbose > 2)
  3841. ast_verbose(VERBOSE_PREFIX_3 "Redirecting %s to fax extension\n", ast->name);
  3842. /* Save the DID/DNIS when we transfer the fax call to a "fax" extension */
  3843. pbx_builtin_setvar_helper(ast, "FAXEXTEN", ast->exten);
  3844. if (ast_async_goto(ast, target_context, "fax", 1))
  3845. ast_log(LOG_WARNING, "Failed to async goto '%s' into fax of '%s'\n", ast->name, target_context);
  3846. } else
  3847. ast_log(LOG_NOTICE, "Fax detected, but no fax extension\n");
  3848. } else
  3849. ast_log(LOG_DEBUG, "Already in a fax extension, not redirecting\n");
  3850. } else
  3851. ast_log(LOG_DEBUG, "Fax already handled\n");
  3852. zt_confmute(p, 0);
  3853. p->subs[index].f.frametype = AST_FRAME_NULL;
  3854. p->subs[index].f.subclass = 0;
  3855. f = &p->subs[index].f;
  3856. } else if (f->subclass == 'm') {
  3857. /* Confmute request */
  3858. zt_confmute(p, 1);
  3859. p->subs[index].f.frametype = AST_FRAME_NULL;
  3860. p->subs[index].f.subclass = 0;
  3861. f = &p->subs[index].f;
  3862. } else if (f->subclass == 'u') {
  3863. /* Unmute */
  3864. zt_confmute(p, 0);
  3865. p->subs[index].f.frametype = AST_FRAME_NULL;
  3866. p->subs[index].f.subclass = 0;
  3867. f = &p->subs[index].f;
  3868. } else
  3869. zt_confmute(p, 0);
  3870. }
  3871. /* If we have a fake_event, trigger exception to handle it */
  3872. if (p->fake_event)
  3873. ast->exception = 1;
  3874. ast_mutex_unlock(&p->lock);
  3875. return f;
  3876. }
  3877. static int my_zt_write(struct zt_pvt *p, unsigned char *buf, int len, int index, int linear)
  3878. {
  3879. int sent=0;
  3880. int size;
  3881. int res;
  3882. int fd;
  3883. fd = p->subs[index].zfd;
  3884. while(len) {
  3885. size = len;
  3886. if (size > (linear ? READ_SIZE * 2 : READ_SIZE))
  3887. size = (linear ? READ_SIZE * 2 : READ_SIZE);
  3888. res = write(fd, buf, size);
  3889. if (res != size) {
  3890. if (option_debug)
  3891. ast_log(LOG_DEBUG, "Write returned %d (%s) on channel %d\n", res, strerror(errno), p->channel);
  3892. return sent;
  3893. }
  3894. len -= size;
  3895. buf += size;
  3896. }
  3897. return sent;
  3898. }
  3899. static int zt_write(struct ast_channel *ast, struct ast_frame *frame)
  3900. {
  3901. struct zt_pvt *p = ast->pvt->pvt;
  3902. int res;
  3903. unsigned char outbuf[4096];
  3904. int index;
  3905. index = zt_get_index(ast, p, 0);
  3906. if (index < 0) {
  3907. ast_log(LOG_WARNING, "%s doesn't really exist?\n", ast->name);
  3908. return -1;
  3909. }
  3910. #ifdef ZAPATA_PRI
  3911. ast_mutex_lock(&p->lock);
  3912. if (!p->proceeding && p->sig==SIG_PRI && p->pri && !p->outgoing) {
  3913. if (p->pri->pri) {
  3914. if (!pri_grab(p, p->pri)) {
  3915. pri_progress(p->pri->pri,p->call, PVT_TO_CHANNEL(p), 1);
  3916. pri_rel(p->pri);
  3917. } else
  3918. ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
  3919. }
  3920. p->proceeding=1;
  3921. }
  3922. ast_mutex_unlock(&p->lock);
  3923. #endif
  3924. /* Write a frame of (presumably voice) data */
  3925. if (frame->frametype != AST_FRAME_VOICE) {
  3926. if (frame->frametype != AST_FRAME_IMAGE)
  3927. ast_log(LOG_WARNING, "Don't know what to do with frame type '%d'\n", frame->frametype);
  3928. return 0;
  3929. }
  3930. if ((frame->subclass != AST_FORMAT_SLINEAR) &&
  3931. (frame->subclass != AST_FORMAT_ULAW) &&
  3932. (frame->subclass != AST_FORMAT_ALAW)) {
  3933. ast_log(LOG_WARNING, "Cannot handle frames in %d format\n", frame->subclass);
  3934. return -1;
  3935. }
  3936. if (p->dialing) {
  3937. if (option_debug)
  3938. ast_log(LOG_DEBUG, "Dropping frame since I'm still dialing on %s...\n",ast->name);
  3939. return 0;
  3940. }
  3941. if (!p->owner) {
  3942. if (option_debug)
  3943. ast_log(LOG_DEBUG, "Dropping frame since there is no active owner on %s...\n",ast->name);
  3944. return 0;
  3945. }
  3946. if (p->cidspill) {
  3947. if (option_debug)
  3948. ast_log(LOG_DEBUG, "Dropping frame since I've still got a callerid spill\n");
  3949. return 0;
  3950. }
  3951. /* Return if it's not valid data */
  3952. if (!frame->data || !frame->datalen)
  3953. return 0;
  3954. if (frame->datalen > sizeof(outbuf) * 2) {
  3955. ast_log(LOG_WARNING, "Frame too large\n");
  3956. return 0;
  3957. }
  3958. if (frame->subclass == AST_FORMAT_SLINEAR) {
  3959. if (!p->subs[index].linear) {
  3960. p->subs[index].linear = 1;
  3961. res = zt_setlinear(p->subs[index].zfd, p->subs[index].linear);
  3962. if (res)
  3963. ast_log(LOG_WARNING, "Unable to set linear mode on channel %d\n", p->channel);
  3964. }
  3965. res = my_zt_write(p, (unsigned char *)frame->data, frame->datalen, index, 1);
  3966. } else {
  3967. /* x-law already */
  3968. if (p->subs[index].linear) {
  3969. p->subs[index].linear = 0;
  3970. res = zt_setlinear(p->subs[index].zfd, p->subs[index].linear);
  3971. if (res)
  3972. ast_log(LOG_WARNING, "Unable to set companded mode on channel %d\n", p->channel);
  3973. }
  3974. res = my_zt_write(p, (unsigned char *)frame->data, frame->datalen, index, 0);
  3975. }
  3976. if (res < 0) {
  3977. ast_log(LOG_WARNING, "write failed: %s\n", strerror(errno));
  3978. return -1;
  3979. }
  3980. return 0;
  3981. }
  3982. static int zt_indicate(struct ast_channel *chan, int condition)
  3983. {
  3984. struct zt_pvt *p = chan->pvt->pvt;
  3985. int res=-1;
  3986. int index;
  3987. ast_mutex_lock(&p->lock);
  3988. index = zt_get_index(chan, p, 0);
  3989. if (index == SUB_REAL) {
  3990. switch(condition) {
  3991. case AST_CONTROL_BUSY:
  3992. #ifdef ZAPATA_PRI
  3993. if (p->priindication_oob && p->sig == SIG_PRI) {
  3994. chan->hangupcause = AST_CAUSE_USER_BUSY;
  3995. chan->_softhangup |= AST_SOFTHANGUP_DEV;
  3996. res = 0;
  3997. } else
  3998. #endif
  3999. res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_BUSY);
  4000. break;
  4001. case AST_CONTROL_RINGING:
  4002. #ifdef ZAPATA_PRI
  4003. if ((!p->alerting) && p->sig==SIG_PRI && p->pri && !p->outgoing && (chan->_state != AST_STATE_UP)) {
  4004. if (p->pri->pri) {
  4005. if (!pri_grab(p, p->pri)) {
  4006. pri_acknowledge(p->pri->pri,p->call, PVT_TO_CHANNEL(p), 1);
  4007. pri_rel(p->pri);
  4008. }
  4009. else
  4010. ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
  4011. }
  4012. p->alerting=1;
  4013. }
  4014. #endif
  4015. res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_RINGTONE);
  4016. if (chan->_state != AST_STATE_UP) {
  4017. if ((chan->_state != AST_STATE_RING) ||
  4018. ((p->sig != SIG_FXSKS) &&
  4019. (p->sig != SIG_FXSLS) &&
  4020. (p->sig != SIG_FXSGS)))
  4021. ast_setstate(chan, AST_STATE_RINGING);
  4022. }
  4023. break;
  4024. case AST_CONTROL_PROCEEDING:
  4025. ast_log(LOG_DEBUG,"Received AST_CONTROL_PROCEEDING on %s\n",chan->name);
  4026. #ifdef ZAPATA_PRI
  4027. if ((p->proceeding < 2) && p->sig==SIG_PRI && p->pri && !p->outgoing) {
  4028. if (p->pri->pri) {
  4029. if (!pri_grab(p, p->pri)) {
  4030. pri_proceeding(p->pri->pri,p->call, PVT_TO_CHANNEL(p), 1);
  4031. pri_rel(p->pri);
  4032. }
  4033. else
  4034. ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
  4035. }
  4036. p->proceeding=2;
  4037. }
  4038. #endif
  4039. /* don't continue in ast_indicate */
  4040. res = 0;
  4041. break;
  4042. case AST_CONTROL_PROGRESS:
  4043. ast_log(LOG_DEBUG,"Received AST_CONTROL_PROGRESS on %s\n",chan->name);
  4044. #ifdef ZAPATA_PRI
  4045. if (!p->proceeding && p->sig==SIG_PRI && p->pri && !p->outgoing) {
  4046. if (p->pri->pri) {
  4047. if (!pri_grab(p, p->pri)) {
  4048. pri_progress(p->pri->pri,p->call, PVT_TO_CHANNEL(p), 1);
  4049. pri_rel(p->pri);
  4050. }
  4051. else
  4052. ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
  4053. }
  4054. p->proceeding=1;
  4055. }
  4056. #endif
  4057. /* don't continue in ast_indicate */
  4058. res = 0;
  4059. break;
  4060. case AST_CONTROL_CONGESTION:
  4061. chan->hangupcause = AST_CAUSE_CONGESTION;
  4062. #ifdef ZAPATA_PRI
  4063. if (p->priindication_oob && p->sig == SIG_PRI) {
  4064. chan->hangupcause = AST_CAUSE_SWITCH_CONGESTION;
  4065. chan->_softhangup |= AST_SOFTHANGUP_DEV;
  4066. res = 0;
  4067. } else
  4068. #endif
  4069. res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_CONGESTION);
  4070. break;
  4071. case AST_CONTROL_RADIO_KEY:
  4072. if (p->radio)
  4073. res = zt_set_hook(p->subs[index].zfd, ZT_OFFHOOK);
  4074. res = 0;
  4075. break;
  4076. case AST_CONTROL_RADIO_UNKEY:
  4077. if (p->radio)
  4078. res = zt_set_hook(p->subs[index].zfd, ZT_RINGOFF);
  4079. res = 0;
  4080. break;
  4081. case -1:
  4082. res = tone_zone_play_tone(p->subs[index].zfd, -1);
  4083. break;
  4084. default:
  4085. ast_log(LOG_WARNING, "Don't know how to set condition %d on channel %s\n", condition, chan->name);
  4086. }
  4087. } else
  4088. res = 0;
  4089. ast_mutex_unlock(&p->lock);
  4090. return res;
  4091. }
  4092. #ifdef ZAPATA_PRI
  4093. static void set_calltype(struct ast_channel *chan, int ctype)
  4094. {
  4095. char *s = "UNKNOWN";
  4096. switch(ctype) {
  4097. case PRI_TRANS_CAP_SPEECH:
  4098. s = "SPEECH";
  4099. break;
  4100. case PRI_TRANS_CAP_DIGITAL:
  4101. s = "DIGITAL";
  4102. break;
  4103. case PRI_TRANS_CAP_RESTRICTED_DIGITAL:
  4104. s = "RESTRICTED_DIGITAL";
  4105. break;
  4106. case PRI_TRANS_CAP_3_1K_AUDIO:
  4107. s = "31KAUDIO";
  4108. break;
  4109. case PRI_TRANS_CAP_7K_AUDIO:
  4110. s = "7KAUDIO";
  4111. break;
  4112. case PRI_TRANS_CAP_VIDEO:
  4113. s = "VIDEO";
  4114. break;
  4115. }
  4116. pbx_builtin_setvar_helper(chan, "CALLTYPE", s);
  4117. }
  4118. #endif
  4119. static struct ast_channel *zt_new(struct zt_pvt *i, int state, int startpbx, int index, int law, int ctype)
  4120. {
  4121. struct ast_channel *tmp;
  4122. int deflaw;
  4123. int res;
  4124. int x,y;
  4125. int features;
  4126. ZT_PARAMS ps;
  4127. tmp = ast_channel_alloc(0);
  4128. if (tmp) {
  4129. ps.channo = i->channel;
  4130. res = ioctl(i->subs[SUB_REAL].zfd, ZT_GET_PARAMS, &ps);
  4131. if (res) {
  4132. ast_log(LOG_WARNING, "Unable to get parameters, assuming MULAW\n");
  4133. ps.curlaw = ZT_LAW_MULAW;
  4134. }
  4135. if (ps.curlaw == ZT_LAW_ALAW)
  4136. deflaw = AST_FORMAT_ALAW;
  4137. else
  4138. deflaw = AST_FORMAT_ULAW;
  4139. if (law) {
  4140. if (law == ZT_LAW_ALAW)
  4141. deflaw = AST_FORMAT_ALAW;
  4142. else
  4143. deflaw = AST_FORMAT_ULAW;
  4144. }
  4145. y = 1;
  4146. do {
  4147. #ifdef ZAPATA_PRI
  4148. if (i->bearer || (i->pri && (i->sig == SIG_FXSKS)))
  4149. snprintf(tmp->name, sizeof(tmp->name), "Zap/%d:%d-%d", i->pri->trunkgroup, i->channel, y);
  4150. else
  4151. #endif
  4152. if (i->channel == CHAN_PSEUDO)
  4153. snprintf(tmp->name, sizeof(tmp->name), "Zap/pseudo-%d", rand());
  4154. else
  4155. snprintf(tmp->name, sizeof(tmp->name), "Zap/%d-%d", i->channel, y);
  4156. for (x=0;x<3;x++) {
  4157. if ((index != x) && i->subs[x].owner && !strcasecmp(tmp->name, i->subs[x].owner->name))
  4158. break;
  4159. }
  4160. y++;
  4161. } while (x < 3);
  4162. tmp->type = type;
  4163. tmp->fds[0] = i->subs[index].zfd;
  4164. tmp->nativeformats = AST_FORMAT_SLINEAR | deflaw;
  4165. /* Start out assuming ulaw since it's smaller :) */
  4166. tmp->pvt->rawreadformat = deflaw;
  4167. tmp->readformat = deflaw;
  4168. tmp->pvt->rawwriteformat = deflaw;
  4169. tmp->writeformat = deflaw;
  4170. i->subs[index].linear = 0;
  4171. zt_setlinear(i->subs[index].zfd, i->subs[index].linear);
  4172. features = 0;
  4173. if (i->busydetect && CANBUSYDETECT(i)) {
  4174. features |= DSP_FEATURE_BUSY_DETECT;
  4175. }
  4176. if ((i->callprogress & 1) && CANPROGRESSDETECT(i)) {
  4177. features |= DSP_FEATURE_CALL_PROGRESS;
  4178. }
  4179. if ((!i->outgoing && (i->callprogress & 4)) ||
  4180. (i->outgoing && (i->callprogress & 2))) {
  4181. features |= DSP_FEATURE_FAX_DETECT;
  4182. }
  4183. #ifdef ZT_TONEDETECT
  4184. x = ZT_TONEDETECT_ON | ZT_TONEDETECT_MUTE;
  4185. if (ioctl(i->subs[index].zfd, ZT_TONEDETECT, &x))
  4186. #endif
  4187. features |= DSP_FEATURE_DTMF_DETECT;
  4188. if (features) {
  4189. if (i->dsp) {
  4190. ast_log(LOG_DEBUG, "Already have a dsp on %s?\n", tmp->name);
  4191. } else {
  4192. i->dsp = ast_dsp_new();
  4193. if (i->dsp) {
  4194. ast_dsp_set_features(i->dsp, features);
  4195. ast_dsp_digitmode(i->dsp, DSP_DIGITMODE_DTMF | i->dtmfrelax);
  4196. if (!ast_strlen_zero(progzone))
  4197. ast_dsp_set_call_progress_zone(i->dsp, progzone);
  4198. if (i->busydetect && CANBUSYDETECT(i)) {
  4199. ast_dsp_set_busy_count(i->dsp, i->busycount);
  4200. }
  4201. }
  4202. }
  4203. }
  4204. if (state == AST_STATE_RING)
  4205. tmp->rings = 1;
  4206. tmp->pvt->pvt = i;
  4207. tmp->pvt->send_digit = zt_digit;
  4208. tmp->pvt->send_text = zt_sendtext;
  4209. tmp->pvt->call = zt_call;
  4210. tmp->pvt->hangup = zt_hangup;
  4211. tmp->pvt->answer = zt_answer;
  4212. tmp->pvt->read = zt_read;
  4213. tmp->pvt->write = zt_write;
  4214. tmp->pvt->bridge = zt_bridge;
  4215. tmp->pvt->exception = zt_exception;
  4216. tmp->pvt->indicate = zt_indicate;
  4217. tmp->pvt->fixup = zt_fixup;
  4218. tmp->pvt->setoption = zt_setoption;
  4219. if ((i->sig == SIG_FXOKS) || (i->sig == SIG_FXOGS) || (i->sig == SIG_FXOLS)) {
  4220. /* Only FXO signalled stuff can be picked up */
  4221. tmp->callgroup = i->callgroup;
  4222. tmp->pickupgroup = i->pickupgroup;
  4223. }
  4224. if (!ast_strlen_zero(i->language))
  4225. strncpy(tmp->language, i->language, sizeof(tmp->language)-1);
  4226. if (!ast_strlen_zero(i->musicclass))
  4227. strncpy(tmp->musicclass, i->musicclass, sizeof(tmp->musicclass)-1);
  4228. if (!i->owner)
  4229. i->owner = tmp;
  4230. if (!ast_strlen_zero(i->accountcode))
  4231. strncpy(tmp->accountcode, i->accountcode, sizeof(tmp->accountcode)-1);
  4232. if (i->amaflags)
  4233. tmp->amaflags = i->amaflags;
  4234. if (i->subs[index].owner) {
  4235. ast_log(LOG_WARNING, "Channel %d already has a %s call\n", i->channel,subnames[index]);
  4236. }
  4237. i->subs[index].owner = tmp;
  4238. strncpy(tmp->context, i->context, sizeof(tmp->context)-1);
  4239. /* Copy call forward info */
  4240. strncpy(tmp->call_forward, i->call_forward, sizeof(tmp->call_forward) - 1);
  4241. /* If we've been told "no ADSI" then enforce it */
  4242. if (!i->adsi)
  4243. tmp->adsicpe = AST_ADSI_UNAVAILABLE;
  4244. if (!ast_strlen_zero(i->exten))
  4245. strncpy(tmp->exten, i->exten, sizeof(tmp->exten)-1);
  4246. if (!ast_strlen_zero(i->rdnis))
  4247. tmp->rdnis = strdup(i->rdnis);
  4248. if (!ast_strlen_zero(i->dnid))
  4249. tmp->dnid = strdup(i->dnid);
  4250. if (!ast_strlen_zero(i->callerid)) {
  4251. tmp->callerid = strdup(i->callerid);
  4252. tmp->ani = strdup(i->callerid);
  4253. }
  4254. tmp->restrictcid = i->restrictcid;
  4255. tmp->callingpres = i->callingpres;
  4256. #ifdef ZAPATA_PRI
  4257. set_calltype(tmp, ctype);
  4258. /* Assume calls are not idle calls unless we're told differently */
  4259. i->isidlecall = 0;
  4260. i->alreadyhungup = 0;
  4261. if (ctype & PRI_TRANS_CAP_DIGITAL) {
  4262. i->digital = 1;
  4263. ast_set_flag(tmp, AST_FLAG_DIGITAL);
  4264. }
  4265. #endif
  4266. /* clear the fake event in case we posted one before we had ast_chanenl */
  4267. i->fake_event = 0;
  4268. /* Assure there is no confmute on this channel */
  4269. zt_confmute(i, 0);
  4270. ast_setstate(tmp, state);
  4271. ast_mutex_lock(&usecnt_lock);
  4272. usecnt++;
  4273. ast_mutex_unlock(&usecnt_lock);
  4274. ast_update_use_count();
  4275. if (startpbx) {
  4276. if (ast_pbx_start(tmp)) {
  4277. ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
  4278. ast_hangup(tmp);
  4279. tmp = NULL;
  4280. }
  4281. }
  4282. } else
  4283. ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
  4284. return tmp;
  4285. }
  4286. static int bump_gains(struct zt_pvt *p)
  4287. {
  4288. int res;
  4289. /* Bump receive gain by 9.0db */
  4290. res = set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain + 5.0, p->txgain, p->law);
  4291. if (res) {
  4292. ast_log(LOG_WARNING, "Unable to bump gain\n");
  4293. return -1;
  4294. }
  4295. return 0;
  4296. }
  4297. static int restore_gains(struct zt_pvt *p)
  4298. {
  4299. int res;
  4300. /* Bump receive gain by 9.0db */
  4301. res = set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain, p->txgain, p->law);
  4302. if (res) {
  4303. ast_log(LOG_WARNING, "Unable to restore gains: %s\n", strerror(errno));
  4304. return -1;
  4305. }
  4306. return 0;
  4307. }
  4308. static int my_getsigstr(struct ast_channel *chan, char *str, char term, int ms)
  4309. {
  4310. char c;
  4311. *str = 0; /* start with empty output buffer */
  4312. for(;;)
  4313. {
  4314. /* Wait for the first digit (up to specified ms). */
  4315. c = ast_waitfordigit(chan,ms);
  4316. /* if timeout, hangup or error, return as such */
  4317. if (c < 1) return(c);
  4318. *str++ = c;
  4319. *str = 0;
  4320. if (c == term) return(1);
  4321. }
  4322. }
  4323. static int zt_wink(struct zt_pvt *p, int index)
  4324. {
  4325. int j;
  4326. zt_set_hook(p->subs[index].zfd, ZT_WINK);
  4327. for(;;)
  4328. {
  4329. /* set bits of interest */
  4330. j = ZT_IOMUX_SIGEVENT;
  4331. /* wait for some happening */
  4332. if (ioctl(p->subs[index].zfd,ZT_IOMUX,&j) == -1) return(-1);
  4333. /* exit loop if we have it */
  4334. if (j & ZT_IOMUX_SIGEVENT) break;
  4335. }
  4336. /* get the event info */
  4337. if (ioctl(p->subs[index].zfd,ZT_GETEVENT,&j) == -1) return(-1);
  4338. return 0;
  4339. }
  4340. static void *ss_thread(void *data)
  4341. {
  4342. struct ast_channel *chan = data;
  4343. struct zt_pvt *p = chan->pvt->pvt;
  4344. char exten[AST_MAX_EXTENSION]="";
  4345. char exten2[AST_MAX_EXTENSION]="";
  4346. unsigned char buf[256];
  4347. char cid[256];
  4348. char dtmfcid[300];
  4349. char dtmfbuf[300];
  4350. struct callerid_state *cs;
  4351. char *name=NULL, *number=NULL;
  4352. int distMatches;
  4353. int curRingData[3];
  4354. int receivedRingT;
  4355. int counter1;
  4356. int counter;
  4357. int flags;
  4358. int i;
  4359. int timeout;
  4360. int getforward=0;
  4361. char *s1, *s2;
  4362. int len = 0;
  4363. int res;
  4364. int index;
  4365. if (option_verbose > 2)
  4366. ast_verbose( VERBOSE_PREFIX_3 "Starting simple switch on '%s'\n", chan->name);
  4367. index = zt_get_index(chan, p, 1);
  4368. if (index < 0) {
  4369. ast_log(LOG_WARNING, "Huh?\n");
  4370. ast_hangup(chan);
  4371. return NULL;
  4372. }
  4373. if (p->dsp)
  4374. ast_dsp_digitreset(p->dsp);
  4375. switch(p->sig) {
  4376. #ifdef ZAPATA_PRI
  4377. case SIG_PRI:
  4378. /* Now loop looking for an extension */
  4379. strncpy(exten, p->exten, sizeof(exten) - 1);
  4380. len = strlen(exten);
  4381. res = 0;
  4382. while((len < AST_MAX_EXTENSION-1) && ast_matchmore_extension(chan, chan->context, exten, 1, p->callerid)) {
  4383. if (len && !ast_ignore_pattern(chan->context, exten))
  4384. tone_zone_play_tone(p->subs[index].zfd, -1);
  4385. else
  4386. tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALTONE);
  4387. if (ast_exists_extension(chan, chan->context, exten, 1, p->callerid))
  4388. timeout = matchdigittimeout;
  4389. else
  4390. timeout = gendigittimeout;
  4391. res = ast_waitfordigit(chan, timeout);
  4392. if (res < 0) {
  4393. ast_log(LOG_DEBUG, "waitfordigit returned < 0...\n");
  4394. ast_hangup(chan);
  4395. return NULL;
  4396. } else if (res) {
  4397. exten[len++] = res;
  4398. } else
  4399. break;
  4400. }
  4401. /* if no extension was received ('unspecified') on overlap call, use the 's' extension */
  4402. if (ast_strlen_zero(exten)) {
  4403. if (option_verbose > 2)
  4404. ast_verbose(VERBOSE_PREFIX_3 "Going to extension s|1 because of empty extension received on overlap call\n");
  4405. exten[0] = 's';
  4406. exten[1] = '\0';
  4407. }
  4408. tone_zone_play_tone(p->subs[index].zfd, -1);
  4409. if (ast_exists_extension(chan, chan->context, exten, 1, p->callerid)) {
  4410. /* Start the real PBX */
  4411. strncpy(chan->exten, exten, sizeof(chan->exten) - 1);
  4412. ast_dsp_digitreset(p->dsp);
  4413. ast_setstate(chan, AST_STATE_RING);
  4414. res = ast_pbx_run(chan);
  4415. if (res) {
  4416. ast_log(LOG_WARNING, "PBX exited non-zero!\n");
  4417. }
  4418. } else {
  4419. ast_log(LOG_DEBUG, "No such possible extension '%s' in context '%s'\n", exten, chan->context);
  4420. chan->hangupcause = AST_CAUSE_UNALLOCATED;
  4421. ast_hangup(chan);
  4422. p->exten[0] = '\0';
  4423. /* Since we send release complete here, we won't get one */
  4424. p->call = NULL;
  4425. }
  4426. return NULL;
  4427. break;
  4428. #endif
  4429. case SIG_FEATD:
  4430. case SIG_FEATDMF:
  4431. case SIG_E911:
  4432. case SIG_FEATB:
  4433. case SIG_EMWINK:
  4434. case SIG_SF_FEATD:
  4435. case SIG_SF_FEATDMF:
  4436. case SIG_SF_FEATB:
  4437. case SIG_SFWINK:
  4438. if (zt_wink(p, index))
  4439. return NULL;
  4440. /* Fall through */
  4441. case SIG_EM:
  4442. case SIG_EM_E1:
  4443. case SIG_SF:
  4444. res = tone_zone_play_tone(p->subs[index].zfd, -1);
  4445. if (p->dsp)
  4446. ast_dsp_digitreset(p->dsp);
  4447. /* set digit mode appropriately */
  4448. if (p->dsp) {
  4449. if ((p->sig == SIG_FEATDMF) || (p->sig == SIG_E911) || (p->sig == SIG_FEATB))
  4450. ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MF | p->dtmfrelax);
  4451. else
  4452. ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax);
  4453. }
  4454. dtmfbuf[0] = 0;
  4455. /* Wait for the first digit only if immediate=no */
  4456. if (!p->immediate)
  4457. /* Wait for the first digit (up to 5 seconds). */
  4458. res = ast_waitfordigit(chan,5000);
  4459. else res = 0;
  4460. if (res > 0) {
  4461. /* save first char */
  4462. dtmfbuf[0] = res;
  4463. switch(p->sig)
  4464. {
  4465. case SIG_FEATD:
  4466. case SIG_SF_FEATD:
  4467. res = my_getsigstr(chan,dtmfbuf + 1,'*',3000);
  4468. if (res > 0)
  4469. res = my_getsigstr(chan,dtmfbuf + strlen(dtmfbuf),'*',3000);
  4470. if (res < 1) ast_dsp_digitreset(p->dsp);
  4471. break;
  4472. case SIG_FEATDMF:
  4473. case SIG_E911:
  4474. case SIG_SF_FEATDMF:
  4475. res = my_getsigstr(chan,dtmfbuf + 1,'#',3000);
  4476. if (res > 0) {
  4477. /* if E911, take off hook */
  4478. if (p->sig == SIG_E911) {
  4479. zt_set_hook(p->subs[SUB_REAL].zfd, ZT_OFFHOOK);
  4480. }
  4481. res = my_getsigstr(chan,dtmfbuf + strlen(dtmfbuf),'#',3000);
  4482. }
  4483. if (res < 1) ast_dsp_digitreset(p->dsp);
  4484. break;
  4485. case SIG_FEATB:
  4486. case SIG_SF_FEATB:
  4487. res = my_getsigstr(chan,dtmfbuf + 1,'#',3000);
  4488. if (res < 1) ast_dsp_digitreset(p->dsp);
  4489. break;
  4490. default:
  4491. /* If we got it, get the rest */
  4492. res = my_getsigstr(chan,dtmfbuf + 1,' ',250);
  4493. break;
  4494. }
  4495. }
  4496. if (res == -1) {
  4497. ast_log(LOG_WARNING, "getdtmf on channel %d: %s\n", p->channel, strerror(errno));
  4498. ast_hangup(chan);
  4499. return NULL;
  4500. } else if (res < 0) {
  4501. ast_log(LOG_DEBUG, "Got hung up before digits finished\n");
  4502. ast_hangup(chan);
  4503. return NULL;
  4504. }
  4505. strncpy(exten, dtmfbuf, sizeof(exten)-1);
  4506. if (ast_strlen_zero(exten))
  4507. strncpy(exten, "s", sizeof(exten)-1);
  4508. if (p->sig == SIG_FEATD || p->sig == SIG_EMWINK) {
  4509. /* Look for Feature Group D on all E&M Wink and Feature Group D trunks */
  4510. if (exten[0] == '*') {
  4511. char *stringp=NULL;
  4512. strncpy(exten2, exten, sizeof(exten2)-1);
  4513. /* Parse out extension and callerid */
  4514. stringp=exten2 +1;
  4515. s1 = strsep(&stringp, "*");
  4516. s2 = strsep(&stringp, "*");
  4517. if (s2) {
  4518. if (!ast_strlen_zero(p->callerid))
  4519. chan->callerid = strdup(p->callerid);
  4520. else
  4521. chan->callerid = strdup(s1);
  4522. if (chan->callerid)
  4523. chan->ani = strdup(chan->callerid);
  4524. strncpy(exten, s2, sizeof(exten)-1);
  4525. } else
  4526. strncpy(exten, s1, sizeof(exten)-1);
  4527. } else if (p->sig == SIG_FEATD)
  4528. ast_log(LOG_WARNING, "Got a non-Feature Group D input on channel %d. Assuming E&M Wink instead\n", p->channel);
  4529. }
  4530. if (p->sig == SIG_FEATDMF) {
  4531. if (exten[0] == '*') {
  4532. char *stringp=NULL;
  4533. strncpy(exten2, exten, sizeof(exten2)-1);
  4534. /* Parse out extension and callerid */
  4535. stringp=exten2 +1;
  4536. s1 = strsep(&stringp, "#");
  4537. s2 = strsep(&stringp, "#");
  4538. if (s2) {
  4539. if (!ast_strlen_zero(p->callerid))
  4540. chan->callerid = strdup(p->callerid);
  4541. else
  4542. if (*(s1 + 2)) chan->callerid = strdup(s1 + 2);
  4543. if (chan->callerid)
  4544. chan->ani = strdup(chan->callerid);
  4545. strncpy(exten, s2 + 1, sizeof(exten)-1);
  4546. } else
  4547. strncpy(exten, s1 + 2, sizeof(exten)-1);
  4548. } else
  4549. ast_log(LOG_WARNING, "Got a non-Feature Group D input on channel %d. Assuming E&M Wink instead\n", p->channel);
  4550. }
  4551. if (p->sig == SIG_E911) {
  4552. if (exten[0] == '*') {
  4553. char *stringp=NULL;
  4554. strncpy(exten2, exten, sizeof(exten2)-1);
  4555. /* Parse out extension and callerid */
  4556. stringp=exten2 +1;
  4557. s1 = strsep(&stringp, "#");
  4558. s2 = strsep(&stringp, "#");
  4559. if (s2 && (*(s2 + 1) == '0')) {
  4560. if (*(s2 + 2)) chan->callerid = strdup(s2 + 2);
  4561. if (chan->callerid)
  4562. chan->ani = strdup(chan->callerid);
  4563. }
  4564. if (s1) strncpy(exten, s1, sizeof(exten)-1);
  4565. else strncpy(exten, "911", sizeof(exten) - 1);
  4566. printf("E911: exten: %s, ANI: %s\n",exten,chan->ani);
  4567. } else
  4568. ast_log(LOG_WARNING, "Got a non-E911 input on channel %d. Assuming E&M Wink instead\n", p->channel);
  4569. }
  4570. if (p->sig == SIG_FEATB) {
  4571. if (exten[0] == '*') {
  4572. char *stringp=NULL;
  4573. strncpy(exten2, exten, sizeof(exten2)-1);
  4574. /* Parse out extension and callerid */
  4575. stringp=exten2 +1;
  4576. s1 = strsep(&stringp, "#");
  4577. strncpy(exten, exten2 + 1, sizeof(exten)-1);
  4578. } else
  4579. ast_log(LOG_WARNING, "Got a non-Feature Group B input on channel %d. Assuming E&M Wink instead\n", p->channel);
  4580. }
  4581. if (p->sig == SIG_FEATDMF) {
  4582. zt_wink(p, index);
  4583. }
  4584. zt_enable_ec(p);
  4585. if ((p->sig == SIG_FEATDMF) || (p->sig == SIG_E911) || (p->sig == SIG_FEATB))
  4586. ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax);
  4587. if (ast_exists_extension(chan, chan->context, exten, 1, chan->callerid)) {
  4588. strncpy(chan->exten, exten, sizeof(chan->exten)-1);
  4589. ast_dsp_digitreset(p->dsp);
  4590. res = ast_pbx_run(chan);
  4591. if (res) {
  4592. ast_log(LOG_WARNING, "PBX exited non-zero\n");
  4593. res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_CONGESTION);
  4594. }
  4595. return NULL;
  4596. } else {
  4597. if (option_verbose > 2)
  4598. ast_verbose(VERBOSE_PREFIX_2 "Unknown extension '%s' in context '%s' requested\n", exten, chan->context);
  4599. sleep(2);
  4600. res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_INFO);
  4601. if (res < 0)
  4602. ast_log(LOG_WARNING, "Unable to start special tone on %d\n", p->channel);
  4603. else
  4604. sleep(1);
  4605. res = ast_streamfile(chan, "ss-noservice", chan->language);
  4606. if (res >= 0)
  4607. ast_waitstream(chan, "");
  4608. res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_CONGESTION);
  4609. ast_hangup(chan);
  4610. return NULL;
  4611. }
  4612. break;
  4613. case SIG_FXOLS:
  4614. case SIG_FXOGS:
  4615. case SIG_FXOKS:
  4616. /* Read the first digit */
  4617. timeout = firstdigittimeout;
  4618. /* If starting a threeway call, never timeout on the first digit so someone
  4619. can use flash-hook as a "hold" feature */
  4620. if (p->subs[SUB_THREEWAY].owner)
  4621. timeout = 999999;
  4622. while(len < AST_MAX_EXTENSION-1) {
  4623. /* Read digit unless it's supposed to be immediate, in which case the
  4624. only answer is 's' */
  4625. if (p->immediate)
  4626. res = 's';
  4627. else
  4628. res = ast_waitfordigit(chan, timeout);
  4629. timeout = 0;
  4630. if (res < 0) {
  4631. ast_log(LOG_DEBUG, "waitfordigit returned < 0...\n");
  4632. res = tone_zone_play_tone(p->subs[index].zfd, -1);
  4633. ast_hangup(chan);
  4634. return NULL;
  4635. } else if (res) {
  4636. exten[len++]=res;
  4637. exten[len] = '\0';
  4638. }
  4639. if (!ast_ignore_pattern(chan->context, exten))
  4640. tone_zone_play_tone(p->subs[index].zfd, -1);
  4641. else
  4642. tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALTONE);
  4643. if (ast_exists_extension(chan, chan->context, exten, 1, p->callerid) && strcmp(exten, ast_parking_ext())) {
  4644. if (!res || !ast_matchmore_extension(chan, chan->context, exten, 1, p->callerid)) {
  4645. if (getforward) {
  4646. /* Record this as the forwarding extension */
  4647. strncpy(p->call_forward, exten, sizeof(p->call_forward) - 1);
  4648. if (option_verbose > 2)
  4649. ast_verbose(VERBOSE_PREFIX_3 "Setting call forward to '%s' on channel %d\n", p->call_forward, p->channel);
  4650. res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALRECALL);
  4651. if (res)
  4652. break;
  4653. usleep(500000);
  4654. res = tone_zone_play_tone(p->subs[index].zfd, -1);
  4655. sleep(1);
  4656. memset(exten, 0, sizeof(exten));
  4657. res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALTONE);
  4658. len = 0;
  4659. getforward = 0;
  4660. } else {
  4661. res = tone_zone_play_tone(p->subs[index].zfd, -1);
  4662. strncpy(chan->exten, exten, sizeof(chan->exten)-1);
  4663. if (!ast_strlen_zero(p->callerid)) {
  4664. if (!p->hidecallerid)
  4665. chan->callerid = strdup(p->callerid);
  4666. chan->ani = strdup(p->callerid);
  4667. }
  4668. ast_setstate(chan, AST_STATE_RING);
  4669. zt_enable_ec(p);
  4670. res = ast_pbx_run(chan);
  4671. if (res) {
  4672. ast_log(LOG_WARNING, "PBX exited non-zero\n");
  4673. res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_CONGESTION);
  4674. }
  4675. return NULL;
  4676. }
  4677. } else {
  4678. /* It's a match, but they just typed a digit, and there is an ambiguous match,
  4679. so just set the timeout to matchdigittimeout and wait some more */
  4680. timeout = matchdigittimeout;
  4681. }
  4682. } else if (res == 0) {
  4683. ast_log(LOG_DEBUG, "not enough digits (and no ambiguous match)...\n");
  4684. res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_CONGESTION);
  4685. zt_wait_event(p->subs[index].zfd);
  4686. ast_hangup(chan);
  4687. return NULL;
  4688. } else if (p->callwaiting && !strcmp(exten, "*70")) {
  4689. if (option_verbose > 2)
  4690. ast_verbose(VERBOSE_PREFIX_3 "Disabling call waiting on %s\n", chan->name);
  4691. /* Disable call waiting if enabled */
  4692. p->callwaiting = 0;
  4693. res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALRECALL);
  4694. if (res) {
  4695. ast_log(LOG_WARNING, "Unable to do dial recall on channel %s: %s\n",
  4696. chan->name, strerror(errno));
  4697. }
  4698. len = 0;
  4699. ioctl(p->subs[index].zfd,ZT_CONFDIAG,&len);
  4700. memset(exten, 0, sizeof(exten));
  4701. timeout = firstdigittimeout;
  4702. } else if (!strcmp(exten,ast_pickup_ext())) {
  4703. /* Scan all channels and see if any there
  4704. * ringing channqels with that have call groups
  4705. * that equal this channels pickup group
  4706. */
  4707. if (index == SUB_REAL) {
  4708. /* Switch us from Third call to Call Wait */
  4709. if (p->subs[SUB_THREEWAY].owner) {
  4710. /* If you make a threeway call and the *8# a call, it should actually
  4711. look like a callwait */
  4712. alloc_sub(p, SUB_CALLWAIT);
  4713. swap_subs(p, SUB_CALLWAIT, SUB_THREEWAY);
  4714. unalloc_sub(p, SUB_THREEWAY);
  4715. }
  4716. zt_enable_ec(p);
  4717. if (ast_pickup_call(chan)) {
  4718. ast_log(LOG_DEBUG, "No call pickup possible...\n");
  4719. res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_CONGESTION);
  4720. zt_wait_event(p->subs[index].zfd);
  4721. }
  4722. ast_hangup(chan);
  4723. return NULL;
  4724. } else {
  4725. ast_log(LOG_WARNING, "Huh? Got *8# on call not on real\n");
  4726. ast_hangup(chan);
  4727. return NULL;
  4728. }
  4729. } else if (!p->hidecallerid && !strcmp(exten, "*67")) {
  4730. if (option_verbose > 2)
  4731. ast_verbose(VERBOSE_PREFIX_3 "Disabling Caller*ID on %s\n", chan->name);
  4732. /* Disable Caller*ID if enabled */
  4733. p->hidecallerid = 1;
  4734. if (chan->callerid)
  4735. free(chan->callerid);
  4736. chan->callerid = NULL;
  4737. res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALRECALL);
  4738. if (res) {
  4739. ast_log(LOG_WARNING, "Unable to do dial recall on channel %s: %s\n",
  4740. chan->name, strerror(errno));
  4741. }
  4742. len = 0;
  4743. memset(exten, 0, sizeof(exten));
  4744. timeout = firstdigittimeout;
  4745. } else if (p->callreturn && !strcmp(exten, "*69")) {
  4746. res = 0;
  4747. if (!ast_strlen_zero(p->lastcallerid)) {
  4748. res = ast_say_digit_str(chan, p->lastcallerid, "", chan->language);
  4749. }
  4750. if (!res)
  4751. res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALRECALL);
  4752. break;
  4753. } else if (!strcmp(exten, "*78")) {
  4754. /* Do not disturb */
  4755. if (option_verbose > 2)
  4756. ast_verbose(VERBOSE_PREFIX_3 "Enabled DND on channel %d\n", p->channel);
  4757. res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALRECALL);
  4758. p->dnd = 1;
  4759. getforward = 0;
  4760. memset(exten, 0, sizeof(exten));
  4761. len = 0;
  4762. } else if (!strcmp(exten, "*79")) {
  4763. /* Do not disturb */
  4764. if (option_verbose > 2)
  4765. ast_verbose(VERBOSE_PREFIX_3 "Disabled DND on channel %d\n", p->channel);
  4766. res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALRECALL);
  4767. p->dnd = 0;
  4768. getforward = 0;
  4769. memset(exten, 0, sizeof(exten));
  4770. len = 0;
  4771. } else if (p->cancallforward && !strcmp(exten, "*72")) {
  4772. res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALRECALL);
  4773. getforward = 1;
  4774. memset(exten, 0, sizeof(exten));
  4775. len = 0;
  4776. } else if (p->cancallforward && !strcmp(exten, "*73")) {
  4777. if (option_verbose > 2)
  4778. ast_verbose(VERBOSE_PREFIX_3 "Cancelling call forwarding on channel %d\n", p->channel);
  4779. res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALRECALL);
  4780. memset(p->call_forward, 0, sizeof(p->call_forward));
  4781. getforward = 0;
  4782. memset(exten, 0, sizeof(exten));
  4783. len = 0;
  4784. } else if (p->transfer && !strcmp(exten, ast_parking_ext()) &&
  4785. p->subs[SUB_THREEWAY].owner &&
  4786. p->subs[SUB_THREEWAY].owner->bridge) {
  4787. /* This is a three way call, the main call being a real channel,
  4788. and we're parking the first call. */
  4789. ast_masq_park_call(p->subs[SUB_THREEWAY].owner->bridge, chan, 0, NULL);
  4790. if (option_verbose > 2)
  4791. ast_verbose(VERBOSE_PREFIX_3 "Parking call to '%s'\n", chan->name);
  4792. break;
  4793. } else if (!ast_strlen_zero(p->lastcallerid) && !strcmp(exten, "*60")) {
  4794. if (option_verbose > 2)
  4795. ast_verbose(VERBOSE_PREFIX_3 "Blacklisting number %s\n", p->lastcallerid);
  4796. res = ast_db_put("blacklist", p->lastcallerid, "1");
  4797. if (!res) {
  4798. res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALRECALL);
  4799. memset(exten, 0, sizeof(exten));
  4800. len = 0;
  4801. }
  4802. } else if (p->hidecallerid && !strcmp(exten, "*82")) {
  4803. if (option_verbose > 2)
  4804. ast_verbose(VERBOSE_PREFIX_3 "Enabling Caller*ID on %s\n", chan->name);
  4805. /* Enable Caller*ID if enabled */
  4806. p->hidecallerid = 0;
  4807. if (chan->callerid)
  4808. free(chan->callerid);
  4809. if (!ast_strlen_zero(p->callerid))
  4810. chan->callerid = strdup(p->callerid);
  4811. res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALRECALL);
  4812. if (res) {
  4813. ast_log(LOG_WARNING, "Unable to do dial recall on channel %s: %s\n",
  4814. chan->name, strerror(errno));
  4815. }
  4816. len = 0;
  4817. memset(exten, 0, sizeof(exten));
  4818. timeout = firstdigittimeout;
  4819. } else if (!strcmp(exten, "*0")) {
  4820. struct ast_channel *nbridge =
  4821. p->subs[SUB_THREEWAY].owner;
  4822. struct zt_pvt *pbridge = NULL;
  4823. /* set up the private struct of the bridged one, if any */
  4824. if (nbridge && nbridge->bridge)
  4825. pbridge = nbridge->bridge->pvt->pvt;
  4826. if (nbridge && pbridge &&
  4827. (!strcmp(nbridge->type,"Zap")) &&
  4828. (!strcmp(nbridge->bridge->type, "Zap")) &&
  4829. ISTRUNK(pbridge)) {
  4830. int func = ZT_FLASH;
  4831. /* Clear out the dial buffer */
  4832. p->dop.dialstr[0] = '\0';
  4833. /* flash hookswitch */
  4834. if ((ioctl(pbridge->subs[SUB_REAL].zfd,ZT_HOOK,&func) == -1) && (errno != EINPROGRESS)) {
  4835. ast_log(LOG_WARNING, "Unable to flash external trunk on channel %s: %s\n",
  4836. nbridge->name, strerror(errno));
  4837. }
  4838. swap_subs(p, SUB_REAL, SUB_THREEWAY);
  4839. unalloc_sub(p, SUB_THREEWAY);
  4840. p->owner = p->subs[SUB_REAL].owner;
  4841. if (p->subs[SUB_REAL].owner->bridge)
  4842. ast_moh_stop(p->subs[SUB_REAL].owner->bridge);
  4843. ast_hangup(chan);
  4844. return NULL;
  4845. } else {
  4846. tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_CONGESTION);
  4847. zt_wait_event(p->subs[index].zfd);
  4848. tone_zone_play_tone(p->subs[index].zfd, -1);
  4849. swap_subs(p, SUB_REAL, SUB_THREEWAY);
  4850. unalloc_sub(p, SUB_THREEWAY);
  4851. p->owner = p->subs[SUB_REAL].owner;
  4852. ast_hangup(chan);
  4853. return NULL;
  4854. }
  4855. } else if (!ast_canmatch_extension(chan, chan->context, exten, 1, chan->callerid) &&
  4856. ((exten[0] != '*') || (strlen(exten) > 2))) {
  4857. if (option_debug)
  4858. ast_log(LOG_DEBUG, "Can't match %s from '%s' in context %s\n", exten, chan->callerid ? chan->callerid : "<Unknown Caller>", chan->context);
  4859. break;
  4860. }
  4861. if (!timeout)
  4862. timeout = gendigittimeout;
  4863. if (len && !ast_ignore_pattern(chan->context, exten))
  4864. tone_zone_play_tone(p->subs[index].zfd, -1);
  4865. }
  4866. break;
  4867. case SIG_FXSLS:
  4868. case SIG_FXSGS:
  4869. case SIG_FXSKS:
  4870. #ifdef ZAPATA_PRI
  4871. if (p->pri) {
  4872. /* This is a GR-303 trunk actually. Wait for the first ring... */
  4873. struct ast_frame *f;
  4874. int res;
  4875. time_t start;
  4876. time(&start);
  4877. ast_setstate(chan, AST_STATE_RING);
  4878. while(time(NULL) < start + 3) {
  4879. res = ast_waitfor(chan, 1000);
  4880. if (res) {
  4881. f = ast_read(chan);
  4882. if (!f) {
  4883. ast_log(LOG_WARNING, "Whoa, hangup while waiting for first ring!\n");
  4884. ast_hangup(chan);
  4885. return NULL;
  4886. } else if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_RING)) {
  4887. res = 1;
  4888. } else
  4889. res = 0;
  4890. ast_frfree(f);
  4891. if (res) {
  4892. ast_log(LOG_DEBUG, "Got ring!\n");
  4893. res = 0;
  4894. break;
  4895. }
  4896. }
  4897. }
  4898. }
  4899. #endif
  4900. /* If we want caller id, we're in a prering state due to a polarity reversal
  4901. * and we're set to use a polarity reversal to trigger the start of caller id,
  4902. * grab the caller id and wait for ringing to start... */
  4903. if (p->use_callerid && (chan->_state == AST_STATE_PRERING && p->cid_start == CID_START_POLARITY)) {
  4904. /* If set to use DTMF CID signalling, listen for DTMF */
  4905. if (p->cid_signalling == CID_SIG_DTMF) {
  4906. int i = 0;
  4907. cs = NULL;
  4908. ast_log(LOG_DEBUG, "Receiving DTMF cid on "
  4909. "channel %s\n", chan->name);
  4910. zt_setlinear(p->subs[index].zfd, 0);
  4911. res = 2000;
  4912. for (;;) {
  4913. struct ast_frame *f;
  4914. res = ast_waitfor(chan, res);
  4915. if (res <= 0) {
  4916. ast_log(LOG_WARNING, "DTMFCID timed out waiting for ring. "
  4917. "Exiting simple switch\n");
  4918. ast_hangup(chan);
  4919. return NULL;
  4920. }
  4921. f = ast_read(chan);
  4922. if (f->frametype == AST_FRAME_DTMF) {
  4923. dtmfbuf[i++] = f->subclass;
  4924. ast_log(LOG_DEBUG, "CID got digit '%c'\n", f->subclass);
  4925. res = 2000;
  4926. }
  4927. ast_frfree(f);
  4928. if (chan->_state == AST_STATE_RING ||
  4929. chan->_state == AST_STATE_RINGING)
  4930. break; /* Got ring */
  4931. }
  4932. dtmfbuf[i] = 0;
  4933. zt_setlinear(p->subs[index].zfd, p->subs[index].linear);
  4934. /* Got cid and ring. */
  4935. ast_log(LOG_DEBUG, "CID got string '%s'\n", dtmfbuf);
  4936. callerid_get_dtmf(dtmfbuf, dtmfcid, &flags);
  4937. ast_log(LOG_DEBUG, "CID is '%s', flags %d\n",
  4938. dtmfcid, flags);
  4939. /* If first byte is NULL, we have no cid */
  4940. if (dtmfcid[0])
  4941. number = dtmfcid;
  4942. else
  4943. number = 0;
  4944. /* If set to use V23 Signalling, launch our FSK gubbins and listen for it */
  4945. } else if (p->cid_signalling == CID_SIG_V23) {
  4946. cs = callerid_new(cid_signalling);
  4947. if (cs) {
  4948. #if 1
  4949. bump_gains(p);
  4950. #endif
  4951. /* Take out of linear mode for Caller*ID processing */
  4952. zt_setlinear(p->subs[index].zfd, 0);
  4953. /* First we wait and listen for the Caller*ID */
  4954. for(;;) {
  4955. i = ZT_IOMUX_READ | ZT_IOMUX_SIGEVENT;
  4956. if ((res = ioctl(p->subs[index].zfd, ZT_IOMUX, &i))) {
  4957. ast_log(LOG_WARNING, "I/O MUX failed: %s\n", strerror(errno));
  4958. callerid_free(cs);
  4959. ast_hangup(chan);
  4960. return NULL;
  4961. }
  4962. if (i & ZT_IOMUX_SIGEVENT) {
  4963. res = zt_get_event(p->subs[index].zfd);
  4964. ast_log(LOG_NOTICE, "Got event %d (%s)...\n", res, event2str(res));
  4965. res = 0;
  4966. } else if (i & ZT_IOMUX_READ) {
  4967. res = read(p->subs[index].zfd, buf, sizeof(buf));
  4968. if (res < 0) {
  4969. if (errno != ELAST) {
  4970. ast_log(LOG_WARNING, "read returned error: %s\n", strerror(errno));
  4971. callerid_free(cs);
  4972. ast_hangup(chan);
  4973. return NULL;
  4974. }
  4975. break;
  4976. }
  4977. res = callerid_feed(cs, buf, res, AST_LAW(p));
  4978. if (res < 0) {
  4979. ast_log(LOG_WARNING, "CallerID feed failed: %s\n", strerror(errno));
  4980. break;
  4981. } else if (res)
  4982. break;
  4983. }
  4984. }
  4985. if (res == 1) {
  4986. callerid_get(cs, &name, &number, &flags);
  4987. if (option_debug)
  4988. ast_log(LOG_DEBUG, "CallerID number: %s, name: %s, flags=%d\n", number, name, flags);
  4989. }
  4990. if (res < 0) {
  4991. ast_log(LOG_WARNING, "CallerID returned with error on channel '%s'\n", chan->name);
  4992. }
  4993. /* Finished with Caller*ID, now wait for a ring to make sure there really is a call coming */
  4994. res = 2000;
  4995. for (;;) {
  4996. struct ast_frame *f;
  4997. res = ast_waitfor(chan, res);
  4998. if (res <= 0) {
  4999. ast_log(LOG_WARNING, "CID timed out waiting for ring. "
  5000. "Exiting simple switch\n");
  5001. ast_hangup(chan);
  5002. return NULL;
  5003. }
  5004. f = ast_read(chan);
  5005. ast_frfree(f);
  5006. if (chan->_state == AST_STATE_RING ||
  5007. chan->_state == AST_STATE_RINGING)
  5008. break; /* Got ring */
  5009. }
  5010. /* We must have a ring by now, so, if configured, lets try to listen for
  5011. * distinctive ringing */
  5012. if (p->usedistinctiveringdetection == 1) {
  5013. len = 0;
  5014. distMatches = 0;
  5015. /* Clear the current ring data array so we dont have old data in it. */
  5016. for (receivedRingT=0; receivedRingT < 3; receivedRingT++) {
  5017. curRingData[receivedRingT] = 0;
  5018. }
  5019. receivedRingT = 0;
  5020. counter = 0;
  5021. counter1 = 0;
  5022. /* Check to see if context is what it should be, if not set to be. */
  5023. if (strcmp(p->context,p->defcontext) != 0) {
  5024. strncpy(p->context, p->defcontext, sizeof(p->context)-1);
  5025. strncpy(chan->context,p->defcontext,sizeof(chan->context)-1);
  5026. }
  5027. for(;;) {
  5028. i = ZT_IOMUX_READ | ZT_IOMUX_SIGEVENT;
  5029. if ((res = ioctl(p->subs[index].zfd, ZT_IOMUX, &i))) {
  5030. ast_log(LOG_WARNING, "I/O MUX failed: %s\n", strerror(errno));
  5031. callerid_free(cs);
  5032. ast_hangup(chan);
  5033. return NULL;
  5034. }
  5035. if (i & ZT_IOMUX_SIGEVENT) {
  5036. res = zt_get_event(p->subs[index].zfd);
  5037. ast_log(LOG_NOTICE, "Got event %d (%s)...\n", res, event2str(res));
  5038. res = 0;
  5039. /* Let us detect distinctive ring */
  5040. curRingData[receivedRingT] = p->ringt;
  5041. if (p->ringt < RINGT/2)
  5042. break;
  5043. ++receivedRingT; /* Increment the ringT counter so we can match it against
  5044. values in zapata.conf for distinctive ring */
  5045. } else if (i & ZT_IOMUX_READ) {
  5046. res = read(p->subs[index].zfd, buf, sizeof(buf));
  5047. if (res < 0) {
  5048. if (errno != ELAST) {
  5049. ast_log(LOG_WARNING, "read returned error: %s\n", strerror(errno));
  5050. callerid_free(cs);
  5051. ast_hangup(chan);
  5052. return NULL;
  5053. }
  5054. break;
  5055. }
  5056. if (p->ringt)
  5057. p->ringt--;
  5058. if (p->ringt == 1) {
  5059. res = -1;
  5060. break;
  5061. }
  5062. }
  5063. }
  5064. if(option_verbose > 2)
  5065. /* this only shows up if you have n of the dring patterns filled in */
  5066. ast_verbose( VERBOSE_PREFIX_3 "Detected ring pattern: %d,%d,%d\n",curRingData[0],curRingData[1],curRingData[2]);
  5067. for (counter=0; counter < 3; counter++) {
  5068. /* Check to see if the rings we received match any of the ones in zapata.conf for this
  5069. channel */
  5070. distMatches = 0;
  5071. for (counter1=0; counter1 < 3; counter1++) {
  5072. if (curRingData[counter1] <= (p->drings.ringnum[counter].ring[counter1]+10) && curRingData[counter1] >=
  5073. (p->drings.ringnum[counter].ring[counter1]-10)) {
  5074. distMatches++;
  5075. }
  5076. }
  5077. if (distMatches == 3) {
  5078. /* The ring matches, set the context to whatever is for distinctive ring.. */
  5079. strncpy(p->context, p->drings.ringContext[counter].contextData, sizeof(p->context)-1);
  5080. strncpy(chan->context, p->drings.ringContext[counter].contextData, sizeof(chan->context)-1);
  5081. if(option_verbose > 2)
  5082. ast_verbose( VERBOSE_PREFIX_3 "Distinctive Ring matched context %s\n",p->context);
  5083. break;
  5084. }
  5085. }
  5086. }
  5087. /* Restore linear mode (if appropriate) for Caller*ID processing */
  5088. zt_setlinear(p->subs[index].zfd, p->subs[index].linear);
  5089. #if 1
  5090. restore_gains(p);
  5091. #endif
  5092. } else
  5093. ast_log(LOG_WARNING, "Unable to get caller ID space\n");
  5094. } else {
  5095. ast_log(LOG_WARNING, "Channel %s in prering "
  5096. "state, but I have nothing to do. "
  5097. "Terminating simple switch, should be "
  5098. "restarted by the actual ring.\n",
  5099. chan->name);
  5100. ast_hangup(chan);
  5101. return NULL;
  5102. }
  5103. } else if (p->use_callerid && p->cid_start == CID_START_RING) {
  5104. /* FSK Bell202 callerID */
  5105. cs = callerid_new(cid_signalling);
  5106. if (cs) {
  5107. #if 1
  5108. bump_gains(p);
  5109. #endif
  5110. len = 0;
  5111. distMatches = 0;
  5112. /* Clear the current ring data array so we dont have old data in it. */
  5113. for (receivedRingT=0; receivedRingT < 3; receivedRingT++) {
  5114. curRingData[receivedRingT] = 0;
  5115. }
  5116. receivedRingT = 0;
  5117. counter = 0;
  5118. counter1 = 0;
  5119. /* Check to see if context is what it should be, if not set to be. */
  5120. if (strcmp(p->context,p->defcontext) != 0) {
  5121. strncpy(p->context, p->defcontext, sizeof(p->context)-1);
  5122. strncpy(chan->context,p->defcontext,sizeof(chan->context)-1);
  5123. }
  5124. /* Take out of linear mode for Caller*ID processing */
  5125. zt_setlinear(p->subs[index].zfd, 0);
  5126. for(;;) {
  5127. i = ZT_IOMUX_READ | ZT_IOMUX_SIGEVENT;
  5128. if ((res = ioctl(p->subs[index].zfd, ZT_IOMUX, &i))) {
  5129. ast_log(LOG_WARNING, "I/O MUX failed: %s\n", strerror(errno));
  5130. callerid_free(cs);
  5131. ast_hangup(chan);
  5132. return NULL;
  5133. }
  5134. if (i & ZT_IOMUX_SIGEVENT) {
  5135. res = zt_get_event(p->subs[index].zfd);
  5136. ast_log(LOG_NOTICE, "Got event %d (%s)...\n", res, event2str(res));
  5137. res = 0;
  5138. /* Let us detect callerid when the telco uses distinctive ring */
  5139. curRingData[receivedRingT] = p->ringt;
  5140. if (p->ringt < RINGT/2)
  5141. break;
  5142. ++receivedRingT; /* Increment the ringT counter so we can match it against
  5143. values in zapata.conf for distinctive ring */
  5144. } else if (i & ZT_IOMUX_READ) {
  5145. res = read(p->subs[index].zfd, buf, sizeof(buf));
  5146. if (res < 0) {
  5147. if (errno != ELAST) {
  5148. ast_log(LOG_WARNING, "read returned error: %s\n", strerror(errno));
  5149. callerid_free(cs);
  5150. ast_hangup(chan);
  5151. return NULL;
  5152. }
  5153. break;
  5154. }
  5155. if (p->ringt)
  5156. p->ringt--;
  5157. if (p->ringt == 1) {
  5158. res = -1;
  5159. break;
  5160. }
  5161. res = callerid_feed(cs, buf, res, AST_LAW(p));
  5162. if (res < 0) {
  5163. ast_log(LOG_WARNING, "CallerID feed failed: %s\n", strerror(errno));
  5164. break;
  5165. } else if (res)
  5166. break;
  5167. }
  5168. }
  5169. if (p->usedistinctiveringdetection == 1) {
  5170. if(option_verbose > 2)
  5171. /* this only shows up if you have n of the dring patterns filled in */
  5172. ast_verbose( VERBOSE_PREFIX_3 "Detected ring pattern: %d,%d,%d\n",curRingData[0],curRingData[1],curRingData[2]);
  5173. for (counter=0; counter < 3; counter++) {
  5174. /* Check to see if the rings we received match any of the ones in zapata.conf for this
  5175. channel */
  5176. distMatches = 0;
  5177. for (counter1=0; counter1 < 3; counter1++) {
  5178. if (curRingData[counter1] <= (p->drings.ringnum[counter].ring[counter1]+10) && curRingData[counter1] >=
  5179. (p->drings.ringnum[counter].ring[counter1]-10)) {
  5180. distMatches++;
  5181. }
  5182. }
  5183. if (distMatches == 3) {
  5184. /* The ring matches, set the context to whatever is for distinctive ring.. */
  5185. strncpy(p->context, p->drings.ringContext[counter].contextData, sizeof(p->context)-1);
  5186. strncpy(chan->context, p->drings.ringContext[counter].contextData, sizeof(chan->context)-1);
  5187. if(option_verbose > 2)
  5188. ast_verbose( VERBOSE_PREFIX_3 "Distinctive Ring matched context %s\n",p->context);
  5189. break;
  5190. }
  5191. }
  5192. }
  5193. if (res == 1) {
  5194. callerid_get(cs, &name, &number, &flags);
  5195. if (option_debug)
  5196. ast_log(LOG_DEBUG, "CallerID number: %s, name: %s, flags=%d\n", number, name, flags);
  5197. }
  5198. /* Restore linear mode (if appropriate) for Caller*ID processing */
  5199. zt_setlinear(p->subs[index].zfd, p->subs[index].linear);
  5200. #if 1
  5201. restore_gains(p);
  5202. #endif
  5203. if (res < 0) {
  5204. ast_log(LOG_WARNING, "CallerID returned with error on channel '%s'\n", chan->name);
  5205. }
  5206. } else
  5207. ast_log(LOG_WARNING, "Unable to get caller ID space\n");
  5208. }
  5209. else
  5210. cs = NULL;
  5211. if (name && number) {
  5212. snprintf(cid, sizeof(cid), "\"%s\" <%s>", name, number);
  5213. } else if (name) {
  5214. snprintf(cid, sizeof(cid), "\"%s\"", name);
  5215. } else if (number) {
  5216. snprintf(cid, sizeof(cid), "%s", number);
  5217. } else {
  5218. cid[0] = '\0';
  5219. }
  5220. if (cs)
  5221. callerid_free(cs);
  5222. if (!ast_strlen_zero(cid)) {
  5223. chan->callerid = strdup(cid);
  5224. chan->ani = strdup(cid);
  5225. }
  5226. ast_setstate(chan, AST_STATE_RING);
  5227. chan->rings = 1;
  5228. p->ringt = RINGT;
  5229. res = ast_pbx_run(chan);
  5230. if (res) {
  5231. ast_hangup(chan);
  5232. ast_log(LOG_WARNING, "PBX exited non-zero\n");
  5233. }
  5234. return NULL;
  5235. default:
  5236. ast_log(LOG_WARNING, "Don't know how to handle simple switch with signalling %s on channel %d\n", sig2str(p->sig), p->channel);
  5237. res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_CONGESTION);
  5238. if (res < 0)
  5239. ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", p->channel);
  5240. }
  5241. res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_CONGESTION);
  5242. if (res < 0)
  5243. ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", p->channel);
  5244. ast_hangup(chan);
  5245. return NULL;
  5246. }
  5247. #ifdef ZAPATA_R2
  5248. static int handle_init_r2_event(struct zt_pvt *i, mfcr2_event_t *e)
  5249. {
  5250. struct ast_channel *chan;
  5251. switch(e->e) {
  5252. case MFCR2_EVENT_UNBLOCKED:
  5253. i->r2blocked = 0;
  5254. if (option_verbose > 2)
  5255. ast_verbose(VERBOSE_PREFIX_3 "R2 Channel %d unblocked\n", i->channel);
  5256. break;
  5257. case MFCR2_EVENT_BLOCKED:
  5258. i->r2blocked = 1;
  5259. if (option_verbose > 2)
  5260. ast_verbose(VERBOSE_PREFIX_3 "R2 Channel %d unblocked\n", i->channel);
  5261. break;
  5262. case MFCR2_EVENT_IDLE:
  5263. if (option_verbose > 2)
  5264. ast_verbose(VERBOSE_PREFIX_3 "R2 Channel %d idle\n", i->channel);
  5265. break;
  5266. case MFCR2_EVENT_RINGING:
  5267. /* This is what Asterisk refers to as a "RING" event. For some reason they're reversed in
  5268. Steve's code */
  5269. /* Check for callerid, digits, etc */
  5270. i->hasr2call = 1;
  5271. chan = zt_new(i, AST_STATE_RING, 0, SUB_REAL, 0);
  5272. if (!chan) {
  5273. ast_log(LOG_WARNING, "Unable to create channel for channel %d\n", i->channel);
  5274. mfcr2_DropCall(i->r2, NULL, UC_NETWORK_CONGESTION);
  5275. i->hasr2call = 0;
  5276. }
  5277. if (ast_pbx_start(chan)) {
  5278. ast_log(LOG_WARNING, "Unable to start PBX on channel %s\n", chan->name);
  5279. ast_hangup(chan);
  5280. }
  5281. break;
  5282. default:
  5283. ast_log(LOG_WARNING, "Don't know how to handle initial R2 event %s on channel %d\n", mfcr2_event2str(e->e), i->channel);
  5284. return -1;
  5285. }
  5286. return 0;
  5287. }
  5288. #endif
  5289. static int handle_init_event(struct zt_pvt *i, int event)
  5290. {
  5291. int res;
  5292. pthread_t threadid;
  5293. pthread_attr_t attr;
  5294. struct ast_channel *chan;
  5295. pthread_attr_init(&attr);
  5296. pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  5297. /* Handle an event on a given channel for the monitor thread. */
  5298. switch(event) {
  5299. case ZT_EVENT_NONE:
  5300. case ZT_EVENT_BITSCHANGED:
  5301. if (i->radio) break;
  5302. #ifdef ZAPATA_R2
  5303. if (i->r2) {
  5304. mfcr2_event_t *e;
  5305. e = r2_get_event_bits(i);
  5306. i->sigchecked = 1;
  5307. if (e)
  5308. handle_init_r2_event(i, e);
  5309. }
  5310. #endif
  5311. break;
  5312. case ZT_EVENT_WINKFLASH:
  5313. case ZT_EVENT_RINGOFFHOOK:
  5314. if (i->inalarm) break;
  5315. if (i->radio) break;
  5316. /* Got a ring/answer. What kind of channel are we? */
  5317. switch(i->sig) {
  5318. case SIG_FXOLS:
  5319. case SIG_FXOGS:
  5320. case SIG_FXOKS:
  5321. zt_set_hook(i->subs[SUB_REAL].zfd, ZT_OFFHOOK);
  5322. if (i->cidspill) {
  5323. /* Cancel VMWI spill */
  5324. free(i->cidspill);
  5325. i->cidspill = NULL;
  5326. }
  5327. if (i->immediate) {
  5328. zt_enable_ec(i);
  5329. /* The channel is immediately up. Start right away */
  5330. res = tone_zone_play_tone(i->subs[SUB_REAL].zfd, ZT_TONE_RINGTONE);
  5331. chan = zt_new(i, AST_STATE_RING, 1, SUB_REAL, 0, 0);
  5332. if (!chan) {
  5333. ast_log(LOG_WARNING, "Unable to start PBX on channel %d\n", i->channel);
  5334. res = tone_zone_play_tone(i->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION);
  5335. if (res < 0)
  5336. ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", i->channel);
  5337. }
  5338. } else {
  5339. /* Check for callerid, digits, etc */
  5340. chan = zt_new(i, AST_STATE_RESERVED, 0, SUB_REAL, 0, 0);
  5341. if (chan) {
  5342. if (has_voicemail(i))
  5343. res = tone_zone_play_tone(i->subs[SUB_REAL].zfd, ZT_TONE_STUTTER);
  5344. else
  5345. res = tone_zone_play_tone(i->subs[SUB_REAL].zfd, ZT_TONE_DIALTONE);
  5346. if (res < 0)
  5347. ast_log(LOG_WARNING, "Unable to play dialtone on channel %d\n", i->channel);
  5348. if (ast_pthread_create(&threadid, &attr, ss_thread, chan)) {
  5349. ast_log(LOG_WARNING, "Unable to start simple switch thread on channel %d\n", i->channel);
  5350. res = tone_zone_play_tone(i->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION);
  5351. if (res < 0)
  5352. ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", i->channel);
  5353. ast_hangup(chan);
  5354. }
  5355. } else
  5356. ast_log(LOG_WARNING, "Unable to create channel\n");
  5357. #if 0
  5358. printf("Created thread %ld detached in switch\n", threadid);
  5359. #endif
  5360. }
  5361. break;
  5362. case SIG_FXSLS:
  5363. case SIG_FXSGS:
  5364. case SIG_FXSKS:
  5365. i->ringt = RINGT;
  5366. /* Fall through */
  5367. case SIG_EMWINK:
  5368. case SIG_FEATD:
  5369. case SIG_FEATDMF:
  5370. case SIG_E911:
  5371. case SIG_FEATB:
  5372. case SIG_EM:
  5373. case SIG_EM_E1:
  5374. case SIG_SFWINK:
  5375. case SIG_SF_FEATD:
  5376. case SIG_SF_FEATDMF:
  5377. case SIG_SF_FEATB:
  5378. case SIG_SF:
  5379. /* Check for callerid, digits, etc */
  5380. chan = zt_new(i, AST_STATE_RING, 0, SUB_REAL, 0, 0);
  5381. if (chan && ast_pthread_create(&threadid, &attr, ss_thread, chan)) {
  5382. ast_log(LOG_WARNING, "Unable to start simple switch thread on channel %d\n", i->channel);
  5383. res = tone_zone_play_tone(i->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION);
  5384. if (res < 0)
  5385. ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", i->channel);
  5386. ast_hangup(chan);
  5387. } else if (!chan) {
  5388. ast_log(LOG_WARNING, "Cannot allocate new structure on channel %d\n", i->channel);
  5389. }
  5390. #if 0
  5391. printf("Created thread %ld detached in switch(2)\n", threadid);
  5392. #endif
  5393. break;
  5394. default:
  5395. ast_log(LOG_WARNING, "Don't know how to handle ring/answer with signalling %s on channel %d\n", sig2str(i->sig), i->channel);
  5396. res = tone_zone_play_tone(i->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION);
  5397. if (res < 0)
  5398. ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", i->channel);
  5399. return -1;
  5400. }
  5401. break;
  5402. case ZT_EVENT_NOALARM:
  5403. i->inalarm = 0;
  5404. ast_log(LOG_NOTICE, "Alarm cleared on channel %d\n", i->channel);
  5405. break;
  5406. case ZT_EVENT_ALARM:
  5407. i->inalarm = 1;
  5408. res = get_alarms(i);
  5409. ast_log(LOG_WARNING, "Detected alarm on channel %d: %s\n", i->channel, alarm2str(res));
  5410. /* fall thru intentionally */
  5411. case ZT_EVENT_ONHOOK:
  5412. if (i->radio) break;
  5413. /* Back on hook. Hang up. */
  5414. switch(i->sig) {
  5415. case SIG_FXOLS:
  5416. case SIG_FXOGS:
  5417. case SIG_FEATD:
  5418. case SIG_FEATDMF:
  5419. case SIG_E911:
  5420. case SIG_FEATB:
  5421. case SIG_EM:
  5422. case SIG_EM_E1:
  5423. case SIG_EMWINK:
  5424. case SIG_SF_FEATD:
  5425. case SIG_SF_FEATDMF:
  5426. case SIG_SF_FEATB:
  5427. case SIG_SF:
  5428. case SIG_SFWINK:
  5429. case SIG_FXSLS:
  5430. case SIG_FXSGS:
  5431. case SIG_FXSKS:
  5432. case SIG_GR303FXSKS:
  5433. zt_disable_ec(i);
  5434. res = tone_zone_play_tone(i->subs[SUB_REAL].zfd, -1);
  5435. zt_set_hook(i->subs[SUB_REAL].zfd, ZT_ONHOOK);
  5436. break;
  5437. case SIG_GR303FXOKS:
  5438. case SIG_FXOKS:
  5439. zt_disable_ec(i);
  5440. /* Diddle the battery for the zhone */
  5441. #ifdef ZHONE_HACK
  5442. zt_set_hook(i->subs[SUB_REAL].zfd, ZT_OFFHOOK);
  5443. usleep(1);
  5444. #endif
  5445. res = tone_zone_play_tone(i->subs[SUB_REAL].zfd, -1);
  5446. zt_set_hook(i->subs[SUB_REAL].zfd, ZT_ONHOOK);
  5447. break;
  5448. case SIG_PRI:
  5449. zt_disable_ec(i);
  5450. res = tone_zone_play_tone(i->subs[SUB_REAL].zfd, -1);
  5451. break;
  5452. default:
  5453. ast_log(LOG_WARNING, "Don't know how to handle on hook with signalling %s on channel %d\n", sig2str(i->sig), i->channel);
  5454. res = tone_zone_play_tone(i->subs[SUB_REAL].zfd, -1);
  5455. return -1;
  5456. }
  5457. break;
  5458. case ZT_EVENT_POLARITY:
  5459. switch(i->sig) {
  5460. case SIG_FXSLS:
  5461. case SIG_FXSKS:
  5462. case SIG_FXSGS:
  5463. if (i->cid_start == CID_START_POLARITY) {
  5464. ast_verbose(VERBOSE_PREFIX_2 "Starting post polarity "
  5465. "CID detection on channel %d\n",
  5466. i->channel);
  5467. chan = zt_new(i, AST_STATE_PRERING, 0, SUB_REAL, 0, 0);
  5468. if (chan && ast_pthread_create(&threadid, &attr, ss_thread, chan)) {
  5469. ast_log(LOG_WARNING, "Unable to start simple switch thread on channel %d\n", i->channel);
  5470. }
  5471. }
  5472. break;
  5473. default:
  5474. ast_log(LOG_WARNING, "handle_init_event detected "
  5475. "polarity reversal on non-FXO (SIG_FXS) "
  5476. "interface %d\n", i->channel);
  5477. }
  5478. }
  5479. return 0;
  5480. }
  5481. static void *do_monitor(void *data)
  5482. {
  5483. int count, res, res2, spoint, pollres=0;
  5484. struct zt_pvt *i;
  5485. struct zt_pvt *last = NULL;
  5486. time_t thispass = 0, lastpass = 0;
  5487. int found;
  5488. char buf[1024];
  5489. struct pollfd *pfds=NULL;
  5490. int lastalloc = -1;
  5491. /* This thread monitors all the frame relay interfaces which are not yet in use
  5492. (and thus do not have a separate thread) indefinitely */
  5493. /* From here on out, we die whenever asked */
  5494. #if 0
  5495. if (pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL)) {
  5496. ast_log(LOG_WARNING, "Unable to set cancel type to asynchronous\n");
  5497. return NULL;
  5498. }
  5499. ast_log(LOG_DEBUG, "Monitor starting...\n");
  5500. #endif
  5501. for(;;) {
  5502. /* Lock the interface list */
  5503. if (ast_mutex_lock(&iflock)) {
  5504. ast_log(LOG_ERROR, "Unable to grab interface lock\n");
  5505. return NULL;
  5506. }
  5507. if (!pfds || (lastalloc != ifcount)) {
  5508. if (pfds)
  5509. free(pfds);
  5510. if (ifcount) {
  5511. pfds = malloc(ifcount * sizeof(struct pollfd));
  5512. if (!pfds) {
  5513. ast_log(LOG_WARNING, "Critical memory error. Zap dies.\n");
  5514. ast_mutex_unlock(&iflock);
  5515. return NULL;
  5516. }
  5517. }
  5518. lastalloc = ifcount;
  5519. }
  5520. /* Build the stuff we're going to poll on, that is the socket of every
  5521. zt_pvt that does not have an associated owner channel */
  5522. count = 0;
  5523. i = iflist;
  5524. while(i) {
  5525. if ((i->subs[SUB_REAL].zfd > -1) && i->sig && (!i->radio)) {
  5526. if (!i->owner && !i->subs[SUB_REAL].owner) {
  5527. /* This needs to be watched, as it lacks an owner */
  5528. pfds[count].fd = i->subs[SUB_REAL].zfd;
  5529. pfds[count].events = POLLPRI;
  5530. /* Message waiting or r2 channels also get watched for reading */
  5531. #ifdef ZAPATA_R2
  5532. if (i->cidspill || i->r2)
  5533. #else
  5534. if (i->cidspill)
  5535. #endif
  5536. pfds[count].events |= POLLIN;
  5537. count++;
  5538. }
  5539. }
  5540. i = i->next;
  5541. }
  5542. /* Okay, now that we know what to do, release the interface lock */
  5543. ast_mutex_unlock(&iflock);
  5544. pthread_testcancel();
  5545. /* Wait at least a second for something to happen */
  5546. res = poll(pfds, count, 1000);
  5547. pthread_testcancel();
  5548. /* Okay, poll has finished. Let's see what happened. */
  5549. if (res < 0) {
  5550. if ((errno != EAGAIN) && (errno != EINTR))
  5551. ast_log(LOG_WARNING, "poll return %d: %s\n", res, strerror(errno));
  5552. continue;
  5553. }
  5554. /* Alright, lock the interface list again, and let's look and see what has
  5555. happened */
  5556. if (ast_mutex_lock(&iflock)) {
  5557. ast_log(LOG_WARNING, "Unable to lock the interface list\n");
  5558. continue;
  5559. }
  5560. found = 0;
  5561. spoint = 0;
  5562. lastpass = thispass;
  5563. thispass = time(NULL);
  5564. i = iflist;
  5565. while(i) {
  5566. if (thispass != lastpass) {
  5567. if (!found && ((i == last) || ((i == iflist) && !last))) {
  5568. last = i;
  5569. if (last) {
  5570. #if 0
  5571. printf("Checking channel %d\n", last->channel);
  5572. #endif
  5573. if (!last->cidspill && !last->owner && !ast_strlen_zero(last->mailbox) && (thispass - last->onhooktime > 3) &&
  5574. (last->sig & __ZT_SIG_FXO)) {
  5575. #if 0
  5576. printf("Channel %d has mailbox %s\n", last->channel, last->mailbox);
  5577. #endif
  5578. res = ast_app_has_voicemail(last->mailbox);
  5579. if (last->msgstate != res) {
  5580. int x;
  5581. ast_log(LOG_DEBUG, "Message status for %s changed from %d to %d on %d\n", last->mailbox, last->msgstate, res, last->channel);
  5582. x = ZT_FLUSH_BOTH;
  5583. res2 = ioctl(last->subs[SUB_REAL].zfd, ZT_FLUSH, &x);
  5584. if (res2)
  5585. ast_log(LOG_WARNING, "Unable to flush input on channel %d\n", last->channel);
  5586. last->cidspill = malloc(MAX_CALLERID_SIZE);
  5587. if (last->cidspill) {
  5588. /* Turn on on hook transfer for 4 seconds */
  5589. x = 4000;
  5590. ioctl(last->subs[SUB_REAL].zfd, ZT_ONHOOKTRANSFER, &x);
  5591. last->cidlen = vmwi_generate(last->cidspill, res, 1, AST_LAW(last));
  5592. last->cidpos = 0;
  5593. #if 0
  5594. printf("Made %d bytes of message waiting for %d\n", last->cidlen, res);
  5595. #endif
  5596. last->msgstate = res;
  5597. last->onhooktime = thispass;
  5598. }
  5599. found ++;
  5600. }
  5601. }
  5602. last = last->next;
  5603. }
  5604. }
  5605. }
  5606. if ((i->subs[SUB_REAL].zfd > -1) && i->sig) {
  5607. if (i->radio && !i->owner)
  5608. {
  5609. res = zt_get_event(i->subs[SUB_REAL].zfd);
  5610. if (res)
  5611. {
  5612. if (option_debug)
  5613. ast_log(LOG_DEBUG, "Monitor doohicky got event %s on radio channel %d\n", event2str(res), i->channel);
  5614. /* Don't hold iflock while handling init events */
  5615. ast_mutex_unlock(&iflock);
  5616. handle_init_event(i, res);
  5617. ast_mutex_lock(&iflock);
  5618. }
  5619. i = i->next;
  5620. continue;
  5621. }
  5622. pollres = ast_fdisset(pfds, i->subs[SUB_REAL].zfd, count, &spoint);
  5623. if (pollres & POLLIN) {
  5624. if (i->owner || i->subs[SUB_REAL].owner) {
  5625. #ifdef ZAPATA_PRI
  5626. if (!i->pri)
  5627. #endif
  5628. ast_log(LOG_WARNING, "Whoa.... I'm owned but found (%d) in read...\n", i->subs[SUB_REAL].zfd);
  5629. i = i->next;
  5630. continue;
  5631. }
  5632. #ifdef ZAPATA_R2
  5633. if (i->r2) {
  5634. /* If it's R2 signalled, we always have to check for events */
  5635. mfcr2_event_t *e;
  5636. e = mfcr2_check_event(i->r2);
  5637. if (e)
  5638. handle_init_r2_event(i, e);
  5639. else {
  5640. e = mfcr2_schedule_run(i->r2);
  5641. if (e)
  5642. handle_init_r2_event(i, e);
  5643. }
  5644. i = i->next;
  5645. continue;
  5646. }
  5647. #endif
  5648. if (!i->cidspill) {
  5649. ast_log(LOG_WARNING, "Whoa.... I'm reading but have no cidspill (%d)...\n", i->subs[SUB_REAL].zfd);
  5650. i = i->next;
  5651. continue;
  5652. }
  5653. res = read(i->subs[SUB_REAL].zfd, buf, sizeof(buf));
  5654. if (res > 0) {
  5655. /* We read some number of bytes. Write an equal amount of data */
  5656. if (res > i->cidlen - i->cidpos)
  5657. res = i->cidlen - i->cidpos;
  5658. res2 = write(i->subs[SUB_REAL].zfd, i->cidspill + i->cidpos, res);
  5659. if (res2 > 0) {
  5660. i->cidpos += res2;
  5661. if (i->cidpos >= i->cidlen) {
  5662. free(i->cidspill);
  5663. i->cidspill = 0;
  5664. i->cidpos = 0;
  5665. i->cidlen = 0;
  5666. }
  5667. } else {
  5668. ast_log(LOG_WARNING, "Write failed: %s\n", strerror(errno));
  5669. i->msgstate = -1;
  5670. }
  5671. } else {
  5672. ast_log(LOG_WARNING, "Read failed with %d: %s\n", res, strerror(errno));
  5673. }
  5674. if (option_debug)
  5675. ast_log(LOG_DEBUG, "Monitor doohicky got event %s on channel %d\n", event2str(res), i->channel);
  5676. /* Don't hold iflock while handling init events -- race with chlock */
  5677. ast_mutex_unlock(&iflock);
  5678. handle_init_event(i, res);
  5679. ast_mutex_lock(&iflock);
  5680. }
  5681. #ifdef ZAPATA_R2
  5682. if ((pollres & POLLPRI) || (i->r2 && !i->sigchecked))
  5683. #else
  5684. if (pollres & POLLPRI)
  5685. #endif
  5686. {
  5687. if (i->owner || i->subs[SUB_REAL].owner) {
  5688. #ifdef ZAPATA_PRI
  5689. if (!i->pri)
  5690. #endif
  5691. ast_log(LOG_WARNING, "Whoa.... I'm owned but found (%d)...\n", i->subs[SUB_REAL].zfd);
  5692. i = i->next;
  5693. continue;
  5694. }
  5695. res = zt_get_event(i->subs[SUB_REAL].zfd);
  5696. if (option_debug)
  5697. ast_log(LOG_DEBUG, "Monitor doohicky got event %s on channel %d\n", event2str(res), i->channel);
  5698. /* Don't hold iflock while handling init events */
  5699. ast_mutex_unlock(&iflock);
  5700. handle_init_event(i, res);
  5701. ast_mutex_lock(&iflock);
  5702. }
  5703. }
  5704. i=i->next;
  5705. }
  5706. ast_mutex_unlock(&iflock);
  5707. }
  5708. /* Never reached */
  5709. return NULL;
  5710. }
  5711. static int restart_monitor(void)
  5712. {
  5713. pthread_attr_t attr;
  5714. pthread_attr_init(&attr);
  5715. pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  5716. /* If we're supposed to be stopped -- stay stopped */
  5717. if (monitor_thread == AST_PTHREADT_STOP)
  5718. return 0;
  5719. if (ast_mutex_lock(&monlock)) {
  5720. ast_log(LOG_WARNING, "Unable to lock monitor\n");
  5721. return -1;
  5722. }
  5723. if (monitor_thread == pthread_self()) {
  5724. ast_mutex_unlock(&monlock);
  5725. ast_log(LOG_WARNING, "Cannot kill myself\n");
  5726. return -1;
  5727. }
  5728. if (monitor_thread != AST_PTHREADT_NULL) {
  5729. /* Just signal it to be sure it wakes up */
  5730. #if 0
  5731. pthread_cancel(monitor_thread);
  5732. #endif
  5733. pthread_kill(monitor_thread, SIGURG);
  5734. #if 0
  5735. pthread_join(monitor_thread, NULL);
  5736. #endif
  5737. } else {
  5738. /* Start a new monitor */
  5739. if (ast_pthread_create(&monitor_thread, &attr, do_monitor, NULL) < 0) {
  5740. ast_mutex_unlock(&monlock);
  5741. ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
  5742. return -1;
  5743. }
  5744. }
  5745. #if 0
  5746. printf("Created thread %ld detached in restart monitor\n", monitor_thread);
  5747. #endif
  5748. ast_mutex_unlock(&monlock);
  5749. return 0;
  5750. }
  5751. static int reset_channel(struct zt_pvt *p)
  5752. {
  5753. int ioctlflag = 1;
  5754. int res = 0;
  5755. int i = 0;
  5756. ast_log(LOG_DEBUG, "reset_channel()\n");
  5757. if (p->owner) {
  5758. ioctlflag = 0;
  5759. p->owner->_softhangup |= AST_SOFTHANGUP_DEV;
  5760. }
  5761. for (i = 0; i < 3; i++) {
  5762. if (p->subs[i].owner) {
  5763. ioctlflag = 0;
  5764. p->subs[i].owner->_softhangup |= AST_SOFTHANGUP_DEV;
  5765. }
  5766. }
  5767. if (ioctlflag) {
  5768. res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_ONHOOK);
  5769. if (res < 0) {
  5770. ast_log(LOG_ERROR, "Unable to hangup chan_zap channel %d (ioctl)\n", p->channel);
  5771. return -1;
  5772. }
  5773. }
  5774. return 0;
  5775. }
  5776. #ifdef ZAPATA_PRI
  5777. static int pri_resolve_span(int *span, int channel, int offset, struct zt_spaninfo *si)
  5778. {
  5779. int x;
  5780. int trunkgroup;
  5781. /* Get appropriate trunk group if there is one */
  5782. trunkgroup = pris[*span].mastertrunkgroup;
  5783. if (trunkgroup) {
  5784. /* Select a specific trunk group */
  5785. for (x=0;x<NUM_SPANS;x++) {
  5786. if (pris[x].trunkgroup == trunkgroup) {
  5787. *span = x;
  5788. return 0;
  5789. }
  5790. }
  5791. ast_log(LOG_WARNING, "Channel %d on span %d configured to use nonexistent trunk group %d\n", channel, *span, trunkgroup);
  5792. *span = -1;
  5793. } else {
  5794. if (pris[*span].trunkgroup) {
  5795. ast_log(LOG_WARNING, "Unable to use span %d implicitly since it is trunk group %d (please use spanmap)\n", *span, pris[*span].trunkgroup);
  5796. *span = -1;
  5797. } else if (pris[*span].mastertrunkgroup) {
  5798. ast_log(LOG_WARNING, "Unable to use span %d implicitly since it is already part of trunk group %d\n", *span, pris[*span].mastertrunkgroup);
  5799. *span = -1;
  5800. } else {
  5801. if (si->totalchans == 31) { /* if it's an E1 */
  5802. pris[*span].dchannels[0] = 16 + offset;
  5803. } else {
  5804. pris[*span].dchannels[0] = 24 + offset;
  5805. }
  5806. pris[*span].dchanavail[0] |= DCHAN_PROVISIONED;
  5807. pris[*span].offset = offset;
  5808. pris[*span].span = *span + 1;
  5809. }
  5810. }
  5811. return 0;
  5812. }
  5813. static int pri_create_trunkgroup(int trunkgroup, int *channels)
  5814. {
  5815. struct zt_spaninfo si;
  5816. ZT_PARAMS p;
  5817. int fd;
  5818. int span;
  5819. int ospan=0;
  5820. int x,y;
  5821. for (x=0;x<NUM_SPANS;x++) {
  5822. if (pris[x].trunkgroup == trunkgroup) {
  5823. ast_log(LOG_WARNING, "Trunk group %d already exists on span %d, Primary d-channel %d\n", trunkgroup, x + 1, pris[x].dchannels[0]);
  5824. return -1;
  5825. }
  5826. }
  5827. for (y=0;y<NUM_DCHANS;y++) {
  5828. if (!channels[y])
  5829. break;
  5830. memset(&si, 0, sizeof(si));
  5831. memset(&p, 0, sizeof(p));
  5832. fd = open("/dev/zap/channel", O_RDWR);
  5833. if (fd < 0) {
  5834. ast_log(LOG_WARNING, "Failed to open channel: %s\n", strerror(errno));
  5835. return -1;
  5836. }
  5837. x = channels[y];
  5838. if (ioctl(fd, ZT_SPECIFY, &x)) {
  5839. ast_log(LOG_WARNING, "Failed to specify channel %d: %s\n", channels[y], strerror(errno));
  5840. zt_close(fd);
  5841. return -1;
  5842. }
  5843. if (ioctl(fd, ZT_GET_PARAMS, &p)) {
  5844. ast_log(LOG_WARNING, "Failed to get channel parameters for channel %d: %s\n", channels[y], strerror(errno));
  5845. return -1;
  5846. }
  5847. if (ioctl(fd, ZT_SPANSTAT, &si)) {
  5848. ast_log(LOG_WARNING, "Failed go get span information on channel %d (span %d)\n", channels[y], p.spanno);
  5849. zt_close(fd);
  5850. return -1;
  5851. }
  5852. span = p.spanno - 1;
  5853. if (pris[span].trunkgroup) {
  5854. ast_log(LOG_WARNING, "Span %d is already provisioned for trunk group %d\n", span + 1, pris[span].trunkgroup);
  5855. zt_close(fd);
  5856. return -1;
  5857. }
  5858. if (pris[span].pvts[0]) {
  5859. ast_log(LOG_WARNING, "Span %d is already provisioned with channels (implicit PRI maybe?)\n", span + 1);
  5860. zt_close(fd);
  5861. return -1;
  5862. }
  5863. if (!y) {
  5864. pris[span].trunkgroup = trunkgroup;
  5865. pris[span].offset = channels[y] - p.chanpos;
  5866. ospan = span;
  5867. }
  5868. pris[ospan].dchannels[y] = channels[y];
  5869. pris[ospan].dchanavail[y] |= DCHAN_PROVISIONED;
  5870. pris[span].span = span + 1;
  5871. zt_close(fd);
  5872. }
  5873. return 0;
  5874. }
  5875. static int pri_create_spanmap(int span, int trunkgroup, int logicalspan)
  5876. {
  5877. if (pris[span].mastertrunkgroup) {
  5878. ast_log(LOG_WARNING, "Span %d is already part of trunk group %d, cannot add to trunk group %d\n", span + 1, pris[span].mastertrunkgroup, trunkgroup);
  5879. return -1;
  5880. }
  5881. pris[span].mastertrunkgroup = trunkgroup;
  5882. pris[span].prilogicalspan = logicalspan;
  5883. return 0;
  5884. }
  5885. #endif
  5886. static struct zt_pvt *mkintf(int channel, int signalling, int radio, struct zt_pri *pri)
  5887. {
  5888. /* Make a zt_pvt structure for this interface (or CRV if "pri" is specified) */
  5889. struct zt_pvt *tmp = NULL, *tmp2, *prev = NULL;
  5890. char fn[80];
  5891. #if 1
  5892. struct zt_bufferinfo bi;
  5893. #endif
  5894. struct zt_spaninfo si;
  5895. int res;
  5896. int span=0;
  5897. int here = 0;
  5898. int x;
  5899. struct zt_pvt **wlist;
  5900. struct zt_pvt **wend;
  5901. ZT_PARAMS p;
  5902. wlist = &iflist;
  5903. wend = &ifend;
  5904. #ifdef ZAPATA_PRI
  5905. if (pri) {
  5906. wlist = &pri->crvs;
  5907. wend = &pri->crvend;
  5908. }
  5909. #endif
  5910. tmp2 = *wlist;
  5911. prev = NULL;
  5912. while (tmp2) {
  5913. if (tmp2->channel == channel) {
  5914. tmp = tmp2;
  5915. here = 1;
  5916. break;
  5917. }
  5918. if (tmp2->channel > channel) {
  5919. break;
  5920. }
  5921. prev = tmp2;
  5922. tmp2 = tmp2->next;
  5923. }
  5924. if (!here) {
  5925. tmp = (struct zt_pvt*)malloc(sizeof(struct zt_pvt));
  5926. if (!tmp) {
  5927. ast_log(LOG_ERROR, "MALLOC FAILED\n");
  5928. destroy_zt_pvt(&tmp);
  5929. return NULL;
  5930. }
  5931. memset(tmp, 0, sizeof(struct zt_pvt));
  5932. ast_mutex_init(&tmp->lock);
  5933. ifcount++;
  5934. for (x=0;x<3;x++)
  5935. tmp->subs[x].zfd = -1;
  5936. tmp->channel = channel;
  5937. }
  5938. if (tmp) {
  5939. if ((channel != CHAN_PSEUDO) && !pri) {
  5940. snprintf(fn, sizeof(fn), "%d", channel);
  5941. /* Open non-blocking */
  5942. if (!here)
  5943. tmp->subs[SUB_REAL].zfd = zt_open(fn);
  5944. /* Allocate a zapata structure */
  5945. if (tmp->subs[SUB_REAL].zfd < 0) {
  5946. ast_log(LOG_ERROR, "Unable to open channel %d: %s\nhere = %d, tmp->channel = %d, channel = %d\n", channel, strerror(errno), here, tmp->channel, channel);
  5947. destroy_zt_pvt(&tmp);
  5948. return NULL;
  5949. }
  5950. memset(&p, 0, sizeof(p));
  5951. res = ioctl(tmp->subs[SUB_REAL].zfd, ZT_GET_PARAMS, &p);
  5952. if (res < 0) {
  5953. ast_log(LOG_ERROR, "Unable to get parameters\n");
  5954. destroy_zt_pvt(&tmp);
  5955. return NULL;
  5956. }
  5957. if (p.sigtype != (signalling & 0x3ffff)) {
  5958. ast_log(LOG_ERROR, "Signalling requested is %s but line is in %s signalling\n", sig2str(signalling), sig2str(p.sigtype));
  5959. destroy_zt_pvt(&tmp);
  5960. return tmp;
  5961. }
  5962. if (here) {
  5963. if (tmp->sig != signalling) {
  5964. if (reset_channel(tmp)) {
  5965. ast_log(LOG_ERROR, "Failed to reset chan_zap channel %d\n", tmp->channel);
  5966. return NULL;
  5967. }
  5968. }
  5969. }
  5970. tmp->law = p.curlaw;
  5971. tmp->span = p.spanno;
  5972. span = p.spanno - 1;
  5973. } else {
  5974. if (channel == CHAN_PSEUDO)
  5975. signalling = 0;
  5976. else if ((signalling != SIG_FXOKS) && (signalling != SIG_FXSKS)) {
  5977. ast_log(LOG_ERROR, "CRV's must use FXO/FXS Kewl Start (fxo_ks/fxs_ks) signalling only.\n");
  5978. return NULL;
  5979. }
  5980. }
  5981. #ifdef ZAPATA_PRI
  5982. if ((signalling == SIG_PRI) || (signalling == SIG_GR303FXOKS) || (signalling == SIG_GR303FXSKS)) {
  5983. int offset;
  5984. int myswitchtype;
  5985. int matchesdchan;
  5986. int x,y;
  5987. offset = 0;
  5988. if ((signalling == SIG_PRI) && ioctl(tmp->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &offset)) {
  5989. ast_log(LOG_ERROR, "Unable to set clear mode on clear channel %d of span %d: %s\n", channel, p.spanno, strerror(errno));
  5990. destroy_zt_pvt(&tmp);
  5991. return NULL;
  5992. }
  5993. if (span >= NUM_SPANS) {
  5994. ast_log(LOG_ERROR, "Channel %d does not lie on a span I know of (%d)\n", channel, span);
  5995. destroy_zt_pvt(&tmp);
  5996. return NULL;
  5997. } else {
  5998. si.spanno = 0;
  5999. if (ioctl(tmp->subs[SUB_REAL].zfd,ZT_SPANSTAT,&si) == -1) {
  6000. ast_log(LOG_ERROR, "Unable to get span status: %s\n", strerror(errno));
  6001. destroy_zt_pvt(&tmp);
  6002. return NULL;
  6003. }
  6004. /* Store the logical span first based upon the real span */
  6005. tmp->logicalspan = pris[span].prilogicalspan;
  6006. pri_resolve_span(&span, channel, (channel - p.chanpos), &si);
  6007. if (span < 0) {
  6008. ast_log(LOG_WARNING, "Channel %d: Unable to find locate channel/trunk group!\n", channel);
  6009. destroy_zt_pvt(&tmp);
  6010. return NULL;
  6011. }
  6012. if (signalling == SIG_PRI)
  6013. myswitchtype = switchtype;
  6014. else
  6015. myswitchtype = PRI_SWITCH_GR303_TMC;
  6016. /* Make sure this isn't a d-channel */
  6017. matchesdchan=0;
  6018. for (x=0;x<NUM_SPANS;x++) {
  6019. for (y=0;y<NUM_DCHANS;y++) {
  6020. if (pris[x].dchannels[y] == tmp->channel) {
  6021. matchesdchan = 1;
  6022. break;
  6023. }
  6024. }
  6025. }
  6026. offset = p.chanpos;
  6027. if (!matchesdchan) {
  6028. if (pris[span].nodetype && (pris[span].nodetype != pritype)) {
  6029. ast_log(LOG_ERROR, "Span %d is already a %s node\n", span + 1, pri_node2str(pris[span].nodetype));
  6030. destroy_zt_pvt(&tmp);
  6031. return NULL;
  6032. }
  6033. if (pris[span].switchtype && (pris[span].switchtype != myswitchtype)) {
  6034. ast_log(LOG_ERROR, "Span %d is already a %s switch\n", span + 1, pri_switch2str(pris[span].switchtype));
  6035. destroy_zt_pvt(&tmp);
  6036. return NULL;
  6037. }
  6038. if ((pris[span].dialplan) && (pris[span].dialplan != dialplan)) {
  6039. ast_log(LOG_ERROR, "Span %d is already a %s dialing plan\n", span + 1, pri_plan2str(pris[span].dialplan));
  6040. destroy_zt_pvt(&tmp);
  6041. return NULL;
  6042. }
  6043. if (!ast_strlen_zero(pris[span].idledial) && strcmp(pris[span].idledial, idledial)) {
  6044. ast_log(LOG_ERROR, "Span %d already has idledial '%s'.\n", span + 1, idledial);
  6045. destroy_zt_pvt(&tmp);
  6046. return NULL;
  6047. }
  6048. if (!ast_strlen_zero(pris[span].idleext) && strcmp(pris[span].idleext, idleext)) {
  6049. ast_log(LOG_ERROR, "Span %d already has idleext '%s'.\n", span + 1, idleext);
  6050. destroy_zt_pvt(&tmp);
  6051. return NULL;
  6052. }
  6053. if (pris[span].minunused && (pris[span].minunused != minunused)) {
  6054. ast_log(LOG_ERROR, "Span %d already has minunused of %d.\n", span + 1, minunused);
  6055. destroy_zt_pvt(&tmp);
  6056. return NULL;
  6057. }
  6058. if (pris[span].minidle && (pris[span].minidle != minidle)) {
  6059. ast_log(LOG_ERROR, "Span %d already has minidle of %d.\n", span + 1, minidle);
  6060. destroy_zt_pvt(&tmp);
  6061. return NULL;
  6062. }
  6063. if (pris[span].numchans >= MAX_CHANNELS) {
  6064. ast_log(LOG_ERROR, "Unable to add channel %d: Too many channels in trunk group %d!\n", channel,
  6065. pris[span].trunkgroup);
  6066. destroy_zt_pvt(&tmp);
  6067. return NULL;
  6068. }
  6069. pris[span].nodetype = pritype;
  6070. pris[span].switchtype = myswitchtype;
  6071. pris[span].nsf = nsf;
  6072. pris[span].dialplan = dialplan;
  6073. pris[span].localdialplan = localdialplan;
  6074. pris[span].pvts[pris[span].numchans++] = tmp;
  6075. pris[span].minunused = minunused;
  6076. pris[span].minidle = minidle;
  6077. pris[span].overlapdial = overlapdial;
  6078. strncpy(pris[span].idledial, idledial, sizeof(pris[span].idledial) - 1);
  6079. strncpy(pris[span].idleext, idleext, sizeof(pris[span].idleext) - 1);
  6080. tmp->pri = &pris[span];
  6081. tmp->prioffset = offset;
  6082. tmp->call = NULL;
  6083. } else {
  6084. ast_log(LOG_ERROR, "Channel %d is reserved for D-channel.\n", offset);
  6085. destroy_zt_pvt(&tmp);
  6086. return NULL;
  6087. }
  6088. }
  6089. } else {
  6090. tmp->prioffset = 0;
  6091. }
  6092. #endif
  6093. #ifdef ZAPATA_R2
  6094. if (signalling == SIG_R2) {
  6095. if (r2prot < 0) {
  6096. ast_log(LOG_WARNING, "R2 Country not specified for channel %d -- Assuming China\n", tmp->channel);
  6097. tmp->r2prot = MFCR2_PROT_CHINA;
  6098. } else
  6099. tmp->r2prot = r2prot;
  6100. tmp->r2 = mfcr2_new(tmp->subs[SUB_REAL].zfd, tmp->r2prot, 1);
  6101. if (!tmp->r2) {
  6102. ast_log(LOG_WARNING, "Unable to create r2 call :(\n");
  6103. zt_close(tmp->subs[SUB_REAL].zfd);
  6104. destroy_zt_pvt(&tmp);
  6105. return NULL;
  6106. }
  6107. } else {
  6108. if (tmp->r2)
  6109. mfcr2_free(tmp->r2);
  6110. tmp->r2 = NULL;
  6111. }
  6112. #endif
  6113. /* Adjust starttime on loopstart and kewlstart trunks to reasonable values */
  6114. if ((signalling == SIG_FXSKS) || (signalling == SIG_FXSLS) ||
  6115. (signalling == SIG_EM) || (signalling == SIG_EM_E1) || (signalling == SIG_EMWINK) ||
  6116. (signalling == SIG_FEATD) || (signalling == SIG_FEATDMF) ||
  6117. (signalling == SIG_FEATB) || (signalling == SIG_E911) ||
  6118. (signalling == SIG_SF) || (signalling == SIG_SFWINK) ||
  6119. (signalling == SIG_SF_FEATD) || (signalling == SIG_SF_FEATDMF) ||
  6120. (signalling == SIG_SF_FEATB)) {
  6121. p.starttime = 250;
  6122. } else if (radio) {
  6123. /* XXX Waiting to hear back from Jim if these should be adjustable XXX */
  6124. p.channo = channel;
  6125. p.rxwinktime = 1;
  6126. p.rxflashtime = 1;
  6127. p.starttime = 1;
  6128. p.debouncetime = 5;
  6129. }
  6130. if (!radio) {
  6131. p.channo = channel;
  6132. /* Override timing settings based on config file */
  6133. if (cur_prewink >= 0)
  6134. p.prewinktime = cur_prewink;
  6135. if (cur_preflash >= 0)
  6136. p.preflashtime = cur_preflash;
  6137. if (cur_wink >= 0)
  6138. p.winktime = cur_wink;
  6139. if (cur_flash >= 0)
  6140. p.flashtime = cur_flash;
  6141. if (cur_start >= 0)
  6142. p.starttime = cur_start;
  6143. if (cur_rxwink >= 0)
  6144. p.rxwinktime = cur_rxwink;
  6145. if (cur_rxflash >= 0)
  6146. p.rxflashtime = cur_rxflash;
  6147. if (cur_debounce >= 0)
  6148. p.debouncetime = cur_debounce;
  6149. }
  6150. /* dont set parms on a pseudo-channel (or CRV) */
  6151. if (tmp->subs[SUB_REAL].zfd >= 0)
  6152. {
  6153. res = ioctl(tmp->subs[SUB_REAL].zfd, ZT_SET_PARAMS, &p);
  6154. if (res < 0) {
  6155. ast_log(LOG_ERROR, "Unable to set parameters\n");
  6156. destroy_zt_pvt(&tmp);
  6157. return NULL;
  6158. }
  6159. }
  6160. #if 1
  6161. if (!here && (tmp->subs[SUB_REAL].zfd > -1)) {
  6162. memset(&bi, 0, sizeof(bi));
  6163. res = ioctl(tmp->subs[SUB_REAL].zfd, ZT_GET_BUFINFO, &bi);
  6164. if (!res) {
  6165. bi.txbufpolicy = ZT_POLICY_IMMEDIATE;
  6166. bi.rxbufpolicy = ZT_POLICY_IMMEDIATE;
  6167. bi.numbufs = numbufs;
  6168. res = ioctl(tmp->subs[SUB_REAL].zfd, ZT_SET_BUFINFO, &bi);
  6169. if (res < 0) {
  6170. ast_log(LOG_WARNING, "Unable to set buffer policy on channel %d\n", channel);
  6171. }
  6172. } else
  6173. ast_log(LOG_WARNING, "Unable to check buffer policy on channel %d\n", channel);
  6174. }
  6175. #endif
  6176. tmp->immediate = immediate;
  6177. tmp->sig = signalling;
  6178. tmp->radio = radio;
  6179. tmp->firstradio = 0;
  6180. if ((signalling == SIG_FXOKS) || (signalling == SIG_FXOLS) || (signalling == SIG_FXOGS))
  6181. tmp->permcallwaiting = callwaiting;
  6182. else
  6183. tmp->permcallwaiting = 0;
  6184. /* Flag to destroy the channel must be cleared on new mkif. Part of changes for reload to work */
  6185. tmp->destroy = 0;
  6186. tmp->drings = drings;
  6187. tmp->usedistinctiveringdetection = usedistinctiveringdetection;
  6188. tmp->callwaitingcallerid = callwaitingcallerid;
  6189. tmp->threewaycalling = threewaycalling;
  6190. tmp->adsi = adsi;
  6191. tmp->permhidecallerid = hidecallerid;
  6192. tmp->callreturn = callreturn;
  6193. tmp->echocancel = echocancel;
  6194. tmp->echotraining = echotraining;
  6195. tmp->pulse = pulse;
  6196. tmp->echocanbridged = echocanbridged;
  6197. tmp->busydetect = busydetect;
  6198. tmp->busycount = busycount;
  6199. tmp->callprogress = callprogress;
  6200. tmp->cancallforward = cancallforward;
  6201. tmp->dtmfrelax = relaxdtmf;
  6202. tmp->callwaiting = tmp->permcallwaiting;
  6203. tmp->hidecallerid = tmp->permhidecallerid;
  6204. tmp->channel = channel;
  6205. tmp->stripmsd = stripmsd;
  6206. tmp->use_callerid = use_callerid;
  6207. tmp->cid_signalling = cid_signalling;
  6208. tmp->cid_start = cid_start;
  6209. tmp->zaptrcallerid = zaptrcallerid;
  6210. tmp->restrictcid = restrictcid;
  6211. tmp->use_callingpres = use_callingpres;
  6212. tmp->priindication_oob = priindication_oob;
  6213. if (tmp->usedistinctiveringdetection) {
  6214. if (!tmp->use_callerid) {
  6215. ast_log(LOG_NOTICE, "Distinctive Ring detect requires 'usecallerid' be on\n");
  6216. tmp->use_callerid = 1;
  6217. }
  6218. }
  6219. strncpy(tmp->accountcode, accountcode, sizeof(tmp->accountcode)-1);
  6220. tmp->amaflags = amaflags;
  6221. if (!here) {
  6222. tmp->confno = -1;
  6223. tmp->propconfno = -1;
  6224. }
  6225. tmp->transfer = transfer;
  6226. strncpy(tmp->defcontext,context,sizeof(tmp->defcontext)-1);
  6227. strncpy(tmp->language, language, sizeof(tmp->language)-1);
  6228. strncpy(tmp->musicclass, musicclass, sizeof(tmp->musicclass)-1);
  6229. strncpy(tmp->context, context, sizeof(tmp->context)-1);
  6230. strncpy(tmp->callerid, callerid, sizeof(tmp->callerid)-1);
  6231. strncpy(tmp->mailbox, mailbox, sizeof(tmp->mailbox)-1);
  6232. tmp->msgstate = -1;
  6233. tmp->group = cur_group;
  6234. tmp->callgroup=cur_callergroup;
  6235. tmp->pickupgroup=cur_pickupgroup;
  6236. tmp->rxgain = rxgain;
  6237. tmp->txgain = txgain;
  6238. tmp->tonezone = tonezone;
  6239. tmp->onhooktime = time(NULL);
  6240. if (tmp->subs[SUB_REAL].zfd > -1) {
  6241. set_actual_gain(tmp->subs[SUB_REAL].zfd, 0, tmp->rxgain, tmp->txgain, tmp->law);
  6242. if (tmp->dsp)
  6243. ast_dsp_digitmode(tmp->dsp, DSP_DIGITMODE_DTMF | tmp->dtmfrelax);
  6244. update_conf(tmp);
  6245. if (!here) {
  6246. if ((signalling != SIG_PRI) && (signalling != SIG_R2))
  6247. /* Hang it up to be sure it's good */
  6248. zt_set_hook(tmp->subs[SUB_REAL].zfd, ZT_ONHOOK);
  6249. }
  6250. ioctl(tmp->subs[SUB_REAL].zfd,ZT_SETTONEZONE,&tmp->tonezone);
  6251. #ifdef ZAPATA_PRI
  6252. /* the dchannel is down so put the channel in alarm */
  6253. if (tmp->pri && !pri_is_up(tmp->pri))
  6254. tmp->inalarm = 1;
  6255. else
  6256. tmp->inalarm = 0;
  6257. #endif
  6258. memset(&si, 0, sizeof(si));
  6259. if (ioctl(tmp->subs[SUB_REAL].zfd,ZT_SPANSTAT,&si) == -1) {
  6260. ast_log(LOG_ERROR, "Unable to get span status: %s\n", strerror(errno));
  6261. destroy_zt_pvt(&tmp);
  6262. return NULL;
  6263. }
  6264. if (si.alarms) tmp->inalarm = 1;
  6265. }
  6266. tmp->sendcalleridafter = sendcalleridafter;
  6267. }
  6268. if (tmp && !here) {
  6269. /* nothing on the iflist */
  6270. if (!*wlist) {
  6271. *wlist = tmp;
  6272. tmp->prev = NULL;
  6273. tmp->next = NULL;
  6274. *wend = tmp;
  6275. } else {
  6276. /* at least one member on the iflist */
  6277. struct zt_pvt *working = *wlist;
  6278. /* check if we maybe have to put it on the begining */
  6279. if (working->channel > tmp->channel) {
  6280. tmp->next = *wlist;
  6281. tmp->prev = NULL;
  6282. (*wlist)->prev = tmp;
  6283. *wlist = tmp;
  6284. } else {
  6285. /* go through all the members and put the member in the right place */
  6286. while (working) {
  6287. /* in the middle */
  6288. if (working->next) {
  6289. if (working->channel < tmp->channel && working->next->channel > tmp->channel) {
  6290. tmp->next = working->next;
  6291. tmp->prev = working;
  6292. working->next->prev = tmp;
  6293. working->next = tmp;
  6294. break;
  6295. }
  6296. } else {
  6297. /* the last */
  6298. if (working->channel < tmp->channel) {
  6299. working->next = tmp;
  6300. tmp->next = NULL;
  6301. tmp->prev = working;
  6302. *wend = tmp;
  6303. break;
  6304. }
  6305. }
  6306. working = working->next;
  6307. }
  6308. }
  6309. }
  6310. }
  6311. return tmp;
  6312. }
  6313. static inline int available(struct zt_pvt *p, int channelmatch, int groupmatch, int *busy)
  6314. {
  6315. int res;
  6316. ZT_PARAMS par;
  6317. /* First, check group matching */
  6318. if ((p->group & groupmatch) != groupmatch)
  6319. return 0;
  6320. /* Check to see if we have a channel match */
  6321. if ((channelmatch > 0) && (p->channel != channelmatch))
  6322. return 0;
  6323. /* We're at least busy at this point */
  6324. if ((p->sig == SIG_FXOKS) || (p->sig == SIG_FXOLS) || (p->sig == SIG_FXOGS)) {
  6325. if (busy)
  6326. *busy = 1;
  6327. }
  6328. /* If do not distrub, definitely not */
  6329. if (p->dnd)
  6330. return 0;
  6331. /* If guard time, definitely not */
  6332. if (p->guardtime && (time(NULL) < p->guardtime))
  6333. return 0;
  6334. /* If no owner definitely available */
  6335. if (!p->owner) {
  6336. /* Trust PRI */
  6337. #ifdef ZAPATA_PRI
  6338. if (p->pri) {
  6339. if (p->resetting || p->call)
  6340. return 0;
  6341. else
  6342. return 1;
  6343. }
  6344. #endif
  6345. #ifdef ZAPATA_R2
  6346. /* Trust R2 as well */
  6347. if (p->r2) {
  6348. if (p->hasr2call || p->r2blocked)
  6349. return 0;
  6350. else
  6351. return 1;
  6352. }
  6353. #endif
  6354. if (!p->radio)
  6355. {
  6356. if (!p->sig || (p->sig == SIG_FXSLS))
  6357. return 1;
  6358. /* Check hook state */
  6359. if (p->subs[SUB_REAL].zfd > -1)
  6360. res = ioctl(p->subs[SUB_REAL].zfd, ZT_GET_PARAMS, &par);
  6361. else {
  6362. /* Assume not off hook on CVRS */
  6363. res = 0;
  6364. par.rxisoffhook = 0;
  6365. }
  6366. if (res) {
  6367. ast_log(LOG_WARNING, "Unable to check hook state on channel %d\n", p->channel);
  6368. } else if ((p->sig == SIG_FXSKS) || (p->sig == SIG_FXSGS)) {
  6369. /* When "onhook" that means no battery on the line, and thus
  6370. it is out of service..., if it's on a TDM card... If it's a channel
  6371. bank, there is no telling... */
  6372. if (par.rxbits > -1)
  6373. return 1;
  6374. if (par.rxisoffhook)
  6375. return 1;
  6376. else
  6377. #ifdef ZAP_CHECK_HOOKSTATE
  6378. return 0;
  6379. #else
  6380. return 1;
  6381. #endif
  6382. } else if (par.rxisoffhook) {
  6383. ast_log(LOG_DEBUG, "Channel %d off hook, can't use\n", p->channel);
  6384. /* Not available when the other end is off hook */
  6385. return 0;
  6386. }
  6387. }
  6388. return 1;
  6389. }
  6390. /* If it's not an FXO, forget about call wait */
  6391. if ((p->sig != SIG_FXOKS) && (p->sig != SIG_FXOLS) && (p->sig != SIG_FXOGS))
  6392. return 0;
  6393. if (!p->callwaiting) {
  6394. /* If they don't have call waiting enabled, then for sure they're unavailable at this point */
  6395. return 0;
  6396. }
  6397. if (p->subs[SUB_CALLWAIT].zfd > -1) {
  6398. /* If there is already a call waiting call, then we can't take a second one */
  6399. return 0;
  6400. }
  6401. if ((p->owner->_state != AST_STATE_UP) &&
  6402. ((p->owner->_state != AST_STATE_RINGING) || p->outgoing)) {
  6403. /* If the current call is not up, then don't allow the call */
  6404. return 0;
  6405. }
  6406. if ((p->subs[SUB_THREEWAY].owner) && (!p->subs[SUB_THREEWAY].inthreeway)) {
  6407. /* Can't take a call wait when the three way calling hasn't been merged yet. */
  6408. return 0;
  6409. }
  6410. /* We're cool */
  6411. return 1;
  6412. }
  6413. static struct zt_pvt *chandup(struct zt_pvt *src)
  6414. {
  6415. struct zt_pvt *p;
  6416. ZT_BUFFERINFO bi;
  6417. int res;
  6418. p = malloc(sizeof(struct zt_pvt));
  6419. if (p) {
  6420. memcpy(p, src, sizeof(struct zt_pvt));
  6421. ast_mutex_init(&p->lock);
  6422. p->subs[SUB_REAL].zfd = zt_open("/dev/zap/pseudo");
  6423. /* Allocate a zapata structure */
  6424. if (p->subs[SUB_REAL].zfd < 0) {
  6425. ast_log(LOG_ERROR, "Unable to dup channel: %s\n", strerror(errno));
  6426. destroy_zt_pvt(&p);
  6427. return NULL;
  6428. }
  6429. res = ioctl(p->subs[SUB_REAL].zfd, ZT_GET_BUFINFO, &bi);
  6430. if (!res) {
  6431. bi.txbufpolicy = ZT_POLICY_IMMEDIATE;
  6432. bi.rxbufpolicy = ZT_POLICY_IMMEDIATE;
  6433. bi.numbufs = numbufs;
  6434. res = ioctl(p->subs[SUB_REAL].zfd, ZT_SET_BUFINFO, &bi);
  6435. if (res < 0) {
  6436. ast_log(LOG_WARNING, "Unable to set buffer policy on dup channel\n");
  6437. }
  6438. } else
  6439. ast_log(LOG_WARNING, "Unable to check buffer policy on dup channel\n");
  6440. }
  6441. p->destroy = 1;
  6442. p->next = iflist;
  6443. iflist = p;
  6444. return p;
  6445. }
  6446. #ifdef ZAPATA_PRI
  6447. static int pri_find_empty_chan(struct zt_pri *pri, int backwards)
  6448. {
  6449. int x;
  6450. if (backwards)
  6451. x = pri->numchans;
  6452. else
  6453. x = 0;
  6454. for (;;) {
  6455. if (backwards && (x < 0))
  6456. break;
  6457. if (!backwards && (x >= pri->numchans))
  6458. break;
  6459. if (pri->pvts[x] && !pri->pvts[x]->inalarm && !pri->pvts[x]->owner) {
  6460. ast_log(LOG_DEBUG, "Found empty available channel %d/%d\n",
  6461. pri->pvts[x]->logicalspan, pri->pvts[x]->prioffset);
  6462. return x;
  6463. }
  6464. if (backwards)
  6465. x--;
  6466. else
  6467. x++;
  6468. }
  6469. return -1;
  6470. }
  6471. #endif
  6472. static struct ast_channel *zt_request(char *type, int format, void *data)
  6473. {
  6474. int oldformat;
  6475. int groupmatch = 0;
  6476. int channelmatch = -1;
  6477. int roundrobin = 0;
  6478. int callwait = 0;
  6479. int busy = 0;
  6480. struct zt_pvt *p;
  6481. struct ast_channel *tmp = NULL;
  6482. char *dest=NULL;
  6483. int x;
  6484. char *s;
  6485. char opt=0;
  6486. int res=0, y=0;
  6487. int backwards = 0;
  6488. #ifdef ZAPATA_PRI
  6489. int crv;
  6490. int bearer = -1;
  6491. int trunkgroup;
  6492. struct zt_pri *pri=NULL;
  6493. #endif
  6494. struct zt_pvt *exit, *start, *end;
  6495. ast_mutex_t *lock;
  6496. /* Assume we're locking the iflock */
  6497. lock = &iflock;
  6498. start = iflist;
  6499. end = ifend;
  6500. /* We do signed linear */
  6501. oldformat = format;
  6502. format &= (AST_FORMAT_SLINEAR | AST_FORMAT_ULAW);
  6503. if (!format) {
  6504. ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%d'\n", oldformat);
  6505. return NULL;
  6506. }
  6507. if (data) {
  6508. dest = ast_strdupa((char *)data);
  6509. } else {
  6510. ast_log(LOG_WARNING, "Channel requested with no data\n");
  6511. return NULL;
  6512. }
  6513. if (toupper(dest[0]) == 'G' || toupper(dest[0])=='R') {
  6514. /* Retrieve the group number */
  6515. char *stringp=NULL;
  6516. stringp=dest + 1;
  6517. s = strsep(&stringp, "/");
  6518. if ((res = sscanf(s, "%d%c%d", &x, &opt, &y)) < 1) {
  6519. ast_log(LOG_WARNING, "Unable to determine group for data %s\n", (char *)data);
  6520. return NULL;
  6521. }
  6522. groupmatch = 1 << x;
  6523. if (toupper(dest[0]) == 'G') {
  6524. if (dest[0] == 'G') {
  6525. backwards = 1;
  6526. p = ifend;
  6527. } else
  6528. p = iflist;
  6529. } else {
  6530. if (dest[0] == 'R') {
  6531. backwards = 1;
  6532. p = round_robin[x]?round_robin[x]->prev:ifend;
  6533. if (!p)
  6534. p = ifend;
  6535. } else {
  6536. p = round_robin[x]?round_robin[x]->next:iflist;
  6537. if (!p)
  6538. p = iflist;
  6539. }
  6540. roundrobin = 1;
  6541. }
  6542. } else {
  6543. char *stringp=NULL;
  6544. stringp=dest;
  6545. s = strsep(&stringp, "/");
  6546. p = iflist;
  6547. if (!strcasecmp(s, "pseudo")) {
  6548. /* Special case for pseudo */
  6549. x = CHAN_PSEUDO;
  6550. channelmatch = x;
  6551. }
  6552. #ifdef ZAPATA_PRI
  6553. else if ((res = sscanf(s, "%d:%d%c%d", &trunkgroup, &crv, &opt, &y)) > 1) {
  6554. if ((trunkgroup < 1) || (crv < 1)) {
  6555. ast_log(LOG_WARNING, "Unable to determine trunk group and CRV for data %s\n", (char *)data);
  6556. return NULL;
  6557. }
  6558. res--;
  6559. for (x=0;x<NUM_SPANS;x++) {
  6560. if (pris[x].trunkgroup == trunkgroup) {
  6561. pri = pris + x;
  6562. lock = &pri->lock;
  6563. start = pri->crvs;
  6564. end = pri->crvend;
  6565. break;
  6566. }
  6567. }
  6568. if (!pri) {
  6569. ast_log(LOG_WARNING, "Unable to find trunk group %d\n", trunkgroup);
  6570. return NULL;
  6571. }
  6572. channelmatch = crv;
  6573. p = pris[x].crvs;
  6574. }
  6575. #endif
  6576. else if ((res = sscanf(s, "%d%c%d", &x, &opt, &y)) < 1) {
  6577. ast_log(LOG_WARNING, "Unable to determine channel for data %s\n", (char *)data);
  6578. return NULL;
  6579. } else {
  6580. channelmatch = x;
  6581. }
  6582. }
  6583. /* Search for an unowned channel */
  6584. if (ast_mutex_lock(lock)) {
  6585. ast_log(LOG_ERROR, "Unable to lock interface list???\n");
  6586. return NULL;
  6587. }
  6588. exit = p;
  6589. while(p && !tmp) {
  6590. if (roundrobin)
  6591. round_robin[x] = p;
  6592. #if 0
  6593. ast_verbose("name = %s, %d, %d, %d\n",p->owner ? p->owner->name : "<none>", p->channel, channelmatch, groupmatch);
  6594. #endif
  6595. if (p && available(p, channelmatch, groupmatch, &busy)) {
  6596. if (option_debug)
  6597. ast_log(LOG_DEBUG, "Using channel %d\n", p->channel);
  6598. if (p->inalarm)
  6599. goto next;
  6600. callwait = (p->owner != NULL);
  6601. #ifdef ZAPATA_PRI
  6602. if (pri && (p->subs[SUB_REAL].zfd < 0)) {
  6603. if (p->sig != SIG_FXSKS) {
  6604. /* Gotta find an actual channel to use for this
  6605. CRV if this isn't a callwait */
  6606. bearer = pri_find_empty_chan(pri, 0);
  6607. if (bearer < 0) {
  6608. ast_log(LOG_NOTICE, "Out of bearer channels on span %d for call to CRV %d:%d\n", pri->span, trunkgroup, crv);
  6609. p = NULL;
  6610. break;
  6611. }
  6612. pri_assign_bearer(p, pri, pri->pvts[bearer]);
  6613. } else {
  6614. if (alloc_sub(p, 0)) {
  6615. ast_log(LOG_NOTICE, "Failed to allocate place holder pseudo channel!\n");
  6616. p = NULL;
  6617. break;
  6618. } else
  6619. ast_log(LOG_DEBUG, "Allocated placeholder pseudo channel\n");
  6620. p->pri = pri;
  6621. }
  6622. }
  6623. #endif
  6624. if (p->channel == CHAN_PSEUDO) {
  6625. p = chandup(p);
  6626. if (!p) {
  6627. break;
  6628. }
  6629. }
  6630. if (p->owner) {
  6631. if (alloc_sub(p, SUB_CALLWAIT)) {
  6632. p = NULL;
  6633. break;
  6634. }
  6635. }
  6636. p->outgoing = 1;
  6637. tmp = zt_new(p, AST_STATE_RESERVED, 0, p->owner ? SUB_CALLWAIT : SUB_REAL, 0, 0);
  6638. #ifdef ZAPATA_PRI
  6639. if (p->bearer) {
  6640. /* Log owner to bearer channel, too */
  6641. p->bearer->owner = tmp;
  6642. }
  6643. #endif
  6644. /* Make special notes */
  6645. if (res > 1) {
  6646. if (opt == 'c') {
  6647. /* Confirm answer */
  6648. p->confirmanswer = 1;
  6649. } else if (opt == 'r') {
  6650. /* Distinctive ring */
  6651. if (res < 3)
  6652. ast_log(LOG_WARNING, "Distinctive ring missing identifier in '%s'\n", (char *)data);
  6653. else
  6654. p->distinctivering = y;
  6655. } else if (opt == 'd') {
  6656. /* If this is an ISDN call, make it digital */
  6657. p->digital = 1;
  6658. if (tmp)
  6659. ast_set_flag(tmp, AST_FLAG_DIGITAL);
  6660. } else {
  6661. ast_log(LOG_WARNING, "Unknown option '%c' in '%s'\n", opt, (char *)data);
  6662. }
  6663. }
  6664. /* Note if the call is a call waiting call */
  6665. if (tmp && callwait)
  6666. tmp->cdrflags |= AST_CDR_CALLWAIT;
  6667. break;
  6668. }
  6669. next:
  6670. if (backwards) {
  6671. p = p->prev;
  6672. if (!p)
  6673. p = end;
  6674. } else {
  6675. p = p->next;
  6676. if (!p)
  6677. p = start;
  6678. }
  6679. /* stop when you roll to the one that we started from */
  6680. if (p == exit)
  6681. break;
  6682. }
  6683. ast_mutex_unlock(lock);
  6684. restart_monitor();
  6685. if (!tmp) {
  6686. if (busy && (channelmatch != CHAN_PSEUDO)) {
  6687. tmp = zt_request("Zap", format, "pseudo");
  6688. if (tmp) {
  6689. char newname[80];
  6690. ast_mutex_lock(&tmp->lock);
  6691. snprintf(newname, sizeof(newname), "Zap/%s-busy-%d", (char *)data, rand());
  6692. ast_change_name(tmp, newname);
  6693. ast_setstate(tmp, AST_STATE_BUSY);
  6694. ast_mutex_unlock(&tmp->lock);
  6695. }
  6696. } else if (busy) {
  6697. ast_log(LOG_WARNING, "Whoa, the pseudo was busy somehow!\n");
  6698. }
  6699. }
  6700. return tmp;
  6701. }
  6702. #ifdef ZAPATA_PRI
  6703. static struct zt_pvt *pri_find_crv(struct zt_pri *pri, int crv)
  6704. {
  6705. struct zt_pvt *p;
  6706. p = pri->crvs;
  6707. while(p) {
  6708. if (p->channel == crv)
  6709. return p;
  6710. p = p->next;
  6711. }
  6712. return NULL;
  6713. }
  6714. static int pri_find_principle(struct zt_pri *pri, int channel)
  6715. {
  6716. int x;
  6717. int span;
  6718. int principle = -1;
  6719. span = PRI_SPAN(channel);
  6720. channel = PRI_CHANNEL(channel);
  6721. for (x=0;x<pri->numchans;x++) {
  6722. if (pri->pvts[x] && (pri->pvts[x]->prioffset == channel) && (pri->pvts[x]->logicalspan == span)) {
  6723. principle = x;
  6724. break;
  6725. }
  6726. }
  6727. return principle;
  6728. }
  6729. static int pri_fixup_principle(struct zt_pri *pri, int principle, q931_call *c)
  6730. {
  6731. int x;
  6732. struct zt_pvt *crv;
  6733. if (!c) {
  6734. if (principle < 0)
  6735. return -1;
  6736. return principle;
  6737. }
  6738. if ((principle > -1) &&
  6739. (principle < pri->numchans) &&
  6740. (pri->pvts[principle]) &&
  6741. (pri->pvts[principle]->call == c))
  6742. return principle;
  6743. /* First, check for other bearers */
  6744. for (x=0;x<pri->numchans;x++) {
  6745. if (!pri->pvts[x]) continue;
  6746. if (pri->pvts[x]->call == c) {
  6747. /* Found our call */
  6748. if (principle != x) {
  6749. if (option_verbose > 2)
  6750. ast_verbose(VERBOSE_PREFIX_3 "Moving call from channel %d to channel %d\n",
  6751. pri->pvts[x]->channel, pri->pvts[principle]->channel);
  6752. if (pri->pvts[principle]->owner) {
  6753. ast_log(LOG_WARNING, "Can't fix up channel from %d to %d because %d is already in use\n",
  6754. pri->pvts[x]->channel, pri->pvts[principle]->channel, pri->pvts[principle]->channel);
  6755. return -1;
  6756. }
  6757. /* Fix it all up now */
  6758. pri->pvts[principle]->owner = pri->pvts[x]->owner;
  6759. if (pri->pvts[principle]->owner) {
  6760. pri->pvts[principle]->owner->pvt->pvt = pri->pvts[principle];
  6761. pri->pvts[principle]->owner->fds[0] = pri->pvts[principle]->subs[SUB_REAL].zfd;
  6762. pri->pvts[principle]->subs[SUB_REAL].owner = pri->pvts[x]->subs[SUB_REAL].owner;
  6763. } else
  6764. ast_log(LOG_WARNING, "Whoa, there's no owner, and we're having to fix up channel %d to channel %d\n", pri->pvts[x]->channel, pri->pvts[principle]->channel);
  6765. pri->pvts[principle]->call = pri->pvts[x]->call;
  6766. /* Free up the old channel, now not in use */
  6767. pri->pvts[x]->subs[SUB_REAL].owner = NULL;
  6768. pri->pvts[x]->owner = NULL;
  6769. pri->pvts[x]->call = NULL;
  6770. }
  6771. return principle;
  6772. }
  6773. }
  6774. /* Now check for a CRV with no bearer */
  6775. crv = pri->crvs;
  6776. while(crv) {
  6777. if (crv->call == c) {
  6778. /* This is our match... Perform some basic checks */
  6779. if (crv->bearer)
  6780. ast_log(LOG_WARNING, "Trying to fix up call which already has a bearer which isn't the one we think it is\n");
  6781. else if (pri->pvts[principle]->owner)
  6782. ast_log(LOG_WARNING, "Tring to fix up a call to a bearer which already has an owner!\n");
  6783. else {
  6784. /* Looks good. Drop the pseudo channel now, clear up the assignment, and
  6785. wakeup the potential sleeper */
  6786. zt_close(crv->subs[SUB_REAL].zfd);
  6787. pri->pvts[principle]->call = crv->call;
  6788. pri_assign_bearer(crv, pri, pri->pvts[principle]);
  6789. ast_log(LOG_DEBUG, "Assigning bearer %d/%d to CRV %d:%d\n",
  6790. pri->pvts[principle]->logicalspan, pri->pvts[principle]->prioffset,
  6791. pri->trunkgroup, crv->channel);
  6792. wakeup_sub(crv, SUB_REAL, pri);
  6793. }
  6794. return principle;
  6795. }
  6796. crv = crv->next;
  6797. }
  6798. ast_log(LOG_WARNING, "Call specified, but not found?\n");
  6799. return -1;
  6800. }
  6801. static void *do_idle_thread(void *vchan)
  6802. {
  6803. struct ast_channel *chan = vchan;
  6804. struct zt_pvt *pvt = chan->pvt->pvt;
  6805. struct ast_frame *f;
  6806. char ex[80];
  6807. /* Wait up to 30 seconds for an answer */
  6808. int newms, ms = 30000;
  6809. if (option_verbose > 2)
  6810. ast_verbose(VERBOSE_PREFIX_3 "Initiating idle call on channel %s\n", chan->name);
  6811. snprintf(ex, sizeof(ex), "%d/%s", pvt->channel, pvt->pri->idledial);
  6812. if (ast_call(chan, ex, 0)) {
  6813. ast_log(LOG_WARNING, "Idle dial failed on '%s' to '%s'\n", chan->name, ex);
  6814. ast_hangup(chan);
  6815. return NULL;
  6816. }
  6817. while((newms = ast_waitfor(chan, ms)) > 0) {
  6818. f = ast_read(chan);
  6819. if (!f) {
  6820. /* Got hangup */
  6821. break;
  6822. }
  6823. if (f->frametype == AST_FRAME_CONTROL) {
  6824. switch(f->subclass) {
  6825. case AST_CONTROL_ANSWER:
  6826. /* Launch the PBX */
  6827. strncpy(chan->exten, pvt->pri->idleext, sizeof(chan->exten) - 1);
  6828. strncpy(chan->context, pvt->pri->idlecontext, sizeof(chan->context) - 1);
  6829. chan->priority = 1;
  6830. if (option_verbose > 3)
  6831. ast_verbose(VERBOSE_PREFIX_3 "Idle channel '%s' answered, sending to %s@%s\n", chan->name, chan->exten, chan->context);
  6832. ast_pbx_run(chan);
  6833. /* It's already hungup, return immediately */
  6834. return NULL;
  6835. case AST_CONTROL_BUSY:
  6836. if (option_verbose > 3)
  6837. ast_verbose(VERBOSE_PREFIX_3 "Idle channel '%s' busy, waiting...\n", chan->name);
  6838. break;
  6839. case AST_CONTROL_CONGESTION:
  6840. if (option_verbose > 3)
  6841. ast_verbose(VERBOSE_PREFIX_3 "Idle channel '%s' congested, waiting...\n", chan->name);
  6842. break;
  6843. };
  6844. }
  6845. ast_frfree(f);
  6846. ms = newms;
  6847. }
  6848. #if 0
  6849. printf("Hanging up '%s'\n", chan->name);
  6850. #endif
  6851. /* Hangup the channel since nothing happend */
  6852. ast_hangup(chan);
  6853. return NULL;
  6854. }
  6855. static void zt_pri_message(char *s)
  6856. {
  6857. ast_verbose("%s", s);
  6858. }
  6859. static void zt_pri_error(char *s)
  6860. {
  6861. ast_log(LOG_WARNING, "PRI: %s", s);
  6862. }
  6863. static int pri_check_restart(struct zt_pri *pri)
  6864. {
  6865. do {
  6866. pri->resetpos++;
  6867. } while((pri->resetpos < pri->numchans) &&
  6868. (!pri->pvts[pri->resetpos] ||
  6869. pri->pvts[pri->resetpos]->call ||
  6870. pri->pvts[pri->resetpos]->resetting));
  6871. if (pri->resetpos < pri->numchans) {
  6872. /* Mark the channel as resetting and restart it */
  6873. pri->pvts[pri->resetpos]->resetting = 1;
  6874. pri_reset(pri->pri, PVT_TO_CHANNEL(pri->pvts[pri->resetpos]));
  6875. } else {
  6876. pri->resetting = 0;
  6877. time(&pri->lastreset);
  6878. }
  6879. return 0;
  6880. }
  6881. static int pri_hangup_all(struct zt_pvt *p, struct zt_pri *pri)
  6882. {
  6883. int x;
  6884. int redo;
  6885. ast_mutex_unlock(&pri->lock);
  6886. ast_mutex_lock(&p->lock);
  6887. do {
  6888. redo = 0;
  6889. for (x=0;x<3;x++) {
  6890. while(p->subs[x].owner && ast_mutex_trylock(&p->subs[x].owner->lock)) {
  6891. redo++;
  6892. ast_mutex_unlock(&p->lock);
  6893. usleep(1);
  6894. ast_mutex_lock(&p->lock);
  6895. }
  6896. if (p->subs[x].owner) {
  6897. ast_queue_hangup(p->subs[x].owner);
  6898. ast_mutex_unlock(&p->subs[x].owner->lock);
  6899. }
  6900. }
  6901. } while (redo);
  6902. ast_mutex_unlock(&p->lock);
  6903. ast_mutex_lock(&pri->lock);
  6904. return 0;
  6905. }
  6906. static void *pri_dchannel(void *vpri)
  6907. {
  6908. struct zt_pri *pri = vpri;
  6909. pri_event *e;
  6910. struct pollfd fds[NUM_DCHANS];
  6911. int res;
  6912. int chanpos = 0;
  6913. int x;
  6914. int haveidles;
  6915. int activeidles;
  6916. int nextidle = -1;
  6917. struct ast_channel *c;
  6918. struct timeval tv, lowest, *next;
  6919. struct timeval lastidle = { 0, 0 };
  6920. int doidling=0;
  6921. char *cc;
  6922. char idlen[80];
  6923. struct ast_channel *idle;
  6924. pthread_t p;
  6925. time_t t;
  6926. int i, which=-1;
  6927. int numdchans;
  6928. struct zt_pvt *crv;
  6929. pthread_t threadid;
  6930. pthread_attr_t attr;
  6931. pthread_attr_init(&attr);
  6932. pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  6933. gettimeofday(&lastidle, NULL);
  6934. if (!ast_strlen_zero(pri->idledial) && !ast_strlen_zero(pri->idleext)) {
  6935. /* Need to do idle dialing, check to be sure though */
  6936. cc = strchr(pri->idleext, '@');
  6937. if (cc) {
  6938. *cc = '\0';
  6939. cc++;
  6940. strncpy(pri->idlecontext, cc, sizeof(pri->idlecontext) - 1);
  6941. #if 0
  6942. /* Extensions may not be loaded yet */
  6943. if (!ast_exists_extension(NULL, pri->idlecontext, pri->idleext, 1, NULL))
  6944. ast_log(LOG_WARNING, "Extension '%s @ %s' does not exist\n", pri->idleext, pri->idlecontext);
  6945. else
  6946. #endif
  6947. doidling = 1;
  6948. } else
  6949. ast_log(LOG_WARNING, "Idle dial string '%s' lacks '@context'\n", pri->idleext);
  6950. }
  6951. for(;;) {
  6952. for (i=0;i<NUM_DCHANS;i++) {
  6953. if (!pri->dchannels[i])
  6954. break;
  6955. fds[i].fd = pri->fds[i];
  6956. fds[i].events = POLLIN | POLLPRI;
  6957. }
  6958. numdchans = i;
  6959. time(&t);
  6960. ast_mutex_lock(&pri->lock);
  6961. if (pri->switchtype != PRI_SWITCH_GR303_TMC) {
  6962. if (pri->resetting && pri_is_up(pri)) {
  6963. if (pri->resetpos < 0)
  6964. pri_check_restart(pri);
  6965. } else {
  6966. if (!pri->resetting && ((t - pri->lastreset) >= RESET_INTERVAL)) {
  6967. pri->resetting = 1;
  6968. pri->resetpos = -1;
  6969. }
  6970. }
  6971. }
  6972. /* Look for any idle channels if appropriate */
  6973. if (doidling && pri_is_up(pri)) {
  6974. nextidle = -1;
  6975. haveidles = 0;
  6976. activeidles = 0;
  6977. for (x=pri->numchans;x>=0;x--) {
  6978. if (pri->pvts[x] && !pri->pvts[x]->owner &&
  6979. !pri->pvts[x]->call) {
  6980. if (haveidles < pri->minunused) {
  6981. haveidles++;
  6982. } else if (!pri->pvts[x]->resetting) {
  6983. nextidle = x;
  6984. break;
  6985. }
  6986. } else if (pri->pvts[x] && pri->pvts[x]->owner && pri->pvts[x]->isidlecall)
  6987. activeidles++;
  6988. }
  6989. #if 0
  6990. printf("nextidle: %d, haveidles: %d, minunsed: %d\n",
  6991. nextidle, haveidles, minunused);
  6992. gettimeofday(&tv, NULL);
  6993. printf("nextidle: %d, haveidles: %d, ms: %ld, minunsed: %d\n",
  6994. nextidle, haveidles, (tv.tv_sec - lastidle.tv_sec) * 1000 +
  6995. (tv.tv_usec - lastidle.tv_usec) / 1000, minunused);
  6996. #endif
  6997. if (nextidle > -1) {
  6998. gettimeofday(&tv, NULL);
  6999. if (((tv.tv_sec - lastidle.tv_sec) * 1000 +
  7000. (tv.tv_usec - lastidle.tv_usec) / 1000) > 1000) {
  7001. /* Don't create a new idle call more than once per second */
  7002. snprintf(idlen, sizeof(idlen), "%d/%s", pri->pvts[nextidle]->channel, pri->idledial);
  7003. idle = zt_request("Zap", AST_FORMAT_ULAW, idlen);
  7004. if (idle) {
  7005. pri->pvts[nextidle]->isidlecall = 1;
  7006. if (ast_pthread_create(&p, NULL, do_idle_thread, idle)) {
  7007. ast_log(LOG_WARNING, "Unable to start new thread for idle channel '%s'\n", idle->name);
  7008. zt_hangup(idle);
  7009. }
  7010. } else
  7011. ast_log(LOG_WARNING, "Unable to request channel 'Zap/%s' for idle call\n", idlen);
  7012. gettimeofday(&lastidle, NULL);
  7013. }
  7014. } else if ((haveidles < pri->minunused) &&
  7015. (activeidles > pri->minidle)) {
  7016. /* Mark something for hangup if there is something
  7017. that can be hungup */
  7018. for (x=pri->numchans;x>=0;x--) {
  7019. /* find a candidate channel */
  7020. if (pri->pvts[x] && pri->pvts[x]->owner && pri->pvts[x]->isidlecall) {
  7021. pri->pvts[x]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
  7022. haveidles++;
  7023. /* Stop if we have enough idle channels or
  7024. can't spare any more active idle ones */
  7025. if ((haveidles >= pri->minunused) ||
  7026. (activeidles <= pri->minidle))
  7027. break;
  7028. }
  7029. }
  7030. }
  7031. }
  7032. /* Start with reasonable max */
  7033. lowest.tv_sec = 60;
  7034. lowest.tv_usec = 0;
  7035. for (i=0; i<NUM_DCHANS; i++) {
  7036. /* Find lowest available d-channel */
  7037. if (!pri->dchannels[i])
  7038. break;
  7039. if ((next = pri_schedule_next(pri->dchans[i]))) {
  7040. /* We need relative time here */
  7041. gettimeofday(&tv, NULL);
  7042. tv.tv_sec = next->tv_sec - tv.tv_sec;
  7043. tv.tv_usec = next->tv_usec - tv.tv_usec;
  7044. if (tv.tv_usec < 0) {
  7045. tv.tv_usec += 1000000;
  7046. tv.tv_sec -= 1;
  7047. }
  7048. if (tv.tv_sec < 0) {
  7049. tv.tv_sec = 0;
  7050. tv.tv_usec = 0;
  7051. }
  7052. if (doidling || pri->resetting) {
  7053. if (tv.tv_sec > 1) {
  7054. tv.tv_sec = 1;
  7055. tv.tv_usec = 0;
  7056. }
  7057. } else {
  7058. if (tv.tv_sec > 60) {
  7059. tv.tv_sec = 60;
  7060. tv.tv_usec = 0;
  7061. }
  7062. }
  7063. } else if (doidling || pri->resetting) {
  7064. /* Make sure we stop at least once per second if we're
  7065. monitoring idle channels */
  7066. tv.tv_sec = 1;
  7067. tv.tv_usec = 0;
  7068. } else {
  7069. /* Don't poll for more than 60 seconds */
  7070. tv.tv_sec = 60;
  7071. tv.tv_usec = 0;
  7072. }
  7073. if (!i || (tv.tv_sec < lowest.tv_sec) || ((tv.tv_sec == lowest.tv_sec) && (tv.tv_usec < lowest.tv_usec))) {
  7074. lowest.tv_sec = tv.tv_sec;
  7075. lowest.tv_usec = tv.tv_usec;
  7076. }
  7077. }
  7078. ast_mutex_unlock(&pri->lock);
  7079. e = NULL;
  7080. res = poll(fds, numdchans, lowest.tv_sec * 1000 + lowest.tv_usec / 1000);
  7081. ast_mutex_lock(&pri->lock);
  7082. if (!res) {
  7083. for (which=0;which<NUM_DCHANS;which++) {
  7084. if (!pri->dchans[which])
  7085. break;
  7086. /* Just a timeout, run the scheduler */
  7087. e = pri_schedule_run(pri->dchans[which]);
  7088. if (e)
  7089. break;
  7090. }
  7091. } else if (res > -1) {
  7092. for (which=0;which<NUM_DCHANS;which++) {
  7093. if (!pri->dchans[which])
  7094. break;
  7095. if (fds[which].revents & POLLPRI) {
  7096. /* Check for an event */
  7097. x = 0;
  7098. res = ioctl(pri->fds[which], ZT_GETEVENT, &x);
  7099. if (x)
  7100. ast_log(LOG_NOTICE, "PRI got event: %s (%d) on %s D-channel of span %d\n", event2str(x), x, pri_order(which), pri->span);
  7101. /* Keep track of alarm state */
  7102. if (x == ZT_EVENT_ALARM) {
  7103. pri->dchanavail[which] &= ~(DCHAN_NOTINALARM | DCHAN_UP);
  7104. pri_find_dchan(pri);
  7105. } else if (x == ZT_EVENT_NOALARM) {
  7106. pri->dchanavail[which] |= DCHAN_NOTINALARM;
  7107. pri_find_dchan(pri);
  7108. }
  7109. if (option_debug)
  7110. ast_log(LOG_DEBUG, "Got event %s (%d) on D-channel for span %d\n", event2str(x), x, pri->span);
  7111. } else if (fds[which].revents & POLLIN) {
  7112. e = pri_check_event(pri->dchans[which]);
  7113. }
  7114. if (e)
  7115. break;
  7116. }
  7117. } else if (errno != EINTR)
  7118. ast_log(LOG_WARNING, "pri_event returned error %d (%s)\n", errno, strerror(errno));
  7119. if (e) {
  7120. if (pri->debug)
  7121. pri_dump_event(pri->dchans[which], e);
  7122. switch(e->e) {
  7123. case PRI_EVENT_DCHAN_UP:
  7124. if (option_verbose > 1)
  7125. ast_verbose(VERBOSE_PREFIX_2 "%s D-Channel on span %d up\n", pri_order(which), pri->span);
  7126. pri->dchanavail[which] |= DCHAN_UP;
  7127. pri_find_dchan(pri);
  7128. /* Note presense of D-channel */
  7129. time(&pri->lastreset);
  7130. /* Restart in 5 seconds */
  7131. pri->lastreset -= RESET_INTERVAL;
  7132. pri->lastreset += 5;
  7133. pri->resetting = 0;
  7134. /* Take the channels from inalarm condition */
  7135. for (i=0; i<pri->numchans; i++)
  7136. if (pri->pvts[i]) {
  7137. pri->pvts[i]->inalarm = 0;
  7138. }
  7139. break;
  7140. case PRI_EVENT_DCHAN_DOWN:
  7141. if (option_verbose > 1)
  7142. ast_verbose(VERBOSE_PREFIX_2 "%s D-Channel on span %d down\n", pri_order(which), pri->span);
  7143. pri->dchanavail[which] &= ~DCHAN_UP;
  7144. pri_find_dchan(pri);
  7145. if (!pri_is_up(pri)) {
  7146. pri->resetting = 0;
  7147. /* Hangup active channels and put them in alarm mode */
  7148. for (i=0; i<pri->numchans; i++) {
  7149. struct zt_pvt *p = pri->pvts[i];
  7150. if (p) {
  7151. if (p->call) {
  7152. if (p->pri && p->pri->pri) {
  7153. pri_hangup(p->pri->pri, p->call, -1);
  7154. pri_destroycall(p->pri->pri, p->call);
  7155. p->call = NULL;
  7156. } else
  7157. ast_log(LOG_WARNING, "The PRI Call have not been destroyed\n");
  7158. }
  7159. if (p->master) {
  7160. pri_hangup_all(p->master, pri);
  7161. } else if (p->owner)
  7162. p->owner->_softhangup |= AST_SOFTHANGUP_DEV;
  7163. p->inalarm = 1;
  7164. }
  7165. }
  7166. }
  7167. break;
  7168. case PRI_EVENT_RESTART:
  7169. if (e->restart.channel > -1) {
  7170. chanpos = pri_find_principle(pri, e->restart.channel);
  7171. if (chanpos < 0)
  7172. ast_log(LOG_WARNING, "Restart requested on odd/unavailable channel number %d/%d on span %d\n",
  7173. PRI_SPAN(e->restart.channel), PRI_CHANNEL(e->restart.channel), pri->span);
  7174. else {
  7175. if (option_verbose > 2)
  7176. ast_verbose(VERBOSE_PREFIX_3 "B-channel %d/%d restarted on span %d\n",
  7177. PRI_SPAN(e->restart.channel), PRI_CHANNEL(e->restart.channel), pri->span);
  7178. ast_mutex_lock(&pri->pvts[chanpos]->lock);
  7179. if (pri->pvts[chanpos]->call) {
  7180. pri_destroycall(pri->pri, pri->pvts[chanpos]->call);
  7181. pri->pvts[chanpos]->call = NULL;
  7182. }
  7183. /* Force soft hangup if appropriate */
  7184. if (pri->pvts[chanpos]->master)
  7185. pri_hangup_all(pri->pvts[chanpos]->master, pri);
  7186. else if (pri->pvts[chanpos]->owner)
  7187. pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
  7188. ast_mutex_unlock(&pri->pvts[chanpos]->lock);
  7189. }
  7190. } else {
  7191. if (option_verbose > 2)
  7192. ast_verbose(VERBOSE_PREFIX_2 "Restart on requested on entire span %d\n", pri->span);
  7193. for (x=0;x < pri->numchans;x++)
  7194. if (pri->pvts[x]) {
  7195. ast_mutex_lock(&pri->pvts[x]->lock);
  7196. if (pri->pvts[x]->call) {
  7197. pri_destroycall(pri->pri, pri->pvts[x]->call);
  7198. pri->pvts[x]->call = NULL;
  7199. }
  7200. if (pri->pvts[chanpos]->master)
  7201. pri_hangup_all(pri->pvts[chanpos]->master, pri);
  7202. else if (pri->pvts[x]->owner)
  7203. pri->pvts[x]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
  7204. ast_mutex_unlock(&pri->pvts[x]->lock);
  7205. }
  7206. }
  7207. break;
  7208. case PRI_EVENT_INFO_RECEIVED:
  7209. chanpos = pri_find_principle(pri, e->ring.channel);
  7210. if (chanpos < 0) {
  7211. ast_log(LOG_WARNING, "INFO received on unconfigured channel %d/%d span %d\n",
  7212. PRI_SPAN(e->ring.channel), PRI_CHANNEL(e->ring.channel), pri->span);
  7213. } else {
  7214. chanpos = pri_fixup_principle(pri, chanpos, e->ring.call);
  7215. if (chanpos > -1) {
  7216. ast_mutex_lock(&pri->pvts[chanpos]->lock);
  7217. /* queue DTMF frame if the PBX for this call was already started (we're forwarding INFORMATION further on */
  7218. if (pri->overlapdial && pri->pvts[chanpos]->call==e->ring.call && pri->pvts[chanpos]->owner) {
  7219. /* how to do that */
  7220. int digitlen = strlen(e->ring.callednum);
  7221. char digit;
  7222. int i;
  7223. for (i=0; i<digitlen; i++) {
  7224. digit = e->ring.callednum[i];
  7225. {
  7226. struct ast_frame f = { AST_FRAME_DTMF, digit, };
  7227. zap_queue_frame(pri->pvts[chanpos], &f, pri);
  7228. }
  7229. }
  7230. }
  7231. ast_mutex_unlock(&pri->pvts[chanpos]->lock);
  7232. }
  7233. }
  7234. break;
  7235. case PRI_EVENT_RING:
  7236. crv = NULL;
  7237. if (e->ring.channel == -1)
  7238. chanpos = pri_find_empty_chan(pri, 1);
  7239. else
  7240. chanpos = pri_find_principle(pri, e->ring.channel);
  7241. /* if no channel specified find one empty */
  7242. if (chanpos < 0) {
  7243. ast_log(LOG_WARNING, "Ring requested on unconfigured channel %d/%d span %d\n",
  7244. PRI_SPAN(e->ring.channel), PRI_CHANNEL(e->ring.channel), pri->span);
  7245. } else {
  7246. ast_mutex_lock(&pri->pvts[chanpos]->lock);
  7247. if (pri->pvts[chanpos]->owner) {
  7248. if (pri->pvts[chanpos]->call == e->ring.call) {
  7249. ast_log(LOG_WARNING, "Duplicate setup requested on channel %d/%d already in use on span %d\n",
  7250. PRI_SPAN(e->ring.channel), PRI_CHANNEL(e->ring.channel), pri->span);
  7251. break;
  7252. } else {
  7253. ast_log(LOG_WARNING, "Ring requested on channel %d/%d already in use on span %d. Hanging up owner.\n",
  7254. PRI_SPAN(e->ring.channel), PRI_CHANNEL(e->ring.channel), pri->span);
  7255. if (pri->pvts[chanpos]->master)
  7256. pri_hangup_all(pri->pvts[chanpos]->master, pri);
  7257. else
  7258. pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
  7259. ast_mutex_unlock(&pri->pvts[chanpos]->lock);
  7260. chanpos = -1;
  7261. }
  7262. }
  7263. if (chanpos > -1)
  7264. ast_mutex_unlock(&pri->pvts[chanpos]->lock);
  7265. }
  7266. if ((chanpos < 0) && (e->ring.flexible))
  7267. chanpos = pri_find_empty_chan(pri, 1);
  7268. if (chanpos > -1) {
  7269. ast_mutex_lock(&pri->pvts[chanpos]->lock);
  7270. if (pri->switchtype == PRI_SWITCH_GR303_TMC) {
  7271. /* Should be safe to lock CRV AFAIK while bearer is still locked */
  7272. crv = pri_find_crv(pri, pri_get_crv(pri->pri, e->ring.call, NULL));
  7273. if (crv)
  7274. ast_mutex_lock(&crv->lock);
  7275. if (!crv || crv->owner) {
  7276. pri->pvts[chanpos]->call = NULL;
  7277. if (crv) {
  7278. if (crv->owner)
  7279. crv->owner->_softhangup |= AST_SOFTHANGUP_DEV;
  7280. ast_log(LOG_WARNING, "Call received for busy CRV %d on span %d\n", pri_get_crv(pri->pri, e->ring.call, NULL), pri->span);
  7281. } else
  7282. ast_log(LOG_NOTICE, "Call received for unconfigured CRV %d on span %d\n", pri_get_crv(pri->pri, e->ring.call, NULL), pri->span);
  7283. pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_INVALID_CALL_REFERENCE);
  7284. if (crv)
  7285. ast_mutex_unlock(&crv->lock);
  7286. ast_mutex_unlock(&pri->pvts[chanpos]->lock);
  7287. break;
  7288. }
  7289. }
  7290. pri->pvts[chanpos]->call = e->ring.call;
  7291. /* Get caller ID */
  7292. if (pri->pvts[chanpos]->use_callerid) {
  7293. if (!ast_strlen_zero(e->ring.callingname)) {
  7294. snprintf(pri->pvts[chanpos]->callerid, sizeof(pri->pvts[chanpos]->callerid), "\"%s\" <%s>", e->ring.callingname, e->ring.callingnum);
  7295. } else
  7296. strncpy(pri->pvts[chanpos]->callerid, e->ring.callingnum, sizeof(pri->pvts[chanpos]->callerid)-1);
  7297. } else
  7298. pri->pvts[chanpos]->callerid[0] = '\0';
  7299. strncpy(pri->pvts[chanpos]->rdnis, e->ring.redirectingnum, sizeof(pri->pvts[chanpos]->rdnis) - 1);
  7300. /* If immediate=yes go to s|1 */
  7301. if (pri->pvts[chanpos]->immediate) {
  7302. if (option_verbose > 2)
  7303. ast_verbose(VERBOSE_PREFIX_3 "Going to extension s|1 because of immediate=yes\n");
  7304. pri->pvts[chanpos]->exten[0] = 's';
  7305. pri->pvts[chanpos]->exten[1] = '\0';
  7306. }
  7307. /* Get called number */
  7308. else if (!ast_strlen_zero(e->ring.callednum)) {
  7309. strncpy(pri->pvts[chanpos]->exten, e->ring.callednum, sizeof(pri->pvts[chanpos]->exten)-1);
  7310. strncpy(pri->pvts[chanpos]->dnid, e->ring.callednum, sizeof(pri->pvts[chanpos]->dnid) - 1);
  7311. } else
  7312. pri->pvts[chanpos]->exten[0] = '\0';
  7313. /* Set DNID on all incoming calls -- even immediate */
  7314. if (!ast_strlen_zero(e->ring.callednum))
  7315. strncpy(pri->pvts[chanpos]->dnid, e->ring.callednum, sizeof(pri->pvts[chanpos]->dnid) - 1);
  7316. /* No number yet, but received "sending complete"? */
  7317. if (e->ring.complete && (ast_strlen_zero(e->ring.callednum))) {
  7318. if (option_verbose > 2)
  7319. ast_verbose(VERBOSE_PREFIX_3 "Going to extension s|1 because of Complete received\n");
  7320. pri->pvts[chanpos]->exten[0] = 's';
  7321. pri->pvts[chanpos]->exten[1] = '\0';
  7322. }
  7323. /* Make sure extension exists (or in overlap dial mode, can exist) */
  7324. if ((pri->overlapdial && ast_canmatch_extension(NULL, pri->pvts[chanpos]->context, pri->pvts[chanpos]->exten, 1, pri->pvts[chanpos]->callerid)) ||
  7325. ast_exists_extension(NULL, pri->pvts[chanpos]->context, pri->pvts[chanpos]->exten, 1, pri->pvts[chanpos]->callerid)) {
  7326. /* Setup law */
  7327. int law;
  7328. if (pri->switchtype != PRI_SWITCH_GR303_TMC) {
  7329. /* Set to audio mode at this point */
  7330. law = 1;
  7331. if (ioctl(pri->pvts[chanpos]->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &law) == -1)
  7332. ast_log(LOG_WARNING, "Unable to set audio mode on channel %d to %d\n", pri->pvts[chanpos]->channel, law);
  7333. }
  7334. if (e->ring.layer1 == PRI_LAYER_1_ALAW)
  7335. law = ZT_LAW_ALAW;
  7336. else
  7337. law = ZT_LAW_MULAW;
  7338. res = zt_setlaw(pri->pvts[chanpos]->subs[SUB_REAL].zfd, law);
  7339. if (res < 0)
  7340. ast_log(LOG_WARNING, "Unable to set law on channel %d\n", pri->pvts[chanpos]->channel);
  7341. res = set_actual_gain(pri->pvts[chanpos]->subs[SUB_REAL].zfd, 0, pri->pvts[chanpos]->rxgain, pri->pvts[chanpos]->txgain, law);
  7342. if (res < 0)
  7343. ast_log(LOG_WARNING, "Unable to set gains on channel %d\n", pri->pvts[chanpos]->channel);
  7344. if (e->ring.complete || !pri->overlapdial) {
  7345. /* Just announce proceeding */
  7346. pri_proceeding(pri->pri, e->ring.call, PVT_TO_CHANNEL(pri->pvts[chanpos]), 0);
  7347. } else {
  7348. if (pri->switchtype != PRI_SWITCH_GR303_TMC)
  7349. pri_need_more_info(pri->pri, e->ring.call, PVT_TO_CHANNEL(pri->pvts[chanpos]), 1);
  7350. else
  7351. pri_answer(pri->pri, e->ring.call, PVT_TO_CHANNEL(pri->pvts[chanpos]), 1);
  7352. }
  7353. /* Get the use_callingpres state */
  7354. pri->pvts[chanpos]->callingpres = e->ring.callingpres;
  7355. /* Start PBX */
  7356. if (pri->overlapdial && ast_matchmore_extension(NULL, pri->pvts[chanpos]->context, pri->pvts[chanpos]->exten, 1, pri->pvts[chanpos]->callerid)) {
  7357. /* Release the PRI lock while we create the channel */
  7358. ast_mutex_unlock(&pri->lock);
  7359. if (crv) {
  7360. /* Set bearer and such */
  7361. pri_assign_bearer(crv, pri, pri->pvts[chanpos]);
  7362. c = zt_new(crv, AST_STATE_RESERVED, 0, SUB_REAL, law, e->ring.ctype);
  7363. pri->pvts[chanpos]->owner = &inuse;
  7364. ast_log(LOG_DEBUG, "Started up crv %d:%d on bearer channel %d\n", pri->trunkgroup, crv->channel, crv->bearer->channel);
  7365. } else {
  7366. c = zt_new(pri->pvts[chanpos], AST_STATE_RESERVED, 0, SUB_REAL, law, e->ring.ctype);
  7367. }
  7368. if(!ast_strlen_zero(e->ring.callingsubaddr)) {
  7369. pbx_builtin_setvar_helper(c, "CALLINGSUBADDR", e->ring.callingsubaddr);
  7370. }
  7371. ast_mutex_lock(&pri->lock);
  7372. if (c && !ast_pthread_create(&threadid, &attr, ss_thread, c)) {
  7373. if (option_verbose > 2)
  7374. ast_verbose(VERBOSE_PREFIX_3 "Accepting overlap call from '%s' to '%s' on channel %d/%d, span %d\n",
  7375. e->ring.callingnum, !ast_strlen_zero(pri->pvts[chanpos]->exten) ? pri->pvts[chanpos]->exten : "<unspecified>",
  7376. pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
  7377. } else {
  7378. ast_log(LOG_WARNING, "Unable to start PBX on channel %d/%d, span %d\n",
  7379. pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
  7380. if (c)
  7381. ast_hangup(c);
  7382. else {
  7383. pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_SWITCH_CONGESTION);
  7384. pri->pvts[chanpos]->call = NULL;
  7385. }
  7386. }
  7387. } else {
  7388. ast_mutex_unlock(&pri->lock);
  7389. /* Release PRI lock while we create the channel */
  7390. c = zt_new(pri->pvts[chanpos], AST_STATE_RING, 1, SUB_REAL, law, e->ring.ctype);
  7391. ast_mutex_lock(&pri->lock);
  7392. if (c) {
  7393. if (option_verbose > 2)
  7394. ast_verbose(VERBOSE_PREFIX_3 "Accepting call from '%s' to '%s' on channel %d/%d, span %d\n",
  7395. e->ring.callingnum, pri->pvts[chanpos]->exten,
  7396. pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
  7397. zt_enable_ec(pri->pvts[chanpos]);
  7398. } else {
  7399. ast_log(LOG_WARNING, "Unable to start PBX on channel %d/%d, span %d\n",
  7400. pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
  7401. pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_SWITCH_CONGESTION);
  7402. pri->pvts[chanpos]->call = NULL;
  7403. }
  7404. }
  7405. } else {
  7406. if (option_verbose > 2)
  7407. ast_verbose(VERBOSE_PREFIX_3 "Extension '%s' in context '%s' from '%s' does not exist. Rejecting call on channel %d/%d, span %d\n",
  7408. pri->pvts[chanpos]->exten, pri->pvts[chanpos]->context, pri->pvts[chanpos]->callerid, pri->pvts[chanpos]->logicalspan,
  7409. pri->pvts[chanpos]->prioffset, pri->span);
  7410. pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_UNALLOCATED);
  7411. pri->pvts[chanpos]->call = NULL;
  7412. }
  7413. if (crv)
  7414. ast_mutex_unlock(&crv->lock);
  7415. ast_mutex_unlock(&pri->pvts[chanpos]->lock);
  7416. } else
  7417. pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_REQUESTED_CHAN_UNAVAIL);
  7418. break;
  7419. case PRI_EVENT_RINGING:
  7420. chanpos = pri_find_principle(pri, e->ringing.channel);
  7421. if (chanpos < 0) {
  7422. ast_log(LOG_WARNING, "Ringing requested on unconfigured channel %d/%d span %d\n",
  7423. PRI_SPAN(e->ringing.channel), PRI_CHANNEL(e->ringing.channel), pri->span);
  7424. chanpos = -1;
  7425. }
  7426. if (chanpos > -1) {
  7427. chanpos = pri_fixup_principle(pri, chanpos, e->ringing.call);
  7428. if (chanpos < 0) {
  7429. ast_log(LOG_WARNING, "Ringing requested on channel %d/%d not in use on span %d\n",
  7430. PRI_SPAN(e->ringing.channel), PRI_CHANNEL(e->ringing.channel), pri->span);
  7431. chanpos = -1;
  7432. } else {
  7433. ast_mutex_lock(&pri->pvts[chanpos]->lock);
  7434. if (ast_strlen_zero(pri->pvts[chanpos]->dop.dialstr)) {
  7435. zt_enable_ec(pri->pvts[chanpos]);
  7436. pri->pvts[chanpos]->subs[SUB_REAL].needringing =1;
  7437. pri->pvts[chanpos]->proceeding=2;
  7438. } else
  7439. ast_log(LOG_DEBUG, "Deferring ringing notification because of extra digits to dial...\n");
  7440. ast_mutex_unlock(&pri->pvts[chanpos]->lock);
  7441. }
  7442. }
  7443. break;
  7444. case PRI_EVENT_PROGRESS:
  7445. /* Get chan value if e->e is not PRI_EVNT_RINGING */
  7446. chanpos = pri_find_principle(pri, e->proceeding.channel);
  7447. if (chanpos > -1) {
  7448. if (pri->overlapdial && !pri->pvts[chanpos]->proceeding) {
  7449. struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_PROGRESS, };
  7450. ast_mutex_lock(&pri->pvts[chanpos]->lock);
  7451. ast_log(LOG_DEBUG, "Queuing frame from PRI_EVENT_PROGRESS on channel %d/%d span %d\n",
  7452. pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset,pri->span);
  7453. zap_queue_frame(pri->pvts[chanpos], &f, pri);
  7454. ast_mutex_unlock(&pri->pvts[chanpos]->lock);
  7455. }
  7456. }
  7457. break;
  7458. case PRI_EVENT_PROCEEDING:
  7459. chanpos = pri_find_principle(pri, e->proceeding.channel);
  7460. if (chanpos > -1) {
  7461. if (pri->overlapdial && !pri->pvts[chanpos]->proceeding) {
  7462. struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_PROGRESS, };
  7463. ast_mutex_lock(&pri->pvts[chanpos]->lock);
  7464. ast_log(LOG_DEBUG, "Queuing frame from PRI_EVENT_PROCEEDING on channel %d/%d span %d\n",
  7465. pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset,pri->span);
  7466. zap_queue_frame(pri->pvts[chanpos], &f, pri);
  7467. f.subclass = AST_CONTROL_PROCEEDING;
  7468. zap_queue_frame(pri->pvts[chanpos], &f, pri);
  7469. pri->pvts[chanpos]->proceeding=2;
  7470. ast_mutex_unlock(&pri->pvts[chanpos]->lock);
  7471. }
  7472. }
  7473. break;
  7474. case PRI_EVENT_FACNAME:
  7475. chanpos = pri_find_principle(pri, e->facname.channel);
  7476. if (chanpos < 0) {
  7477. ast_log(LOG_WARNING, "Facility Name requested on unconfigured channel %d/%d span %d\n",
  7478. PRI_SPAN(e->facname.channel), PRI_CHANNEL(e->facname.channel), pri->span);
  7479. chanpos = -1;
  7480. }
  7481. if (chanpos > -1) {
  7482. chanpos = pri_fixup_principle(pri, chanpos, e->facname.call);
  7483. if (chanpos < 0) {
  7484. ast_log(LOG_WARNING, "Facility Name requested on channel %d/%d not in use on span %d\n",
  7485. PRI_SPAN(e->facname.channel), PRI_CHANNEL(e->facname.channel), pri->span);
  7486. chanpos = -1;
  7487. } else {
  7488. /* Re-use *69 field for PRI */
  7489. ast_mutex_lock(&pri->pvts[chanpos]->lock);
  7490. snprintf(pri->pvts[chanpos]->lastcallerid, sizeof(pri->pvts[chanpos]->lastcallerid), "\"%s\" <%s>", e->facname.callingname, e->facname.callingnum);
  7491. pri->pvts[chanpos]->subs[SUB_REAL].needcallerid =1;
  7492. zt_enable_ec(pri->pvts[chanpos]);
  7493. ast_mutex_unlock(&pri->pvts[chanpos]->lock);
  7494. }
  7495. }
  7496. break;
  7497. case PRI_EVENT_ANSWER:
  7498. chanpos = pri_find_principle(pri, e->answer.channel);
  7499. if (chanpos < 0) {
  7500. ast_log(LOG_WARNING, "Answer on unconfigured channel %d/%d span %d\n",
  7501. PRI_SPAN(e->answer.channel), PRI_CHANNEL(e->answer.channel), pri->span);
  7502. chanpos = -1;
  7503. }
  7504. if (chanpos > -1) {
  7505. chanpos = pri_fixup_principle(pri, chanpos, e->answer.call);
  7506. if (chanpos < 0) {
  7507. ast_log(LOG_WARNING, "Answer requested on channel %d/%d not in use on span %d\n",
  7508. PRI_SPAN(e->answer.channel), PRI_CHANNEL(e->answer.channel), pri->span);
  7509. chanpos = -1;
  7510. } else {
  7511. ast_mutex_lock(&pri->pvts[chanpos]->lock);
  7512. if (pri->pvts[chanpos]->master && (pri->pvts[chanpos]->master->sig == SIG_FXSKS)) {
  7513. ast_log(LOG_DEBUG, "Starting up GR-303 trunk now that we got CONNECT...\n");
  7514. x = ZT_START;
  7515. res = ioctl(pri->pvts[chanpos]->subs[SUB_REAL].zfd, ZT_HOOK, &x);
  7516. if (res < 0) {
  7517. if (errno != EINPROGRESS) {
  7518. ast_log(LOG_WARNING, "Unable to start channel: %s\n", strerror(errno));
  7519. }
  7520. }
  7521. } else if (!ast_strlen_zero(pri->pvts[chanpos]->dop.dialstr)) {
  7522. pri->pvts[chanpos]->dialing = 1;
  7523. /* Send any "w" waited stuff */
  7524. res = ioctl(pri->pvts[chanpos]->subs[SUB_REAL].zfd, ZT_DIAL, &pri->pvts[chanpos]->dop);
  7525. if (res < 0) {
  7526. ast_log(LOG_WARNING, "Unable to initiate dialing on trunk channel %d\n", pri->pvts[chanpos]->channel);
  7527. pri->pvts[chanpos]->dop.dialstr[0] = '\0';
  7528. } else
  7529. ast_log(LOG_DEBUG, "Sent deferred digit string: %s\n", pri->pvts[chanpos]->dop.dialstr);
  7530. pri->pvts[chanpos]->dop.dialstr[0] = '\0';
  7531. } else if (pri->pvts[chanpos]->confirmanswer) {
  7532. ast_log(LOG_DEBUG, "Waiting on answer confirmation on channel %d!\n", pri->pvts[chanpos]->channel);
  7533. } else {
  7534. pri->pvts[chanpos]->subs[SUB_REAL].needanswer =1;
  7535. /* Enable echo cancellation if it's not on already */
  7536. zt_enable_ec(pri->pvts[chanpos]);
  7537. }
  7538. ast_mutex_unlock(&pri->pvts[chanpos]->lock);
  7539. }
  7540. }
  7541. break;
  7542. case PRI_EVENT_HANGUP:
  7543. chanpos = pri_find_principle(pri, e->hangup.channel);
  7544. if (chanpos < 0) {
  7545. ast_log(LOG_WARNING, "Hangup requested on unconfigured channel %d/%d span %d\n",
  7546. PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
  7547. chanpos = -1;
  7548. }
  7549. if (chanpos > -1) {
  7550. chanpos = pri_fixup_principle(pri, chanpos, e->hangup.call);
  7551. if (chanpos > -1) {
  7552. ast_mutex_lock(&pri->pvts[chanpos]->lock);
  7553. if (!pri->pvts[chanpos]->alreadyhungup) {
  7554. /* we're calling here zt_hangup so once we get there we need to clear p->call after calling pri_hangup */
  7555. pri->pvts[chanpos]->alreadyhungup = 1;
  7556. if (pri->pvts[chanpos]->master)
  7557. pri_hangup_all(pri->pvts[chanpos]->master, pri);
  7558. else if (pri->pvts[chanpos]->owner) {
  7559. /* Queue a BUSY instead of a hangup if our cause is appropriate */
  7560. pri->pvts[chanpos]->owner->hangupcause = e->hangup.cause;
  7561. switch(e->hangup.cause) {
  7562. case PRI_CAUSE_USER_BUSY:
  7563. pri->pvts[chanpos]->subs[SUB_REAL].needbusy =1;
  7564. break;
  7565. case PRI_CAUSE_CALL_REJECTED:
  7566. case PRI_CAUSE_NETWORK_OUT_OF_ORDER:
  7567. case PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION:
  7568. case PRI_CAUSE_SWITCH_CONGESTION:
  7569. case PRI_CAUSE_DESTINATION_OUT_OF_ORDER:
  7570. case PRI_CAUSE_NORMAL_TEMPORARY_FAILURE:
  7571. pri->pvts[chanpos]->subs[SUB_REAL].needcongestion =1;
  7572. break;
  7573. default:
  7574. pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
  7575. }
  7576. }
  7577. if (option_verbose > 2)
  7578. ast_verbose(VERBOSE_PREFIX_3 "Channel %d/%d, span %d got hangup\n",
  7579. pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
  7580. } else {
  7581. pri_hangup(pri->pri, pri->pvts[chanpos]->call, e->hangup.cause);
  7582. pri->pvts[chanpos]->call = NULL;
  7583. }
  7584. if (e->hangup.cause == PRI_CAUSE_REQUESTED_CHAN_UNAVAIL) {
  7585. if (option_verbose > 2)
  7586. ast_verbose(VERBOSE_PREFIX_3 "Forcing restart of channel %d/%d on span %d since channel reported in use\n",
  7587. PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
  7588. pri_reset(pri->pri, PVT_TO_CHANNEL(pri->pvts[chanpos]));
  7589. pri->pvts[chanpos]->resetting = 1;
  7590. }
  7591. ast_mutex_unlock(&pri->pvts[chanpos]->lock);
  7592. } else {
  7593. ast_log(LOG_WARNING, "Hangup on bad channel %d/%d on span %d\n",
  7594. PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
  7595. }
  7596. }
  7597. break;
  7598. #ifndef PRI_EVENT_HANGUP_REQ
  7599. #error please update libpri
  7600. #endif
  7601. case PRI_EVENT_HANGUP_REQ:
  7602. chanpos = pri_find_principle(pri, e->hangup.channel);
  7603. if (chanpos < 0) {
  7604. ast_log(LOG_WARNING, "Hangup REQ requested on unconfigured channel %d/%d span %d\n",
  7605. PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
  7606. chanpos = -1;
  7607. }
  7608. if (chanpos > -1) {
  7609. chanpos = pri_fixup_principle(pri, chanpos, e->hangup.call);
  7610. if (chanpos > -1) {
  7611. ast_mutex_lock(&pri->pvts[chanpos]->lock);
  7612. if (pri->pvts[chanpos]->master)
  7613. pri_hangup_all(pri->pvts[chanpos]->master, pri);
  7614. else if (pri->pvts[chanpos]->owner) {
  7615. pri->pvts[chanpos]->owner->hangupcause = e->hangup.cause;
  7616. switch(e->hangup.cause) {
  7617. case PRI_CAUSE_USER_BUSY:
  7618. pri->pvts[chanpos]->subs[SUB_REAL].needbusy =1;
  7619. break;
  7620. case PRI_CAUSE_CALL_REJECTED:
  7621. case PRI_CAUSE_NETWORK_OUT_OF_ORDER:
  7622. case PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION:
  7623. case PRI_CAUSE_SWITCH_CONGESTION:
  7624. case PRI_CAUSE_DESTINATION_OUT_OF_ORDER:
  7625. case PRI_CAUSE_NORMAL_TEMPORARY_FAILURE:
  7626. pri->pvts[chanpos]->subs[SUB_REAL].needcongestion =1;
  7627. break;
  7628. default:
  7629. pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
  7630. }
  7631. if (option_verbose > 2)
  7632. ast_verbose(VERBOSE_PREFIX_3 "Channel %d/%d, span %d got hangup\n", PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
  7633. } else {
  7634. pri_hangup(pri->pri, pri->pvts[chanpos]->call, e->hangup.cause);
  7635. pri->pvts[chanpos]->call = NULL;
  7636. }
  7637. if (e->hangup.cause == PRI_CAUSE_REQUESTED_CHAN_UNAVAIL) {
  7638. if (option_verbose > 2)
  7639. ast_verbose(VERBOSE_PREFIX_3 "Forcing restart of channel %d/%d span %d since channel reported in use\n",
  7640. PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
  7641. pri_reset(pri->pri, PVT_TO_CHANNEL(pri->pvts[chanpos]));
  7642. pri->pvts[chanpos]->resetting = 1;
  7643. }
  7644. ast_mutex_unlock(&pri->pvts[chanpos]->lock);
  7645. } else {
  7646. ast_log(LOG_WARNING, "Hangup REQ on bad channel %d/%d on span %d\n", PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
  7647. }
  7648. }
  7649. break;
  7650. case PRI_EVENT_HANGUP_ACK:
  7651. chanpos = pri_find_principle(pri, e->hangup.channel);
  7652. if (chanpos < 0) {
  7653. ast_log(LOG_WARNING, "Hangup ACK requested on unconfigured channel number %d/%d span %d\n",
  7654. PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
  7655. chanpos = -1;
  7656. }
  7657. if (chanpos > -1) {
  7658. chanpos = pri_fixup_principle(pri, chanpos, e->hangup.call);
  7659. if (chanpos > -1) {
  7660. ast_mutex_lock(&pri->pvts[chanpos]->lock);
  7661. pri->pvts[chanpos]->call = NULL;
  7662. pri->pvts[chanpos]->resetting = 0;
  7663. if (pri->pvts[chanpos]->owner) {
  7664. if (option_verbose > 2)
  7665. ast_verbose(VERBOSE_PREFIX_3 "Channel %d/%d, span %d got hangup ACK\n", PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
  7666. }
  7667. ast_mutex_unlock(&pri->pvts[chanpos]->lock);
  7668. }
  7669. }
  7670. break;
  7671. case PRI_EVENT_CONFIG_ERR:
  7672. ast_log(LOG_WARNING, "PRI Error: %s\n", e->err.err);
  7673. break;
  7674. case PRI_EVENT_RESTART_ACK:
  7675. chanpos = pri_find_principle(pri, e->restartack.channel);
  7676. if (chanpos < 0) {
  7677. /* Sometime switches (e.g. I421 / British Telecom) don't give us the
  7678. channel number, so we have to figure it out... This must be why
  7679. everybody resets exactly a channel at a time. */
  7680. for (x=0;x<pri->numchans;x++) {
  7681. if (pri->pvts[x] && pri->pvts[x]->resetting) {
  7682. chanpos = x;
  7683. ast_mutex_lock(&pri->pvts[chanpos]->lock);
  7684. ast_log(LOG_DEBUG, "Assuming restart ack is really for channel %d/%d span %d\n", pri->pvts[chanpos]->logicalspan,
  7685. pri->pvts[chanpos]->prioffset, pri->span);
  7686. if (pri->pvts[chanpos]->master)
  7687. pri_hangup_all(pri->pvts[chanpos]->master, pri);
  7688. else if (pri->pvts[chanpos]->owner) {
  7689. ast_log(LOG_WARNING, "Got restart ack on channel %d/%d with owner on span %d\n", pri->pvts[chanpos]->logicalspan,
  7690. pri->pvts[chanpos]->prioffset, pri->span);
  7691. pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
  7692. }
  7693. pri->pvts[chanpos]->resetting = 0;
  7694. if (option_verbose > 2)
  7695. ast_verbose(VERBOSE_PREFIX_3 "B-channel %d/%d successfully restarted on span %d\n", pri->pvts[chanpos]->logicalspan,
  7696. pri->pvts[chanpos]->prioffset, pri->span);
  7697. ast_mutex_unlock(&pri->pvts[chanpos]->lock);
  7698. if (pri->resetting)
  7699. pri_check_restart(pri);
  7700. break;
  7701. }
  7702. }
  7703. if (chanpos < 0) {
  7704. ast_log(LOG_WARNING, "Restart ACK requested on strange channel %d/%d span %d\n",
  7705. PRI_SPAN(e->restartack.channel), PRI_CHANNEL(e->restartack.channel), pri->span);
  7706. }
  7707. chanpos = -1;
  7708. }
  7709. if (chanpos > -1) {
  7710. if (pri->pvts[chanpos]) {
  7711. ast_mutex_lock(&pri->pvts[chanpos]->lock);
  7712. if (pri->pvts[chanpos]->master)
  7713. pri_hangup_all(pri->pvts[chanpos]->master, pri);
  7714. else if (pri->pvts[chanpos]->owner) {
  7715. ast_log(LOG_WARNING, "Got restart ack on channel %d/%d span %d with owner\n",
  7716. PRI_SPAN(e->restartack.channel), PRI_CHANNEL(e->restartack.channel), pri->span);
  7717. pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
  7718. }
  7719. pri->pvts[chanpos]->resetting = 0;
  7720. if (option_verbose > 2)
  7721. ast_verbose(VERBOSE_PREFIX_3 "B-channel %d/%d successfully restarted on span %d\n", pri->pvts[chanpos]->logicalspan,
  7722. pri->pvts[chanpos]->prioffset, pri->span);
  7723. ast_mutex_unlock(&pri->pvts[chanpos]->lock);
  7724. if (pri->resetting)
  7725. pri_check_restart(pri);
  7726. }
  7727. }
  7728. break;
  7729. case PRI_EVENT_SETUP_ACK:
  7730. chanpos = pri_find_principle(pri, e->setup_ack.channel);
  7731. if (chanpos < 0) {
  7732. ast_log(LOG_WARNING, "Received SETUP_ACKNOWLEDGE on unconfigured channel %d/%d span %d\n",
  7733. PRI_SPAN(e->setup_ack.channel), PRI_CHANNEL(e->setup_ack.channel), pri->span);
  7734. } else {
  7735. ast_mutex_lock(&pri->pvts[chanpos]->lock);
  7736. pri->pvts[chanpos]->setup_ack = 1;
  7737. /* Send any queued digits */
  7738. for (x=0;x<strlen(pri->pvts[chanpos]->dialdest);x++) {
  7739. ast_log(LOG_DEBUG, "Sending pending digit '%c'\n", pri->pvts[chanpos]->dialdest[x]);
  7740. pri_information(pri->pri, pri->pvts[chanpos]->call,
  7741. pri->pvts[chanpos]->dialdest[x]);
  7742. }
  7743. ast_mutex_unlock(&pri->pvts[chanpos]->lock);
  7744. }
  7745. break;
  7746. default:
  7747. ast_log(LOG_DEBUG, "Event: %d\n", e->e);
  7748. }
  7749. }
  7750. ast_mutex_unlock(&pri->lock);
  7751. }
  7752. /* Never reached */
  7753. return NULL;
  7754. }
  7755. static int start_pri(struct zt_pri *pri)
  7756. {
  7757. int res, x;
  7758. ZT_PARAMS p;
  7759. ZT_BUFFERINFO bi;
  7760. struct zt_spaninfo si;
  7761. int i;
  7762. for (i=0;i<NUM_DCHANS;i++) {
  7763. if (!pri->dchannels[i])
  7764. break;
  7765. pri->fds[i] = open("/dev/zap/channel", O_RDWR, 0600);
  7766. x = pri->dchannels[i];
  7767. if ((pri->fds[i] < 0) || (ioctl(pri->fds[i],ZT_SPECIFY,&x) == -1)) {
  7768. ast_log(LOG_ERROR, "Unable to open D-channel %d (%s)\n", x, strerror(errno));
  7769. return -1;
  7770. }
  7771. res = ioctl(pri->fds[i], ZT_GET_PARAMS, &p);
  7772. if (res) {
  7773. zt_close(pri->fds[i]);
  7774. pri->fds[i] = -1;
  7775. ast_log(LOG_ERROR, "Unable to get parameters for D-channel %d (%s)\n", x, strerror(errno));
  7776. return -1;
  7777. }
  7778. if (p.sigtype != ZT_SIG_HDLCFCS) {
  7779. zt_close(pri->fds[i]);
  7780. pri->fds[i] = -1;
  7781. ast_log(LOG_ERROR, "D-channel %d is not in HDLC/FCS mode. See /etc/zaptel.conf\n", x);
  7782. return -1;
  7783. }
  7784. memset(&si, 0, sizeof(si));
  7785. res = ioctl(pri->fds[i], ZT_SPANSTAT, &si);
  7786. if (res) {
  7787. zt_close(pri->fds[i]);
  7788. pri->fds[i] = -1;
  7789. ast_log(LOG_ERROR, "Unable to get span state for D-channel %d (%s)\n", x, strerror(errno));
  7790. }
  7791. if (!si.alarms)
  7792. pri->dchanavail[i] |= DCHAN_NOTINALARM;
  7793. else
  7794. pri->dchanavail[i] &= ~DCHAN_NOTINALARM;
  7795. bi.txbufpolicy = ZT_POLICY_IMMEDIATE;
  7796. bi.rxbufpolicy = ZT_POLICY_IMMEDIATE;
  7797. bi.numbufs = 16;
  7798. bi.bufsize = 1024;
  7799. if (ioctl(pri->fds[i], ZT_SET_BUFINFO, &bi)) {
  7800. ast_log(LOG_ERROR, "Unable to set appropriate buffering on channel %d\n", x);
  7801. zt_close(pri->fds[i]);
  7802. pri->fds[i] = -1;
  7803. return -1;
  7804. }
  7805. pri->dchans[i] = pri_new(pri->fds[i], pri->nodetype, pri->switchtype);
  7806. /* Force overlap dial if we're doing GR-303! */
  7807. if (pri->switchtype == PRI_SWITCH_GR303_TMC)
  7808. pri->overlapdial = 1;
  7809. pri_set_overlapdial(pri->dchans[i],pri->overlapdial);
  7810. /* Enslave to master if appropriate */
  7811. if (i)
  7812. pri_enslave(pri->dchans[0], pri->dchans[i]);
  7813. if (!pri->dchans[i]) {
  7814. zt_close(pri->fds[i]);
  7815. pri->fds[i] = -1;
  7816. ast_log(LOG_ERROR, "Unable to create PRI structure\n");
  7817. return -1;
  7818. }
  7819. pri_set_debug(pri->dchans[i], DEFAULT_PRI_DEBUG);
  7820. pri_set_nsf(pri->dchans[i], pri->nsf);
  7821. }
  7822. /* Assume primary is the one we use */
  7823. pri->pri = pri->dchans[0];
  7824. pri->resetpos = -1;
  7825. if (ast_pthread_create(&pri->master, NULL, pri_dchannel, pri)) {
  7826. for (i=0;i<NUM_DCHANS;i++) {
  7827. if (!pri->dchannels[i])
  7828. break;
  7829. zt_close(pri->fds[i]);
  7830. pri->fds[i] = -1;
  7831. }
  7832. ast_log(LOG_ERROR, "Unable to spawn D-channel: %s\n", strerror(errno));
  7833. return -1;
  7834. }
  7835. return 0;
  7836. }
  7837. static char *complete_span_helper(char *line, char *word, int pos, int state, int rpos)
  7838. {
  7839. int span=1;
  7840. char tmp[50];
  7841. if (pos != rpos)
  7842. return 0;
  7843. while(span <= NUM_SPANS) {
  7844. if (span > state && pris[span-1].pri)
  7845. break;
  7846. span++;
  7847. }
  7848. if (span <= NUM_SPANS) {
  7849. snprintf(tmp, sizeof(tmp), "%d", span);
  7850. return strdup(tmp);
  7851. } else
  7852. return NULL;
  7853. }
  7854. static char *complete_span_4(char *line, char *word, int pos, int state)
  7855. {
  7856. return complete_span_helper(line,word,pos,state,3);
  7857. }
  7858. static char *complete_span_5(char *line, char *word, int pos, int state)
  7859. {
  7860. return complete_span_helper(line,word,pos,state,4);
  7861. }
  7862. static int handle_pri_debug(int fd, int argc, char *argv[])
  7863. {
  7864. int span;
  7865. int x;
  7866. if (argc < 4) {
  7867. return RESULT_SHOWUSAGE;
  7868. }
  7869. span = atoi(argv[3]);
  7870. if ((span < 1) || (span > NUM_SPANS)) {
  7871. ast_cli(fd, "Invalid span %s. Should be a number %d to %d\n", argv[3], 1, NUM_SPANS);
  7872. return RESULT_SUCCESS;
  7873. }
  7874. if (!pris[span-1].pri) {
  7875. ast_cli(fd, "No PRI running on span %d\n", span);
  7876. return RESULT_SUCCESS;
  7877. }
  7878. for (x=0;x<NUM_DCHANS;x++) {
  7879. if (pris[span-1].dchans[x])
  7880. pri_set_debug(pris[span-1].dchans[x], PRI_DEBUG_Q931_DUMP | PRI_DEBUG_Q931_STATE);
  7881. }
  7882. ast_cli(fd, "Enabled debugging on span %d\n", span);
  7883. return RESULT_SUCCESS;
  7884. }
  7885. static int handle_pri_no_debug(int fd, int argc, char *argv[])
  7886. {
  7887. int span;
  7888. int x;
  7889. if (argc < 5)
  7890. return RESULT_SHOWUSAGE;
  7891. span = atoi(argv[4]);
  7892. if ((span < 1) || (span > NUM_SPANS)) {
  7893. ast_cli(fd, "Invalid span %s. Should be a number %d to %d\n", argv[4], 1, NUM_SPANS);
  7894. return RESULT_SUCCESS;
  7895. }
  7896. if (!pris[span-1].pri) {
  7897. ast_cli(fd, "No PRI running on span %d\n", span);
  7898. return RESULT_SUCCESS;
  7899. }
  7900. for (x=0;x<NUM_DCHANS;x++) {
  7901. if (pris[span-1].dchans[x])
  7902. pri_set_debug(pris[span-1].dchans[x], 0);
  7903. }
  7904. ast_cli(fd, "Disabled debugging on span %d\n", span);
  7905. return RESULT_SUCCESS;
  7906. }
  7907. static int handle_pri_really_debug(int fd, int argc, char *argv[])
  7908. {
  7909. int span;
  7910. int x;
  7911. if (argc < 5)
  7912. return RESULT_SHOWUSAGE;
  7913. span = atoi(argv[4]);
  7914. if ((span < 1) || (span > NUM_SPANS)) {
  7915. ast_cli(fd, "Invalid span %s. Should be a number %d to %d\n", argv[4], 1, NUM_SPANS);
  7916. return RESULT_SUCCESS;
  7917. }
  7918. if (!pris[span-1].pri) {
  7919. ast_cli(fd, "No PRI running on span %d\n", span);
  7920. return RESULT_SUCCESS;
  7921. }
  7922. for (x=0;x<NUM_DCHANS;x++) {
  7923. if (pris[span-1].dchans[x])
  7924. pri_set_debug(pris[span-1].dchans[x], (PRI_DEBUG_Q931_DUMP | PRI_DEBUG_Q921_DUMP | PRI_DEBUG_Q921_RAW | PRI_DEBUG_Q921_STATE));
  7925. }
  7926. ast_cli(fd, "Enabled EXTENSIVE debugging on span %d\n", span);
  7927. return RESULT_SUCCESS;
  7928. }
  7929. static void build_status(char *s, size_t len, int status, int active)
  7930. {
  7931. if (!s || len < 1) {
  7932. return;
  7933. }
  7934. s[0] = '\0';
  7935. if (status & DCHAN_PROVISIONED)
  7936. strncat(s, "Provisioned, ", len - strlen(s) - 1);
  7937. if (!(status & DCHAN_NOTINALARM))
  7938. strncat(s, "In Alarm, ", len - strlen(s) - 1);
  7939. if (status & DCHAN_UP)
  7940. strncat(s, "Up", len - strlen(s) - 1);
  7941. else
  7942. strncat(s, "Down", len - strlen(s) - 1);
  7943. if (active)
  7944. strncat(s, ", Active", len - strlen(s) - 1);
  7945. else
  7946. strncat(s, ", Standby", len - strlen(s) - 1);
  7947. s[len - 1] = '\0';
  7948. }
  7949. static int handle_pri_show_span(int fd, int argc, char *argv[])
  7950. {
  7951. int span;
  7952. int x;
  7953. char status[256];
  7954. if (argc < 4)
  7955. return RESULT_SHOWUSAGE;
  7956. span = atoi(argv[3]);
  7957. if ((span < 1) || (span > NUM_SPANS)) {
  7958. ast_cli(fd, "Invalid span %s. Should be a number %d to %d\n", argv[4], 1, NUM_SPANS);
  7959. return RESULT_SUCCESS;
  7960. }
  7961. if (!pris[span-1].pri) {
  7962. ast_cli(fd, "No PRI running on span %d\n", span);
  7963. return RESULT_SUCCESS;
  7964. }
  7965. for(x=0;x<NUM_DCHANS;x++) {
  7966. if (pris[span-1].dchannels[x]) {
  7967. char *info_str = NULL;
  7968. ast_cli(fd, "%s D-channel: %d\n", pri_order(x), pris[span-1].dchannels[x]);
  7969. build_status(status, sizeof(status), pris[span-1].dchanavail[x], pris[span-1].dchans[x] == pris[span-1].pri);
  7970. ast_cli(fd, "Status: %s\n", status);
  7971. #ifdef PRI_DUMP_INFO_STR
  7972. info_str = pri_dump_info_str(pris[span-1].pri);
  7973. if (info_str) {
  7974. ast_cli(fd, "%s", info_str);
  7975. free(info_str);
  7976. }
  7977. #else
  7978. pri_dump_info(pris[span-1].pri);
  7979. #endif
  7980. ast_cli(fd, "\n");
  7981. }
  7982. }
  7983. return RESULT_SUCCESS;
  7984. }
  7985. static char pri_debug_help[] =
  7986. "Usage: pri debug span <span>\n"
  7987. " Enables debugging on a given PRI span\n";
  7988. static char pri_no_debug_help[] =
  7989. "Usage: pri no debug span <span>\n"
  7990. " Disables debugging on a given PRI span\n";
  7991. static char pri_really_debug_help[] =
  7992. "Usage: pri intensive debug span <span>\n"
  7993. " Enables debugging down to the Q.921 level\n";
  7994. static char pri_show_span_help[] =
  7995. "Usage: pri show span <span>\n"
  7996. " Displays PRI Information\n";
  7997. static struct ast_cli_entry pri_debug = {
  7998. { "pri", "debug", "span", NULL }, handle_pri_debug, "Enables PRI debugging on a span", pri_debug_help, complete_span_4 };
  7999. static struct ast_cli_entry pri_no_debug = {
  8000. { "pri", "no", "debug", "span", NULL }, handle_pri_no_debug, "Disables PRI debugging on a span", pri_no_debug_help, complete_span_5 };
  8001. static struct ast_cli_entry pri_really_debug = {
  8002. { "pri", "intense", "debug", "span", NULL }, handle_pri_really_debug, "Enables REALLY INTENSE PRI debugging", pri_really_debug_help, complete_span_5 };
  8003. static struct ast_cli_entry pri_show_span = {
  8004. { "pri", "show", "span", NULL }, handle_pri_show_span, "Displays PRI Information", pri_show_span_help, complete_span_4 };
  8005. #endif /* ZAPATA_PRI */
  8006. #ifdef ZAPATA_R2
  8007. static int handle_r2_no_debug(int fd, int argc, char *argv[])
  8008. {
  8009. int chan;
  8010. struct zt_pvt *tmp = NULL;;
  8011. if (argc < 5)
  8012. return RESULT_SHOWUSAGE;
  8013. chan = atoi(argv[4]);
  8014. if ((chan < 1) || (chan > NUM_SPANS)) {
  8015. ast_cli(fd, "Invalid channel %s. Should be a number greater than 0\n", argv[4]);
  8016. return RESULT_SUCCESS;
  8017. }
  8018. tmp = iflist;
  8019. while(tmp) {
  8020. if (tmp->channel == chan) {
  8021. if (tmp->r2) {
  8022. mfcr2_set_debug(tmp->r2, 0);
  8023. ast_cli(fd, "Disabled R2 debugging on channel %d\n", chan);
  8024. return RESULT_SUCCESS;
  8025. }
  8026. break;
  8027. }
  8028. tmp = tmp->next;
  8029. }
  8030. if (tmp)
  8031. ast_cli(fd, "No R2 running on channel %d\n", chan);
  8032. else
  8033. ast_cli(fd, "No such zap channel %d\n", chan);
  8034. return RESULT_SUCCESS;
  8035. }
  8036. static int handle_r2_debug(int fd, int argc, char *argv[])
  8037. {
  8038. int chan;
  8039. struct zt_pvt *tmp = NULL;;
  8040. if (argc < 4) {
  8041. return RESULT_SHOWUSAGE;
  8042. }
  8043. chan = atoi(argv[3]);
  8044. if ((chan < 1) || (chan > NUM_SPANS)) {
  8045. ast_cli(fd, "Invalid channel %s. Should be a number greater than 0\n", argv[3]);
  8046. return RESULT_SUCCESS;
  8047. }
  8048. tmp = iflist;
  8049. while(tmp) {
  8050. if (tmp->channel == chan) {
  8051. if (tmp->r2) {
  8052. mfcr2_set_debug(tmp->r2, 0xFFFFFFFF);
  8053. ast_cli(fd, "Enabled R2 debugging on channel %d\n", chan);
  8054. return RESULT_SUCCESS;
  8055. }
  8056. break;
  8057. }
  8058. tmp = tmp->next;
  8059. }
  8060. if (tmp)
  8061. ast_cli(fd, "No R2 running on channel %d\n", chan);
  8062. else
  8063. ast_cli(fd, "No such zap channel %d\n", chan);
  8064. return RESULT_SUCCESS;
  8065. }
  8066. static char r2_debug_help[] =
  8067. "Usage: r2 debug channel <channel>\n"
  8068. " Enables R2 protocol level debugging on a given channel\n";
  8069. static char r2_no_debug_help[] =
  8070. "Usage: r2 no debug channel <channel>\n"
  8071. " Enables R2 protocol level debugging on a given channel\n";
  8072. static struct ast_cli_entry r2_debug = {
  8073. { "r2", "debug", "channel", NULL }, handle_r2_debug, "Enables R2 debugging on a channel", r2_debug_help };
  8074. static struct ast_cli_entry r2_no_debug = {
  8075. { "r2", "no", "debug", "channel", NULL }, handle_r2_no_debug, "Disables R2 debugging on a channel", r2_no_debug_help };
  8076. #endif
  8077. static int zap_destroy_channel(int fd, int argc, char **argv)
  8078. {
  8079. int channel = 0;
  8080. struct zt_pvt *tmp = NULL;
  8081. struct zt_pvt *prev = NULL;
  8082. if (argc != 4) {
  8083. return RESULT_SHOWUSAGE;
  8084. }
  8085. channel = atoi(argv[3]);
  8086. tmp = iflist;
  8087. while (tmp) {
  8088. if (tmp->channel == channel) {
  8089. destroy_channel(prev, tmp, 1);
  8090. return RESULT_SUCCESS;
  8091. }
  8092. prev = tmp;
  8093. tmp = tmp->next;
  8094. }
  8095. return RESULT_FAILURE;
  8096. }
  8097. static int zap_show_channels(int fd, int argc, char **argv)
  8098. {
  8099. #define FORMAT "%7s %-10.10s %-15.15s %-10.10s %-20.20s\n"
  8100. #define FORMAT2 "%7s %-10.10s %-15.15s %-10.10s %-20.20s\n"
  8101. struct zt_pvt *tmp = NULL;
  8102. char tmps[20] = "";
  8103. ast_mutex_t *lock;
  8104. struct zt_pvt *start;
  8105. #ifdef ZAPATA_PRI
  8106. int trunkgroup;
  8107. struct zt_pri *pri=NULL;
  8108. int x;
  8109. #endif
  8110. lock = &iflock;
  8111. start = iflist;
  8112. #ifdef ZAPATA_PRI
  8113. if (argc == 4) {
  8114. if ((trunkgroup = atoi(argv[3])) < 1)
  8115. return RESULT_SHOWUSAGE;
  8116. for (x=0;x<NUM_SPANS;x++) {
  8117. if (pris[x].trunkgroup == trunkgroup) {
  8118. pri = pris + x;
  8119. break;
  8120. }
  8121. }
  8122. if (pri) {
  8123. start = pri->crvs;
  8124. lock = &pri->lock;
  8125. } else {
  8126. ast_cli(fd, "No such trunk group %d\n", trunkgroup);
  8127. return RESULT_FAILURE;
  8128. }
  8129. } else
  8130. #endif
  8131. if (argc != 3)
  8132. return RESULT_SHOWUSAGE;
  8133. ast_mutex_lock(lock);
  8134. #ifdef ZAPATA_PRI
  8135. ast_cli(fd, FORMAT2, pri ? "CRV" : "Chan", "Extension", "Context", "Language", "MusicOnHold");
  8136. #else
  8137. ast_cli(fd, FORMAT2, "Chan", "Extension", "Context", "Language", "MusicOnHold");
  8138. #endif
  8139. tmp = start;
  8140. while (tmp) {
  8141. if (tmp->channel > 0) {
  8142. snprintf(tmps, sizeof(tmps), "%d", tmp->channel);
  8143. } else
  8144. strncpy(tmps, "pseudo", sizeof(tmps) - 1);
  8145. ast_cli(fd, FORMAT, tmps, tmp->exten, tmp->context, tmp->language, tmp->musicclass);
  8146. tmp = tmp->next;
  8147. }
  8148. ast_mutex_unlock(lock);
  8149. return RESULT_SUCCESS;
  8150. #undef FORMAT
  8151. #undef FORMAT2
  8152. }
  8153. static int zap_show_channel(int fd, int argc, char **argv)
  8154. {
  8155. int channel;
  8156. struct zt_pvt *tmp = NULL;
  8157. ZT_CONFINFO ci;
  8158. ZT_PARAMS ps;
  8159. int x;
  8160. ast_mutex_t *lock;
  8161. struct zt_pvt *start;
  8162. #ifdef ZAPATA_PRI
  8163. char *c;
  8164. int trunkgroup;
  8165. struct zt_pri *pri=NULL;
  8166. #endif
  8167. lock = &iflock;
  8168. start = iflist;
  8169. if (argc != 4)
  8170. return RESULT_SHOWUSAGE;
  8171. #ifdef ZAPATA_PRI
  8172. if ((c = strchr(argv[3], ':'))) {
  8173. if (sscanf(argv[3], "%d:%d", &trunkgroup, &channel) != 2)
  8174. return RESULT_SHOWUSAGE;
  8175. if ((trunkgroup < 1) || (channel < 1))
  8176. return RESULT_SHOWUSAGE;
  8177. for (x=0;x<NUM_SPANS;x++) {
  8178. if (pris[x].trunkgroup == trunkgroup) {
  8179. pri = pris + x;
  8180. break;
  8181. }
  8182. }
  8183. if (pri) {
  8184. start = pri->crvs;
  8185. lock = &pri->lock;
  8186. } else {
  8187. ast_cli(fd, "No such trunk group %d\n", trunkgroup);
  8188. return RESULT_FAILURE;
  8189. }
  8190. } else
  8191. #endif
  8192. channel = atoi(argv[3]);
  8193. ast_mutex_lock(lock);
  8194. tmp = start;
  8195. while (tmp) {
  8196. if (tmp->channel == channel) {
  8197. #ifdef ZAPATA_PRI
  8198. if (pri)
  8199. ast_cli(fd, "Trunk/CRV: %d/%d\n", trunkgroup, tmp->channel);
  8200. else
  8201. #endif
  8202. ast_cli(fd, "Channel: %d\n", tmp->channel);
  8203. ast_cli(fd, "File Descriptor: %d\n", tmp->subs[SUB_REAL].zfd);
  8204. ast_cli(fd, "Span: %d\n", tmp->span);
  8205. ast_cli(fd, "Extension: %s\n", tmp->exten);
  8206. ast_cli(fd, "Dialing: %s\n", tmp->dialing ? "yes" : "no");
  8207. ast_cli(fd, "Context: %s\n", tmp->context);
  8208. ast_cli(fd, "Caller ID string: %s\n", tmp->callerid);
  8209. ast_cli(fd, "Destroy: %d\n", tmp->destroy);
  8210. ast_cli(fd, "InAlarm: %d\n", tmp->inalarm);
  8211. ast_cli(fd, "Signalling Type: %s\n", sig2str(tmp->sig));
  8212. ast_cli(fd, "Owner: %s\n", tmp->owner ? tmp->owner->name : "<None>");
  8213. ast_cli(fd, "Real: %s%s%s\n", tmp->subs[SUB_REAL].owner ? tmp->subs[SUB_REAL].owner->name : "<None>", tmp->subs[SUB_REAL].inthreeway ? " (Confed)" : "", tmp->subs[SUB_REAL].linear ? " (Linear)" : "");
  8214. ast_cli(fd, "Callwait: %s%s%s\n", tmp->subs[SUB_CALLWAIT].owner ? tmp->subs[SUB_CALLWAIT].owner->name : "<None>", tmp->subs[SUB_CALLWAIT].inthreeway ? " (Confed)" : "", tmp->subs[SUB_CALLWAIT].linear ? " (Linear)" : "");
  8215. ast_cli(fd, "Threeway: %s%s%s\n", tmp->subs[SUB_THREEWAY].owner ? tmp->subs[SUB_THREEWAY].owner->name : "<None>", tmp->subs[SUB_THREEWAY].inthreeway ? " (Confed)" : "", tmp->subs[SUB_THREEWAY].linear ? " (Linear)" : "");
  8216. ast_cli(fd, "Confno: %d\n", tmp->confno);
  8217. ast_cli(fd, "Propagated Conference: %d\n", tmp->propconfno);
  8218. ast_cli(fd, "Real in conference: %d\n", tmp->inconference);
  8219. ast_cli(fd, "DSP: %s\n", tmp->dsp ? "yes" : "no");
  8220. ast_cli(fd, "Relax DTMF: %s\n", tmp->dtmfrelax ? "yes" : "no");
  8221. ast_cli(fd, "Dialing/CallwaitCAS: %d/%d\n", tmp->dialing, tmp->callwaitcas);
  8222. ast_cli(fd, "Default law: %s\n", tmp->law == ZT_LAW_MULAW ? "ulaw" : tmp->law == ZT_LAW_ALAW ? "alaw" : "unknown");
  8223. ast_cli(fd, "Fax Handled: %s\n", tmp->faxhandled ? "yes" : "no");
  8224. ast_cli(fd, "Pulse phone: %s\n", tmp->pulsedial ? "yes" : "no");
  8225. ast_cli(fd, "Echo Cancellation: %d taps%s, currently %s\n", tmp->echocancel, tmp->echocanbridged ? "" : " unless TDM bridged", tmp->echocanon ? "ON" : "OFF");
  8226. if (tmp->master)
  8227. ast_cli(fd, "Master Channel: %d\n", tmp->master->channel);
  8228. for (x=0;x<MAX_SLAVES;x++) {
  8229. if (tmp->slaves[x])
  8230. ast_cli(fd, "Slave Channel: %d\n", tmp->slaves[x]->channel);
  8231. }
  8232. #ifdef ZAPATA_PRI
  8233. if (tmp->pri) {
  8234. ast_cli(fd, "PRI Flags: ");
  8235. if (tmp->resetting)
  8236. ast_cli(fd, "Resetting ");
  8237. if (tmp->call)
  8238. ast_cli(fd, "Call ");
  8239. if (tmp->bearer)
  8240. ast_cli(fd, "Bearer ");
  8241. ast_cli(fd, "\n");
  8242. if (tmp->logicalspan)
  8243. ast_cli(fd, "PRI Logical Span: %d\n", tmp->logicalspan);
  8244. else
  8245. ast_cli(fd, "PRI Logical Span: Implicit\n");
  8246. }
  8247. #endif
  8248. #ifdef ZAPATA_R2
  8249. if (tmp->r2) {
  8250. ast_cli(fd, "R2 Flags: ");
  8251. if (tmp->r2blocked)
  8252. ast_cli(fd, "Blocked ");
  8253. if (tmp->hasr2call)
  8254. ast_cli(fd, "Call ");
  8255. ast_cli(fd, "\n");
  8256. }
  8257. #endif
  8258. memset(&ci, 0, sizeof(ci));
  8259. ps.channo = tmp->channel;
  8260. if (tmp->subs[SUB_REAL].zfd > -1) {
  8261. if (!ioctl(tmp->subs[SUB_REAL].zfd, ZT_GETCONF, &ci)) {
  8262. ast_cli(fd, "Actual Confinfo: Num/%d, Mode/0x%04x\n", ci.confno, ci.confmode);
  8263. }
  8264. #ifdef ZT_GETCONFMUTE
  8265. if (!ioctl(tmp->subs[SUB_REAL].zfd, ZT_GETCONFMUTE, &x)) {
  8266. ast_cli(fd, "Actual Confmute: %s\n", x ? "Yes" : "No");
  8267. }
  8268. #endif
  8269. if (ioctl(tmp->subs[SUB_REAL].zfd, ZT_GET_PARAMS, &ps) < 0) {
  8270. ast_log(LOG_WARNING, "Failed to get parameters on channel %d\n", tmp->channel);
  8271. } else {
  8272. ast_cli(fd, "Actual Hookstate: %s\n", ps.rxisoffhook ? "Offhook" : "Onhook");
  8273. }
  8274. }
  8275. ast_mutex_unlock(lock);
  8276. return RESULT_SUCCESS;
  8277. }
  8278. tmp = tmp->next;
  8279. }
  8280. ast_cli(fd, "Unable to find given channel %d\n", channel);
  8281. ast_mutex_unlock(lock);
  8282. return RESULT_FAILURE;
  8283. }
  8284. static char zap_show_cadences_help[] =
  8285. "Usage: zap show cadences\n"
  8286. " Shows all cadences currently defined\n";
  8287. static int handle_zap_show_cadences(int fd, int argc, char *argv[])
  8288. {
  8289. int i, j;
  8290. for (i=0;i<num_cadence;i++) {
  8291. char output[1024];
  8292. char tmp[16], tmp2[64];
  8293. snprintf(tmp, sizeof(tmp), "r%d: ", i + 1);
  8294. term_color(output, tmp, COLOR_GREEN, COLOR_BLACK, sizeof(output));
  8295. for (j=0;j<16;j++) {
  8296. if (cadences[i].ringcadence[j] == 0)
  8297. break;
  8298. snprintf(tmp, sizeof(tmp), "%d", cadences[i].ringcadence[j]);
  8299. if (cidrings[i] * 2 - 1 == j)
  8300. term_color(tmp2, tmp, COLOR_MAGENTA, COLOR_BLACK, sizeof(tmp2) - 1);
  8301. else
  8302. term_color(tmp2, tmp, COLOR_GREEN, COLOR_BLACK, sizeof(tmp2) - 1);
  8303. if (j != 0)
  8304. strncat(output, ",", sizeof(output) - strlen(output) - 1);
  8305. strncat(output, tmp2, sizeof(output) - strlen(output) - 1);
  8306. }
  8307. ast_cli(fd,"%s\n",output);
  8308. }
  8309. return 0;
  8310. }
  8311. static struct ast_cli_entry zap_show_cadences_cli =
  8312. { { "zap", "show", "cadences", NULL },
  8313. handle_zap_show_cadences, "List cadences",
  8314. zap_show_cadences_help, NULL };
  8315. static char show_channels_usage[] =
  8316. "Usage: zap show channels\n"
  8317. " Shows a list of available channels\n";
  8318. static char show_channel_usage[] =
  8319. "Usage: zap show channel <chan num>\n"
  8320. " Detailed information about a given channel\n";
  8321. static char destroy_channel_usage[] =
  8322. "Usage: zap destroy channel <chan num>\n"
  8323. " DON'T USE THIS UNLESS YOU KNOW WHAT YOU ARE DOING. Immediately removes a given channel, whether it is in use or not\n";
  8324. static struct ast_cli_entry cli_show_channels = {
  8325. {"zap", "show", "channels", NULL}, zap_show_channels, "Show active zapata channels", show_channels_usage, NULL };
  8326. static struct ast_cli_entry cli_show_channel = {
  8327. {"zap", "show", "channel", NULL}, zap_show_channel, "Show information on a channel", show_channel_usage, NULL };
  8328. static struct ast_cli_entry cli_destroy_channel = {
  8329. {"zap", "destroy", "channel", NULL}, zap_destroy_channel, "Destroy a channel", destroy_channel_usage, NULL };
  8330. static char *synopsis_callingpres = "Change the presentation for the callerid";
  8331. static char *descrip_callingpres = "Callingpres(number): Changes the presentation for the callerid. Should be called before placing an outgoing call\n";
  8332. static char *app_callingpres = "CallingPres";
  8333. static int change_callingpres(struct ast_channel *chan, void *data)
  8334. {
  8335. int mode = 0;
  8336. if (data) {
  8337. mode = atoi((char *)data);
  8338. chan->callingpres = mode;
  8339. } else
  8340. ast_log(LOG_NOTICE, "Application %s requres an argument: %s(number)\n", app_callingpres,app_callingpres);
  8341. return 0;
  8342. }
  8343. #define TRANSFER 0
  8344. #define HANGUP 1
  8345. static int zap_fake_event(struct zt_pvt *p, int mode)
  8346. {
  8347. if (p) {
  8348. switch(mode) {
  8349. case TRANSFER:
  8350. p->fake_event = ZT_EVENT_WINKFLASH;
  8351. break;
  8352. case HANGUP:
  8353. p->fake_event = ZT_EVENT_ONHOOK;
  8354. break;
  8355. default:
  8356. ast_log(LOG_WARNING, "I don't know how to handle transfer event with this: %d on channel %s\n",mode, p->owner->name);
  8357. }
  8358. }
  8359. return 0;
  8360. }
  8361. static struct zt_pvt *find_channel(int channel)
  8362. {
  8363. struct zt_pvt *p = iflist;
  8364. while(p) {
  8365. if (p->channel == channel) {
  8366. break;
  8367. }
  8368. p = p->next;
  8369. }
  8370. return p;
  8371. }
  8372. static int action_zapdndon(struct mansession *s, struct message *m)
  8373. {
  8374. struct zt_pvt *p = NULL;
  8375. char *channel = astman_get_header(m, "ZapChannel");
  8376. if (ast_strlen_zero(channel)) {
  8377. astman_send_error(s, m, "No channel specified");
  8378. return 0;
  8379. }
  8380. p = find_channel(atoi(channel));
  8381. if (!p) {
  8382. astman_send_error(s, m, "No such channel");
  8383. return 0;
  8384. }
  8385. p->dnd = 1;
  8386. astman_send_ack(s, m, "DND Enabled");
  8387. return 0;
  8388. }
  8389. static int action_zapdndoff(struct mansession *s, struct message *m)
  8390. {
  8391. struct zt_pvt *p = NULL;
  8392. char *channel = astman_get_header(m, "ZapChannel");
  8393. if (ast_strlen_zero(channel)) {
  8394. astman_send_error(s, m, "No channel specified");
  8395. return 0;
  8396. }
  8397. p = find_channel(atoi(channel));
  8398. if (!p) {
  8399. astman_send_error(s, m, "No such channel");
  8400. return 0;
  8401. }
  8402. p->dnd = 0;
  8403. astman_send_ack(s, m, "DND Disabled");
  8404. return 0;
  8405. }
  8406. static int action_transfer(struct mansession *s, struct message *m)
  8407. {
  8408. struct zt_pvt *p = NULL;
  8409. char *channel = astman_get_header(m, "ZapChannel");
  8410. if (ast_strlen_zero(channel)) {
  8411. astman_send_error(s, m, "No channel specified");
  8412. return 0;
  8413. }
  8414. p = find_channel(atoi(channel));
  8415. if (!p) {
  8416. astman_send_error(s, m, "No such channel");
  8417. return 0;
  8418. }
  8419. zap_fake_event(p,TRANSFER);
  8420. astman_send_ack(s, m, "ZapTransfer");
  8421. return 0;
  8422. }
  8423. static int action_transferhangup(struct mansession *s, struct message *m)
  8424. {
  8425. struct zt_pvt *p = NULL;
  8426. char *channel = astman_get_header(m, "ZapChannel");
  8427. if (ast_strlen_zero(channel)) {
  8428. astman_send_error(s, m, "No channel specified");
  8429. return 0;
  8430. }
  8431. p = find_channel(atoi(channel));
  8432. if (!p) {
  8433. astman_send_error(s, m, "No such channel");
  8434. return 0;
  8435. }
  8436. zap_fake_event(p,HANGUP);
  8437. astman_send_ack(s, m, "ZapHangup");
  8438. return 0;
  8439. }
  8440. static int action_zapdialoffhook(struct mansession *s, struct message *m)
  8441. {
  8442. struct zt_pvt *p = NULL;
  8443. char *channel = astman_get_header(m, "ZapChannel");
  8444. char *number = astman_get_header(m, "Number");
  8445. int i;
  8446. if (ast_strlen_zero(channel)) {
  8447. astman_send_error(s, m, "No channel specified");
  8448. return 0;
  8449. }
  8450. if (ast_strlen_zero(number)) {
  8451. astman_send_error(s, m, "No number specified");
  8452. return 0;
  8453. }
  8454. p = find_channel(atoi(channel));
  8455. if (!p) {
  8456. astman_send_error(s, m, "No such channel");
  8457. return 0;
  8458. }
  8459. if (!p->owner) {
  8460. astman_send_error(s, m, "Channel does not have it's owner");
  8461. return 0;
  8462. }
  8463. for (i=0; i<strlen(number); i++) {
  8464. struct ast_frame f = { AST_FRAME_DTMF, number[i] };
  8465. zap_queue_frame(p, &f, NULL);
  8466. }
  8467. astman_send_ack(s, m, "ZapDialOffhook");
  8468. return 0;
  8469. }
  8470. static int action_zapshowchannels(struct mansession *s, struct message *m)
  8471. {
  8472. struct zt_pvt *tmp = NULL;
  8473. char *id = astman_get_header(m, "ActionID");
  8474. char idText[256] = "";
  8475. astman_send_ack(s, m, "Zapata channel status will follow");
  8476. if (id && !ast_strlen_zero(id))
  8477. snprintf(idText, sizeof(idText) - 1, "ActionID: %s\r\n", id);
  8478. ast_mutex_lock(&iflock);
  8479. tmp = iflist;
  8480. while (tmp) {
  8481. if (tmp->channel > 0) {
  8482. int alarm = get_alarms(tmp);
  8483. ast_mutex_lock(&s->lock);
  8484. ast_cli(s->fd,
  8485. "Event: ZapShowChannels\r\n"
  8486. "Channel: %d\r\n"
  8487. "Signalling: %s\r\n"
  8488. "Context: %s\r\n"
  8489. "Alarm: %s\r\n"
  8490. "%s"
  8491. "\r\n",
  8492. tmp->channel, sig2str(tmp->sig), tmp->context,
  8493. alarm2str(alarm), idText);
  8494. ast_mutex_unlock(&s->lock);
  8495. }
  8496. tmp = tmp->next;
  8497. }
  8498. ast_mutex_unlock(&iflock);
  8499. ast_mutex_lock(&s->lock);
  8500. ast_cli(s->fd,
  8501. "Event: ZapShowChannelsComplete\r\n"
  8502. "%s"
  8503. "\r\n",
  8504. idText);
  8505. ast_mutex_unlock(&s->lock);
  8506. return 0;
  8507. }
  8508. static int __unload_module(void)
  8509. {
  8510. int x = 0;
  8511. struct zt_pvt *p, *pl;
  8512. #ifdef ZAPATA_PRI
  8513. int i;
  8514. for(i=0;i<NUM_SPANS;i++) {
  8515. if (pris[i].master != AST_PTHREADT_NULL)
  8516. pthread_cancel(pris[i].master);
  8517. }
  8518. ast_cli_unregister(&pri_debug);
  8519. ast_cli_unregister(&pri_no_debug);
  8520. ast_cli_unregister(&pri_really_debug);
  8521. ast_cli_unregister(&pri_show_span);
  8522. #endif
  8523. #ifdef ZAPATA_R2
  8524. ast_cli_unregister(&r2_debug);
  8525. ast_cli_unregister(&r2_no_debug);
  8526. #endif
  8527. ast_cli_unregister(&cli_show_channels);
  8528. ast_cli_unregister(&cli_show_channel);
  8529. ast_cli_unregister(&cli_destroy_channel);
  8530. ast_cli_unregister(&zap_show_cadences_cli);
  8531. ast_manager_unregister( "ZapDialOffhook" );
  8532. ast_manager_unregister( "ZapHangup" );
  8533. ast_manager_unregister( "ZapTransfer" );
  8534. ast_manager_unregister( "ZapDNDoff" );
  8535. ast_manager_unregister( "ZapDNDon" );
  8536. ast_manager_unregister("ZapShowChannels");
  8537. ast_unregister_application(app_callingpres);
  8538. ast_channel_unregister(typecompat);
  8539. ast_channel_unregister(type);
  8540. if (!ast_mutex_lock(&iflock)) {
  8541. /* Hangup all interfaces if they have an owner */
  8542. p = iflist;
  8543. while(p) {
  8544. if (p->owner)
  8545. ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
  8546. p = p->next;
  8547. }
  8548. ast_mutex_unlock(&iflock);
  8549. } else {
  8550. ast_log(LOG_WARNING, "Unable to lock the monitor\n");
  8551. return -1;
  8552. }
  8553. if (!ast_mutex_lock(&monlock)) {
  8554. if (monitor_thread && (monitor_thread != AST_PTHREADT_STOP) && (monitor_thread != AST_PTHREADT_NULL)) {
  8555. pthread_cancel(monitor_thread);
  8556. pthread_kill(monitor_thread, SIGURG);
  8557. pthread_join(monitor_thread, NULL);
  8558. }
  8559. monitor_thread = AST_PTHREADT_STOP;
  8560. ast_mutex_unlock(&monlock);
  8561. } else {
  8562. ast_log(LOG_WARNING, "Unable to lock the monitor\n");
  8563. return -1;
  8564. }
  8565. if (!ast_mutex_lock(&iflock)) {
  8566. /* Destroy all the interfaces and free their memory */
  8567. p = iflist;
  8568. while(p) {
  8569. /* Free any callerid */
  8570. if (p->cidspill)
  8571. free(p->cidspill);
  8572. /* Close the zapata thingy */
  8573. if (p->subs[SUB_REAL].zfd > -1)
  8574. zt_close(p->subs[SUB_REAL].zfd);
  8575. pl = p;
  8576. p = p->next;
  8577. x++;
  8578. /* Free associated memory */
  8579. if(pl)
  8580. destroy_zt_pvt(&pl);
  8581. ast_verbose(VERBOSE_PREFIX_3 "Unregistered channel %d\n", x);
  8582. }
  8583. iflist = NULL;
  8584. ifcount = 0;
  8585. ast_mutex_unlock(&iflock);
  8586. } else {
  8587. ast_log(LOG_WARNING, "Unable to lock the monitor\n");
  8588. return -1;
  8589. }
  8590. #ifdef ZAPATA_PRI
  8591. for(i=0;i<NUM_SPANS;i++) {
  8592. if (pris[i].master && (pris[i].master != AST_PTHREADT_NULL))
  8593. pthread_join(pris[i].master, NULL);
  8594. zt_close(pris[i].fds[i]);
  8595. }
  8596. #endif
  8597. return 0;
  8598. }
  8599. int unload_module()
  8600. {
  8601. #ifdef ZAPATA_PRI
  8602. int y;
  8603. for (y=0;y<NUM_SPANS;y++)
  8604. ast_mutex_destroy(&pris[y].lock);
  8605. #endif
  8606. return __unload_module();
  8607. }
  8608. static int setup_zap(void)
  8609. {
  8610. struct ast_config *cfg;
  8611. struct ast_variable *v;
  8612. struct zt_pvt *tmp;
  8613. char *chan;
  8614. char *c;
  8615. char *ringc;
  8616. int start, finish,x;
  8617. int y;
  8618. int found_pseudo = 0;
  8619. int cur_radio = 0;
  8620. #ifdef ZAPATA_PRI
  8621. int spanno;
  8622. int i;
  8623. int logicalspan;
  8624. int trunkgroup;
  8625. int dchannels[NUM_DCHANS];
  8626. struct zt_pri *pri;
  8627. #endif
  8628. cfg = ast_load(config);
  8629. /* We *must* have a config file otherwise stop immediately */
  8630. if (!cfg) {
  8631. ast_log(LOG_ERROR, "Unable to load config %s\n", config);
  8632. return -1;
  8633. }
  8634. if (ast_mutex_lock(&iflock)) {
  8635. /* It's a little silly to lock it, but we mind as well just to be sure */
  8636. ast_log(LOG_ERROR, "Unable to lock interface list???\n");
  8637. return -1;
  8638. }
  8639. #ifdef ZAPATA_PRI
  8640. /* Process trunkgroups first */
  8641. v = ast_variable_browse(cfg, "trunkgroups");
  8642. while(v) {
  8643. if (!strcasecmp(v->name, "trunkgroup")) {
  8644. trunkgroup = atoi(v->value);
  8645. if (trunkgroup > 0) {
  8646. if ((c = strchr(v->value, ','))) {
  8647. i = 0;
  8648. memset(dchannels, 0, sizeof(dchannels));
  8649. while(c && (i < NUM_DCHANS)) {
  8650. dchannels[i] = atoi(c + 1);
  8651. if (dchannels[i] < 0) {
  8652. ast_log(LOG_WARNING, "D-channel for trunk group %d must be a postiive number at line %d of zapata.conf\n", trunkgroup, v->lineno);
  8653. } else
  8654. i++;
  8655. c = strchr(c + 1, ',');
  8656. }
  8657. if (i) {
  8658. if (pri_create_trunkgroup(trunkgroup, dchannels)) {
  8659. ast_log(LOG_WARNING, "Unable to create trunk group %d with Primary D-channel %d at line %d of zapata.conf\n", trunkgroup, dchannels[0], v->lineno);
  8660. } else if (option_verbose > 1)
  8661. ast_verbose(VERBOSE_PREFIX_2 "Created trunk group %d with Primary D-channel %d and %d backup%s\n", trunkgroup, dchannels[0], i - 1, (i == 1) ? "" : "s");
  8662. } else
  8663. ast_log(LOG_WARNING, "Trunk group %d lacks any valid D-channels at line %d of zapata.conf\n", trunkgroup, v->lineno);
  8664. } else
  8665. ast_log(LOG_WARNING, "Trunk group %d lacks a primary D-channel at line %d of zapata.conf\n", trunkgroup, v->lineno);
  8666. } else
  8667. ast_log(LOG_WARNING, "Trunk group identifier must be a positive integer at line %d of zapata.conf\n", v->lineno);
  8668. } else if (!strcasecmp(v->name, "spanmap")) {
  8669. spanno = atoi(v->value);
  8670. if (spanno > 0) {
  8671. if ((c = strchr(v->value, ','))) {
  8672. trunkgroup = atoi(c + 1);
  8673. if (trunkgroup > 0) {
  8674. if ((c = strchr(c + 1, ',')))
  8675. logicalspan = atoi(c + 1);
  8676. else
  8677. logicalspan = 0;
  8678. if (logicalspan >= 0) {
  8679. if (pri_create_spanmap(spanno - 1, trunkgroup, logicalspan)) {
  8680. ast_log(LOG_WARNING, "Failed to map span %d to trunk group %d (logical span %d)\n", spanno, trunkgroup, logicalspan);
  8681. } else if (option_verbose > 1)
  8682. ast_verbose(VERBOSE_PREFIX_2 "Mapped span %d to trunk group %d (logical span %d)\n", spanno, trunkgroup, logicalspan);
  8683. } else
  8684. ast_log(LOG_WARNING, "Logical span must be a postive number, or '0' (for unspecified) at line %d of zapata.conf\n", v->lineno);
  8685. } else
  8686. ast_log(LOG_WARNING, "Trunk group must be a postive number at line %d of zapata.conf\n", v->lineno);
  8687. } else
  8688. ast_log(LOG_WARNING, "Missing trunk group for span map at line %d of zapata.conf\n", v->lineno);
  8689. } else
  8690. ast_log(LOG_WARNING, "Span number must be a postive integer at line %d of zapata.conf\n", v->lineno);
  8691. } else {
  8692. ast_log(LOG_NOTICE, "Ignoring unknown keyword '%s' in trunkgroups\n", v->name);
  8693. }
  8694. v = v->next;
  8695. }
  8696. #endif
  8697. v = ast_variable_browse(cfg, "channels");
  8698. while(v) {
  8699. /* Create the interface list */
  8700. if (!strcasecmp(v->name, "channel")
  8701. #ifdef ZAPATA_PRI
  8702. || !strcasecmp(v->name, "crv")
  8703. #endif
  8704. ) {
  8705. if (cur_signalling < 0) {
  8706. ast_log(LOG_ERROR, "Signalling must be specified before any channels are.\n");
  8707. ast_destroy(cfg);
  8708. ast_mutex_unlock(&iflock);
  8709. return -1;
  8710. }
  8711. c = v->value;
  8712. #ifdef ZAPATA_PRI
  8713. pri = NULL;
  8714. if (!strcasecmp(v->name, "crv")) {
  8715. if (sscanf(c, "%d:%n", &trunkgroup, &y) != 1) {
  8716. ast_log(LOG_WARNING, "CRV must begin with trunkgroup followed by a colon at line %d\n", v->lineno);
  8717. ast_destroy(cfg);
  8718. ast_mutex_unlock(&iflock);
  8719. return -1;
  8720. }
  8721. if (trunkgroup < 1) {
  8722. ast_log(LOG_WARNING, "CRV trunk group must be a postive number at line %d\n", v->lineno);
  8723. ast_destroy(cfg);
  8724. ast_mutex_unlock(&iflock);
  8725. return -1;
  8726. }
  8727. c+=y;
  8728. for (y=0;y<NUM_SPANS;y++) {
  8729. if (pris[y].trunkgroup == trunkgroup) {
  8730. pri = pris + y;
  8731. break;
  8732. }
  8733. }
  8734. if (!pri) {
  8735. ast_log(LOG_WARNING, "No such trunk group %d at CRV declaration at line %d\n", trunkgroup, v->lineno);
  8736. ast_destroy(cfg);
  8737. ast_mutex_unlock(&iflock);
  8738. return -1;
  8739. }
  8740. }
  8741. #endif
  8742. chan = strsep(&c, ",");
  8743. while(chan) {
  8744. if (sscanf(chan, "%d-%d", &start, &finish) == 2) {
  8745. /* Range */
  8746. } else if (sscanf(chan, "%d", &start)) {
  8747. /* Just one */
  8748. finish = start;
  8749. } else if (!strcasecmp(chan, "pseudo")) {
  8750. finish = start = CHAN_PSEUDO;
  8751. found_pseudo = 1;
  8752. } else {
  8753. ast_log(LOG_ERROR, "Syntax error parsing '%s' at '%s'\n", v->value, chan);
  8754. ast_destroy(cfg);
  8755. ast_mutex_unlock(&iflock);
  8756. return -1;
  8757. }
  8758. if (finish < start) {
  8759. ast_log(LOG_WARNING, "Sillyness: %d < %d\n", start, finish);
  8760. x = finish;
  8761. finish = start;
  8762. start = x;
  8763. }
  8764. for (x=start;x<=finish;x++) {
  8765. #ifdef ZAPATA_PRI
  8766. tmp = mkintf(x, cur_signalling, cur_radio, pri);
  8767. #else
  8768. tmp = mkintf(x, cur_signalling, cur_radio, NULL);
  8769. #endif
  8770. if (tmp) {
  8771. if (option_verbose > 2) {
  8772. #ifdef ZAPATA_PRI
  8773. if (pri)
  8774. ast_verbose(VERBOSE_PREFIX_3 "Registered CRV %d:%d, %s signalling\n", trunkgroup,x, sig2str(tmp->sig));
  8775. else
  8776. ast_verbose(VERBOSE_PREFIX_3 "Registered channel %d, %s signalling\n", x, sig2str(tmp->sig));
  8777. #else
  8778. ast_verbose(VERBOSE_PREFIX_3 "Registered channel %d, %s signalling\n", x, sig2str(tmp->sig));
  8779. #endif
  8780. }
  8781. } else {
  8782. ast_log(LOG_ERROR, "Unable to register channel '%s'\n", v->value);
  8783. ast_destroy(cfg);
  8784. ast_mutex_unlock(&iflock);
  8785. return -1;
  8786. }
  8787. }
  8788. chan = strsep(&c, ",");
  8789. }
  8790. } else if (!strcasecmp(v->name, "usedistinctiveringdetection")) {
  8791. if (ast_true(v->value))
  8792. usedistinctiveringdetection = 1;
  8793. } else if (!strcasecmp(v->name, "dring1context")) {
  8794. strncpy(drings.ringContext[0].contextData,v->value,sizeof(drings.ringContext[0].contextData)-1);
  8795. } else if (!strcasecmp(v->name, "dring2context")) {
  8796. strncpy(drings.ringContext[1].contextData,v->value,sizeof(drings.ringContext[1].contextData)-1);
  8797. } else if (!strcasecmp(v->name, "dring3context")) {
  8798. strncpy(drings.ringContext[2].contextData,v->value,sizeof(drings.ringContext[2].contextData)-1);
  8799. } else if (!strcasecmp(v->name, "dring1")) {
  8800. ringc = v->value;
  8801. sscanf(ringc, "%d,%d,%d", &drings.ringnum[0].ring[0], &drings.ringnum[0].ring[1], &drings.ringnum[0].ring[2]);
  8802. } else if (!strcasecmp(v->name, "dring2")) {
  8803. ringc = v->value;
  8804. sscanf(ringc,"%d,%d,%d", &drings.ringnum[1].ring[0], &drings.ringnum[1].ring[1], &drings.ringnum[1].ring[2]);
  8805. } else if (!strcasecmp(v->name, "dring3")) {
  8806. ringc = v->value;
  8807. sscanf(ringc, "%d,%d,%d", &drings.ringnum[2].ring[0], &drings.ringnum[2].ring[1], &drings.ringnum[2].ring[2]);
  8808. } else if (!strcasecmp(v->name, "usecallerid")) {
  8809. use_callerid = ast_true(v->value);
  8810. } else if (!strcasecmp(v->name, "cidsignalling")) {
  8811. if (!strcasecmp(v->value, "bell"))
  8812. cid_signalling = CID_SIG_BELL;
  8813. else if (!strcasecmp(v->value, "v23"))
  8814. cid_signalling = CID_SIG_V23;
  8815. else if (!strcasecmp(v->value, "dtmf"))
  8816. cid_signalling = CID_SIG_DTMF;
  8817. else if (ast_true(v->value))
  8818. cid_signalling = CID_SIG_BELL;
  8819. } else if (!strcasecmp(v->name, "cidstart")) {
  8820. if (!strcasecmp(v->value, "ring"))
  8821. cid_start = CID_START_RING;
  8822. else if (!strcasecmp(v->value, "polarity"))
  8823. cid_start = CID_START_POLARITY;
  8824. else if (ast_true(v->value))
  8825. cid_start = CID_START_RING;
  8826. } else if (!strcasecmp(v->name, "threewaycalling")) {
  8827. threewaycalling = ast_true(v->value);
  8828. } else if (!strcasecmp(v->name, "cancallforward")) {
  8829. cancallforward = ast_true(v->value);
  8830. } else if (!strcasecmp(v->name, "relaxdtmf")) {
  8831. if (ast_true(v->value))
  8832. relaxdtmf = DSP_DIGITMODE_RELAXDTMF;
  8833. else
  8834. relaxdtmf = 0;
  8835. } else if (!strcasecmp(v->name, "mailbox")) {
  8836. strncpy(mailbox, v->value, sizeof(mailbox) -1);
  8837. } else if (!strcasecmp(v->name, "adsi")) {
  8838. adsi = ast_true(v->value);
  8839. } else if (!strcasecmp(v->name, "transfer")) {
  8840. transfer = ast_true(v->value);
  8841. } else if (!strcasecmp(v->name, "echocancelwhenbridged")) {
  8842. echocanbridged = ast_true(v->value);
  8843. } else if (!strcasecmp(v->name, "busydetect")) {
  8844. busydetect = ast_true(v->value);
  8845. } else if (!strcasecmp(v->name, "busycount")) {
  8846. busycount = atoi(v->value);
  8847. } else if (!strcasecmp(v->name, "callprogress")) {
  8848. if (ast_true(v->value))
  8849. callprogress |= 1;
  8850. else
  8851. callprogress &= ~1;
  8852. } else if (!strcasecmp(v->name, "faxdetect")) {
  8853. if (!strcasecmp(v->value, "incoming")) {
  8854. callprogress |= 4;
  8855. callprogress &= ~2;
  8856. } else if (!strcasecmp(v->value, "outgoing")) {
  8857. callprogress &= ~4;
  8858. callprogress |= 2;
  8859. } else if (!strcasecmp(v->value, "both") || ast_true(v->value))
  8860. callprogress |= 6;
  8861. else
  8862. callprogress &= ~6;
  8863. } else if (!strcasecmp(v->name, "echocancel")) {
  8864. if (v->value && !ast_strlen_zero(v->value)) {
  8865. y = atoi(v->value);
  8866. } else
  8867. y = 0;
  8868. if ((y == 32) || (y == 64) || (y == 128) || (y == 256))
  8869. echocancel = y;
  8870. else {
  8871. echocancel = ast_true(v->value);
  8872. if (echocancel)
  8873. echocancel=128;
  8874. }
  8875. } else if (!strcasecmp(v->name, "echotraining")) {
  8876. if (sscanf(v->value, "%i", &y) == 1) {
  8877. if ((y < 10) || (y > 4000)) {
  8878. ast_log(LOG_WARNING, "Echo training time must be within the range of 10 to 2000 ms at line %d\n", v->lineno);
  8879. } else {
  8880. echotraining = y;
  8881. }
  8882. } else if (ast_true(v->value)) {
  8883. echotraining = 400;
  8884. } else
  8885. echotraining = 0;
  8886. } else if (!strcasecmp(v->name, "hidecallerid")) {
  8887. hidecallerid = ast_true(v->value);
  8888. } else if (!strcasecmp(v->name, "pulsedial")) {
  8889. pulse = ast_true(v->value);
  8890. } else if (!strcasecmp(v->name, "callreturn")) {
  8891. callreturn = ast_true(v->value);
  8892. } else if (!strcasecmp(v->name, "callwaiting")) {
  8893. callwaiting = ast_true(v->value);
  8894. } else if (!strcasecmp(v->name, "callwaitingcallerid")) {
  8895. callwaitingcallerid = ast_true(v->value);
  8896. } else if (!strcasecmp(v->name, "context")) {
  8897. strncpy(context, v->value, sizeof(context)-1);
  8898. } else if (!strcasecmp(v->name, "language")) {
  8899. strncpy(language, v->value, sizeof(language)-1);
  8900. } else if (!strcasecmp(v->name, "progzone")) {
  8901. strncpy(progzone, v->value, sizeof(progzone)-1);
  8902. } else if (!strcasecmp(v->name, "musiconhold")) {
  8903. strncpy(musicclass, v->value, sizeof(musicclass)-1);
  8904. } else if (!strcasecmp(v->name, "stripmsd")) {
  8905. stripmsd = atoi(v->value);
  8906. } else if (!strcasecmp(v->name, "jitterbuffers")) {
  8907. numbufs = atoi(v->value);
  8908. } else if (!strcasecmp(v->name, "group")) {
  8909. cur_group = ast_get_group(v->value);
  8910. } else if (!strcasecmp(v->name, "callgroup")) {
  8911. cur_callergroup = ast_get_group(v->value);
  8912. } else if (!strcasecmp(v->name, "pickupgroup")) {
  8913. cur_pickupgroup = ast_get_group(v->value);
  8914. } else if (!strcasecmp(v->name, "immediate")) {
  8915. immediate = ast_true(v->value);
  8916. } else if (!strcasecmp(v->name, "rxgain")) {
  8917. if (sscanf(v->value, "%f", &rxgain) != 1) {
  8918. ast_log(LOG_WARNING, "Invalid rxgain: %s\n", v->value);
  8919. }
  8920. } else if (!strcasecmp(v->name, "txgain")) {
  8921. if (sscanf(v->value, "%f", &txgain) != 1) {
  8922. ast_log(LOG_WARNING, "Invalid txgain: %s\n", v->value);
  8923. }
  8924. } else if (!strcasecmp(v->name, "tonezone")) {
  8925. if (sscanf(v->value, "%d", &tonezone) != 1) {
  8926. ast_log(LOG_WARNING, "Invalid tonezone: %s\n", v->value);
  8927. }
  8928. } else if (!strcasecmp(v->name, "callerid")) {
  8929. if (!strcasecmp(v->value, "asreceived"))
  8930. callerid[0] = '\0';
  8931. else
  8932. strncpy(callerid, v->value, sizeof(callerid)-1);
  8933. } else if (!strcasecmp(v->name, "useincomingcalleridonzaptransfer")) {
  8934. zaptrcallerid = ast_true(v->value);
  8935. } else if (!strcasecmp(v->name, "restrictcid")) {
  8936. restrictcid = ast_true(v->value);
  8937. } else if (!strcasecmp(v->name, "usecallingpres")) {
  8938. use_callingpres = ast_true(v->value);
  8939. } else if (!strcasecmp(v->name, "accountcode")) {
  8940. strncpy(accountcode, v->value, sizeof(accountcode)-1);
  8941. } else if (!strcasecmp(v->name, "amaflags")) {
  8942. y = ast_cdr_amaflags2int(v->value);
  8943. if (y < 0)
  8944. ast_log(LOG_WARNING, "Invalid AMA flags: %s at line %d\n", v->value, v->lineno);
  8945. else
  8946. amaflags = y;
  8947. } else if (!strcasecmp(v->name, "signalling")) {
  8948. if (!strcasecmp(v->value, "em")) {
  8949. cur_signalling = SIG_EM;
  8950. } else if (!strcasecmp(v->value, "em_e1")) {
  8951. cur_signalling = SIG_EM_E1;
  8952. } else if (!strcasecmp(v->value, "em_w")) {
  8953. cur_signalling = SIG_EMWINK;
  8954. cur_radio = 0;
  8955. } else if (!strcasecmp(v->value, "fxs_ls")) {
  8956. cur_signalling = SIG_FXSLS;
  8957. cur_radio = 0;
  8958. } else if (!strcasecmp(v->value, "fxs_gs")) {
  8959. cur_signalling = SIG_FXSGS;
  8960. cur_radio = 0;
  8961. } else if (!strcasecmp(v->value, "fxs_ks")) {
  8962. cur_signalling = SIG_FXSKS;
  8963. cur_radio = 0;
  8964. } else if (!strcasecmp(v->value, "fxo_ls")) {
  8965. cur_signalling = SIG_FXOLS;
  8966. cur_radio = 0;
  8967. } else if (!strcasecmp(v->value, "fxo_gs")) {
  8968. cur_signalling = SIG_FXOGS;
  8969. cur_radio = 0;
  8970. } else if (!strcasecmp(v->value, "fxo_ks")) {
  8971. cur_signalling = SIG_FXOKS;
  8972. cur_radio = 0;
  8973. } else if (!strcasecmp(v->value, "fxs_rx")) {
  8974. cur_signalling = SIG_FXSKS;
  8975. cur_radio = 1;
  8976. } else if (!strcasecmp(v->value, "fxo_rx")) {
  8977. cur_signalling = SIG_FXOLS;
  8978. cur_radio = 1;
  8979. } else if (!strcasecmp(v->value, "fxs_tx")) {
  8980. cur_signalling = SIG_FXSLS;
  8981. cur_radio = 1;
  8982. } else if (!strcasecmp(v->value, "fxo_tx")) {
  8983. cur_signalling = SIG_FXOGS;
  8984. cur_radio = 1;
  8985. } else if (!strcasecmp(v->value, "em_rx")) {
  8986. cur_signalling = SIG_EM;
  8987. cur_radio = 1;
  8988. } else if (!strcasecmp(v->value, "em_tx")) {
  8989. cur_signalling = SIG_EM;
  8990. cur_radio = 1;
  8991. } else if (!strcasecmp(v->value, "em_rxtx")) {
  8992. cur_signalling = SIG_EM;
  8993. cur_radio = 2;
  8994. } else if (!strcasecmp(v->value, "em_txrx")) {
  8995. cur_signalling = SIG_EM;
  8996. cur_radio = 2;
  8997. } else if (!strcasecmp(v->value, "sf")) {
  8998. cur_signalling = SIG_SF;
  8999. cur_radio = 0;
  9000. } else if (!strcasecmp(v->value, "sf_w")) {
  9001. cur_signalling = SIG_SFWINK;
  9002. cur_radio = 0;
  9003. } else if (!strcasecmp(v->value, "sf_featd")) {
  9004. cur_signalling = SIG_FEATD;
  9005. cur_radio = 0;
  9006. } else if (!strcasecmp(v->value, "sf_featdmf")) {
  9007. cur_signalling = SIG_FEATDMF;
  9008. cur_radio = 0;
  9009. } else if (!strcasecmp(v->value, "sf_featb")) {
  9010. cur_signalling = SIG_SF_FEATB;
  9011. cur_radio = 0;
  9012. } else if (!strcasecmp(v->value, "sf")) {
  9013. cur_signalling = SIG_SF;
  9014. cur_radio = 0;
  9015. } else if (!strcasecmp(v->value, "sf_rx")) {
  9016. cur_signalling = SIG_SF;
  9017. cur_radio = 1;
  9018. } else if (!strcasecmp(v->value, "sf_tx")) {
  9019. cur_signalling = SIG_SF;
  9020. cur_radio = 1;
  9021. } else if (!strcasecmp(v->value, "sf_rxtx")) {
  9022. cur_signalling = SIG_SF;
  9023. cur_radio = 2;
  9024. } else if (!strcasecmp(v->value, "sf_txrx")) {
  9025. cur_signalling = SIG_SF;
  9026. cur_radio = 2;
  9027. } else if (!strcasecmp(v->value, "featd")) {
  9028. cur_signalling = SIG_FEATD;
  9029. cur_radio = 0;
  9030. } else if (!strcasecmp(v->value, "featdmf")) {
  9031. cur_signalling = SIG_FEATDMF;
  9032. cur_radio = 0;
  9033. } else if (!strcasecmp(v->value, "e911")) {
  9034. cur_signalling = SIG_E911;
  9035. cur_radio = 0;
  9036. } else if (!strcasecmp(v->value, "featb")) {
  9037. cur_signalling = SIG_FEATB;
  9038. cur_radio = 0;
  9039. #ifdef ZAPATA_PRI
  9040. } else if (!strcasecmp(v->value, "pri_net")) {
  9041. cur_radio = 0;
  9042. cur_signalling = SIG_PRI;
  9043. pritype = PRI_NETWORK;
  9044. } else if (!strcasecmp(v->value, "pri_cpe")) {
  9045. cur_signalling = SIG_PRI;
  9046. cur_radio = 0;
  9047. pritype = PRI_CPE;
  9048. } else if (!strcasecmp(v->value, "gr303fxoks_net")) {
  9049. cur_signalling = SIG_GR303FXOKS;
  9050. cur_radio = 0;
  9051. pritype = PRI_NETWORK;
  9052. } else if (!strcasecmp(v->value, "gr303fxsks_cpe")) {
  9053. cur_signalling = SIG_GR303FXSKS;
  9054. cur_radio = 0;
  9055. pritype = PRI_CPE;
  9056. #endif
  9057. #ifdef ZAPATA_R2
  9058. } else if (!strcasecmp(v->value, "r2")) {
  9059. cur_signalling = SIG_R2;
  9060. cur_radio = 0;
  9061. #endif
  9062. } else {
  9063. ast_log(LOG_ERROR, "Unknown signalling method '%s'\n", v->value);
  9064. }
  9065. #ifdef ZAPATA_R2
  9066. } else if (!strcasecmp(v->name, "r2country")) {
  9067. r2prot = str2r2prot(v->value);
  9068. if (r2prot < 0) {
  9069. ast_log(LOG_WARNING, "Unknown R2 Country '%s' at line %d.\n", v->value, v->lineno);
  9070. }
  9071. #endif
  9072. #ifdef ZAPATA_PRI
  9073. } else if (!strcasecmp(v->name, "pridialplan")) {
  9074. if (!strcasecmp(v->value, "national")) {
  9075. dialplan = PRI_NATIONAL_ISDN + 1;
  9076. } else if (!strcasecmp(v->value, "unknown")) {
  9077. dialplan = PRI_UNKNOWN + 1;
  9078. } else if (!strcasecmp(v->value, "private")) {
  9079. dialplan = PRI_PRIVATE + 1;
  9080. } else if (!strcasecmp(v->value, "international")) {
  9081. dialplan = PRI_INTERNATIONAL_ISDN + 1;
  9082. } else if (!strcasecmp(v->value, "local")) {
  9083. dialplan = PRI_LOCAL_ISDN + 1;
  9084. } else {
  9085. ast_log(LOG_WARNING, "Unknown PRI dialplan '%s' at line %d.\n", v->value, v->lineno);
  9086. }
  9087. } else if (!strcasecmp(v->name, "prilocaldialplan")) {
  9088. if (!strcasecmp(v->value, "national")) {
  9089. localdialplan = PRI_NATIONAL_ISDN + 1;
  9090. } else if (!strcasecmp(v->value, "unknown")) {
  9091. localdialplan = PRI_UNKNOWN + 1;
  9092. } else if (!strcasecmp(v->value, "private")) {
  9093. localdialplan = PRI_PRIVATE + 1;
  9094. } else if (!strcasecmp(v->value, "international")) {
  9095. localdialplan = PRI_INTERNATIONAL_ISDN + 1;
  9096. } else if (!strcasecmp(v->value, "local")) {
  9097. localdialplan = PRI_LOCAL_ISDN + 1;
  9098. } else {
  9099. ast_log(LOG_WARNING, "Unknown PRI dialplan '%s' at line %d.\n", v->value, v->lineno);
  9100. }
  9101. } else if (!strcasecmp(v->name, "switchtype")) {
  9102. if (!strcasecmp(v->value, "national"))
  9103. switchtype = PRI_SWITCH_NI2;
  9104. else if (!strcasecmp(v->value, "ni1"))
  9105. switchtype = PRI_SWITCH_NI1;
  9106. else if (!strcasecmp(v->value, "dms100"))
  9107. switchtype = PRI_SWITCH_DMS100;
  9108. else if (!strcasecmp(v->value, "4ess"))
  9109. switchtype = PRI_SWITCH_ATT4ESS;
  9110. else if (!strcasecmp(v->value, "5ess"))
  9111. switchtype = PRI_SWITCH_LUCENT5E;
  9112. else if (!strcasecmp(v->value, "euroisdn"))
  9113. switchtype = PRI_SWITCH_EUROISDN_E1;
  9114. else {
  9115. ast_log(LOG_ERROR, "Unknown switchtype '%s'\n", v->value);
  9116. ast_destroy(cfg);
  9117. ast_mutex_unlock(&iflock);
  9118. return -1;
  9119. }
  9120. } else if (!strcasecmp(v->name, "nsf")) {
  9121. if (!strcasecmp(v->value, "sdn"))
  9122. nsf = PRI_NSF_SDN;
  9123. else if (!strcasecmp(v->value, "megacom"))
  9124. nsf = PRI_NSF_MEGACOM;
  9125. else if (!strcasecmp(v->value, "accunet"))
  9126. nsf = PRI_NSF_ACCUNET;
  9127. else if (!strcasecmp(v->value, "none"))
  9128. nsf = PRI_NSF_NONE;
  9129. else {
  9130. ast_log(LOG_WARNING, "Unknown network-specific facility '%s'\n", v->value);
  9131. nsf = PRI_NSF_NONE;
  9132. }
  9133. } else if (!strcasecmp(v->name, "priindication")) {
  9134. if (!strcasecmp(v->value, "outofband"))
  9135. priindication_oob = 1;
  9136. else if (!strcasecmp(v->value, "inband"))
  9137. priindication_oob = 0;
  9138. else
  9139. ast_log(LOG_WARNING, "'%s' is not a valid pri indication value, should be 'inband' or 'outofband' at line %d\n",
  9140. v->value, v->lineno);
  9141. } else if (!strcasecmp(v->name, "minunused")) {
  9142. minunused = atoi(v->value);
  9143. } else if (!strcasecmp(v->name, "idleext")) {
  9144. strncpy(idleext, v->value, sizeof(idleext) - 1);
  9145. } else if (!strcasecmp(v->name, "idledial")) {
  9146. strncpy(idledial, v->value, sizeof(idledial) - 1);
  9147. } else if (!strcasecmp(v->name, "overlapdial")) {
  9148. overlapdial = ast_true(v->value);
  9149. #endif
  9150. } else if (!strcasecmp(v->name, "cadence")) {
  9151. /* setup to scan our argument */
  9152. int element_count, c[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  9153. int i;
  9154. struct zt_ring_cadence new_cadence;
  9155. int cid_location = -1;
  9156. char original_args[80];
  9157. int cadence_is_ok = 1;
  9158. strncpy(original_args, v->value, sizeof(original_args) - 1);
  9159. /* 16 cadences allowed (8 pairs) */
  9160. element_count = sscanf(v->value, "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d", &c[0], &c[1], &c[2], &c[3], &c[4], &c[5], &c[6], &c[7], &c[8], &c[9], &c[10], &c[11], &c[12], &c[13], &c[14], &c[15]);
  9161. /* Cadence must be even (on/off) */
  9162. if (element_count % 2 == 1) {
  9163. ast_log(LOG_ERROR, "Must be a silence duration for each ring duration: %s\n",original_args);
  9164. cadence_is_ok = 0;
  9165. }
  9166. /* Ring cadences cannot be negative */
  9167. for (i=0;i<element_count;i++) {
  9168. if (c[i] < 1) {
  9169. if ((i % 2 == 1) && (cid_location == -1)) {
  9170. /* Silence duration, negative possibly okay */
  9171. if (c[i] == 0) {
  9172. ast_log(LOG_ERROR, "Silence duration cannot be zero: %s\n", original_args);
  9173. cadence_is_ok = 0;
  9174. } else {
  9175. cid_location = i;
  9176. c[i] *= -1;
  9177. }
  9178. } else if (cid_location) {
  9179. ast_log(LOG_ERROR, "CID location specified twice: %s\n",original_args);
  9180. cadence_is_ok = 0;
  9181. } else {
  9182. ast_log(LOG_ERROR, "Negative or zero ring duration: %s\n",original_args);
  9183. cadence_is_ok = 0;
  9184. break;
  9185. }
  9186. }
  9187. }
  9188. /* Substitute our scanned cadence */
  9189. for (i=0;i<16;i++) {
  9190. new_cadence.ringcadence[i] = c[i];
  9191. }
  9192. if (cadence_is_ok) {
  9193. /* ---we scanned it without getting annoyed; now some sanity checks--- */
  9194. if (element_count < 2) {
  9195. ast_log(LOG_ERROR, "Minimum cadence is ring,pause: %s\n", original_args);
  9196. } else {
  9197. if (cid_location == -1) {
  9198. /* user didn't say; default to first pause */
  9199. cid_location = 1;
  9200. } else {
  9201. /* convert element_index to cidrings value */
  9202. cid_location = (cid_location + 1) / 2;
  9203. }
  9204. /* ---we like their cadence; try to install it--- */
  9205. if (!user_has_defined_cadences++)
  9206. /* this is the first user-defined cadence; clear the default user cadences */
  9207. num_cadence = 0;
  9208. if ((num_cadence+1) >= NUM_CADENCE_MAX)
  9209. ast_log(LOG_ERROR, "Already %d cadences; can't add another: %s\n", NUM_CADENCE_MAX, original_args);
  9210. else {
  9211. cadences[num_cadence] = new_cadence;
  9212. cidrings[num_cadence++] = cid_location;
  9213. if (option_verbose > 2)
  9214. ast_verbose(VERBOSE_PREFIX_3 "cadence 'r%d' added: %s\n",num_cadence,original_args);
  9215. }
  9216. }
  9217. }
  9218. } else if (!strcasecmp(v->name, "prewink")) {
  9219. cur_prewink = atoi(v->value);
  9220. } else if (!strcasecmp(v->name, "preflash")) {
  9221. cur_preflash = atoi(v->value);
  9222. } else if (!strcasecmp(v->name, "wink")) {
  9223. cur_wink = atoi(v->value);
  9224. } else if (!strcasecmp(v->name, "flash")) {
  9225. cur_flash = atoi(v->value);
  9226. } else if (!strcasecmp(v->name, "start")) {
  9227. cur_start = atoi(v->value);
  9228. } else if (!strcasecmp(v->name, "rxwink")) {
  9229. cur_rxwink = atoi(v->value);
  9230. } else if (!strcasecmp(v->name, "rxflash")) {
  9231. cur_rxflash = atoi(v->value);
  9232. } else if (!strcasecmp(v->name, "debounce")) {
  9233. cur_debounce = atoi(v->value);
  9234. } else if (!strcasecmp(v->name, "sendcalleridafter")) {
  9235. sendcalleridafter = atoi(v->value);
  9236. } else
  9237. ast_log(LOG_WARNING, "Ignoring %s\n", v->name);
  9238. v = v->next;
  9239. }
  9240. if (!found_pseudo) {
  9241. /* Make sure pseudo isn't a member of any groups if
  9242. we're automatically making it. */
  9243. cur_group = 0;
  9244. cur_callergroup = 0;
  9245. cur_pickupgroup = 0;
  9246. tmp = mkintf(CHAN_PSEUDO, cur_signalling, cur_radio, NULL);
  9247. if (tmp) {
  9248. if (option_verbose > 2)
  9249. ast_verbose(VERBOSE_PREFIX_3 "Automatically generated pseudo channel\n");
  9250. } else {
  9251. ast_log(LOG_WARNING, "Unable to register pseudo channel!\n");
  9252. }
  9253. }
  9254. ast_mutex_unlock(&iflock);
  9255. ast_destroy(cfg);
  9256. #ifdef ZAPATA_PRI
  9257. for (x=0;x<NUM_SPANS;x++) {
  9258. if (pris[x].pvts[0]) {
  9259. if (start_pri(pris + x)) {
  9260. ast_log(LOG_ERROR, "Unable to start D-channel on span %d\n", x + 1);
  9261. return -1;
  9262. } else if (option_verbose > 1)
  9263. ast_verbose(VERBOSE_PREFIX_2 "Starting D-Channel on span %d\n", x + 1);
  9264. }
  9265. }
  9266. #endif
  9267. /* And start the monitor for the first time */
  9268. restart_monitor();
  9269. return 0;
  9270. }
  9271. int load_module(void)
  9272. {
  9273. int res;
  9274. #ifdef ZAPATA_PRI
  9275. int y,i;
  9276. memset(pris, 0, sizeof(pris));
  9277. for (y=0;y<NUM_SPANS;y++) {
  9278. ast_mutex_init(&pris[y].lock);
  9279. pris[y].offset = -1;
  9280. pris[y].master = AST_PTHREADT_NULL;
  9281. for (i=0;i<NUM_DCHANS;i++)
  9282. pris[y].fds[i] = -1;
  9283. }
  9284. pri_set_error(zt_pri_error);
  9285. pri_set_message(zt_pri_message);
  9286. #endif
  9287. res = setup_zap();
  9288. /* Make sure we can register our Zap channel type */
  9289. if(res) {
  9290. return -1;
  9291. }
  9292. if (ast_channel_register(type, tdesc, AST_FORMAT_SLINEAR | AST_FORMAT_ULAW, zt_request)) {
  9293. ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
  9294. __unload_module();
  9295. return -1;
  9296. }
  9297. if (ast_channel_register(typecompat, tdesc, AST_FORMAT_SLINEAR | AST_FORMAT_ULAW, zt_request)) {
  9298. ast_log(LOG_ERROR, "Unable to register channel class %s\n", typecompat);
  9299. __unload_module();
  9300. return -1;
  9301. }
  9302. #ifdef ZAPATA_PRI
  9303. ast_cli_register(&pri_debug);
  9304. ast_cli_register(&pri_no_debug);
  9305. ast_cli_register(&pri_really_debug);
  9306. ast_cli_register(&pri_show_span);
  9307. #endif
  9308. #ifdef ZAPATA_R2
  9309. ast_cli_register(&r2_debug);
  9310. ast_cli_register(&r2_no_debug);
  9311. #endif
  9312. ast_cli_register(&cli_show_channels);
  9313. ast_cli_register(&cli_show_channel);
  9314. ast_cli_register(&cli_destroy_channel);
  9315. ast_cli_register(&zap_show_cadences_cli);
  9316. ast_register_application(app_callingpres, change_callingpres, synopsis_callingpres, descrip_callingpres);
  9317. memset(round_robin, 0, sizeof(round_robin));
  9318. ast_manager_register( "ZapTransfer", 0, action_transfer, "Transfer Zap Channel" );
  9319. ast_manager_register( "ZapHangup", 0, action_transferhangup, "Hangup Zap Channel" );
  9320. ast_manager_register( "ZapDialOffhook", 0, action_zapdialoffhook, "Dial over Zap channel while offhook" );
  9321. ast_manager_register( "ZapDNDon", 0, action_zapdndon, "Toggle Zap channel Do Not Disturb status ON" );
  9322. ast_manager_register( "ZapDNDoff", 0, action_zapdndoff, "Toggle Zap channel Do Not Disturb status OFF" );
  9323. ast_manager_register("ZapShowChannels", 0, action_zapshowchannels, "Show status zapata channels");
  9324. return res;
  9325. }
  9326. #if 0
  9327. static int reload_zt(void)
  9328. {
  9329. struct ast_config *cfg;
  9330. struct ast_variable *v;
  9331. struct zt_pvt *tmp;
  9332. struct zt_pvt *prev = NULL;
  9333. char *chan;
  9334. char *ringc;
  9335. int start, finish,x;
  9336. char *stringp=NULL;
  9337. /* Some crap that needs to be reinitialized on the reload */
  9338. strncpy(context, "default", sizeof(context) - 1);
  9339. language[0] = '\0';
  9340. musicclass[0] = '\0';
  9341. use_callerid = 1;
  9342. cid_signalling = CID_SIG_BELL;
  9343. cid_start = CID_START_RING;
  9344. cur_signalling = -1;
  9345. cur_group = 0;
  9346. cur_callergroup = 0;
  9347. cur_pickupgroup = 0;
  9348. immediate = 0;
  9349. stripmsd = 0;
  9350. callwaiting = 0;
  9351. busydetect = 0;
  9352. busycount = 3;
  9353. callprogress = 0;
  9354. callwaitingcallerid = 0;
  9355. hidecallerid = 0;
  9356. callreturn = 0;
  9357. threewaycalling = 0;
  9358. transfer = 0;
  9359. rxgain = 0.0;
  9360. txgain = 0.0;
  9361. tonezone = -1;
  9362. firstdigittimeout = 16000;
  9363. gendigittimeout = 8000;
  9364. amaflags = 0;
  9365. adsi = 0;
  9366. memset(drings,0,sizeof(drings));
  9367. strncpy(accountcode, "", sizeof(accountcode)-1);
  9368. #ifdef ZAPATA_PRI
  9369. strncpy(idleext, "", sizeof(idleext) - 1);
  9370. strncpy(idledial, "", sizeof(idledial) - 1);
  9371. minunused = 2;
  9372. minidle = 0;
  9373. #endif
  9374. // usecnt = 0;
  9375. #if 0
  9376. #ifdef ZAPATA_PRI
  9377. int y;
  9378. #endif
  9379. #endif
  9380. #if 0
  9381. #ifdef ZAPATA_PRI
  9382. for (y=0;y<NUM_SPANS;y++)
  9383. ast_mutex_destroy(&pris[y]->lock);
  9384. memset(pris, 0, sizeof(pris));
  9385. for (y=0;y<NUM_SPANS;y++) {
  9386. ast_mutex_init(&pris[y]->lock);
  9387. pris[y].fd = -1;
  9388. }
  9389. #endif
  9390. #endif /* 0 */
  9391. cfg = ast_load(config);
  9392. /* We *must* have a config file otherwise stop immediately */
  9393. if (!cfg) {
  9394. ast_log(LOG_ERROR, "Unable to load config %s\n", config);
  9395. return -1;
  9396. }
  9397. if (ast_mutex_lock(&iflock)) {
  9398. /* It's a little silly to lock it, but we mind as well just to be sure */
  9399. ast_log(LOG_ERROR, "Unable to lock interface list???\n");
  9400. return -1;
  9401. }
  9402. /* Part of the primary changes for the reload... */
  9403. tmp = iflist;
  9404. while (tmp) {
  9405. tmp->destroy = 1;
  9406. tmp = tmp->next;
  9407. }
  9408. v = ast_variable_browse(cfg, "channels");
  9409. while(v) {
  9410. /* Create the interface list */
  9411. if (!strcasecmp(v->name, "channel")) {
  9412. if (cur_signalling < 0) {
  9413. ast_log(LOG_ERROR, "Signalling must be specified before any channels are.\n");
  9414. ast_destroy(cfg);
  9415. ast_mutex_unlock(&iflock);
  9416. return -1;
  9417. }
  9418. stringp=v->value;
  9419. chan = strsep(&stringp, ",");
  9420. while(chan) {
  9421. if (sscanf(chan, "%d-%d", &start, &finish) == 2) {
  9422. /* Range */
  9423. } else if (sscanf(chan, "%d", &start)) {
  9424. /* Just one */
  9425. finish = start;
  9426. } else {
  9427. ast_log(LOG_ERROR, "Syntax error parsing '%s' at '%s'\n", v->value, chan);
  9428. ast_destroy(cfg);
  9429. ast_mutex_unlock(&iflock);
  9430. return -1;
  9431. }
  9432. if (finish < start) {
  9433. ast_log(LOG_WARNING, "Sillyness: %d < %d\n", start, finish);
  9434. x = finish;
  9435. finish = start;
  9436. start = x;
  9437. }
  9438. for (x = start; x <= finish; x++) {
  9439. tmp = mkintf(x, cur_signalling);
  9440. if (tmp) {
  9441. if (option_verbose > 2)
  9442. ast_verbose(VERBOSE_PREFIX_3 "Registered channel %d, %s signalling\n", x, sig2str(tmp->sig));
  9443. } else {
  9444. ast_log(LOG_ERROR, "Unable to register channel '%s'\n", v->value);
  9445. ast_destroy(cfg);
  9446. ast_mutex_unlock(&iflock);
  9447. return -1;
  9448. }
  9449. }
  9450. chan = strsep(&stringp, ",");
  9451. }
  9452. } else if (!strcasecmp(v->name, "usedistinctiveringdetection")) {
  9453. if (ast_true(v->value))
  9454. usedistinctiveringdetection = 1;
  9455. } else if (!strcasecmp(v->name, "dring1context")) {
  9456. strncpy(drings.ringContext[0].contextData,v->value,sizeof(drings.ringContext[0].contextData)-1);
  9457. } else if (!strcasecmp(v->name, "dring2context")) {
  9458. strncpy(drings.ringContext[1].contextData,v->value,sizeof(drings.ringContext[1].contextData)-1);
  9459. } else if (!strcasecmp(v->name, "dring3context")) {
  9460. strncpy(drings.ringContext[2].contextData,v->value,sizeof(drings.ringContext[2].contextData)-1);
  9461. } else if (!strcasecmp(v->name, "dring1")) {
  9462. ringc = v->value;
  9463. sscanf(ringc, "%d,%d,%d", &drings.ringnum[0].ring[0], &drings.ringnum[0].ring[1], &drings.ringnum[0].ring[2]);
  9464. } else if (!strcasecmp(v->name, "dring2")) {
  9465. ringc = v->value;
  9466. sscanf(ringc,"%d,%d,%d", &drings.ringnum[1].ring[0], &drings.ringnum[1].ring[1], &drings.ringnum[1].ring[2]);
  9467. } else if (!strcasecmp(v->name, "dring3")) {
  9468. ringc = v->value;
  9469. sscanf(ringc, "%d,%d,%d", &drings.ringnum[2].ring[0], &drings.ringnum[2].ring[1], &drings.ringnum[2].ring[2]);
  9470. } else if (!strcasecmp(v->name, "usecallerid")) {
  9471. use_callerid = ast_true(v->value);
  9472. } else if (!strcasecmp(v->name, "cidsignalling")) {
  9473. if (!strcasecmp(v->value, "bell"))
  9474. cid_signalling = CID_SIG_BELL;
  9475. else if (!strcasecmp(v->value, "v23"))
  9476. cid_signalling = CID_SIG_V23;
  9477. else if (!strcasecmp(v->value, "dtmf"))
  9478. cid_signalling = CID_SIG_DTMF;
  9479. else if (ast_true(v->value))
  9480. cid_signalling = CID_SIG_BELL;
  9481. } else if (!strcasecmp(v->name, "cidstart")) {
  9482. if (!strcasecmp(v->value, "ring"))
  9483. cid_start = CID_START_RING;
  9484. else if (!strcasecmp(v->value, "polarity"))
  9485. cid_start = CID_START_POLARITY;
  9486. else if (ast_true(v->value))
  9487. cid_start = CID_START_RING;
  9488. } else if (!strcasecmp(v->name, "threewaycalling")) {
  9489. threewaycalling = ast_true(v->value);
  9490. } else if (!strcasecmp(v->name, "transfer")) {
  9491. transfer = ast_true(v->value);
  9492. } else if (!strcasecmp(v->name, "busydetect")) {
  9493. busydetect = ast_true(v->value);
  9494. } else if (!strcasecmp(v->name, "busycount")) {
  9495. busycount = atoi(v->value);
  9496. } else if (!strcasecmp(v->name, "callprogress")) {
  9497. callprogress = ast_true(v->value);
  9498. } else if (!strcasecmp(v->name, "hidecallerid")) {
  9499. hidecallerid = ast_true(v->value);
  9500. } else if (!strcasecmp(v->name, "callreturn")) {
  9501. callreturn = ast_true(v->value);
  9502. } else if (!strcasecmp(v->name, "callwaiting")) {
  9503. callwaiting = ast_true(v->value);
  9504. } else if (!strcasecmp(v->name, "callwaitingcallerid")) {
  9505. callwaitingcallerid = ast_true(v->value);
  9506. } else if (!strcasecmp(v->name, "context")) {
  9507. strncpy(context, v->value, sizeof(context)-1);
  9508. } else if (!strcasecmp(v->name, "language")) {
  9509. strncpy(language, v->value, sizeof(language)-1);
  9510. } else if (!strcasecmp(v->name, "progzone")) {
  9511. strncpy(progzone, v->value, sizeof(progzone) - 1);
  9512. } else if (!strcasecmp(v->name, "musiconhold")) {
  9513. strncpy(musicclass, v->value, sizeof(musicclass)-1);
  9514. } else if (!strcasecmp(v->name, "stripmsd")) {
  9515. stripmsd = atoi(v->value);
  9516. } else if (!strcasecmp(v->name, "group")) {
  9517. cur_group = get_group(v->value);
  9518. } else if (!strcasecmp(v->name, "callgroup")) {
  9519. cur_callergroup = get_group(v->value);
  9520. } else if (!strcasecmp(v->name, "pickupgroup")) {
  9521. cur_pickupgroup = get_group(v->value);
  9522. } else if (!strcasecmp(v->name, "immediate")) {
  9523. immediate = ast_true(v->value);
  9524. } else if (!strcasecmp(v->name, "mailbox")) {
  9525. strncpy(mailbox, v->value, sizeof(mailbox) -1);
  9526. } else if (!strcasecmp(v->name, "rxgain")) {
  9527. if (sscanf(v->value, "%f", &rxgain) != 1) {
  9528. ast_log(LOG_WARNING, "Invalid rxgain: %s\n", v->value);
  9529. }
  9530. } else if (!strcasecmp(v->name, "txgain")) {
  9531. if (sscanf(v->value, "%f", &txgain) != 1) {
  9532. ast_log(LOG_WARNING, "Invalid txgain: %s\n", v->value);
  9533. }
  9534. } else if (!strcasecmp(v->name, "tonezone")) {
  9535. if (sscanf(v->value, "%d", &tonezone) != 1) {
  9536. ast_log(LOG_WARNING, "Invalid tonezone: %s\n", v->value);
  9537. }
  9538. } else if (!strcasecmp(v->name, "callerid")) {
  9539. if (!strcasecmp(v->value, "asreceived"))
  9540. callerid[0] = '\0';
  9541. else
  9542. strncpy(callerid, v->value, sizeof(callerid)-1);
  9543. } else if (!strcasecmp(v->name, "signalling")) {
  9544. if (!strcasecmp(v->value, "em")) {
  9545. cur_signalling = SIG_EM;
  9546. } else if (!strcasecmp(v->value, "em_w")) {
  9547. cur_signalling = SIG_EMWINK;
  9548. } else if (!strcasecmp(v->value, "fxs_ls")) {
  9549. cur_signalling = SIG_FXSLS;
  9550. } else if (!strcasecmp(v->value, "fxs_gs")) {
  9551. cur_signalling = SIG_FXSGS;
  9552. } else if (!strcasecmp(v->value, "fxs_ks")) {
  9553. cur_signalling = SIG_FXSKS;
  9554. } else if (!strcasecmp(v->value, "fxo_ls")) {
  9555. cur_signalling = SIG_FXOLS;
  9556. } else if (!strcasecmp(v->value, "fxo_gs")) {
  9557. cur_signalling = SIG_FXOGS;
  9558. } else if (!strcasecmp(v->value, "fxo_ks")) {
  9559. cur_signalling = SIG_FXOKS;
  9560. } else if (!strcasecmp(v->value, "featd")) {
  9561. cur_signalling = SIG_FEATD;
  9562. } else if (!strcasecmp(v->value, "featdmf")) {
  9563. cur_signalling = SIG_FEATDMF;
  9564. } else if (!strcasecmp(v->value, "e911")) {
  9565. cur_signalling = SIG_E911;
  9566. } else if (!strcasecmp(v->value, "featb")) {
  9567. cur_signalling = SIG_FEATB;
  9568. #ifdef ZAPATA_PRI
  9569. } else if (!strcasecmp(v->value, "pri_net")) {
  9570. cur_signalling = SIG_PRI;
  9571. pritype = PRI_NETWORK;
  9572. } else if (!strcasecmp(v->value, "pri_cpe")) {
  9573. cur_signalling = SIG_PRI;
  9574. pritype = PRI_CPE;
  9575. #endif
  9576. } else {
  9577. ast_log(LOG_ERROR, "Unknown signalling method '%s'\n", v->value);
  9578. }
  9579. #ifdef ZAPATA_PRI
  9580. } else if (!strcasecmp(v->name, "switchtype")) {
  9581. if (!strcasecmp(v->value, "national"))
  9582. switchtype = PRI_SWITCH_NI2;
  9583. else if (!strcasecmp(v->value, "dms100"))
  9584. switchtype = PRI_SWITCH_DMS100;
  9585. else if (!strcasecmp(v->value, "4ess"))
  9586. switchtype = PRI_SWITCH_ATT4ESS;
  9587. else if (!strcasecmp(v->value, "5ess"))
  9588. switchtype = PRI_SWITCH_LUCENT5E;
  9589. else {
  9590. ast_log(LOG_ERROR, "Unknown switchtype '%s'\n", v->value);
  9591. ast_destroy(cfg);
  9592. ast_mutex_unlock(&iflock);
  9593. return -1;
  9594. }
  9595. } else if (!strcasecmp(v->name, "nsf")) {
  9596. if (!strcasecmp(v->value, "sdn"))
  9597. nsf = PRI_NSF_SDN;
  9598. else if (!strcasecmp(v->value, "megacom"))
  9599. nsf = PRI_NSF_MEGACOM;
  9600. else if (!strcasecmp(v->value, "accunet"))
  9601. nsf = PRI_NSF_ACCUNET
  9602. else if (!strcasecmp(v->value, "none"))
  9603. nsf = PRI_NSF_NONE;
  9604. else {
  9605. ast_log(LOG_WARN, "Unknown network-specific facility '%s'\n", v->value);
  9606. nsf = PRI_NSF_NONE;
  9607. }
  9608. } else if (!strcasecmp(v->name, "jitterbuffers")) {
  9609. numbufs = atoi(v->value);
  9610. } else if (!strcasecmp(v->name, "minunused")) {
  9611. minunused = atoi(v->value);
  9612. } else if (!strcasecmp(v->name, "idleext")) {
  9613. strncpy(idleext, v->value, sizeof(idleext) - 1);
  9614. } else if (!strcasecmp(v->name, "idledial")) {
  9615. strncpy(idledial, v->value, sizeof(idledial) - 1);
  9616. #endif
  9617. } else
  9618. ast_log(LOG_WARNING, "Ignoring %s\n", v->name);
  9619. v = v->next;
  9620. }
  9621. tmp = iflist;
  9622. prev = NULL;
  9623. while (tmp) {
  9624. if (tmp->destroy) {
  9625. if (destroy_channel(prev, tmp, 0)) {
  9626. ast_log(LOG_ERROR, "Unable to destroy chan_zap channel %d\n", tmp->channel);
  9627. ast_mutex_unlock(&iflock);
  9628. return -1;
  9629. }
  9630. tmp = tmp->next;
  9631. } else {
  9632. prev = tmp;
  9633. tmp = tmp->next;
  9634. }
  9635. }
  9636. ast_mutex_unlock(&iflock);
  9637. ast_destroy(cfg);
  9638. /* And start the monitor for the first time */
  9639. restart_monitor();
  9640. return 0;
  9641. }
  9642. #endif
  9643. static int zt_sendtext(struct ast_channel *c, char *text)
  9644. {
  9645. #define END_SILENCE_LEN 400
  9646. #define HEADER_MS 50
  9647. #define TRAILER_MS 5
  9648. #define HEADER_LEN ((HEADER_MS + TRAILER_MS) * 8)
  9649. #define ASCII_BYTES_PER_CHAR 80
  9650. unsigned char *buf,*mybuf;
  9651. struct zt_pvt *p = c->pvt->pvt;
  9652. struct pollfd fds[1];
  9653. int size,res,fd,len,x;
  9654. int bytes=0;
  9655. /* Initial carrier (imaginary) */
  9656. float cr = 1.0;
  9657. float ci = 0.0;
  9658. float scont = 0.0;
  9659. int index;
  9660. index = zt_get_index(c, p, 0);
  9661. if (index < 0) {
  9662. ast_log(LOG_WARNING, "Huh? I don't exist?\n");
  9663. return -1;
  9664. }
  9665. if (!text[0]) return(0); /* if nothing to send, dont */
  9666. if ((!p->tdd) && (!p->mate)) return(0); /* if not in TDD mode, just return */
  9667. if (p->mate)
  9668. buf = malloc(((strlen(text) + 1) * ASCII_BYTES_PER_CHAR) + END_SILENCE_LEN + HEADER_LEN);
  9669. else
  9670. buf = malloc(((strlen(text) + 1) * TDD_BYTES_PER_CHAR) + END_SILENCE_LEN);
  9671. if (!buf) {
  9672. ast_log(LOG_ERROR, "MALLOC FAILED\n");
  9673. return -1;
  9674. }
  9675. mybuf = buf;
  9676. if (p->mate) {
  9677. int codec = AST_LAW(p);
  9678. for (x=0;x<HEADER_MS;x++) { /* 50 ms of Mark */
  9679. PUT_CLID_MARKMS;
  9680. }
  9681. /* Put actual message */
  9682. for (x=0;text[x];x++) {
  9683. PUT_CLID(text[x]);
  9684. }
  9685. for (x=0;x<TRAILER_MS;x++) { /* 5 ms of Mark */
  9686. PUT_CLID_MARKMS;
  9687. }
  9688. len = bytes;
  9689. buf = mybuf;
  9690. }
  9691. else {
  9692. len = tdd_generate(p->tdd,buf,text);
  9693. if (len < 1) {
  9694. ast_log(LOG_ERROR, "TDD generate (len %d) failed!!\n",(int)strlen(text));
  9695. free(mybuf);
  9696. return -1;
  9697. }
  9698. }
  9699. memset(buf + len,0x7f,END_SILENCE_LEN);
  9700. len += END_SILENCE_LEN;
  9701. fd = p->subs[index].zfd;
  9702. while(len) {
  9703. if (ast_check_hangup(c)) {
  9704. free(mybuf);
  9705. return -1;
  9706. }
  9707. size = len;
  9708. if (size > READ_SIZE)
  9709. size = READ_SIZE;
  9710. fds[0].fd = fd;
  9711. fds[0].events = POLLOUT | POLLPRI;
  9712. res = poll(fds, 1, -1);
  9713. if (!res) {
  9714. ast_log(LOG_DEBUG, "poll (for write) ret. 0 on channel %d\n", p->channel);
  9715. continue;
  9716. }
  9717. /* if got exception */
  9718. if (fds[0].revents & POLLPRI) return -1;
  9719. if (!(fds[0].revents & POLLOUT)) {
  9720. ast_log(LOG_DEBUG, "write fd not ready on channel %d\n", p->channel);
  9721. continue;
  9722. }
  9723. res = write(fd, buf, size);
  9724. if (res != size) {
  9725. if (res == -1) {
  9726. free(mybuf);
  9727. return -1;
  9728. }
  9729. if (option_debug)
  9730. ast_log(LOG_DEBUG, "Write returned %d (%s) on channel %d\n", res, strerror(errno), p->channel);
  9731. break;
  9732. }
  9733. len -= size;
  9734. buf += size;
  9735. }
  9736. free(mybuf);
  9737. return(0);
  9738. }
  9739. #if 0
  9740. /* XXX Very broken on PRI XXX */
  9741. int reload(void)
  9742. {
  9743. if (reload_zt()) {
  9744. ast_log(LOG_WARNING, "Reload of chan_zap is unsuccessful\n");
  9745. return -1;
  9746. }
  9747. return 0;
  9748. }
  9749. #endif
  9750. int usecount()
  9751. {
  9752. int res;
  9753. ast_mutex_lock(&usecnt_lock);
  9754. res = usecnt;
  9755. ast_mutex_unlock(&usecnt_lock);
  9756. return res;
  9757. }
  9758. char *description()
  9759. {
  9760. return desc;
  9761. }
  9762. char *key()
  9763. {
  9764. return ASTERISK_GPL_KEY;
  9765. }