chan_misdn.c 385 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2004 - 2006, Christian Richter
  5. *
  6. * Christian Richter <crich@beronet.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. *
  18. */
  19. /*!
  20. * \file
  21. *
  22. * \brief the chan_misdn channel driver for Asterisk
  23. *
  24. * \author Christian Richter <crich@beronet.com>
  25. *
  26. * MISDN http://www.misdn.org/
  27. *
  28. * \ingroup channel_drivers
  29. */
  30. /*! \li \ref chan_misdn.c uses the configuration file \ref misdn.conf
  31. * \addtogroup configuration_file
  32. */
  33. /*! \page misdn.conf misdn.conf
  34. * \verbinclude misdn.conf.sample
  35. */
  36. /*!
  37. * \note
  38. * To use the CCBS/CCNR supplementary service feature and other
  39. * supplementary services using FACILITY messages requires a
  40. * modified version of mISDN.
  41. *
  42. * \note
  43. * The latest modified mISDN v1.1.x based version is available at:
  44. * http://svn.digium.com/svn/thirdparty/mISDN/trunk
  45. * http://svn.digium.com/svn/thirdparty/mISDNuser/trunk
  46. *
  47. * \note
  48. * Taged versions of the modified mISDN code are available under:
  49. * http://svn.digium.com/svn/thirdparty/mISDN/tags
  50. * http://svn.digium.com/svn/thirdparty/mISDNuser/tags
  51. */
  52. /* Define to enable cli commands to generate canned CCBS messages. */
  53. // #define CCBS_TEST_MESSAGES 1
  54. /*
  55. * XXX The mISDN channel driver needs its native bridge code
  56. * converted to the new bridge technology scheme. The
  57. * chan_dahdi native bridge code can be used as an example. It
  58. * is unlikely that this will ever get done. Support for this
  59. * channel driver is dwindling because the supported version of
  60. * mISDN does not support newer kernels.
  61. *
  62. * Without native bridge support, the following config file
  63. * parameters have no effect: bridging.
  64. *
  65. * The existing native bridge code is marked with the
  66. * mISDN_NATIVE_BRIDGING conditional.
  67. */
  68. /*** MODULEINFO
  69. <depend>isdnnet</depend>
  70. <depend>misdn</depend>
  71. <depend>suppserv</depend>
  72. <support_level>extended</support_level>
  73. ***/
  74. #include "asterisk.h"
  75. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  76. #include <pthread.h>
  77. #include <sys/socket.h>
  78. #include <sys/time.h>
  79. #include <arpa/inet.h>
  80. #include <fcntl.h>
  81. #include <sys/ioctl.h>
  82. #include <signal.h>
  83. #include <sys/file.h>
  84. #include <semaphore.h>
  85. #include <ctype.h>
  86. #include <time.h>
  87. #include "asterisk/channel.h"
  88. #include "asterisk/config.h"
  89. #include "asterisk/module.h"
  90. #include "asterisk/pbx.h"
  91. #include "asterisk/io.h"
  92. #include "asterisk/frame.h"
  93. #include "asterisk/translate.h"
  94. #include "asterisk/cli.h"
  95. #include "asterisk/musiconhold.h"
  96. #include "asterisk/dsp.h"
  97. #include "asterisk/file.h"
  98. #include "asterisk/callerid.h"
  99. #include "asterisk/indications.h"
  100. #include "asterisk/app.h"
  101. #include "asterisk/features.h"
  102. #include "asterisk/term.h"
  103. #include "asterisk/sched.h"
  104. #include "asterisk/stringfields.h"
  105. #include "asterisk/abstract_jb.h"
  106. #include "asterisk/causes.h"
  107. #include "asterisk/format.h"
  108. #include "asterisk/format_cap.h"
  109. #include "asterisk/features_config.h"
  110. #include "asterisk/bridge.h"
  111. #include "asterisk/pickup.h"
  112. #include "asterisk/format_cache.h"
  113. #include "chan_misdn_config.h"
  114. #include "isdn_lib.h"
  115. static char global_tracefile[BUFFERSIZE + 1];
  116. static int g_config_initialized = 0;
  117. struct misdn_jb{
  118. int size;
  119. int upper_threshold;
  120. char *samples, *ok;
  121. int wp,rp;
  122. int state_empty;
  123. int state_full;
  124. int state_buffer;
  125. int bytes_wrote;
  126. ast_mutex_t mutexjb;
  127. };
  128. /*! \brief allocates the jb-structure and initialize the elements */
  129. struct misdn_jb *misdn_jb_init(int size, int upper_threshold);
  130. /*! \brief frees the data and destroys the given jitterbuffer struct */
  131. void misdn_jb_destroy(struct misdn_jb *jb);
  132. /*! \brief fills the jitterbuffer with len data returns < 0 if there was an
  133. error (buffer overrun). */
  134. int misdn_jb_fill(struct misdn_jb *jb, const char *data, int len);
  135. /*! \brief gets len bytes out of the jitterbuffer if available, else only the
  136. available data is returned and the return value indicates the number
  137. of data. */
  138. int misdn_jb_empty(struct misdn_jb *jb, char *data, int len);
  139. static char *complete_ch(struct ast_cli_args *a);
  140. static char *complete_debug_port(struct ast_cli_args *a);
  141. static char *complete_show_config(struct ast_cli_args *a);
  142. /* BEGIN: chan_misdn.h */
  143. #if defined(AST_MISDN_ENHANCEMENTS)
  144. /*
  145. * This timeout duration is to clean up any call completion records that
  146. * are forgotten about by the switch.
  147. */
  148. #define MISDN_CC_RECORD_AGE_MAX (6UL * 60 * 60) /* seconds */
  149. #define MISDN_CC_REQUEST_WAIT_MAX 5 /* seconds */
  150. /*!
  151. * \brief Caller that initialized call completion services
  152. *
  153. * \details
  154. * This data is the payload for a datastore that is put on the channel that
  155. * initializes call completion services. This datastore is set to be inherited
  156. * by the outbound mISDN channel. When one of these channels hangs up, the
  157. * channel pointer will be set to NULL. That way, we can ensure that we do not
  158. * touch this channel after it gets destroyed.
  159. */
  160. struct misdn_cc_caller {
  161. /*! \brief The channel that initialized call completion services */
  162. struct ast_channel *chan;
  163. };
  164. struct misdn_cc_notify {
  165. /*! \brief Dialplan: Notify extension priority */
  166. int priority;
  167. /*! \brief Dialplan: Notify extension context */
  168. char context[AST_MAX_CONTEXT];
  169. /*! \brief Dialplan: Notify extension number (User-A) */
  170. char exten[AST_MAX_EXTENSION];
  171. };
  172. /*! \brief mISDN call completion record */
  173. struct misdn_cc_record {
  174. /*! \brief Call completion record linked list */
  175. AST_LIST_ENTRY(misdn_cc_record) list;
  176. /*! \brief Time the record was created. */
  177. time_t time_created;
  178. /*! \brief MISDN_CC_RECORD_ID value */
  179. long record_id;
  180. /*!
  181. * \brief Logical Layer 1 port associated with this
  182. * call completion record
  183. */
  184. int port;
  185. /*! \brief TRUE if point-to-point mode (CCBS-T/CCNR-T mode) */
  186. int ptp;
  187. /*! \brief Mode specific parameters */
  188. union {
  189. /*! \brief point-to-point specific parameters. */
  190. struct {
  191. /*!
  192. * \brief Call-completion signaling link.
  193. * NULL if signaling link not established.
  194. */
  195. struct misdn_bchannel *bc;
  196. /*!
  197. * \brief TRUE if we requested the request retention option
  198. * to be enabled.
  199. */
  200. int requested_retention;
  201. /*!
  202. * \brief TRUE if the request retention option is enabled.
  203. */
  204. int retention_enabled;
  205. } ptp;
  206. /*! \brief point-to-multi-point specific parameters. */
  207. struct {
  208. /*! \brief CallLinkageID (valid when port determined) */
  209. int linkage_id;
  210. /*! \breif CCBSReference (valid when activated is TRUE) */
  211. int reference_id;
  212. /*! \brief globalRecall(0), specificRecall(1) */
  213. int recall_mode;
  214. } ptmp;
  215. } mode;
  216. /*! \brief TRUE if call completion activated */
  217. int activated;
  218. /*! \brief Outstanding message ID (valid when outstanding_message) */
  219. int invoke_id;
  220. /*! \brief TRUE if waiting for a response from a message (invoke_id is valid) */
  221. int outstanding_message;
  222. /*! \brief TRUE if activation has been requested */
  223. int activation_requested;
  224. /*!
  225. * \brief TRUE if User-A is free
  226. * \note PTMP - Used to answer CCBSStatusRequest.
  227. * PTP - Determines how to respond to CCBS_T_RemoteUserFree.
  228. */
  229. int party_a_free;
  230. /*! \brief Error code received from last outstanding message. */
  231. enum FacErrorCode error_code;
  232. /*! \brief Reject code received from last outstanding message. */
  233. enum FacRejectCode reject_code;
  234. /*!
  235. * \brief Saved struct misdn_bchannel call information when
  236. * attempted to call User-B
  237. */
  238. struct {
  239. /*! \brief User-A caller id information */
  240. struct misdn_party_id caller;
  241. /*! \brief User-B number information */
  242. struct misdn_party_dialing dialed;
  243. /*! \brief The BC, HLC (optional) and LLC (optional) contents from the SETUP message. */
  244. struct Q931_Bc_Hlc_Llc setup_bc_hlc_llc;
  245. /*! \brief SETUP message bearer capability field code value */
  246. int capability;
  247. /*! \brief TRUE if call made in digital HDLC mode */
  248. int hdlc;
  249. } redial;
  250. /*! \brief Dialplan location to indicate User-B free and User-A is free */
  251. struct misdn_cc_notify remote_user_free;
  252. /*! \brief Dialplan location to indicate User-B free and User-A is busy */
  253. struct misdn_cc_notify b_free;
  254. };
  255. /*! \brief mISDN call completion record database */
  256. static AST_LIST_HEAD_STATIC(misdn_cc_records_db, misdn_cc_record);
  257. /*! \brief Next call completion record ID to use */
  258. static __u16 misdn_cc_record_id;
  259. /*! \brief Next invoke ID to use */
  260. static __s16 misdn_invoke_id;
  261. static const char misdn_no_response_from_network[] = "No response from network";
  262. static const char misdn_cc_record_not_found[] = "Call completion record not found";
  263. /* mISDN channel variable names */
  264. #define MISDN_CC_RECORD_ID "MISDN_CC_RECORD_ID"
  265. #define MISDN_CC_STATUS "MISDN_CC_STATUS"
  266. #define MISDN_ERROR_MSG "MISDN_ERROR_MSG"
  267. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  268. static ast_mutex_t release_lock;
  269. enum misdn_chan_state {
  270. MISDN_NOTHING = 0, /*!< at beginning */
  271. MISDN_WAITING4DIGS, /*!< when waiting for info */
  272. MISDN_EXTCANTMATCH, /*!< when asterisk couldn't match our ext */
  273. MISDN_INCOMING_SETUP, /*!< for incoming setup */
  274. MISDN_DIALING, /*!< when pbx_start */
  275. MISDN_PROGRESS, /*!< we have progress */
  276. MISDN_PROCEEDING, /*!< we have progress */
  277. MISDN_CALLING, /*!< when misdn_call is called */
  278. MISDN_CALLING_ACKNOWLEDGE, /*!< when we get SETUP_ACK */
  279. MISDN_ALERTING, /*!< when Alerting */
  280. MISDN_BUSY, /*!< when BUSY */
  281. MISDN_CONNECTED, /*!< when connected */
  282. MISDN_DISCONNECTED, /*!< when connected */
  283. MISDN_CLEANING, /*!< when hangup from * but we were connected before */
  284. };
  285. /*! Asterisk created the channel (outgoing call) */
  286. #define ORG_AST 1
  287. /*! mISDN created the channel (incoming call) */
  288. #define ORG_MISDN 2
  289. enum misdn_hold_state {
  290. MISDN_HOLD_IDLE, /*!< HOLD not active */
  291. MISDN_HOLD_ACTIVE, /*!< Call is held */
  292. MISDN_HOLD_TRANSFER, /*!< Held call is being transferred */
  293. MISDN_HOLD_DISCONNECT, /*!< Held call is being disconnected */
  294. };
  295. struct hold_info {
  296. /*!
  297. * \brief Call HOLD state.
  298. */
  299. enum misdn_hold_state state;
  300. /*!
  301. * \brief Logical port the channel call record is HELD on
  302. * because the B channel is no longer associated.
  303. */
  304. int port;
  305. /*!
  306. * \brief Original B channel number the HELD call was using.
  307. * \note Used only for debug display messages.
  308. */
  309. int channel;
  310. };
  311. #define chan_list_ref(obj, debug) ao2_t_ref((obj), +1, (debug))
  312. #define chan_list_unref(obj, debug) ao2_t_ref((obj), -1, (debug))
  313. /*!
  314. * \brief Channel call record structure
  315. */
  316. struct chan_list {
  317. /*!
  318. * \brief The "allowed_bearers" string read in from /etc/asterisk/misdn.conf
  319. */
  320. char allowed_bearers[BUFFERSIZE + 1];
  321. /*!
  322. * \brief State of the channel
  323. */
  324. enum misdn_chan_state state;
  325. /*!
  326. * \brief TRUE if a hangup needs to be queued
  327. * \note This is a debug flag only used to catch calls to hangup_chan() that are already hungup.
  328. */
  329. int need_queue_hangup;
  330. /*!
  331. * \brief TRUE if a channel can be hung up by calling asterisk directly when done.
  332. */
  333. int need_hangup;
  334. /*!
  335. * \brief TRUE if we could send an AST_CONTROL_BUSY if needed.
  336. */
  337. int need_busy;
  338. /*!
  339. * \brief Who originally created this channel. ORG_AST or ORG_MISDN
  340. */
  341. int originator;
  342. /*!
  343. * \brief TRUE of we are not to respond immediately to a SETUP message. Check the dialplan first.
  344. * \note The "noautorespond_on_setup" boolean read in from /etc/asterisk/misdn.conf
  345. */
  346. int noautorespond_on_setup;
  347. int norxtone; /*!< Boolean assigned values but the value is not used. */
  348. /*!
  349. * \brief TRUE if we are not to generate tones (Playtones)
  350. */
  351. int notxtone;
  352. /*!
  353. * \brief TRUE if echo canceller is enabled. Value is toggled.
  354. */
  355. int toggle_ec;
  356. /*!
  357. * \brief TRUE if you want to send Tone Indications to an incoming
  358. * ISDN channel on a TE Port.
  359. * \note The "incoming_early_audio" boolean read in from /etc/asterisk/misdn.conf
  360. */
  361. int incoming_early_audio;
  362. /*!
  363. * \brief TRUE if DTMF digits are to be passed inband only.
  364. * \note It is settable by the misdn_set_opt() application.
  365. */
  366. int ignore_dtmf;
  367. /*!
  368. * \brief Pipe file descriptor handles array.
  369. * Read from pipe[0], write to pipe[1]
  370. */
  371. int pipe[2];
  372. /*!
  373. * \brief Read buffer for inbound audio from pipe[0]
  374. */
  375. char ast_rd_buf[4096];
  376. /*!
  377. * \brief Inbound audio frame returned by misdn_read().
  378. */
  379. struct ast_frame frame;
  380. /*!
  381. * \brief Fax detection option. (0:no 1:yes 2:yes+nojump)
  382. * \note The "faxdetect" option string read in from /etc/asterisk/misdn.conf
  383. * \note It is settable by the misdn_set_opt() application.
  384. */
  385. int faxdetect;
  386. /*!
  387. * \brief Number of seconds to detect a Fax machine when detection enabled.
  388. * \note 0 disables the timeout.
  389. * \note The "faxdetect_timeout" value read in from /etc/asterisk/misdn.conf
  390. */
  391. int faxdetect_timeout;
  392. /*!
  393. * \brief Starting time of fax detection with timeout when nonzero.
  394. */
  395. struct timeval faxdetect_tv;
  396. /*!
  397. * \brief TRUE if a fax has been detected.
  398. */
  399. int faxhandled;
  400. /*!
  401. * \brief TRUE if we will use the Asterisk DSP to detect DTMF/Fax
  402. * \note The "astdtmf" boolean read in from /etc/asterisk/misdn.conf
  403. */
  404. int ast_dsp;
  405. /*!
  406. * \brief Jitterbuffer length
  407. * \note The "jitterbuffer" value read in from /etc/asterisk/misdn.conf
  408. */
  409. int jb_len;
  410. /*!
  411. * \brief Jitterbuffer upper threshold
  412. * \note The "jitterbuffer_upper_threshold" value read in from /etc/asterisk/misdn.conf
  413. */
  414. int jb_upper_threshold;
  415. /*!
  416. * \brief Allocated jitterbuffer controller
  417. * \note misdn_jb_init() creates the jitterbuffer.
  418. * \note Must use misdn_jb_destroy() to clean up.
  419. */
  420. struct misdn_jb *jb;
  421. /*!
  422. * \brief Allocated DSP controller
  423. * \note ast_dsp_new() creates the DSP controller.
  424. * \note Must use ast_dsp_free() to clean up.
  425. */
  426. struct ast_dsp *dsp;
  427. /*!
  428. * \brief Associated Asterisk channel structure.
  429. */
  430. struct ast_channel * ast;
  431. /*!
  432. * \brief Associated B channel structure.
  433. */
  434. struct misdn_bchannel *bc;
  435. #if defined(AST_MISDN_ENHANCEMENTS)
  436. /*!
  437. * \brief Peer channel for which call completion was initialized.
  438. */
  439. struct misdn_cc_caller *peer;
  440. /*! \brief Associated call completion record ID (-1 if not associated) */
  441. long record_id;
  442. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  443. /*!
  444. * \brief HELD channel call information
  445. */
  446. struct hold_info hold;
  447. /*!
  448. * \brief From associated B channel: Layer 3 process ID
  449. * \note Used to find the HELD channel call record when retrieving a call.
  450. */
  451. unsigned int l3id;
  452. /*!
  453. * \brief From associated B channel: B Channel mISDN driver layer ID from mISDN_get_layerid()
  454. * \note Used only for debug display messages.
  455. */
  456. int addr;
  457. /*!
  458. * \brief Incoming call dialplan context identifier.
  459. * \note The "context" string read in from /etc/asterisk/misdn.conf
  460. */
  461. char context[AST_MAX_CONTEXT];
  462. /*!
  463. * \brief The configured music-on-hold class to use for this call.
  464. * \note The "musicclass" string read in from /etc/asterisk/misdn.conf
  465. */
  466. char mohinterpret[MAX_MUSICCLASS];
  467. /*!
  468. * \brief Number of outgoing audio frames dropped since last debug gripe message.
  469. */
  470. int dropped_frame_cnt;
  471. /*!
  472. * \brief TRUE if we must do the ringback tones.
  473. * \note The "far_alerting" boolean read in from /etc/asterisk/misdn.conf
  474. */
  475. int far_alerting;
  476. /*!
  477. * \brief TRUE if NT should disconnect an overlap dialing call when a timeout occurs.
  478. * \note The "nttimeout" boolean read in from /etc/asterisk/misdn.conf
  479. */
  480. int nttimeout;
  481. /*!
  482. * \brief Tone zone sound used for dialtone generation.
  483. * \note Used as a boolean. Non-NULL to prod generation if enabled.
  484. */
  485. struct ast_tone_zone_sound *ts;
  486. /*!
  487. * \brief Enables overlap dialing for the set amount of seconds. (0 = Disabled)
  488. * \note The "overlapdial" value read in from /etc/asterisk/misdn.conf
  489. */
  490. int overlap_dial;
  491. /*!
  492. * \brief Overlap dialing timeout Task ID. -1 if not running.
  493. */
  494. int overlap_dial_task;
  495. /*!
  496. * \brief overlap_tv access lock.
  497. */
  498. ast_mutex_t overlap_tv_lock;
  499. /*!
  500. * \brief Overlap timer start time. Timer restarted for every digit received.
  501. */
  502. struct timeval overlap_tv;
  503. /*!
  504. * \brief Next channel call record in the list.
  505. */
  506. struct chan_list *next;
  507. };
  508. int MAXTICS = 8;
  509. void export_ch(struct ast_channel *chan, struct misdn_bchannel *bc, struct chan_list *ch);
  510. void import_ch(struct ast_channel *chan, struct misdn_bchannel *bc, struct chan_list *ch);
  511. static struct ast_frame *process_ast_dsp(struct chan_list *tmp, struct ast_frame *frame);
  512. struct robin_list {
  513. char *group;
  514. int port;
  515. int channel;
  516. struct robin_list *next;
  517. struct robin_list *prev;
  518. };
  519. static struct robin_list *robin = NULL;
  520. static void free_robin_list(void)
  521. {
  522. struct robin_list *r;
  523. struct robin_list *next;
  524. for (r = robin, robin = NULL; r; r = next) {
  525. next = r->next;
  526. ast_free(r->group);
  527. ast_free(r);
  528. }
  529. }
  530. static struct robin_list *get_robin_position(char *group)
  531. {
  532. struct robin_list *new;
  533. struct robin_list *iter = robin;
  534. for (; iter; iter = iter->next) {
  535. if (!strcasecmp(iter->group, group)) {
  536. return iter;
  537. }
  538. }
  539. new = ast_calloc(1, sizeof(*new));
  540. if (!new) {
  541. return NULL;
  542. }
  543. new->group = ast_strdup(group);
  544. if (!new->group) {
  545. ast_free(new);
  546. return NULL;
  547. }
  548. new->channel = 1;
  549. if (robin) {
  550. new->next = robin;
  551. robin->prev = new;
  552. }
  553. robin = new;
  554. return robin;
  555. }
  556. /*! \brief the main schedule context for stuff like l1 watcher, overlap dial, ... */
  557. static struct ast_sched_context *misdn_tasks = NULL;
  558. static pthread_t misdn_tasks_thread;
  559. static int *misdn_ports;
  560. static void chan_misdn_log(int level, int port, char *tmpl, ...)
  561. __attribute__((format(printf, 3, 4)));
  562. static struct ast_channel *misdn_new(struct chan_list *cl, int state, char *exten, char *callerid, struct ast_format_cap *cap, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, int port, int c);
  563. static void send_digit_to_chan(struct chan_list *cl, char digit);
  564. static int pbx_start_chan(struct chan_list *ch);
  565. #define MISDN_ASTERISK_TECH_PVT(ast) ast_channel_tech_pvt(ast)
  566. #define MISDN_ASTERISK_TECH_PVT_SET(ast, value) ast_channel_tech_pvt_set(ast, value)
  567. #include "asterisk/strings.h"
  568. /* #define MISDN_DEBUG 1 */
  569. static const char misdn_type[] = "mISDN";
  570. static int tracing = 0;
  571. static int *misdn_debug;
  572. static int *misdn_debug_only;
  573. static int max_ports;
  574. static int *misdn_in_calls;
  575. static int *misdn_out_calls;
  576. /*!
  577. * \brief Global channel call record list head.
  578. */
  579. static struct chan_list *cl_te=NULL;
  580. static ast_mutex_t cl_te_lock;
  581. static enum event_response_e
  582. cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data);
  583. static int send_cause2ast(struct ast_channel *ast, struct misdn_bchannel *bc, struct chan_list *ch);
  584. static void cl_queue_chan(struct chan_list *chan);
  585. static int dialtone_indicate(struct chan_list *cl);
  586. static void hanguptone_indicate(struct chan_list *cl);
  587. static int stop_indicate(struct chan_list *cl);
  588. static int start_bc_tones(struct chan_list *cl);
  589. static int stop_bc_tones(struct chan_list *cl);
  590. static void release_chan_early(struct chan_list *ch);
  591. static void release_chan(struct chan_list *ch, struct misdn_bchannel *bc);
  592. #if defined(AST_MISDN_ENHANCEMENTS)
  593. static const char misdn_command_name[] = "misdn_command";
  594. static int misdn_command_exec(struct ast_channel *chan, const char *data);
  595. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  596. static int misdn_check_l2l1(struct ast_channel *chan, const char *data);
  597. static int misdn_set_opt_exec(struct ast_channel *chan, const char *data);
  598. static int misdn_facility_exec(struct ast_channel *chan, const char *data);
  599. int chan_misdn_jb_empty(struct misdn_bchannel *bc, char *buf, int len);
  600. void debug_numtype(int port, int numtype, char *type);
  601. int add_out_calls(int port);
  602. int add_in_calls(int port);
  603. #ifdef MISDN_1_2
  604. static int update_pipeline_config(struct misdn_bchannel *bc);
  605. #else
  606. static int update_ec_config(struct misdn_bchannel *bc);
  607. #endif
  608. /*************** Helpers *****************/
  609. static int misdn_chan_is_valid(struct chan_list *ch)
  610. {
  611. struct chan_list *list;
  612. ast_mutex_lock(&cl_te_lock);
  613. for (list = cl_te; list; list = list->next) {
  614. if (list == ch) {
  615. ast_mutex_unlock(&cl_te_lock);
  616. return 1;
  617. }
  618. }
  619. ast_mutex_unlock(&cl_te_lock);
  620. return 0;
  621. }
  622. #if defined(mISDN_NATIVE_BRIDGING)
  623. /*! Returns a reference to the found chan_list. */
  624. static struct chan_list *get_chan_by_ast(struct ast_channel *ast)
  625. {
  626. struct chan_list *tmp;
  627. ast_mutex_lock(&cl_te_lock);
  628. for (tmp = cl_te; tmp; tmp = tmp->next) {
  629. if (tmp->ast == ast) {
  630. chan_list_ref(tmp, "Found chan_list by ast");
  631. ast_mutex_unlock(&cl_te_lock);
  632. return tmp;
  633. }
  634. }
  635. ast_mutex_unlock(&cl_te_lock);
  636. return NULL;
  637. }
  638. #endif /* defined(mISDN_NATIVE_BRIDGING) */
  639. /*! Returns a reference to the found chan_list. */
  640. static struct chan_list *get_chan_by_ast_name(const char *name)
  641. {
  642. struct chan_list *tmp;
  643. ast_mutex_lock(&cl_te_lock);
  644. for (tmp = cl_te; tmp; tmp = tmp->next) {
  645. if (tmp->ast && strcmp(ast_channel_name(tmp->ast), name) == 0) {
  646. chan_list_ref(tmp, "Found chan_list by ast name");
  647. ast_mutex_unlock(&cl_te_lock);
  648. return tmp;
  649. }
  650. }
  651. ast_mutex_unlock(&cl_te_lock);
  652. return NULL;
  653. }
  654. #if defined(AST_MISDN_ENHANCEMENTS)
  655. /*!
  656. * \internal
  657. * \brief Destroy the misdn_cc_ds_info datastore payload
  658. *
  659. * \param[in] data the datastore payload, a reference to an misdn_cc_caller
  660. *
  661. * \details
  662. * Since the payload is a reference to an astobj2 object, we just decrement its
  663. * reference count. Before doing so, we NULL out the channel pointer inside of
  664. * the misdn_cc_caller instance. This function will be called in one of two
  665. * cases. In both cases, we no longer need the channel pointer:
  666. *
  667. * - The original channel that initialized call completion services, the same
  668. * channel that is stored here, has been destroyed early. This could happen
  669. * if it transferred the mISDN channel, for example.
  670. *
  671. * - The mISDN channel that had this datastore inherited on to it is now being
  672. * destroyed. If this is the case, then the call completion events have
  673. * already occurred and the appropriate channel variables have already been
  674. * set on the original channel that requested call completion services.
  675. *
  676. * \return Nothing
  677. */
  678. static void misdn_cc_ds_destroy(void *data)
  679. {
  680. struct misdn_cc_caller *cc_caller = data;
  681. ao2_lock(cc_caller);
  682. cc_caller->chan = NULL;
  683. ao2_unlock(cc_caller);
  684. ao2_ref(cc_caller, -1);
  685. }
  686. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  687. #if defined(AST_MISDN_ENHANCEMENTS)
  688. /*!
  689. * \internal
  690. * \brief Duplicate the misdn_cc_ds_info datastore payload
  691. *
  692. * \param[in] data the datastore payload, a reference to an misdn_cc_caller
  693. *
  694. * \details
  695. * All we need to do is bump the reference count and return the same instance.
  696. *
  697. * \return A reference to an instance of a misdn_cc_caller
  698. */
  699. static void *misdn_cc_ds_duplicate(void *data)
  700. {
  701. struct misdn_cc_caller *cc_caller = data;
  702. ao2_ref(cc_caller, +1);
  703. return cc_caller;
  704. }
  705. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  706. #if defined(AST_MISDN_ENHANCEMENTS)
  707. static const struct ast_datastore_info misdn_cc_ds_info = {
  708. .type = "misdn_cc",
  709. .destroy = misdn_cc_ds_destroy,
  710. .duplicate = misdn_cc_ds_duplicate,
  711. };
  712. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  713. #if defined(AST_MISDN_ENHANCEMENTS)
  714. /*!
  715. * \internal
  716. * \brief Set a channel var on the peer channel for call completion services
  717. *
  718. * \param[in] peer The peer that initialized call completion services
  719. * \param[in] var The variable name to set
  720. * \param[in] value The variable value to set
  721. *
  722. * This function may be called from outside of the channel thread. It handles
  723. * the fact that the peer channel may be hung up and destroyed at any time.
  724. *
  725. * \return nothing
  726. */
  727. static void misdn_cc_set_peer_var(struct misdn_cc_caller *peer, const char *var,
  728. const char *value)
  729. {
  730. ao2_lock(peer);
  731. /*! \todo XXX This nastiness can go away once ast_channel is ref counted! */
  732. while (peer->chan && ast_channel_trylock(peer->chan)) {
  733. ao2_unlock(peer);
  734. sched_yield();
  735. ao2_lock(peer);
  736. }
  737. if (peer->chan) {
  738. pbx_builtin_setvar_helper(peer->chan, var, value);
  739. ast_channel_unlock(peer->chan);
  740. }
  741. ao2_unlock(peer);
  742. }
  743. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  744. #if defined(AST_MISDN_ENHANCEMENTS)
  745. /*!
  746. * \internal
  747. * \brief Get a reference to the CC caller if it exists
  748. */
  749. static struct misdn_cc_caller *misdn_cc_caller_get(struct ast_channel *chan)
  750. {
  751. struct ast_datastore *datastore;
  752. struct misdn_cc_caller *cc_caller;
  753. ast_channel_lock(chan);
  754. if (!(datastore = ast_channel_datastore_find(chan, &misdn_cc_ds_info, NULL))) {
  755. ast_channel_unlock(chan);
  756. return NULL;
  757. }
  758. ao2_ref(datastore->data, +1);
  759. cc_caller = datastore->data;
  760. ast_channel_unlock(chan);
  761. return cc_caller;
  762. }
  763. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  764. #if defined(AST_MISDN_ENHANCEMENTS)
  765. /*!
  766. * \internal
  767. * \brief Find the call completion record given the record id.
  768. *
  769. * \param record_id
  770. *
  771. * \retval pointer to found call completion record
  772. * \retval NULL if not found
  773. *
  774. * \note Assumes the misdn_cc_records_db lock is already obtained.
  775. */
  776. static struct misdn_cc_record *misdn_cc_find_by_id(long record_id)
  777. {
  778. struct misdn_cc_record *current;
  779. AST_LIST_TRAVERSE(&misdn_cc_records_db, current, list) {
  780. if (current->record_id == record_id) {
  781. /* Found the record */
  782. break;
  783. }
  784. }
  785. return current;
  786. }
  787. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  788. #if defined(AST_MISDN_ENHANCEMENTS)
  789. /*!
  790. * \internal
  791. * \brief Find the call completion record given the port and call linkage id.
  792. *
  793. * \param port Logical port number
  794. * \param linkage_id Call linkage ID number from switch.
  795. *
  796. * \retval pointer to found call completion record
  797. * \retval NULL if not found
  798. *
  799. * \note Assumes the misdn_cc_records_db lock is already obtained.
  800. */
  801. static struct misdn_cc_record *misdn_cc_find_by_linkage(int port, int linkage_id)
  802. {
  803. struct misdn_cc_record *current;
  804. AST_LIST_TRAVERSE(&misdn_cc_records_db, current, list) {
  805. if (current->port == port
  806. && !current->ptp
  807. && current->mode.ptmp.linkage_id == linkage_id) {
  808. /* Found the record */
  809. break;
  810. }
  811. }
  812. return current;
  813. }
  814. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  815. #if defined(AST_MISDN_ENHANCEMENTS)
  816. /*!
  817. * \internal
  818. * \brief Find the call completion record given the port and outstanding invocation id.
  819. *
  820. * \param port Logical port number
  821. * \param invoke_id Outstanding message invocation ID number.
  822. *
  823. * \retval pointer to found call completion record
  824. * \retval NULL if not found
  825. *
  826. * \note Assumes the misdn_cc_records_db lock is already obtained.
  827. */
  828. static struct misdn_cc_record *misdn_cc_find_by_invoke(int port, int invoke_id)
  829. {
  830. struct misdn_cc_record *current;
  831. AST_LIST_TRAVERSE(&misdn_cc_records_db, current, list) {
  832. if (current->outstanding_message
  833. && current->invoke_id == invoke_id
  834. && current->port == port) {
  835. /* Found the record */
  836. break;
  837. }
  838. }
  839. return current;
  840. }
  841. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  842. #if defined(AST_MISDN_ENHANCEMENTS)
  843. /*!
  844. * \internal
  845. * \brief Find the call completion record given the port and CCBS reference id.
  846. *
  847. * \param port Logical port number
  848. * \param reference_id CCBS reference ID number from switch.
  849. *
  850. * \retval pointer to found call completion record
  851. * \retval NULL if not found
  852. *
  853. * \note Assumes the misdn_cc_records_db lock is already obtained.
  854. */
  855. static struct misdn_cc_record *misdn_cc_find_by_reference(int port, int reference_id)
  856. {
  857. struct misdn_cc_record *current;
  858. AST_LIST_TRAVERSE(&misdn_cc_records_db, current, list) {
  859. if (current->activated
  860. && current->port == port
  861. && !current->ptp
  862. && current->mode.ptmp.reference_id == reference_id) {
  863. /* Found the record */
  864. break;
  865. }
  866. }
  867. return current;
  868. }
  869. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  870. #if defined(AST_MISDN_ENHANCEMENTS)
  871. /*!
  872. * \internal
  873. * \brief Find the call completion record given the B channel pointer
  874. *
  875. * \param bc B channel control structure pointer.
  876. *
  877. * \retval pointer to found call completion record
  878. * \retval NULL if not found
  879. *
  880. * \note Assumes the misdn_cc_records_db lock is already obtained.
  881. */
  882. static struct misdn_cc_record *misdn_cc_find_by_bc(const struct misdn_bchannel *bc)
  883. {
  884. struct misdn_cc_record *current;
  885. if (bc) {
  886. AST_LIST_TRAVERSE(&misdn_cc_records_db, current, list) {
  887. if (current->ptp
  888. && current->mode.ptp.bc == bc) {
  889. /* Found the record */
  890. break;
  891. }
  892. }
  893. } else {
  894. current = NULL;
  895. }
  896. return current;
  897. }
  898. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  899. #if defined(AST_MISDN_ENHANCEMENTS)
  900. /*!
  901. * \internal
  902. * \brief Delete the given call completion record
  903. *
  904. * \param doomed Call completion record to destroy
  905. *
  906. * \return Nothing
  907. *
  908. * \note Assumes the misdn_cc_records_db lock is already obtained.
  909. */
  910. static void misdn_cc_delete(struct misdn_cc_record *doomed)
  911. {
  912. struct misdn_cc_record *current;
  913. AST_LIST_TRAVERSE_SAFE_BEGIN(&misdn_cc_records_db, current, list) {
  914. if (current == doomed) {
  915. AST_LIST_REMOVE_CURRENT(list);
  916. ast_free(current);
  917. return;
  918. }
  919. }
  920. AST_LIST_TRAVERSE_SAFE_END;
  921. /* The doomed node is not in the call completion database */
  922. }
  923. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  924. #if defined(AST_MISDN_ENHANCEMENTS)
  925. /*!
  926. * \internal
  927. * \brief Delete all old call completion records
  928. *
  929. * \return Nothing
  930. *
  931. * \note Assumes the misdn_cc_records_db lock is already obtained.
  932. */
  933. static void misdn_cc_remove_old(void)
  934. {
  935. struct misdn_cc_record *current;
  936. time_t now;
  937. now = time(NULL);
  938. AST_LIST_TRAVERSE_SAFE_BEGIN(&misdn_cc_records_db, current, list) {
  939. if (MISDN_CC_RECORD_AGE_MAX < now - current->time_created) {
  940. if (current->ptp && current->mode.ptp.bc) {
  941. /* Close the old call-completion signaling link */
  942. current->mode.ptp.bc->fac_out.Function = Fac_None;
  943. current->mode.ptp.bc->out_cause = AST_CAUSE_NORMAL_CLEARING;
  944. misdn_lib_send_event(current->mode.ptp.bc, EVENT_RELEASE_COMPLETE);
  945. }
  946. /* Remove the old call completion record */
  947. AST_LIST_REMOVE_CURRENT(list);
  948. ast_free(current);
  949. }
  950. }
  951. AST_LIST_TRAVERSE_SAFE_END;
  952. }
  953. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  954. #if defined(AST_MISDN_ENHANCEMENTS)
  955. /*!
  956. * \internal
  957. * \brief Allocate the next record id.
  958. *
  959. * \retval New record id on success.
  960. * \retval -1 on error.
  961. *
  962. * \note Assumes the misdn_cc_records_db lock is already obtained.
  963. */
  964. static long misdn_cc_record_id_new(void)
  965. {
  966. long record_id;
  967. long first_id;
  968. record_id = ++misdn_cc_record_id;
  969. first_id = record_id;
  970. while (misdn_cc_find_by_id(record_id)) {
  971. record_id = ++misdn_cc_record_id;
  972. if (record_id == first_id) {
  973. /*
  974. * We have a resource leak.
  975. * We should never need to allocate 64k records.
  976. */
  977. chan_misdn_log(0, 0, " --> ERROR Too many call completion records!\n");
  978. record_id = -1;
  979. break;
  980. }
  981. }
  982. return record_id;
  983. }
  984. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  985. #if defined(AST_MISDN_ENHANCEMENTS)
  986. /*!
  987. * \internal
  988. * \brief Create a new call completion record
  989. *
  990. * \retval pointer to new call completion record
  991. * \retval NULL if failed
  992. *
  993. * \note Assumes the misdn_cc_records_db lock is already obtained.
  994. */
  995. static struct misdn_cc_record *misdn_cc_new(void)
  996. {
  997. struct misdn_cc_record *cc_record;
  998. long record_id;
  999. misdn_cc_remove_old();
  1000. cc_record = ast_calloc(1, sizeof(*cc_record));
  1001. if (cc_record) {
  1002. record_id = misdn_cc_record_id_new();
  1003. if (record_id < 0) {
  1004. ast_free(cc_record);
  1005. return NULL;
  1006. }
  1007. /* Initialize the new record */
  1008. cc_record->record_id = record_id;
  1009. cc_record->port = -1;/* Invalid port so it will never be found this way */
  1010. cc_record->invoke_id = ++misdn_invoke_id;
  1011. cc_record->party_a_free = 1;/* Default User-A as free */
  1012. cc_record->error_code = FacError_None;
  1013. cc_record->reject_code = FacReject_None;
  1014. cc_record->time_created = time(NULL);
  1015. /* Insert the new record into the database */
  1016. AST_LIST_INSERT_HEAD(&misdn_cc_records_db, cc_record, list);
  1017. }
  1018. return cc_record;
  1019. }
  1020. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  1021. #if defined(AST_MISDN_ENHANCEMENTS)
  1022. /*!
  1023. * \internal
  1024. * \brief Destroy the call completion record database
  1025. *
  1026. * \return Nothing
  1027. */
  1028. static void misdn_cc_destroy(void)
  1029. {
  1030. struct misdn_cc_record *current;
  1031. while ((current = AST_LIST_REMOVE_HEAD(&misdn_cc_records_db, list))) {
  1032. /* Do a misdn_cc_delete(current) inline */
  1033. ast_free(current);
  1034. }
  1035. }
  1036. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  1037. #if defined(AST_MISDN_ENHANCEMENTS)
  1038. /*!
  1039. * \internal
  1040. * \brief Initialize the call completion record database
  1041. *
  1042. * \return Nothing
  1043. */
  1044. static void misdn_cc_init(void)
  1045. {
  1046. misdn_cc_record_id = 0;
  1047. }
  1048. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  1049. #if defined(AST_MISDN_ENHANCEMENTS)
  1050. /*!
  1051. * \internal
  1052. * \brief Check the status of an outstanding invocation request.
  1053. *
  1054. * \param data Points to an integer containing the call completion record id.
  1055. *
  1056. * \retval 0 if got a response.
  1057. * \retval -1 if no response yet.
  1058. */
  1059. static int misdn_cc_response_check(void *data)
  1060. {
  1061. int not_responded;
  1062. struct misdn_cc_record *cc_record;
  1063. AST_LIST_LOCK(&misdn_cc_records_db);
  1064. cc_record = misdn_cc_find_by_id(*(long *) data);
  1065. if (cc_record) {
  1066. if (cc_record->outstanding_message) {
  1067. not_responded = -1;
  1068. } else {
  1069. not_responded = 0;
  1070. }
  1071. } else {
  1072. /* No record so there is no response to check. */
  1073. not_responded = 0;
  1074. }
  1075. AST_LIST_UNLOCK(&misdn_cc_records_db);
  1076. return not_responded;
  1077. }
  1078. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  1079. #if defined(AST_MISDN_ENHANCEMENTS)
  1080. /*!
  1081. * \internal
  1082. * \brief Wait for a response from the switch for an outstanding
  1083. * invocation request.
  1084. *
  1085. * \param chan Asterisk channel to operate upon.
  1086. * \param wait_seconds Number of seconds to wait
  1087. * \param record_id Call completion record ID.
  1088. *
  1089. * \return Nothing
  1090. */
  1091. static void misdn_cc_response_wait(struct ast_channel *chan, int wait_seconds, long record_id)
  1092. {
  1093. unsigned count;
  1094. for (count = 2 * MISDN_CC_REQUEST_WAIT_MAX; count--;) {
  1095. /* Sleep in 500 ms increments */
  1096. if (ast_safe_sleep_conditional(chan, 500, misdn_cc_response_check, &record_id) != 0) {
  1097. /* We got hung up or our response came in. */
  1098. break;
  1099. }
  1100. }
  1101. }
  1102. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  1103. #if defined(AST_MISDN_ENHANCEMENTS)
  1104. /*!
  1105. * \internal
  1106. * \brief Convert the mISDN reject code to a string
  1107. *
  1108. * \param code mISDN reject code.
  1109. *
  1110. * \return The mISDN reject code as a string
  1111. */
  1112. static const char *misdn_to_str_reject_code(enum FacRejectCode code)
  1113. {
  1114. static const struct {
  1115. enum FacRejectCode code;
  1116. char *name;
  1117. } arr[] = {
  1118. /* *INDENT-OFF* */
  1119. { FacReject_None, "No reject occurred" },
  1120. { FacReject_Unknown, "Unknown reject code" },
  1121. { FacReject_Gen_UnrecognizedComponent, "General: Unrecognized Component" },
  1122. { FacReject_Gen_MistypedComponent, "General: Mistyped Component" },
  1123. { FacReject_Gen_BadlyStructuredComponent, "General: Badly Structured Component" },
  1124. { FacReject_Inv_DuplicateInvocation, "Invoke: Duplicate Invocation" },
  1125. { FacReject_Inv_UnrecognizedOperation, "Invoke: Unrecognized Operation" },
  1126. { FacReject_Inv_MistypedArgument, "Invoke: Mistyped Argument" },
  1127. { FacReject_Inv_ResourceLimitation, "Invoke: Resource Limitation" },
  1128. { FacReject_Inv_InitiatorReleasing, "Invoke: Initiator Releasing" },
  1129. { FacReject_Inv_UnrecognizedLinkedID, "Invoke: Unrecognized Linked ID" },
  1130. { FacReject_Inv_LinkedResponseUnexpected, "Invoke: Linked Response Unexpected" },
  1131. { FacReject_Inv_UnexpectedChildOperation, "Invoke: Unexpected Child Operation" },
  1132. { FacReject_Res_UnrecognizedInvocation, "Result: Unrecognized Invocation" },
  1133. { FacReject_Res_ResultResponseUnexpected, "Result: Result Response Unexpected" },
  1134. { FacReject_Res_MistypedResult, "Result: Mistyped Result" },
  1135. { FacReject_Err_UnrecognizedInvocation, "Error: Unrecognized Invocation" },
  1136. { FacReject_Err_ErrorResponseUnexpected, "Error: Error Response Unexpected" },
  1137. { FacReject_Err_UnrecognizedError, "Error: Unrecognized Error" },
  1138. { FacReject_Err_UnexpectedError, "Error: Unexpected Error" },
  1139. { FacReject_Err_MistypedParameter, "Error: Mistyped Parameter" },
  1140. /* *INDENT-ON* */
  1141. };
  1142. unsigned index;
  1143. for (index = 0; index < ARRAY_LEN(arr); ++index) {
  1144. if (arr[index].code == code) {
  1145. return arr[index].name;
  1146. }
  1147. }
  1148. return "unknown";
  1149. }
  1150. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  1151. #if defined(AST_MISDN_ENHANCEMENTS)
  1152. /*!
  1153. * \internal
  1154. * \brief Convert the mISDN error code to a string
  1155. *
  1156. * \param code mISDN error code.
  1157. *
  1158. * \return The mISDN error code as a string
  1159. */
  1160. static const char *misdn_to_str_error_code(enum FacErrorCode code)
  1161. {
  1162. static const struct {
  1163. enum FacErrorCode code;
  1164. char *name;
  1165. } arr[] = {
  1166. /* *INDENT-OFF* */
  1167. { FacError_None, "No error occurred" },
  1168. { FacError_Unknown, "Unknown OID error code" },
  1169. { FacError_Gen_NotSubscribed, "General: Not Subscribed" },
  1170. { FacError_Gen_NotAvailable, "General: Not Available" },
  1171. { FacError_Gen_NotImplemented, "General: Not Implemented" },
  1172. { FacError_Gen_InvalidServedUserNr, "General: Invalid Served User Number" },
  1173. { FacError_Gen_InvalidCallState, "General: Invalid Call State" },
  1174. { FacError_Gen_BasicServiceNotProvided, "General: Basic Service Not Provided" },
  1175. { FacError_Gen_NotIncomingCall, "General: Not Incoming Call" },
  1176. { FacError_Gen_SupplementaryServiceInteractionNotAllowed,"General: Supplementary Service Interaction Not Allowed" },
  1177. { FacError_Gen_ResourceUnavailable, "General: Resource Unavailable" },
  1178. { FacError_Div_InvalidDivertedToNr, "Diversion: Invalid Diverted To Number" },
  1179. { FacError_Div_SpecialServiceNr, "Diversion: Special Service Number" },
  1180. { FacError_Div_DiversionToServedUserNr, "Diversion: Diversion To Served User Number" },
  1181. { FacError_Div_IncomingCallAccepted, "Diversion: Incoming Call Accepted" },
  1182. { FacError_Div_NumberOfDiversionsExceeded, "Diversion: Number Of Diversions Exceeded" },
  1183. { FacError_Div_NotActivated, "Diversion: Not Activated" },
  1184. { FacError_Div_RequestAlreadyAccepted, "Diversion: Request Already Accepted" },
  1185. { FacError_AOC_NoChargingInfoAvailable, "AOC: No Charging Info Available" },
  1186. { FacError_CCBS_InvalidCallLinkageID, "CCBS: Invalid Call Linkage ID" },
  1187. { FacError_CCBS_InvalidCCBSReference, "CCBS: Invalid CCBS Reference" },
  1188. { FacError_CCBS_LongTermDenial, "CCBS: Long Term Denial" },
  1189. { FacError_CCBS_ShortTermDenial, "CCBS: Short Term Denial" },
  1190. { FacError_CCBS_IsAlreadyActivated, "CCBS: Is Already Activated" },
  1191. { FacError_CCBS_AlreadyAccepted, "CCBS: Already Accepted" },
  1192. { FacError_CCBS_OutgoingCCBSQueueFull, "CCBS: Outgoing CCBS Queue Full" },
  1193. { FacError_CCBS_CallFailureReasonNotBusy, "CCBS: Call Failure Reason Not Busy" },
  1194. { FacError_CCBS_NotReadyForCall, "CCBS: Not Ready For Call" },
  1195. { FacError_CCBS_T_LongTermDenial, "CCBS-T: Long Term Denial" },
  1196. { FacError_CCBS_T_ShortTermDenial, "CCBS-T: Short Term Denial" },
  1197. { FacError_ECT_LinkIdNotAssignedByNetwork, "ECT: Link ID Not Assigned By Network" },
  1198. /* *INDENT-ON* */
  1199. };
  1200. unsigned index;
  1201. for (index = 0; index < ARRAY_LEN(arr); ++index) {
  1202. if (arr[index].code == code) {
  1203. return arr[index].name;
  1204. }
  1205. }
  1206. return "unknown";
  1207. }
  1208. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  1209. #if defined(AST_MISDN_ENHANCEMENTS)
  1210. /*!
  1211. * \internal
  1212. * \brief Convert mISDN redirecting reason to diversion reason.
  1213. *
  1214. * \param reason mISDN redirecting reason code.
  1215. *
  1216. * \return Supported diversion reason code.
  1217. */
  1218. static unsigned misdn_to_diversion_reason(enum mISDN_REDIRECTING_REASON reason)
  1219. {
  1220. unsigned diversion_reason;
  1221. switch (reason) {
  1222. case mISDN_REDIRECTING_REASON_CALL_FWD:
  1223. diversion_reason = 1;/* cfu */
  1224. break;
  1225. case mISDN_REDIRECTING_REASON_CALL_FWD_BUSY:
  1226. diversion_reason = 2;/* cfb */
  1227. break;
  1228. case mISDN_REDIRECTING_REASON_NO_REPLY:
  1229. diversion_reason = 3;/* cfnr */
  1230. break;
  1231. default:
  1232. diversion_reason = 0;/* unknown */
  1233. break;
  1234. }
  1235. return diversion_reason;
  1236. }
  1237. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  1238. #if defined(AST_MISDN_ENHANCEMENTS)
  1239. /*!
  1240. * \internal
  1241. * \brief Convert diversion reason to mISDN redirecting reason
  1242. *
  1243. * \param diversion_reason Diversion reason to convert
  1244. *
  1245. * \return Supported redirecting reason code.
  1246. */
  1247. static enum mISDN_REDIRECTING_REASON diversion_reason_to_misdn(unsigned diversion_reason)
  1248. {
  1249. enum mISDN_REDIRECTING_REASON reason;
  1250. switch (diversion_reason) {
  1251. case 1:/* cfu */
  1252. reason = mISDN_REDIRECTING_REASON_CALL_FWD;
  1253. break;
  1254. case 2:/* cfb */
  1255. reason = mISDN_REDIRECTING_REASON_CALL_FWD_BUSY;
  1256. break;
  1257. case 3:/* cfnr */
  1258. reason = mISDN_REDIRECTING_REASON_NO_REPLY;
  1259. break;
  1260. default:
  1261. reason = mISDN_REDIRECTING_REASON_UNKNOWN;
  1262. break;
  1263. }
  1264. return reason;
  1265. }
  1266. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  1267. #if defined(AST_MISDN_ENHANCEMENTS)
  1268. /*!
  1269. * \internal
  1270. * \brief Convert the mISDN presentation to PresentedNumberUnscreened type
  1271. *
  1272. * \param presentation mISDN presentation to convert
  1273. * \param number_present TRUE if the number is present
  1274. *
  1275. * \return PresentedNumberUnscreened type
  1276. */
  1277. static unsigned misdn_to_PresentedNumberUnscreened_type(int presentation, int number_present)
  1278. {
  1279. unsigned type;
  1280. switch (presentation) {
  1281. case 0:/* allowed */
  1282. if (number_present) {
  1283. type = 0;/* presentationAllowedNumber */
  1284. } else {
  1285. type = 2;/* numberNotAvailableDueToInterworking */
  1286. }
  1287. break;
  1288. case 1:/* restricted */
  1289. if (number_present) {
  1290. type = 3;/* presentationRestrictedNumber */
  1291. } else {
  1292. type = 1;/* presentationRestricted */
  1293. }
  1294. break;
  1295. default:
  1296. type = 2;/* numberNotAvailableDueToInterworking */
  1297. break;
  1298. }
  1299. return type;
  1300. }
  1301. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  1302. #if defined(AST_MISDN_ENHANCEMENTS)
  1303. /*!
  1304. * \internal
  1305. * \brief Convert the PresentedNumberUnscreened type to mISDN presentation
  1306. *
  1307. * \param type PresentedNumberUnscreened type
  1308. *
  1309. * \return mISDN presentation
  1310. */
  1311. static int PresentedNumberUnscreened_to_misdn_pres(unsigned type)
  1312. {
  1313. int presentation;
  1314. switch (type) {
  1315. default:
  1316. case 0:/* presentationAllowedNumber */
  1317. presentation = 0;/* allowed */
  1318. break;
  1319. case 1:/* presentationRestricted */
  1320. case 3:/* presentationRestrictedNumber */
  1321. presentation = 1;/* restricted */
  1322. break;
  1323. case 2:/* numberNotAvailableDueToInterworking */
  1324. presentation = 2;/* unavailable */
  1325. break;
  1326. }
  1327. return presentation;
  1328. }
  1329. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  1330. #if defined(AST_MISDN_ENHANCEMENTS)
  1331. /*!
  1332. * \internal
  1333. * \brief Convert the mISDN numbering plan to PartyNumber numbering plan
  1334. *
  1335. * \param number_plan mISDN numbering plan
  1336. *
  1337. * \return PartyNumber numbering plan
  1338. */
  1339. static unsigned misdn_to_PartyNumber_plan(enum mISDN_NUMBER_PLAN number_plan)
  1340. {
  1341. unsigned party_plan;
  1342. switch (number_plan) {
  1343. default:
  1344. case NUMPLAN_UNKNOWN:
  1345. party_plan = 0;/* unknown */
  1346. break;
  1347. case NUMPLAN_ISDN:
  1348. party_plan = 1;/* public */
  1349. break;
  1350. case NUMPLAN_DATA:
  1351. party_plan = 3;/* data */
  1352. break;
  1353. case NUMPLAN_TELEX:
  1354. party_plan = 4;/* telex */
  1355. break;
  1356. case NUMPLAN_NATIONAL:
  1357. party_plan = 8;/* nationalStandard */
  1358. break;
  1359. case NUMPLAN_PRIVATE:
  1360. party_plan = 5;/* private */
  1361. break;
  1362. }
  1363. return party_plan;
  1364. }
  1365. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  1366. #if defined(AST_MISDN_ENHANCEMENTS)
  1367. /*!
  1368. * \internal
  1369. * \brief Convert PartyNumber numbering plan to mISDN numbering plan
  1370. *
  1371. * \param party_plan PartyNumber numbering plan
  1372. *
  1373. * \return mISDN numbering plan
  1374. */
  1375. static enum mISDN_NUMBER_PLAN PartyNumber_to_misdn_plan(unsigned party_plan)
  1376. {
  1377. enum mISDN_NUMBER_PLAN number_plan;
  1378. switch (party_plan) {
  1379. default:
  1380. case 0:/* unknown */
  1381. number_plan = NUMPLAN_UNKNOWN;
  1382. break;
  1383. case 1:/* public */
  1384. number_plan = NUMPLAN_ISDN;
  1385. break;
  1386. case 3:/* data */
  1387. number_plan = NUMPLAN_DATA;
  1388. break;
  1389. case 4:/* telex */
  1390. number_plan = NUMPLAN_TELEX;
  1391. break;
  1392. case 8:/* nationalStandard */
  1393. number_plan = NUMPLAN_NATIONAL;
  1394. break;
  1395. case 5:/* private */
  1396. number_plan = NUMPLAN_PRIVATE;
  1397. break;
  1398. }
  1399. return number_plan;
  1400. }
  1401. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  1402. #if defined(AST_MISDN_ENHANCEMENTS)
  1403. /*!
  1404. * \internal
  1405. * \brief Convert mISDN type-of-number to PartyNumber public type-of-number
  1406. *
  1407. * \param ton mISDN type-of-number
  1408. *
  1409. * \return PartyNumber public type-of-number
  1410. */
  1411. static unsigned misdn_to_PartyNumber_ton_public(enum mISDN_NUMBER_TYPE ton)
  1412. {
  1413. unsigned party_ton;
  1414. switch (ton) {
  1415. default:
  1416. case NUMTYPE_UNKNOWN:
  1417. party_ton = 0;/* unknown */
  1418. break;
  1419. case NUMTYPE_INTERNATIONAL:
  1420. party_ton = 1;/* internationalNumber */
  1421. break;
  1422. case NUMTYPE_NATIONAL:
  1423. party_ton = 2;/* nationalNumber */
  1424. break;
  1425. case NUMTYPE_NETWORK_SPECIFIC:
  1426. party_ton = 3;/* networkSpecificNumber */
  1427. break;
  1428. case NUMTYPE_SUBSCRIBER:
  1429. party_ton = 4;/* subscriberNumber */
  1430. break;
  1431. case NUMTYPE_ABBREVIATED:
  1432. party_ton = 6;/* abbreviatedNumber */
  1433. break;
  1434. }
  1435. return party_ton;
  1436. }
  1437. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  1438. #if defined(AST_MISDN_ENHANCEMENTS)
  1439. /*!
  1440. * \internal
  1441. * \brief Convert the PartyNumber public type-of-number to mISDN type-of-number
  1442. *
  1443. * \param party_ton PartyNumber public type-of-number
  1444. *
  1445. * \return mISDN type-of-number
  1446. */
  1447. static enum mISDN_NUMBER_TYPE PartyNumber_to_misdn_ton_public(unsigned party_ton)
  1448. {
  1449. enum mISDN_NUMBER_TYPE ton;
  1450. switch (party_ton) {
  1451. default:
  1452. case 0:/* unknown */
  1453. ton = NUMTYPE_UNKNOWN;
  1454. break;
  1455. case 1:/* internationalNumber */
  1456. ton = NUMTYPE_INTERNATIONAL;
  1457. break;
  1458. case 2:/* nationalNumber */
  1459. ton = NUMTYPE_NATIONAL;
  1460. break;
  1461. case 3:/* networkSpecificNumber */
  1462. ton = NUMTYPE_NETWORK_SPECIFIC;
  1463. break;
  1464. case 4:/* subscriberNumber */
  1465. ton = NUMTYPE_SUBSCRIBER;
  1466. break;
  1467. case 6:/* abbreviatedNumber */
  1468. ton = NUMTYPE_ABBREVIATED;
  1469. break;
  1470. }
  1471. return ton;
  1472. }
  1473. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  1474. #if defined(AST_MISDN_ENHANCEMENTS)
  1475. /*!
  1476. * \internal
  1477. * \brief Convert mISDN type-of-number to PartyNumber private type-of-number
  1478. *
  1479. * \param ton mISDN type-of-number
  1480. *
  1481. * \return PartyNumber private type-of-number
  1482. */
  1483. static unsigned misdn_to_PartyNumber_ton_private(enum mISDN_NUMBER_TYPE ton)
  1484. {
  1485. unsigned party_ton;
  1486. switch (ton) {
  1487. default:
  1488. case NUMTYPE_UNKNOWN:
  1489. party_ton = 0;/* unknown */
  1490. break;
  1491. case NUMTYPE_INTERNATIONAL:
  1492. party_ton = 1;/* level2RegionalNumber */
  1493. break;
  1494. case NUMTYPE_NATIONAL:
  1495. party_ton = 2;/* level1RegionalNumber */
  1496. break;
  1497. case NUMTYPE_NETWORK_SPECIFIC:
  1498. party_ton = 3;/* pTNSpecificNumber */
  1499. break;
  1500. case NUMTYPE_SUBSCRIBER:
  1501. party_ton = 4;/* localNumber */
  1502. break;
  1503. case NUMTYPE_ABBREVIATED:
  1504. party_ton = 6;/* abbreviatedNumber */
  1505. break;
  1506. }
  1507. return party_ton;
  1508. }
  1509. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  1510. #if defined(AST_MISDN_ENHANCEMENTS)
  1511. /*!
  1512. * \internal
  1513. * \brief Convert the PartyNumber private type-of-number to mISDN type-of-number
  1514. *
  1515. * \param party_ton PartyNumber private type-of-number
  1516. *
  1517. * \return mISDN type-of-number
  1518. */
  1519. static enum mISDN_NUMBER_TYPE PartyNumber_to_misdn_ton_private(unsigned party_ton)
  1520. {
  1521. enum mISDN_NUMBER_TYPE ton;
  1522. switch (party_ton) {
  1523. default:
  1524. case 0:/* unknown */
  1525. ton = NUMTYPE_UNKNOWN;
  1526. break;
  1527. case 1:/* level2RegionalNumber */
  1528. ton = NUMTYPE_INTERNATIONAL;
  1529. break;
  1530. case 2:/* level1RegionalNumber */
  1531. ton = NUMTYPE_NATIONAL;
  1532. break;
  1533. case 3:/* pTNSpecificNumber */
  1534. ton = NUMTYPE_NETWORK_SPECIFIC;
  1535. break;
  1536. case 4:/* localNumber */
  1537. ton = NUMTYPE_SUBSCRIBER;
  1538. break;
  1539. case 6:/* abbreviatedNumber */
  1540. ton = NUMTYPE_ABBREVIATED;
  1541. break;
  1542. }
  1543. return ton;
  1544. }
  1545. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  1546. /*!
  1547. * \internal
  1548. * \brief Convert the mISDN type of number code to a string
  1549. *
  1550. * \param number_type mISDN type of number code.
  1551. *
  1552. * \return The mISDN type of number code as a string
  1553. */
  1554. static const char *misdn_to_str_ton(enum mISDN_NUMBER_TYPE number_type)
  1555. {
  1556. const char *str;
  1557. switch (number_type) {
  1558. default:
  1559. case NUMTYPE_UNKNOWN:
  1560. str = "Unknown";
  1561. break;
  1562. case NUMTYPE_INTERNATIONAL:
  1563. str = "International";
  1564. break;
  1565. case NUMTYPE_NATIONAL:
  1566. str = "National";
  1567. break;
  1568. case NUMTYPE_NETWORK_SPECIFIC:
  1569. str = "Network Specific";
  1570. break;
  1571. case NUMTYPE_SUBSCRIBER:
  1572. str = "Subscriber";
  1573. break;
  1574. case NUMTYPE_ABBREVIATED:
  1575. str = "Abbreviated";
  1576. break;
  1577. }
  1578. return str;
  1579. }
  1580. /*!
  1581. * \internal
  1582. * \brief Convert the mISDN type of number code to Asterisk type of number code
  1583. *
  1584. * \param number_type mISDN type of number code.
  1585. *
  1586. * \return Asterisk type of number code
  1587. */
  1588. static int misdn_to_ast_ton(enum mISDN_NUMBER_TYPE number_type)
  1589. {
  1590. int ast_number_type;
  1591. switch (number_type) {
  1592. default:
  1593. case NUMTYPE_UNKNOWN:
  1594. ast_number_type = NUMTYPE_UNKNOWN << 4;
  1595. break;
  1596. case NUMTYPE_INTERNATIONAL:
  1597. ast_number_type = NUMTYPE_INTERNATIONAL << 4;
  1598. break;
  1599. case NUMTYPE_NATIONAL:
  1600. ast_number_type = NUMTYPE_NATIONAL << 4;
  1601. break;
  1602. case NUMTYPE_NETWORK_SPECIFIC:
  1603. ast_number_type = NUMTYPE_NETWORK_SPECIFIC << 4;
  1604. break;
  1605. case NUMTYPE_SUBSCRIBER:
  1606. ast_number_type = NUMTYPE_SUBSCRIBER << 4;
  1607. break;
  1608. case NUMTYPE_ABBREVIATED:
  1609. ast_number_type = NUMTYPE_ABBREVIATED << 4;
  1610. break;
  1611. }
  1612. return ast_number_type;
  1613. }
  1614. /*!
  1615. * \internal
  1616. * \brief Convert the Asterisk type of number code to mISDN type of number code
  1617. *
  1618. * \param ast_number_type Asterisk type of number code.
  1619. *
  1620. * \return mISDN type of number code
  1621. */
  1622. static enum mISDN_NUMBER_TYPE ast_to_misdn_ton(unsigned ast_number_type)
  1623. {
  1624. enum mISDN_NUMBER_TYPE number_type;
  1625. switch ((ast_number_type >> 4) & 0x07) {
  1626. default:
  1627. case NUMTYPE_UNKNOWN:
  1628. number_type = NUMTYPE_UNKNOWN;
  1629. break;
  1630. case NUMTYPE_INTERNATIONAL:
  1631. number_type = NUMTYPE_INTERNATIONAL;
  1632. break;
  1633. case NUMTYPE_NATIONAL:
  1634. number_type = NUMTYPE_NATIONAL;
  1635. break;
  1636. case NUMTYPE_NETWORK_SPECIFIC:
  1637. number_type = NUMTYPE_NETWORK_SPECIFIC;
  1638. break;
  1639. case NUMTYPE_SUBSCRIBER:
  1640. number_type = NUMTYPE_SUBSCRIBER;
  1641. break;
  1642. case NUMTYPE_ABBREVIATED:
  1643. number_type = NUMTYPE_ABBREVIATED;
  1644. break;
  1645. }
  1646. return number_type;
  1647. }
  1648. /*!
  1649. * \internal
  1650. * \brief Convert the mISDN numbering plan code to a string
  1651. *
  1652. * \param number_plan mISDN numbering plan code.
  1653. *
  1654. * \return The mISDN numbering plan code as a string
  1655. */
  1656. static const char *misdn_to_str_plan(enum mISDN_NUMBER_PLAN number_plan)
  1657. {
  1658. const char *str;
  1659. switch (number_plan) {
  1660. default:
  1661. case NUMPLAN_UNKNOWN:
  1662. str = "Unknown";
  1663. break;
  1664. case NUMPLAN_ISDN:
  1665. str = "ISDN";
  1666. break;
  1667. case NUMPLAN_DATA:
  1668. str = "Data";
  1669. break;
  1670. case NUMPLAN_TELEX:
  1671. str = "Telex";
  1672. break;
  1673. case NUMPLAN_NATIONAL:
  1674. str = "National";
  1675. break;
  1676. case NUMPLAN_PRIVATE:
  1677. str = "Private";
  1678. break;
  1679. }
  1680. return str;
  1681. }
  1682. /*!
  1683. * \internal
  1684. * \brief Convert the mISDN numbering plan code to Asterisk numbering plan code
  1685. *
  1686. * \param number_plan mISDN numbering plan code.
  1687. *
  1688. * \return Asterisk numbering plan code
  1689. */
  1690. static int misdn_to_ast_plan(enum mISDN_NUMBER_PLAN number_plan)
  1691. {
  1692. int ast_number_plan;
  1693. switch (number_plan) {
  1694. default:
  1695. case NUMPLAN_UNKNOWN:
  1696. ast_number_plan = NUMPLAN_UNKNOWN;
  1697. break;
  1698. case NUMPLAN_ISDN:
  1699. ast_number_plan = NUMPLAN_ISDN;
  1700. break;
  1701. case NUMPLAN_DATA:
  1702. ast_number_plan = NUMPLAN_DATA;
  1703. break;
  1704. case NUMPLAN_TELEX:
  1705. ast_number_plan = NUMPLAN_TELEX;
  1706. break;
  1707. case NUMPLAN_NATIONAL:
  1708. ast_number_plan = NUMPLAN_NATIONAL;
  1709. break;
  1710. case NUMPLAN_PRIVATE:
  1711. ast_number_plan = NUMPLAN_PRIVATE;
  1712. break;
  1713. }
  1714. return ast_number_plan;
  1715. }
  1716. /*!
  1717. * \internal
  1718. * \brief Convert the Asterisk numbering plan code to mISDN numbering plan code
  1719. *
  1720. * \param ast_number_plan Asterisk numbering plan code.
  1721. *
  1722. * \return mISDN numbering plan code
  1723. */
  1724. static enum mISDN_NUMBER_PLAN ast_to_misdn_plan(unsigned ast_number_plan)
  1725. {
  1726. enum mISDN_NUMBER_PLAN number_plan;
  1727. switch (ast_number_plan & 0x0F) {
  1728. default:
  1729. case NUMPLAN_UNKNOWN:
  1730. number_plan = NUMPLAN_UNKNOWN;
  1731. break;
  1732. case NUMPLAN_ISDN:
  1733. number_plan = NUMPLAN_ISDN;
  1734. break;
  1735. case NUMPLAN_DATA:
  1736. number_plan = NUMPLAN_DATA;
  1737. break;
  1738. case NUMPLAN_TELEX:
  1739. number_plan = NUMPLAN_TELEX;
  1740. break;
  1741. case NUMPLAN_NATIONAL:
  1742. number_plan = NUMPLAN_NATIONAL;
  1743. break;
  1744. case NUMPLAN_PRIVATE:
  1745. number_plan = NUMPLAN_PRIVATE;
  1746. break;
  1747. }
  1748. return number_plan;
  1749. }
  1750. /*!
  1751. * \internal
  1752. * \brief Convert the mISDN presentation code to a string
  1753. *
  1754. * \param presentation mISDN number presentation restriction code.
  1755. *
  1756. * \return The mISDN presentation code as a string
  1757. */
  1758. static const char *misdn_to_str_pres(int presentation)
  1759. {
  1760. const char *str;
  1761. switch (presentation) {
  1762. case 0:
  1763. str = "Allowed";
  1764. break;
  1765. case 1:
  1766. str = "Restricted";
  1767. break;
  1768. case 2:
  1769. str = "Unavailable";
  1770. break;
  1771. default:
  1772. str = "Unknown";
  1773. break;
  1774. }
  1775. return str;
  1776. }
  1777. /*!
  1778. * \internal
  1779. * \brief Convert the mISDN presentation code to Asterisk presentation code
  1780. *
  1781. * \param presentation mISDN number presentation restriction code.
  1782. *
  1783. * \return Asterisk presentation code
  1784. */
  1785. static int misdn_to_ast_pres(int presentation)
  1786. {
  1787. switch (presentation) {
  1788. default:
  1789. case 0:
  1790. presentation = AST_PRES_ALLOWED;
  1791. break;
  1792. case 1:
  1793. presentation = AST_PRES_RESTRICTED;
  1794. break;
  1795. case 2:
  1796. presentation = AST_PRES_UNAVAILABLE;
  1797. break;
  1798. }
  1799. return presentation;
  1800. }
  1801. /*!
  1802. * \internal
  1803. * \brief Convert the Asterisk presentation code to mISDN presentation code
  1804. *
  1805. * \param presentation Asterisk number presentation restriction code.
  1806. *
  1807. * \return mISDN presentation code
  1808. */
  1809. static int ast_to_misdn_pres(int presentation)
  1810. {
  1811. switch (presentation & AST_PRES_RESTRICTION) {
  1812. default:
  1813. case AST_PRES_ALLOWED:
  1814. presentation = 0;
  1815. break;
  1816. case AST_PRES_RESTRICTED:
  1817. presentation = 1;
  1818. break;
  1819. case AST_PRES_UNAVAILABLE:
  1820. presentation = 2;
  1821. break;
  1822. }
  1823. return presentation;
  1824. }
  1825. /*!
  1826. * \internal
  1827. * \brief Convert the mISDN screening code to a string
  1828. *
  1829. * \param screening mISDN number screening code.
  1830. *
  1831. * \return The mISDN screening code as a string
  1832. */
  1833. static const char *misdn_to_str_screen(int screening)
  1834. {
  1835. const char *str;
  1836. switch (screening) {
  1837. case 0:
  1838. str = "Unscreened";
  1839. break;
  1840. case 1:
  1841. str = "Passed Screen";
  1842. break;
  1843. case 2:
  1844. str = "Failed Screen";
  1845. break;
  1846. case 3:
  1847. str = "Network Number";
  1848. break;
  1849. default:
  1850. str = "Unknown";
  1851. break;
  1852. }
  1853. return str;
  1854. }
  1855. /*!
  1856. * \internal
  1857. * \brief Convert the mISDN screening code to Asterisk screening code
  1858. *
  1859. * \param screening mISDN number screening code.
  1860. *
  1861. * \return Asterisk screening code
  1862. */
  1863. static int misdn_to_ast_screen(int screening)
  1864. {
  1865. switch (screening) {
  1866. default:
  1867. case 0:
  1868. screening = AST_PRES_USER_NUMBER_UNSCREENED;
  1869. break;
  1870. case 1:
  1871. screening = AST_PRES_USER_NUMBER_PASSED_SCREEN;
  1872. break;
  1873. case 2:
  1874. screening = AST_PRES_USER_NUMBER_FAILED_SCREEN;
  1875. break;
  1876. case 3:
  1877. screening = AST_PRES_NETWORK_NUMBER;
  1878. break;
  1879. }
  1880. return screening;
  1881. }
  1882. /*!
  1883. * \internal
  1884. * \brief Convert the Asterisk screening code to mISDN screening code
  1885. *
  1886. * \param screening Asterisk number screening code.
  1887. *
  1888. * \return mISDN screening code
  1889. */
  1890. static int ast_to_misdn_screen(int screening)
  1891. {
  1892. switch (screening & AST_PRES_NUMBER_TYPE) {
  1893. default:
  1894. case AST_PRES_USER_NUMBER_UNSCREENED:
  1895. screening = 0;
  1896. break;
  1897. case AST_PRES_USER_NUMBER_PASSED_SCREEN:
  1898. screening = 1;
  1899. break;
  1900. case AST_PRES_USER_NUMBER_FAILED_SCREEN:
  1901. screening = 2;
  1902. break;
  1903. case AST_PRES_NETWORK_NUMBER:
  1904. screening = 3;
  1905. break;
  1906. }
  1907. return screening;
  1908. }
  1909. /*!
  1910. * \internal
  1911. * \brief Convert Asterisk redirecting reason to mISDN redirecting reason code.
  1912. *
  1913. * \param ast Asterisk redirecting reason code.
  1914. *
  1915. * \return mISDN reason code
  1916. */
  1917. static enum mISDN_REDIRECTING_REASON ast_to_misdn_reason(const enum AST_REDIRECTING_REASON ast)
  1918. {
  1919. unsigned index;
  1920. static const struct misdn_reasons {
  1921. enum AST_REDIRECTING_REASON ast;
  1922. enum mISDN_REDIRECTING_REASON q931;
  1923. } misdn_reason_table[] = {
  1924. /* *INDENT-OFF* */
  1925. { AST_REDIRECTING_REASON_UNKNOWN, mISDN_REDIRECTING_REASON_UNKNOWN },
  1926. { AST_REDIRECTING_REASON_USER_BUSY, mISDN_REDIRECTING_REASON_CALL_FWD_BUSY },
  1927. { AST_REDIRECTING_REASON_NO_ANSWER, mISDN_REDIRECTING_REASON_NO_REPLY },
  1928. { AST_REDIRECTING_REASON_UNAVAILABLE, mISDN_REDIRECTING_REASON_NO_REPLY },
  1929. { AST_REDIRECTING_REASON_UNCONDITIONAL, mISDN_REDIRECTING_REASON_CALL_FWD },
  1930. { AST_REDIRECTING_REASON_TIME_OF_DAY, mISDN_REDIRECTING_REASON_UNKNOWN },
  1931. { AST_REDIRECTING_REASON_DO_NOT_DISTURB, mISDN_REDIRECTING_REASON_UNKNOWN },
  1932. { AST_REDIRECTING_REASON_DEFLECTION, mISDN_REDIRECTING_REASON_DEFLECTION },
  1933. { AST_REDIRECTING_REASON_FOLLOW_ME, mISDN_REDIRECTING_REASON_UNKNOWN },
  1934. { AST_REDIRECTING_REASON_OUT_OF_ORDER, mISDN_REDIRECTING_REASON_OUT_OF_ORDER },
  1935. { AST_REDIRECTING_REASON_AWAY, mISDN_REDIRECTING_REASON_UNKNOWN },
  1936. { AST_REDIRECTING_REASON_CALL_FWD_DTE, mISDN_REDIRECTING_REASON_CALL_FWD_DTE }
  1937. /* *INDENT-ON* */
  1938. };
  1939. for (index = 0; index < ARRAY_LEN(misdn_reason_table); ++index) {
  1940. if (misdn_reason_table[index].ast == ast) {
  1941. return misdn_reason_table[index].q931;
  1942. }
  1943. }
  1944. return mISDN_REDIRECTING_REASON_UNKNOWN;
  1945. }
  1946. /*!
  1947. * \internal
  1948. * \brief Convert the mISDN redirecting reason to Asterisk redirecting reason code
  1949. *
  1950. * \param q931 mISDN redirecting reason code.
  1951. *
  1952. * \return Asterisk redirecting reason code
  1953. */
  1954. static enum AST_REDIRECTING_REASON misdn_to_ast_reason(const enum mISDN_REDIRECTING_REASON q931)
  1955. {
  1956. enum AST_REDIRECTING_REASON ast;
  1957. switch (q931) {
  1958. default:
  1959. case mISDN_REDIRECTING_REASON_UNKNOWN:
  1960. ast = AST_REDIRECTING_REASON_UNKNOWN;
  1961. break;
  1962. case mISDN_REDIRECTING_REASON_CALL_FWD_BUSY:
  1963. ast = AST_REDIRECTING_REASON_USER_BUSY;
  1964. break;
  1965. case mISDN_REDIRECTING_REASON_NO_REPLY:
  1966. ast = AST_REDIRECTING_REASON_NO_ANSWER;
  1967. break;
  1968. case mISDN_REDIRECTING_REASON_DEFLECTION:
  1969. ast = AST_REDIRECTING_REASON_DEFLECTION;
  1970. break;
  1971. case mISDN_REDIRECTING_REASON_OUT_OF_ORDER:
  1972. ast = AST_REDIRECTING_REASON_OUT_OF_ORDER;
  1973. break;
  1974. case mISDN_REDIRECTING_REASON_CALL_FWD_DTE:
  1975. ast = AST_REDIRECTING_REASON_CALL_FWD_DTE;
  1976. break;
  1977. case mISDN_REDIRECTING_REASON_CALL_FWD:
  1978. ast = AST_REDIRECTING_REASON_UNCONDITIONAL;
  1979. break;
  1980. }
  1981. return ast;
  1982. }
  1983. struct allowed_bearers {
  1984. char *name; /*!< Bearer capability name string used in /etc/misdn.conf allowed_bearers */
  1985. char *display; /*!< Bearer capability displayable name */
  1986. int cap; /*!< SETUP message bearer capability field code value */
  1987. int deprecated; /*!< TRUE if this entry is deprecated. (Misspelled or bad name to use) */
  1988. };
  1989. /* *INDENT-OFF* */
  1990. static const struct allowed_bearers allowed_bearers_array[] = {
  1991. /* Name, Displayable Name Bearer Capability, Deprecated */
  1992. { "speech", "Speech", INFO_CAPABILITY_SPEECH, 0 },
  1993. { "3_1khz", "3.1KHz Audio", INFO_CAPABILITY_AUDIO_3_1K, 0 },
  1994. { "digital_unrestricted", "Unrestricted Digital", INFO_CAPABILITY_DIGITAL_UNRESTRICTED, 0 },
  1995. { "digital_restricted", "Restricted Digital", INFO_CAPABILITY_DIGITAL_RESTRICTED, 0 },
  1996. { "digital_restriced", "Restricted Digital", INFO_CAPABILITY_DIGITAL_RESTRICTED, 1 }, /* Allow misspelling for backwards compatibility */
  1997. { "video", "Video", INFO_CAPABILITY_VIDEO, 0 }
  1998. };
  1999. /* *INDENT-ON* */
  2000. static const char *bearer2str(int cap)
  2001. {
  2002. unsigned index;
  2003. for (index = 0; index < ARRAY_LEN(allowed_bearers_array); ++index) {
  2004. if (allowed_bearers_array[index].cap == cap) {
  2005. return allowed_bearers_array[index].display;
  2006. }
  2007. }
  2008. return "Unknown Bearer";
  2009. }
  2010. #if defined(AST_MISDN_ENHANCEMENTS)
  2011. /*!
  2012. * \internal
  2013. * \brief Fill in facility PartyNumber information
  2014. *
  2015. * \param party PartyNumber structure to fill in.
  2016. * \param id Information to put in PartyNumber structure.
  2017. *
  2018. * \return Nothing
  2019. */
  2020. static void misdn_PartyNumber_fill(struct FacPartyNumber *party, const struct misdn_party_id *id)
  2021. {
  2022. ast_copy_string((char *) party->Number, id->number, sizeof(party->Number));
  2023. party->LengthOfNumber = strlen((char *) party->Number);
  2024. party->Type = misdn_to_PartyNumber_plan(id->number_plan);
  2025. switch (party->Type) {
  2026. case 1:/* public */
  2027. party->TypeOfNumber = misdn_to_PartyNumber_ton_public(id->number_type);
  2028. break;
  2029. case 5:/* private */
  2030. party->TypeOfNumber = misdn_to_PartyNumber_ton_private(id->number_type);
  2031. break;
  2032. default:
  2033. party->TypeOfNumber = 0;/* Don't care */
  2034. break;
  2035. }
  2036. }
  2037. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2038. #if defined(AST_MISDN_ENHANCEMENTS)
  2039. /*!
  2040. * \internal
  2041. * \brief Extract the information from PartyNumber
  2042. *
  2043. * \param id Where to put extracted PartyNumber information
  2044. * \param party PartyNumber information to extract
  2045. *
  2046. * \return Nothing
  2047. */
  2048. static void misdn_PartyNumber_extract(struct misdn_party_id *id, const struct FacPartyNumber *party)
  2049. {
  2050. if (party->LengthOfNumber) {
  2051. ast_copy_string(id->number, (char *) party->Number, sizeof(id->number));
  2052. id->number_plan = PartyNumber_to_misdn_plan(party->Type);
  2053. switch (party->Type) {
  2054. case 1:/* public */
  2055. id->number_type = PartyNumber_to_misdn_ton_public(party->TypeOfNumber);
  2056. break;
  2057. case 5:/* private */
  2058. id->number_type = PartyNumber_to_misdn_ton_private(party->TypeOfNumber);
  2059. break;
  2060. default:
  2061. id->number_type = NUMTYPE_UNKNOWN;
  2062. break;
  2063. }
  2064. } else {
  2065. /* Number not present */
  2066. id->number_type = NUMTYPE_UNKNOWN;
  2067. id->number_plan = NUMPLAN_ISDN;
  2068. id->number[0] = 0;
  2069. }
  2070. }
  2071. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2072. #if defined(AST_MISDN_ENHANCEMENTS)
  2073. /*!
  2074. * \internal
  2075. * \brief Fill in facility Address information
  2076. *
  2077. * \param Address Address structure to fill in.
  2078. * \param id Information to put in Address structure.
  2079. *
  2080. * \return Nothing
  2081. */
  2082. static void misdn_Address_fill(struct FacAddress *Address, const struct misdn_party_id *id)
  2083. {
  2084. misdn_PartyNumber_fill(&Address->Party, id);
  2085. /* Subaddresses are not supported yet */
  2086. Address->Subaddress.Length = 0;
  2087. }
  2088. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2089. #if defined(AST_MISDN_ENHANCEMENTS)
  2090. /*!
  2091. * \internal
  2092. * \brief Fill in facility PresentedNumberUnscreened information
  2093. *
  2094. * \param presented PresentedNumberUnscreened structure to fill in.
  2095. * \param id Information to put in PresentedNumberUnscreened structure.
  2096. *
  2097. * \return Nothing
  2098. */
  2099. static void misdn_PresentedNumberUnscreened_fill(struct FacPresentedNumberUnscreened *presented, const struct misdn_party_id *id)
  2100. {
  2101. presented->Type = misdn_to_PresentedNumberUnscreened_type(id->presentation, id->number[0] ? 1 : 0);
  2102. misdn_PartyNumber_fill(&presented->Unscreened, id);
  2103. }
  2104. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2105. #if defined(AST_MISDN_ENHANCEMENTS)
  2106. /*!
  2107. * \internal
  2108. * \brief Extract the information from PartyNumber
  2109. *
  2110. * \param id Where to put extracted PresentedNumberUnscreened information
  2111. * \param presented PresentedNumberUnscreened information to extract
  2112. *
  2113. * \return Nothing
  2114. */
  2115. static void misdn_PresentedNumberUnscreened_extract(struct misdn_party_id *id, const struct FacPresentedNumberUnscreened *presented)
  2116. {
  2117. id->presentation = PresentedNumberUnscreened_to_misdn_pres(presented->Type);
  2118. id->screening = 0;/* unscreened */
  2119. switch (presented->Type) {
  2120. case 0:/* presentationAllowedNumber */
  2121. case 3:/* presentationRestrictedNumber */
  2122. misdn_PartyNumber_extract(id, &presented->Unscreened);
  2123. break;
  2124. case 1:/* presentationRestricted */
  2125. case 2:/* numberNotAvailableDueToInterworking */
  2126. default:
  2127. /* Number not present (And uninitialized so do not even look at it!) */
  2128. id->number_type = NUMTYPE_UNKNOWN;
  2129. id->number_plan = NUMPLAN_ISDN;
  2130. id->number[0] = 0;
  2131. break;
  2132. }
  2133. }
  2134. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2135. #if defined(AST_MISDN_ENHANCEMENTS)
  2136. static const char Level_Spacing[] = " ";/* Work for up to 10 levels */
  2137. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2138. #if defined(AST_MISDN_ENHANCEMENTS)
  2139. static void print_facility_PartyNumber(unsigned Level, const struct FacPartyNumber *Party, const struct misdn_bchannel *bc)
  2140. {
  2141. if (Party->LengthOfNumber) {
  2142. const char *Spacing;
  2143. Spacing = &Level_Spacing[sizeof(Level_Spacing) - 1 - Level];
  2144. chan_misdn_log(1, bc->port, " -->%s PartyNumber: Type:%d\n",
  2145. Spacing, Party->Type);
  2146. switch (Party->Type) {
  2147. case 0: /* Unknown PartyNumber */
  2148. chan_misdn_log(1, bc->port, " -->%s Unknown: %s\n",
  2149. Spacing, Party->Number);
  2150. break;
  2151. case 1: /* Public PartyNumber */
  2152. chan_misdn_log(1, bc->port, " -->%s Public TON:%d %s\n",
  2153. Spacing, Party->TypeOfNumber, Party->Number);
  2154. break;
  2155. case 2: /* NSAP encoded PartyNumber */
  2156. chan_misdn_log(1, bc->port, " -->%s NSAP: %s\n",
  2157. Spacing, Party->Number);
  2158. break;
  2159. case 3: /* Data PartyNumber (Not used) */
  2160. chan_misdn_log(1, bc->port, " -->%s Data: %s\n",
  2161. Spacing, Party->Number);
  2162. break;
  2163. case 4: /* Telex PartyNumber (Not used) */
  2164. chan_misdn_log(1, bc->port, " -->%s Telex: %s\n",
  2165. Spacing, Party->Number);
  2166. break;
  2167. case 5: /* Private PartyNumber */
  2168. chan_misdn_log(1, bc->port, " -->%s Private TON:%d %s\n",
  2169. Spacing, Party->TypeOfNumber, Party->Number);
  2170. break;
  2171. case 8: /* National Standard PartyNumber (Not used) */
  2172. chan_misdn_log(1, bc->port, " -->%s National: %s\n",
  2173. Spacing, Party->Number);
  2174. break;
  2175. default:
  2176. break;
  2177. }
  2178. }
  2179. }
  2180. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2181. #if defined(AST_MISDN_ENHANCEMENTS)
  2182. static void print_facility_Subaddress(unsigned Level, const struct FacPartySubaddress *Subaddress, const struct misdn_bchannel *bc)
  2183. {
  2184. if (Subaddress->Length) {
  2185. const char *Spacing;
  2186. Spacing = &Level_Spacing[sizeof(Level_Spacing) - 1 - Level];
  2187. chan_misdn_log(1, bc->port, " -->%s Subaddress: Type:%d\n",
  2188. Spacing, Subaddress->Type);
  2189. switch (Subaddress->Type) {
  2190. case 0: /* UserSpecified */
  2191. if (Subaddress->u.UserSpecified.OddCountPresent) {
  2192. chan_misdn_log(1, bc->port, " -->%s User BCD OddCount:%d NumOctets:%d\n",
  2193. Spacing, Subaddress->u.UserSpecified.OddCount, Subaddress->Length);
  2194. } else {
  2195. chan_misdn_log(1, bc->port, " -->%s User: %s\n",
  2196. Spacing, Subaddress->u.UserSpecified.Information);
  2197. }
  2198. break;
  2199. case 1: /* NSAP */
  2200. chan_misdn_log(1, bc->port, " -->%s NSAP: %s\n",
  2201. Spacing, Subaddress->u.Nsap);
  2202. break;
  2203. default:
  2204. break;
  2205. }
  2206. }
  2207. }
  2208. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2209. #if defined(AST_MISDN_ENHANCEMENTS)
  2210. static void print_facility_Address(unsigned Level, const struct FacAddress *Address, const struct misdn_bchannel *bc)
  2211. {
  2212. print_facility_PartyNumber(Level, &Address->Party, bc);
  2213. print_facility_Subaddress(Level, &Address->Subaddress, bc);
  2214. }
  2215. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2216. #if defined(AST_MISDN_ENHANCEMENTS)
  2217. static void print_facility_PresentedNumberUnscreened(unsigned Level, const struct FacPresentedNumberUnscreened *Presented, const struct misdn_bchannel *bc)
  2218. {
  2219. const char *Spacing;
  2220. Spacing = &Level_Spacing[sizeof(Level_Spacing) - 1 - Level];
  2221. chan_misdn_log(1, bc->port, " -->%s Unscreened Type:%d\n", Spacing, Presented->Type);
  2222. switch (Presented->Type) {
  2223. case 0: /* presentationAllowedNumber */
  2224. chan_misdn_log(1, bc->port, " -->%s Allowed:\n", Spacing);
  2225. print_facility_PartyNumber(Level + 2, &Presented->Unscreened, bc);
  2226. break;
  2227. case 1: /* presentationRestricted */
  2228. chan_misdn_log(1, bc->port, " -->%s Restricted\n", Spacing);
  2229. break;
  2230. case 2: /* numberNotAvailableDueToInterworking */
  2231. chan_misdn_log(1, bc->port, " -->%s Not Available\n", Spacing);
  2232. break;
  2233. case 3: /* presentationRestrictedNumber */
  2234. chan_misdn_log(1, bc->port, " -->%s Restricted:\n", Spacing);
  2235. print_facility_PartyNumber(Level + 2, &Presented->Unscreened, bc);
  2236. break;
  2237. default:
  2238. break;
  2239. }
  2240. }
  2241. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2242. #if defined(AST_MISDN_ENHANCEMENTS)
  2243. static void print_facility_AddressScreened(unsigned Level, const struct FacAddressScreened *Address, const struct misdn_bchannel *bc)
  2244. {
  2245. const char *Spacing;
  2246. Spacing = &Level_Spacing[sizeof(Level_Spacing) - 1 - Level];
  2247. chan_misdn_log(1, bc->port, " -->%s ScreeningIndicator:%d\n", Spacing, Address->ScreeningIndicator);
  2248. print_facility_PartyNumber(Level, &Address->Party, bc);
  2249. print_facility_Subaddress(Level, &Address->Subaddress, bc);
  2250. }
  2251. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2252. #if defined(AST_MISDN_ENHANCEMENTS)
  2253. static void print_facility_PresentedAddressScreened(unsigned Level, const struct FacPresentedAddressScreened *Presented, const struct misdn_bchannel *bc)
  2254. {
  2255. const char *Spacing;
  2256. Spacing = &Level_Spacing[sizeof(Level_Spacing) - 1 - Level];
  2257. chan_misdn_log(1, bc->port, " -->%s Screened Type:%d\n", Spacing, Presented->Type);
  2258. switch (Presented->Type) {
  2259. case 0: /* presentationAllowedAddress */
  2260. chan_misdn_log(1, bc->port, " -->%s Allowed:\n", Spacing);
  2261. print_facility_AddressScreened(Level + 2, &Presented->Address, bc);
  2262. break;
  2263. case 1: /* presentationRestricted */
  2264. chan_misdn_log(1, bc->port, " -->%s Restricted\n", Spacing);
  2265. break;
  2266. case 2: /* numberNotAvailableDueToInterworking */
  2267. chan_misdn_log(1, bc->port, " -->%s Not Available\n", Spacing);
  2268. break;
  2269. case 3: /* presentationRestrictedAddress */
  2270. chan_misdn_log(1, bc->port, " -->%s Restricted:\n", Spacing);
  2271. print_facility_AddressScreened(Level + 2, &Presented->Address, bc);
  2272. break;
  2273. default:
  2274. break;
  2275. }
  2276. }
  2277. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2278. #if defined(AST_MISDN_ENHANCEMENTS)
  2279. static void print_facility_Q931_Bc_Hlc_Llc(unsigned Level, const struct Q931_Bc_Hlc_Llc *Q931ie, const struct misdn_bchannel *bc)
  2280. {
  2281. const char *Spacing;
  2282. Spacing = &Level_Spacing[sizeof(Level_Spacing) - 1 - Level];
  2283. chan_misdn_log(1, bc->port, " -->%s Q931ie:\n", Spacing);
  2284. if (Q931ie->Bc.Length) {
  2285. chan_misdn_log(1, bc->port, " -->%s Bc Len:%d\n", Spacing, Q931ie->Bc.Length);
  2286. }
  2287. if (Q931ie->Hlc.Length) {
  2288. chan_misdn_log(1, bc->port, " -->%s Hlc Len:%d\n", Spacing, Q931ie->Hlc.Length);
  2289. }
  2290. if (Q931ie->Llc.Length) {
  2291. chan_misdn_log(1, bc->port, " -->%s Llc Len:%d\n", Spacing, Q931ie->Llc.Length);
  2292. }
  2293. }
  2294. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2295. #if defined(AST_MISDN_ENHANCEMENTS)
  2296. static void print_facility_Q931_Bc_Hlc_Llc_Uu(unsigned Level, const struct Q931_Bc_Hlc_Llc_Uu *Q931ie, const struct misdn_bchannel *bc)
  2297. {
  2298. const char *Spacing;
  2299. Spacing = &Level_Spacing[sizeof(Level_Spacing) - 1 - Level];
  2300. chan_misdn_log(1, bc->port, " -->%s Q931ie:\n", Spacing);
  2301. if (Q931ie->Bc.Length) {
  2302. chan_misdn_log(1, bc->port, " -->%s Bc Len:%d\n", Spacing, Q931ie->Bc.Length);
  2303. }
  2304. if (Q931ie->Hlc.Length) {
  2305. chan_misdn_log(1, bc->port, " -->%s Hlc Len:%d\n", Spacing, Q931ie->Hlc.Length);
  2306. }
  2307. if (Q931ie->Llc.Length) {
  2308. chan_misdn_log(1, bc->port, " -->%s Llc Len:%d\n", Spacing, Q931ie->Llc.Length);
  2309. }
  2310. if (Q931ie->UserInfo.Length) {
  2311. chan_misdn_log(1, bc->port, " -->%s UserInfo Len:%d\n", Spacing, Q931ie->UserInfo.Length);
  2312. }
  2313. }
  2314. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2315. #if defined(AST_MISDN_ENHANCEMENTS)
  2316. static void print_facility_CallInformation(unsigned Level, const struct FacCallInformation *CallInfo, const struct misdn_bchannel *bc)
  2317. {
  2318. const char *Spacing;
  2319. Spacing = &Level_Spacing[sizeof(Level_Spacing) - 1 - Level];
  2320. chan_misdn_log(1, bc->port, " -->%s CCBSReference:%d\n",
  2321. Spacing, CallInfo->CCBSReference);
  2322. chan_misdn_log(1, bc->port, " -->%s AddressOfB:\n", Spacing);
  2323. print_facility_Address(Level + 1, &CallInfo->AddressOfB, bc);
  2324. print_facility_Q931_Bc_Hlc_Llc(Level, &CallInfo->Q931ie, bc);
  2325. if (CallInfo->SubaddressOfA.Length) {
  2326. chan_misdn_log(1, bc->port, " -->%s SubaddressOfA:\n", Spacing);
  2327. print_facility_Subaddress(Level + 1, &CallInfo->SubaddressOfA, bc);
  2328. }
  2329. }
  2330. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2331. #if defined(AST_MISDN_ENHANCEMENTS)
  2332. static void print_facility_ServedUserNr(unsigned Level, const struct FacPartyNumber *Party, const struct misdn_bchannel *bc)
  2333. {
  2334. const char *Spacing;
  2335. Spacing = &Level_Spacing[sizeof(Level_Spacing) - 1 - Level];
  2336. if (Party->LengthOfNumber) {
  2337. print_facility_PartyNumber(Level, Party, bc);
  2338. } else {
  2339. chan_misdn_log(1, bc->port, " -->%s All Numbers\n", Spacing);
  2340. }
  2341. }
  2342. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2343. #if defined(AST_MISDN_ENHANCEMENTS)
  2344. static void print_facility_IntResult(unsigned Level, const struct FacForwardingRecord *ForwardingRecord, const struct misdn_bchannel *bc)
  2345. {
  2346. const char *Spacing;
  2347. Spacing = &Level_Spacing[sizeof(Level_Spacing) - 1 - Level];
  2348. chan_misdn_log(1, bc->port, " -->%s Procedure:%d BasicService:%d\n",
  2349. Spacing,
  2350. ForwardingRecord->Procedure,
  2351. ForwardingRecord->BasicService);
  2352. chan_misdn_log(1, bc->port, " -->%s ForwardedTo:\n", Spacing);
  2353. print_facility_Address(Level + 1, &ForwardingRecord->ForwardedTo, bc);
  2354. chan_misdn_log(1, bc->port, " -->%s ServedUserNr:\n", Spacing);
  2355. print_facility_ServedUserNr(Level + 1, &ForwardingRecord->ServedUser, bc);
  2356. }
  2357. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2358. static void print_facility(const struct FacParm *fac, const struct misdn_bchannel *bc)
  2359. {
  2360. #if defined(AST_MISDN_ENHANCEMENTS)
  2361. unsigned Index;
  2362. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2363. switch (fac->Function) {
  2364. #if defined(AST_MISDN_ENHANCEMENTS)
  2365. case Fac_ActivationDiversion:
  2366. chan_misdn_log(1, bc->port, " --> ActivationDiversion: InvokeID:%d\n",
  2367. fac->u.ActivationDiversion.InvokeID);
  2368. switch (fac->u.ActivationDiversion.ComponentType) {
  2369. case FacComponent_Invoke:
  2370. chan_misdn_log(1, bc->port, " --> Invoke: Procedure:%d BasicService:%d\n",
  2371. fac->u.ActivationDiversion.Component.Invoke.Procedure,
  2372. fac->u.ActivationDiversion.Component.Invoke.BasicService);
  2373. chan_misdn_log(1, bc->port, " --> ForwardedTo:\n");
  2374. print_facility_Address(3, &fac->u.ActivationDiversion.Component.Invoke.ForwardedTo, bc);
  2375. chan_misdn_log(1, bc->port, " --> ServedUserNr:\n");
  2376. print_facility_ServedUserNr(3, &fac->u.ActivationDiversion.Component.Invoke.ServedUser, bc);
  2377. break;
  2378. case FacComponent_Result:
  2379. chan_misdn_log(1, bc->port, " --> Result\n");
  2380. break;
  2381. default:
  2382. break;
  2383. }
  2384. break;
  2385. case Fac_DeactivationDiversion:
  2386. chan_misdn_log(1, bc->port, " --> DeactivationDiversion: InvokeID:%d\n",
  2387. fac->u.DeactivationDiversion.InvokeID);
  2388. switch (fac->u.DeactivationDiversion.ComponentType) {
  2389. case FacComponent_Invoke:
  2390. chan_misdn_log(1, bc->port, " --> Invoke: Procedure:%d BasicService:%d\n",
  2391. fac->u.DeactivationDiversion.Component.Invoke.Procedure,
  2392. fac->u.DeactivationDiversion.Component.Invoke.BasicService);
  2393. chan_misdn_log(1, bc->port, " --> ServedUserNr:\n");
  2394. print_facility_ServedUserNr(3, &fac->u.DeactivationDiversion.Component.Invoke.ServedUser, bc);
  2395. break;
  2396. case FacComponent_Result:
  2397. chan_misdn_log(1, bc->port, " --> Result\n");
  2398. break;
  2399. default:
  2400. break;
  2401. }
  2402. break;
  2403. case Fac_ActivationStatusNotificationDiv:
  2404. chan_misdn_log(1, bc->port, " --> ActivationStatusNotificationDiv: InvokeID:%d Procedure:%d BasicService:%d\n",
  2405. fac->u.ActivationStatusNotificationDiv.InvokeID,
  2406. fac->u.ActivationStatusNotificationDiv.Procedure,
  2407. fac->u.ActivationStatusNotificationDiv.BasicService);
  2408. chan_misdn_log(1, bc->port, " --> ForwardedTo:\n");
  2409. print_facility_Address(2, &fac->u.ActivationStatusNotificationDiv.ForwardedTo, bc);
  2410. chan_misdn_log(1, bc->port, " --> ServedUserNr:\n");
  2411. print_facility_ServedUserNr(2, &fac->u.ActivationStatusNotificationDiv.ServedUser, bc);
  2412. break;
  2413. case Fac_DeactivationStatusNotificationDiv:
  2414. chan_misdn_log(1, bc->port, " --> DeactivationStatusNotificationDiv: InvokeID:%d Procedure:%d BasicService:%d\n",
  2415. fac->u.DeactivationStatusNotificationDiv.InvokeID,
  2416. fac->u.DeactivationStatusNotificationDiv.Procedure,
  2417. fac->u.DeactivationStatusNotificationDiv.BasicService);
  2418. chan_misdn_log(1, bc->port, " --> ServedUserNr:\n");
  2419. print_facility_ServedUserNr(2, &fac->u.DeactivationStatusNotificationDiv.ServedUser, bc);
  2420. break;
  2421. case Fac_InterrogationDiversion:
  2422. chan_misdn_log(1, bc->port, " --> InterrogationDiversion: InvokeID:%d\n",
  2423. fac->u.InterrogationDiversion.InvokeID);
  2424. switch (fac->u.InterrogationDiversion.ComponentType) {
  2425. case FacComponent_Invoke:
  2426. chan_misdn_log(1, bc->port, " --> Invoke: Procedure:%d BasicService:%d\n",
  2427. fac->u.InterrogationDiversion.Component.Invoke.Procedure,
  2428. fac->u.InterrogationDiversion.Component.Invoke.BasicService);
  2429. chan_misdn_log(1, bc->port, " --> ServedUserNr:\n");
  2430. print_facility_ServedUserNr(3, &fac->u.InterrogationDiversion.Component.Invoke.ServedUser, bc);
  2431. break;
  2432. case FacComponent_Result:
  2433. chan_misdn_log(1, bc->port, " --> Result:\n");
  2434. if (fac->u.InterrogationDiversion.Component.Result.NumRecords) {
  2435. for (Index = 0; Index < fac->u.InterrogationDiversion.Component.Result.NumRecords; ++Index) {
  2436. chan_misdn_log(1, bc->port, " --> IntResult[%d]:\n", Index);
  2437. print_facility_IntResult(3, &fac->u.InterrogationDiversion.Component.Result.List[Index], bc);
  2438. }
  2439. }
  2440. break;
  2441. default:
  2442. break;
  2443. }
  2444. break;
  2445. case Fac_DiversionInformation:
  2446. chan_misdn_log(1, bc->port, " --> DiversionInformation: InvokeID:%d Reason:%d BasicService:%d\n",
  2447. fac->u.DiversionInformation.InvokeID,
  2448. fac->u.DiversionInformation.DiversionReason,
  2449. fac->u.DiversionInformation.BasicService);
  2450. if (fac->u.DiversionInformation.ServedUserSubaddress.Length) {
  2451. chan_misdn_log(1, bc->port, " --> ServedUserSubaddress:\n");
  2452. print_facility_Subaddress(2, &fac->u.DiversionInformation.ServedUserSubaddress, bc);
  2453. }
  2454. if (fac->u.DiversionInformation.CallingAddressPresent) {
  2455. chan_misdn_log(1, bc->port, " --> CallingAddress:\n");
  2456. print_facility_PresentedAddressScreened(2, &fac->u.DiversionInformation.CallingAddress, bc);
  2457. }
  2458. if (fac->u.DiversionInformation.OriginalCalledPresent) {
  2459. chan_misdn_log(1, bc->port, " --> OriginalCalledNr:\n");
  2460. print_facility_PresentedNumberUnscreened(2, &fac->u.DiversionInformation.OriginalCalled, bc);
  2461. }
  2462. if (fac->u.DiversionInformation.LastDivertingPresent) {
  2463. chan_misdn_log(1, bc->port, " --> LastDivertingNr:\n");
  2464. print_facility_PresentedNumberUnscreened(2, &fac->u.DiversionInformation.LastDiverting, bc);
  2465. }
  2466. if (fac->u.DiversionInformation.LastDivertingReasonPresent) {
  2467. chan_misdn_log(1, bc->port, " --> LastDivertingReason:%d\n", fac->u.DiversionInformation.LastDivertingReason);
  2468. }
  2469. if (fac->u.DiversionInformation.UserInfo.Length) {
  2470. chan_misdn_log(1, bc->port, " --> UserInfo Length:%d\n", fac->u.DiversionInformation.UserInfo.Length);
  2471. }
  2472. break;
  2473. case Fac_CallDeflection:
  2474. chan_misdn_log(1, bc->port, " --> CallDeflection: InvokeID:%d\n",
  2475. fac->u.CallDeflection.InvokeID);
  2476. switch (fac->u.CallDeflection.ComponentType) {
  2477. case FacComponent_Invoke:
  2478. chan_misdn_log(1, bc->port, " --> Invoke:\n");
  2479. if (fac->u.CallDeflection.Component.Invoke.PresentationAllowedToDivertedToUserPresent) {
  2480. chan_misdn_log(1, bc->port, " --> PresentationAllowed:%d\n",
  2481. fac->u.CallDeflection.Component.Invoke.PresentationAllowedToDivertedToUser);
  2482. }
  2483. chan_misdn_log(1, bc->port, " --> DeflectionAddress:\n");
  2484. print_facility_Address(3, &fac->u.CallDeflection.Component.Invoke.Deflection, bc);
  2485. break;
  2486. case FacComponent_Result:
  2487. chan_misdn_log(1, bc->port, " --> Result\n");
  2488. break;
  2489. default:
  2490. break;
  2491. }
  2492. break;
  2493. case Fac_CallRerouteing:
  2494. chan_misdn_log(1, bc->port, " --> CallRerouteing: InvokeID:%d\n",
  2495. fac->u.CallRerouteing.InvokeID);
  2496. switch (fac->u.CallRerouteing.ComponentType) {
  2497. case FacComponent_Invoke:
  2498. chan_misdn_log(1, bc->port, " --> Invoke: Reason:%d Counter:%d\n",
  2499. fac->u.CallRerouteing.Component.Invoke.ReroutingReason,
  2500. fac->u.CallRerouteing.Component.Invoke.ReroutingCounter);
  2501. chan_misdn_log(1, bc->port, " --> CalledAddress:\n");
  2502. print_facility_Address(3, &fac->u.CallRerouteing.Component.Invoke.CalledAddress, bc);
  2503. print_facility_Q931_Bc_Hlc_Llc_Uu(2, &fac->u.CallRerouteing.Component.Invoke.Q931ie, bc);
  2504. chan_misdn_log(1, bc->port, " --> LastReroutingNr:\n");
  2505. print_facility_PresentedNumberUnscreened(3, &fac->u.CallRerouteing.Component.Invoke.LastRerouting, bc);
  2506. chan_misdn_log(1, bc->port, " --> SubscriptionOption:%d\n",
  2507. fac->u.CallRerouteing.Component.Invoke.SubscriptionOption);
  2508. if (fac->u.CallRerouteing.Component.Invoke.CallingPartySubaddress.Length) {
  2509. chan_misdn_log(1, bc->port, " --> CallingParty:\n");
  2510. print_facility_Subaddress(3, &fac->u.CallRerouteing.Component.Invoke.CallingPartySubaddress, bc);
  2511. }
  2512. break;
  2513. case FacComponent_Result:
  2514. chan_misdn_log(1, bc->port, " --> Result\n");
  2515. break;
  2516. default:
  2517. break;
  2518. }
  2519. break;
  2520. case Fac_InterrogateServedUserNumbers:
  2521. chan_misdn_log(1, bc->port, " --> InterrogateServedUserNumbers: InvokeID:%d\n",
  2522. fac->u.InterrogateServedUserNumbers.InvokeID);
  2523. switch (fac->u.InterrogateServedUserNumbers.ComponentType) {
  2524. case FacComponent_Invoke:
  2525. chan_misdn_log(1, bc->port, " --> Invoke\n");
  2526. break;
  2527. case FacComponent_Result:
  2528. chan_misdn_log(1, bc->port, " --> Result:\n");
  2529. if (fac->u.InterrogateServedUserNumbers.Component.Result.NumRecords) {
  2530. for (Index = 0; Index < fac->u.InterrogateServedUserNumbers.Component.Result.NumRecords; ++Index) {
  2531. chan_misdn_log(1, bc->port, " --> ServedUserNr[%d]:\n", Index);
  2532. print_facility_PartyNumber(3, &fac->u.InterrogateServedUserNumbers.Component.Result.List[Index], bc);
  2533. }
  2534. }
  2535. break;
  2536. default:
  2537. break;
  2538. }
  2539. break;
  2540. case Fac_DivertingLegInformation1:
  2541. chan_misdn_log(1, bc->port, " --> DivertingLegInformation1: InvokeID:%d Reason:%d SubscriptionOption:%d\n",
  2542. fac->u.DivertingLegInformation1.InvokeID,
  2543. fac->u.DivertingLegInformation1.DiversionReason,
  2544. fac->u.DivertingLegInformation1.SubscriptionOption);
  2545. if (fac->u.DivertingLegInformation1.DivertedToPresent) {
  2546. chan_misdn_log(1, bc->port, " --> DivertedToNr:\n");
  2547. print_facility_PresentedNumberUnscreened(2, &fac->u.DivertingLegInformation1.DivertedTo, bc);
  2548. }
  2549. break;
  2550. case Fac_DivertingLegInformation2:
  2551. chan_misdn_log(1, bc->port, " --> DivertingLegInformation2: InvokeID:%d Reason:%d Count:%d\n",
  2552. fac->u.DivertingLegInformation2.InvokeID,
  2553. fac->u.DivertingLegInformation2.DiversionReason,
  2554. fac->u.DivertingLegInformation2.DiversionCounter);
  2555. if (fac->u.DivertingLegInformation2.DivertingPresent) {
  2556. chan_misdn_log(1, bc->port, " --> DivertingNr:\n");
  2557. print_facility_PresentedNumberUnscreened(2, &fac->u.DivertingLegInformation2.Diverting, bc);
  2558. }
  2559. if (fac->u.DivertingLegInformation2.OriginalCalledPresent) {
  2560. chan_misdn_log(1, bc->port, " --> OriginalCalledNr:\n");
  2561. print_facility_PresentedNumberUnscreened(2, &fac->u.DivertingLegInformation2.OriginalCalled, bc);
  2562. }
  2563. break;
  2564. case Fac_DivertingLegInformation3:
  2565. chan_misdn_log(1, bc->port, " --> DivertingLegInformation3: InvokeID:%d PresentationAllowed:%d\n",
  2566. fac->u.DivertingLegInformation3.InvokeID,
  2567. fac->u.DivertingLegInformation3.PresentationAllowedIndicator);
  2568. break;
  2569. #else /* !defined(AST_MISDN_ENHANCEMENTS) */
  2570. case Fac_CD:
  2571. chan_misdn_log(1, bc->port, " --> calldeflect to: %s, presentable: %s\n", fac->u.CDeflection.DeflectedToNumber,
  2572. fac->u.CDeflection.PresentationAllowed ? "yes" : "no");
  2573. break;
  2574. #endif /* !defined(AST_MISDN_ENHANCEMENTS) */
  2575. case Fac_AOCDCurrency:
  2576. if (fac->u.AOCDcur.chargeNotAvailable) {
  2577. chan_misdn_log(1, bc->port, " --> AOCD currency: charge not available\n");
  2578. } else if (fac->u.AOCDcur.freeOfCharge) {
  2579. chan_misdn_log(1, bc->port, " --> AOCD currency: free of charge\n");
  2580. } else if (fac->u.AOCDchu.billingId >= 0) {
  2581. chan_misdn_log(1, bc->port, " --> AOCD currency: currency:%s amount:%d multiplier:%d typeOfChargingInfo:%s billingId:%d\n",
  2582. fac->u.AOCDcur.currency, fac->u.AOCDcur.currencyAmount, fac->u.AOCDcur.multiplier,
  2583. (fac->u.AOCDcur.typeOfChargingInfo == 0) ? "subTotal" : "total", fac->u.AOCDcur.billingId);
  2584. } else {
  2585. chan_misdn_log(1, bc->port, " --> AOCD currency: currency:%s amount:%d multiplier:%d typeOfChargingInfo:%s\n",
  2586. fac->u.AOCDcur.currency, fac->u.AOCDcur.currencyAmount, fac->u.AOCDcur.multiplier,
  2587. (fac->u.AOCDcur.typeOfChargingInfo == 0) ? "subTotal" : "total");
  2588. }
  2589. break;
  2590. case Fac_AOCDChargingUnit:
  2591. if (fac->u.AOCDchu.chargeNotAvailable) {
  2592. chan_misdn_log(1, bc->port, " --> AOCD charging unit: charge not available\n");
  2593. } else if (fac->u.AOCDchu.freeOfCharge) {
  2594. chan_misdn_log(1, bc->port, " --> AOCD charging unit: free of charge\n");
  2595. } else if (fac->u.AOCDchu.billingId >= 0) {
  2596. chan_misdn_log(1, bc->port, " --> AOCD charging unit: recordedUnits:%d typeOfChargingInfo:%s billingId:%d\n",
  2597. fac->u.AOCDchu.recordedUnits, (fac->u.AOCDchu.typeOfChargingInfo == 0) ? "subTotal" : "total", fac->u.AOCDchu.billingId);
  2598. } else {
  2599. chan_misdn_log(1, bc->port, " --> AOCD charging unit: recordedUnits:%d typeOfChargingInfo:%s\n",
  2600. fac->u.AOCDchu.recordedUnits, (fac->u.AOCDchu.typeOfChargingInfo == 0) ? "subTotal" : "total");
  2601. }
  2602. break;
  2603. #if defined(AST_MISDN_ENHANCEMENTS)
  2604. case Fac_ERROR:
  2605. chan_misdn_log(1, bc->port, " --> ERROR: InvokeID:%d, Code:0x%02x\n",
  2606. fac->u.ERROR.invokeId, fac->u.ERROR.errorValue);
  2607. break;
  2608. case Fac_RESULT:
  2609. chan_misdn_log(1, bc->port, " --> RESULT: InvokeID:%d\n",
  2610. fac->u.RESULT.InvokeID);
  2611. break;
  2612. case Fac_REJECT:
  2613. if (fac->u.REJECT.InvokeIDPresent) {
  2614. chan_misdn_log(1, bc->port, " --> REJECT: InvokeID:%d, Code:0x%02x\n",
  2615. fac->u.REJECT.InvokeID, fac->u.REJECT.Code);
  2616. } else {
  2617. chan_misdn_log(1, bc->port, " --> REJECT: Code:0x%02x\n",
  2618. fac->u.REJECT.Code);
  2619. }
  2620. break;
  2621. case Fac_EctExecute:
  2622. chan_misdn_log(1, bc->port, " --> EctExecute: InvokeID:%d\n",
  2623. fac->u.EctExecute.InvokeID);
  2624. break;
  2625. case Fac_ExplicitEctExecute:
  2626. chan_misdn_log(1, bc->port, " --> ExplicitEctExecute: InvokeID:%d LinkID:%d\n",
  2627. fac->u.ExplicitEctExecute.InvokeID,
  2628. fac->u.ExplicitEctExecute.LinkID);
  2629. break;
  2630. case Fac_RequestSubaddress:
  2631. chan_misdn_log(1, bc->port, " --> RequestSubaddress: InvokeID:%d\n",
  2632. fac->u.RequestSubaddress.InvokeID);
  2633. break;
  2634. case Fac_SubaddressTransfer:
  2635. chan_misdn_log(1, bc->port, " --> SubaddressTransfer: InvokeID:%d\n",
  2636. fac->u.SubaddressTransfer.InvokeID);
  2637. print_facility_Subaddress(1, &fac->u.SubaddressTransfer.Subaddress, bc);
  2638. break;
  2639. case Fac_EctLinkIdRequest:
  2640. chan_misdn_log(1, bc->port, " --> EctLinkIdRequest: InvokeID:%d\n",
  2641. fac->u.EctLinkIdRequest.InvokeID);
  2642. switch (fac->u.EctLinkIdRequest.ComponentType) {
  2643. case FacComponent_Invoke:
  2644. chan_misdn_log(1, bc->port, " --> Invoke\n");
  2645. break;
  2646. case FacComponent_Result:
  2647. chan_misdn_log(1, bc->port, " --> Result: LinkID:%d\n",
  2648. fac->u.EctLinkIdRequest.Component.Result.LinkID);
  2649. break;
  2650. default:
  2651. break;
  2652. }
  2653. break;
  2654. case Fac_EctInform:
  2655. chan_misdn_log(1, bc->port, " --> EctInform: InvokeID:%d Status:%d\n",
  2656. fac->u.EctInform.InvokeID,
  2657. fac->u.EctInform.Status);
  2658. if (fac->u.EctInform.RedirectionPresent) {
  2659. chan_misdn_log(1, bc->port, " --> Redirection Number\n");
  2660. print_facility_PresentedNumberUnscreened(2, &fac->u.EctInform.Redirection, bc);
  2661. }
  2662. break;
  2663. case Fac_EctLoopTest:
  2664. chan_misdn_log(1, bc->port, " --> EctLoopTest: InvokeID:%d\n",
  2665. fac->u.EctLoopTest.InvokeID);
  2666. switch (fac->u.EctLoopTest.ComponentType) {
  2667. case FacComponent_Invoke:
  2668. chan_misdn_log(1, bc->port, " --> Invoke: CallTransferID:%d\n",
  2669. fac->u.EctLoopTest.Component.Invoke.CallTransferID);
  2670. break;
  2671. case FacComponent_Result:
  2672. chan_misdn_log(1, bc->port, " --> Result: LoopResult:%d\n",
  2673. fac->u.EctLoopTest.Component.Result.LoopResult);
  2674. break;
  2675. default:
  2676. break;
  2677. }
  2678. break;
  2679. case Fac_StatusRequest:
  2680. chan_misdn_log(1, bc->port, " --> StatusRequest: InvokeID:%d\n",
  2681. fac->u.StatusRequest.InvokeID);
  2682. switch (fac->u.StatusRequest.ComponentType) {
  2683. case FacComponent_Invoke:
  2684. chan_misdn_log(1, bc->port, " --> Invoke: Compatibility:%d\n",
  2685. fac->u.StatusRequest.Component.Invoke.CompatibilityMode);
  2686. break;
  2687. case FacComponent_Result:
  2688. chan_misdn_log(1, bc->port, " --> Result: Status:%d\n",
  2689. fac->u.StatusRequest.Component.Result.Status);
  2690. break;
  2691. default:
  2692. break;
  2693. }
  2694. break;
  2695. case Fac_CallInfoRetain:
  2696. chan_misdn_log(1, bc->port, " --> CallInfoRetain: InvokeID:%d, LinkageID:%d\n",
  2697. fac->u.CallInfoRetain.InvokeID, fac->u.CallInfoRetain.CallLinkageID);
  2698. break;
  2699. case Fac_CCBSDeactivate:
  2700. chan_misdn_log(1, bc->port, " --> CCBSDeactivate: InvokeID:%d\n",
  2701. fac->u.CCBSDeactivate.InvokeID);
  2702. switch (fac->u.CCBSDeactivate.ComponentType) {
  2703. case FacComponent_Invoke:
  2704. chan_misdn_log(1, bc->port, " --> Invoke: CCBSReference:%d\n",
  2705. fac->u.CCBSDeactivate.Component.Invoke.CCBSReference);
  2706. break;
  2707. case FacComponent_Result:
  2708. chan_misdn_log(1, bc->port, " --> Result\n");
  2709. break;
  2710. default:
  2711. break;
  2712. }
  2713. break;
  2714. case Fac_CCBSErase:
  2715. chan_misdn_log(1, bc->port, " --> CCBSErase: InvokeID:%d, CCBSReference:%d RecallMode:%d, Reason:%d\n",
  2716. fac->u.CCBSErase.InvokeID, fac->u.CCBSErase.CCBSReference,
  2717. fac->u.CCBSErase.RecallMode, fac->u.CCBSErase.Reason);
  2718. chan_misdn_log(1, bc->port, " --> AddressOfB\n");
  2719. print_facility_Address(2, &fac->u.CCBSErase.AddressOfB, bc);
  2720. print_facility_Q931_Bc_Hlc_Llc(1, &fac->u.CCBSErase.Q931ie, bc);
  2721. break;
  2722. case Fac_CCBSRemoteUserFree:
  2723. chan_misdn_log(1, bc->port, " --> CCBSRemoteUserFree: InvokeID:%d, CCBSReference:%d RecallMode:%d\n",
  2724. fac->u.CCBSRemoteUserFree.InvokeID, fac->u.CCBSRemoteUserFree.CCBSReference,
  2725. fac->u.CCBSRemoteUserFree.RecallMode);
  2726. chan_misdn_log(1, bc->port, " --> AddressOfB\n");
  2727. print_facility_Address(2, &fac->u.CCBSRemoteUserFree.AddressOfB, bc);
  2728. print_facility_Q931_Bc_Hlc_Llc(1, &fac->u.CCBSRemoteUserFree.Q931ie, bc);
  2729. break;
  2730. case Fac_CCBSCall:
  2731. chan_misdn_log(1, bc->port, " --> CCBSCall: InvokeID:%d, CCBSReference:%d\n",
  2732. fac->u.CCBSCall.InvokeID, fac->u.CCBSCall.CCBSReference);
  2733. break;
  2734. case Fac_CCBSStatusRequest:
  2735. chan_misdn_log(1, bc->port, " --> CCBSStatusRequest: InvokeID:%d\n",
  2736. fac->u.CCBSStatusRequest.InvokeID);
  2737. switch (fac->u.CCBSStatusRequest.ComponentType) {
  2738. case FacComponent_Invoke:
  2739. chan_misdn_log(1, bc->port, " --> Invoke: CCBSReference:%d RecallMode:%d\n",
  2740. fac->u.CCBSStatusRequest.Component.Invoke.CCBSReference,
  2741. fac->u.CCBSStatusRequest.Component.Invoke.RecallMode);
  2742. print_facility_Q931_Bc_Hlc_Llc(2, &fac->u.CCBSStatusRequest.Component.Invoke.Q931ie, bc);
  2743. break;
  2744. case FacComponent_Result:
  2745. chan_misdn_log(1, bc->port, " --> Result: Free:%d\n",
  2746. fac->u.CCBSStatusRequest.Component.Result.Free);
  2747. break;
  2748. default:
  2749. break;
  2750. }
  2751. break;
  2752. case Fac_CCBSBFree:
  2753. chan_misdn_log(1, bc->port, " --> CCBSBFree: InvokeID:%d, CCBSReference:%d RecallMode:%d\n",
  2754. fac->u.CCBSBFree.InvokeID, fac->u.CCBSBFree.CCBSReference,
  2755. fac->u.CCBSBFree.RecallMode);
  2756. chan_misdn_log(1, bc->port, " --> AddressOfB\n");
  2757. print_facility_Address(2, &fac->u.CCBSBFree.AddressOfB, bc);
  2758. print_facility_Q931_Bc_Hlc_Llc(1, &fac->u.CCBSBFree.Q931ie, bc);
  2759. break;
  2760. case Fac_EraseCallLinkageID:
  2761. chan_misdn_log(1, bc->port, " --> EraseCallLinkageID: InvokeID:%d, LinkageID:%d\n",
  2762. fac->u.EraseCallLinkageID.InvokeID, fac->u.EraseCallLinkageID.CallLinkageID);
  2763. break;
  2764. case Fac_CCBSStopAlerting:
  2765. chan_misdn_log(1, bc->port, " --> CCBSStopAlerting: InvokeID:%d, CCBSReference:%d\n",
  2766. fac->u.CCBSStopAlerting.InvokeID, fac->u.CCBSStopAlerting.CCBSReference);
  2767. break;
  2768. case Fac_CCBSRequest:
  2769. chan_misdn_log(1, bc->port, " --> CCBSRequest: InvokeID:%d\n",
  2770. fac->u.CCBSRequest.InvokeID);
  2771. switch (fac->u.CCBSRequest.ComponentType) {
  2772. case FacComponent_Invoke:
  2773. chan_misdn_log(1, bc->port, " --> Invoke: LinkageID:%d\n",
  2774. fac->u.CCBSRequest.Component.Invoke.CallLinkageID);
  2775. break;
  2776. case FacComponent_Result:
  2777. chan_misdn_log(1, bc->port, " --> Result: CCBSReference:%d RecallMode:%d\n",
  2778. fac->u.CCBSRequest.Component.Result.CCBSReference,
  2779. fac->u.CCBSRequest.Component.Result.RecallMode);
  2780. break;
  2781. default:
  2782. break;
  2783. }
  2784. break;
  2785. case Fac_CCBSInterrogate:
  2786. chan_misdn_log(1, bc->port, " --> CCBSInterrogate: InvokeID:%d\n",
  2787. fac->u.CCBSInterrogate.InvokeID);
  2788. switch (fac->u.CCBSInterrogate.ComponentType) {
  2789. case FacComponent_Invoke:
  2790. chan_misdn_log(1, bc->port, " --> Invoke\n");
  2791. if (fac->u.CCBSInterrogate.Component.Invoke.CCBSReferencePresent) {
  2792. chan_misdn_log(1, bc->port, " --> CCBSReference:%d\n",
  2793. fac->u.CCBSInterrogate.Component.Invoke.CCBSReference);
  2794. }
  2795. if (fac->u.CCBSInterrogate.Component.Invoke.AParty.LengthOfNumber) {
  2796. chan_misdn_log(1, bc->port, " --> AParty\n");
  2797. print_facility_PartyNumber(3, &fac->u.CCBSInterrogate.Component.Invoke.AParty, bc);
  2798. }
  2799. break;
  2800. case FacComponent_Result:
  2801. chan_misdn_log(1, bc->port, " --> Result: RecallMode:%d\n",
  2802. fac->u.CCBSInterrogate.Component.Result.RecallMode);
  2803. if (fac->u.CCBSInterrogate.Component.Result.NumRecords) {
  2804. for (Index = 0; Index < fac->u.CCBSInterrogate.Component.Result.NumRecords; ++Index) {
  2805. chan_misdn_log(1, bc->port, " --> CallDetails[%d]:\n", Index);
  2806. print_facility_CallInformation(3, &fac->u.CCBSInterrogate.Component.Result.CallDetails[Index], bc);
  2807. }
  2808. }
  2809. break;
  2810. default:
  2811. break;
  2812. }
  2813. break;
  2814. case Fac_CCNRRequest:
  2815. chan_misdn_log(1, bc->port, " --> CCNRRequest: InvokeID:%d\n",
  2816. fac->u.CCNRRequest.InvokeID);
  2817. switch (fac->u.CCNRRequest.ComponentType) {
  2818. case FacComponent_Invoke:
  2819. chan_misdn_log(1, bc->port, " --> Invoke: LinkageID:%d\n",
  2820. fac->u.CCNRRequest.Component.Invoke.CallLinkageID);
  2821. break;
  2822. case FacComponent_Result:
  2823. chan_misdn_log(1, bc->port, " --> Result: CCBSReference:%d RecallMode:%d\n",
  2824. fac->u.CCNRRequest.Component.Result.CCBSReference,
  2825. fac->u.CCNRRequest.Component.Result.RecallMode);
  2826. break;
  2827. default:
  2828. break;
  2829. }
  2830. break;
  2831. case Fac_CCNRInterrogate:
  2832. chan_misdn_log(1, bc->port, " --> CCNRInterrogate: InvokeID:%d\n",
  2833. fac->u.CCNRInterrogate.InvokeID);
  2834. switch (fac->u.CCNRInterrogate.ComponentType) {
  2835. case FacComponent_Invoke:
  2836. chan_misdn_log(1, bc->port, " --> Invoke\n");
  2837. if (fac->u.CCNRInterrogate.Component.Invoke.CCBSReferencePresent) {
  2838. chan_misdn_log(1, bc->port, " --> CCBSReference:%d\n",
  2839. fac->u.CCNRInterrogate.Component.Invoke.CCBSReference);
  2840. }
  2841. if (fac->u.CCNRInterrogate.Component.Invoke.AParty.LengthOfNumber) {
  2842. chan_misdn_log(1, bc->port, " --> AParty\n");
  2843. print_facility_PartyNumber(3, &fac->u.CCNRInterrogate.Component.Invoke.AParty, bc);
  2844. }
  2845. break;
  2846. case FacComponent_Result:
  2847. chan_misdn_log(1, bc->port, " --> Result: RecallMode:%d\n",
  2848. fac->u.CCNRInterrogate.Component.Result.RecallMode);
  2849. if (fac->u.CCNRInterrogate.Component.Result.NumRecords) {
  2850. for (Index = 0; Index < fac->u.CCNRInterrogate.Component.Result.NumRecords; ++Index) {
  2851. chan_misdn_log(1, bc->port, " --> CallDetails[%d]:\n", Index);
  2852. print_facility_CallInformation(3, &fac->u.CCNRInterrogate.Component.Result.CallDetails[Index], bc);
  2853. }
  2854. }
  2855. break;
  2856. default:
  2857. break;
  2858. }
  2859. break;
  2860. case Fac_CCBS_T_Call:
  2861. chan_misdn_log(1, bc->port, " --> CCBS_T_Call: InvokeID:%d\n",
  2862. fac->u.CCBS_T_Call.InvokeID);
  2863. break;
  2864. case Fac_CCBS_T_Suspend:
  2865. chan_misdn_log(1, bc->port, " --> CCBS_T_Suspend: InvokeID:%d\n",
  2866. fac->u.CCBS_T_Suspend.InvokeID);
  2867. break;
  2868. case Fac_CCBS_T_Resume:
  2869. chan_misdn_log(1, bc->port, " --> CCBS_T_Resume: InvokeID:%d\n",
  2870. fac->u.CCBS_T_Resume.InvokeID);
  2871. break;
  2872. case Fac_CCBS_T_RemoteUserFree:
  2873. chan_misdn_log(1, bc->port, " --> CCBS_T_RemoteUserFree: InvokeID:%d\n",
  2874. fac->u.CCBS_T_RemoteUserFree.InvokeID);
  2875. break;
  2876. case Fac_CCBS_T_Available:
  2877. chan_misdn_log(1, bc->port, " --> CCBS_T_Available: InvokeID:%d\n",
  2878. fac->u.CCBS_T_Available.InvokeID);
  2879. break;
  2880. case Fac_CCBS_T_Request:
  2881. chan_misdn_log(1, bc->port, " --> CCBS_T_Request: InvokeID:%d\n",
  2882. fac->u.CCBS_T_Request.InvokeID);
  2883. switch (fac->u.CCBS_T_Request.ComponentType) {
  2884. case FacComponent_Invoke:
  2885. chan_misdn_log(1, bc->port, " --> Invoke\n");
  2886. chan_misdn_log(1, bc->port, " --> DestinationAddress:\n");
  2887. print_facility_Address(3, &fac->u.CCBS_T_Request.Component.Invoke.Destination, bc);
  2888. print_facility_Q931_Bc_Hlc_Llc(2, &fac->u.CCBS_T_Request.Component.Invoke.Q931ie, bc);
  2889. if (fac->u.CCBS_T_Request.Component.Invoke.RetentionSupported) {
  2890. chan_misdn_log(1, bc->port, " --> RetentionSupported:1\n");
  2891. }
  2892. if (fac->u.CCBS_T_Request.Component.Invoke.PresentationAllowedIndicatorPresent) {
  2893. chan_misdn_log(1, bc->port, " --> PresentationAllowed:%d\n",
  2894. fac->u.CCBS_T_Request.Component.Invoke.PresentationAllowedIndicator);
  2895. }
  2896. if (fac->u.CCBS_T_Request.Component.Invoke.Originating.Party.LengthOfNumber) {
  2897. chan_misdn_log(1, bc->port, " --> OriginatingAddress:\n");
  2898. print_facility_Address(3, &fac->u.CCBS_T_Request.Component.Invoke.Originating, bc);
  2899. }
  2900. break;
  2901. case FacComponent_Result:
  2902. chan_misdn_log(1, bc->port, " --> Result: RetentionSupported:%d\n",
  2903. fac->u.CCBS_T_Request.Component.Result.RetentionSupported);
  2904. break;
  2905. default:
  2906. break;
  2907. }
  2908. break;
  2909. case Fac_CCNR_T_Request:
  2910. chan_misdn_log(1, bc->port, " --> CCNR_T_Request: InvokeID:%d\n",
  2911. fac->u.CCNR_T_Request.InvokeID);
  2912. switch (fac->u.CCNR_T_Request.ComponentType) {
  2913. case FacComponent_Invoke:
  2914. chan_misdn_log(1, bc->port, " --> Invoke\n");
  2915. chan_misdn_log(1, bc->port, " --> DestinationAddress:\n");
  2916. print_facility_Address(3, &fac->u.CCNR_T_Request.Component.Invoke.Destination, bc);
  2917. print_facility_Q931_Bc_Hlc_Llc(2, &fac->u.CCNR_T_Request.Component.Invoke.Q931ie, bc);
  2918. if (fac->u.CCNR_T_Request.Component.Invoke.RetentionSupported) {
  2919. chan_misdn_log(1, bc->port, " --> RetentionSupported:1\n");
  2920. }
  2921. if (fac->u.CCNR_T_Request.Component.Invoke.PresentationAllowedIndicatorPresent) {
  2922. chan_misdn_log(1, bc->port, " --> PresentationAllowed:%d\n",
  2923. fac->u.CCNR_T_Request.Component.Invoke.PresentationAllowedIndicator);
  2924. }
  2925. if (fac->u.CCNR_T_Request.Component.Invoke.Originating.Party.LengthOfNumber) {
  2926. chan_misdn_log(1, bc->port, " --> OriginatingAddress:\n");
  2927. print_facility_Address(3, &fac->u.CCNR_T_Request.Component.Invoke.Originating, bc);
  2928. }
  2929. break;
  2930. case FacComponent_Result:
  2931. chan_misdn_log(1, bc->port, " --> Result: RetentionSupported:%d\n",
  2932. fac->u.CCNR_T_Request.Component.Result.RetentionSupported);
  2933. break;
  2934. default:
  2935. break;
  2936. }
  2937. break;
  2938. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2939. case Fac_None:
  2940. /* No facility so print nothing */
  2941. break;
  2942. default:
  2943. chan_misdn_log(1, bc->port, " --> unknown facility\n");
  2944. break;
  2945. }
  2946. }
  2947. static void print_bearer(struct misdn_bchannel *bc)
  2948. {
  2949. chan_misdn_log(2, bc->port, " --> Bearer: %s\n", bearer2str(bc->capability));
  2950. switch(bc->law) {
  2951. case INFO_CODEC_ALAW:
  2952. chan_misdn_log(2, bc->port, " --> Codec: Alaw\n");
  2953. break;
  2954. case INFO_CODEC_ULAW:
  2955. chan_misdn_log(2, bc->port, " --> Codec: Ulaw\n");
  2956. break;
  2957. }
  2958. }
  2959. /*!
  2960. * \internal
  2961. * \brief Prefix a string to another string in place.
  2962. *
  2963. * \param str_prefix String to prefix to the main string.
  2964. * \param str_main String to get the prefix added to it.
  2965. * \param size Buffer size of the main string (Includes null terminator).
  2966. *
  2967. * \note The str_main buffer size must be greater than one.
  2968. *
  2969. * \return Nothing
  2970. */
  2971. static void misdn_prefix_string(const char *str_prefix, char *str_main, size_t size)
  2972. {
  2973. size_t len_over;
  2974. size_t len_total;
  2975. size_t len_main;
  2976. size_t len_prefix;
  2977. len_prefix = strlen(str_prefix);
  2978. if (!len_prefix) {
  2979. /* There is no prefix to prepend. */
  2980. return;
  2981. }
  2982. len_main = strlen(str_main);
  2983. len_total = len_prefix + len_main;
  2984. if (size <= len_total) {
  2985. /* We need to truncate since the buffer is too small. */
  2986. len_over = len_total + 1 - size;
  2987. if (len_over <= len_main) {
  2988. len_main -= len_over;
  2989. } else {
  2990. len_over -= len_main;
  2991. len_main = 0;
  2992. len_prefix -= len_over;
  2993. }
  2994. }
  2995. if (len_main) {
  2996. memmove(str_main + len_prefix, str_main, len_main);
  2997. }
  2998. memcpy(str_main, str_prefix, len_prefix);
  2999. str_main[len_prefix + len_main] = '\0';
  3000. }
  3001. /*!
  3002. * \internal
  3003. * \brief Add a configured prefix to the given number.
  3004. *
  3005. * \param port Logical port number
  3006. * \param number_type Type-of-number passed in.
  3007. * \param number Given number string to add prefix
  3008. * \param size Buffer size number string occupies.
  3009. *
  3010. * \return Nothing
  3011. */
  3012. static void misdn_add_number_prefix(int port, enum mISDN_NUMBER_TYPE number_type, char *number, size_t size)
  3013. {
  3014. enum misdn_cfg_elements type_prefix;
  3015. char num_prefix[MISDN_MAX_NUMBER_LEN];
  3016. /* Get prefix string. */
  3017. switch (number_type) {
  3018. case NUMTYPE_UNKNOWN:
  3019. type_prefix = MISDN_CFG_TON_PREFIX_UNKNOWN;
  3020. break;
  3021. case NUMTYPE_INTERNATIONAL:
  3022. type_prefix = MISDN_CFG_TON_PREFIX_INTERNATIONAL;
  3023. break;
  3024. case NUMTYPE_NATIONAL:
  3025. type_prefix = MISDN_CFG_TON_PREFIX_NATIONAL;
  3026. break;
  3027. case NUMTYPE_NETWORK_SPECIFIC:
  3028. type_prefix = MISDN_CFG_TON_PREFIX_NETWORK_SPECIFIC;
  3029. break;
  3030. case NUMTYPE_SUBSCRIBER:
  3031. type_prefix = MISDN_CFG_TON_PREFIX_SUBSCRIBER;
  3032. break;
  3033. case NUMTYPE_ABBREVIATED:
  3034. type_prefix = MISDN_CFG_TON_PREFIX_ABBREVIATED;
  3035. break;
  3036. default:
  3037. /* Type-of-number does not have a prefix that can be added. */
  3038. return;
  3039. }
  3040. misdn_cfg_get(port, type_prefix, num_prefix, sizeof(num_prefix));
  3041. misdn_prefix_string(num_prefix, number, size);
  3042. }
  3043. static void export_aoc_vars(int originator, struct ast_channel *ast, struct misdn_bchannel *bc)
  3044. {
  3045. RAII_VAR(struct ast_channel *, chan, NULL, ast_channel_cleanup);
  3046. char buf[128];
  3047. if (!bc->AOCD_need_export || !ast) {
  3048. return;
  3049. }
  3050. if (originator == ORG_AST) {
  3051. chan = ast_channel_bridge_peer(ast);
  3052. if (!chan) {
  3053. return;
  3054. }
  3055. } else {
  3056. chan = ast_channel_ref(ast);
  3057. }
  3058. switch (bc->AOCDtype) {
  3059. case Fac_AOCDCurrency:
  3060. pbx_builtin_setvar_helper(chan, "AOCD_Type", "currency");
  3061. if (bc->AOCD.currency.chargeNotAvailable) {
  3062. pbx_builtin_setvar_helper(chan, "AOCD_ChargeAvailable", "no");
  3063. } else {
  3064. pbx_builtin_setvar_helper(chan, "AOCD_ChargeAvailable", "yes");
  3065. if (bc->AOCD.currency.freeOfCharge) {
  3066. pbx_builtin_setvar_helper(chan, "AOCD_FreeOfCharge", "yes");
  3067. } else {
  3068. pbx_builtin_setvar_helper(chan, "AOCD_FreeOfCharge", "no");
  3069. if (snprintf(buf, sizeof(buf), "%d %s", bc->AOCD.currency.currencyAmount * bc->AOCD.currency.multiplier, bc->AOCD.currency.currency) < sizeof(buf)) {
  3070. pbx_builtin_setvar_helper(chan, "AOCD_Amount", buf);
  3071. if (bc->AOCD.currency.billingId >= 0 && snprintf(buf, sizeof(buf), "%d", bc->AOCD.currency.billingId) < sizeof(buf)) {
  3072. pbx_builtin_setvar_helper(chan, "AOCD_BillingId", buf);
  3073. }
  3074. }
  3075. }
  3076. }
  3077. break;
  3078. case Fac_AOCDChargingUnit:
  3079. pbx_builtin_setvar_helper(chan, "AOCD_Type", "charging_unit");
  3080. if (bc->AOCD.chargingUnit.chargeNotAvailable) {
  3081. pbx_builtin_setvar_helper(chan, "AOCD_ChargeAvailable", "no");
  3082. } else {
  3083. pbx_builtin_setvar_helper(chan, "AOCD_ChargeAvailable", "yes");
  3084. if (bc->AOCD.chargingUnit.freeOfCharge) {
  3085. pbx_builtin_setvar_helper(chan, "AOCD_FreeOfCharge", "yes");
  3086. } else {
  3087. pbx_builtin_setvar_helper(chan, "AOCD_FreeOfCharge", "no");
  3088. if (snprintf(buf, sizeof(buf), "%d", bc->AOCD.chargingUnit.recordedUnits) < sizeof(buf)) {
  3089. pbx_builtin_setvar_helper(chan, "AOCD_RecordedUnits", buf);
  3090. if (bc->AOCD.chargingUnit.billingId >= 0 && snprintf(buf, sizeof(buf), "%d", bc->AOCD.chargingUnit.billingId) < sizeof(buf)) {
  3091. pbx_builtin_setvar_helper(chan, "AOCD_BillingId", buf);
  3092. }
  3093. }
  3094. }
  3095. }
  3096. break;
  3097. default:
  3098. break;
  3099. }
  3100. bc->AOCD_need_export = 0;
  3101. }
  3102. /*************** Helpers END *************/
  3103. static void sighandler(int sig)
  3104. {
  3105. }
  3106. static void *misdn_tasks_thread_func(void *data)
  3107. {
  3108. int wait;
  3109. struct sigaction sa;
  3110. sa.sa_handler = sighandler;
  3111. sa.sa_flags = SA_NODEFER;
  3112. sigemptyset(&sa.sa_mask);
  3113. sigaddset(&sa.sa_mask, SIGUSR1);
  3114. sigaction(SIGUSR1, &sa, NULL);
  3115. sem_post((sem_t *)data);
  3116. while (1) {
  3117. wait = ast_sched_wait(misdn_tasks);
  3118. if (wait < 0) {
  3119. wait = 8000;
  3120. }
  3121. if (poll(NULL, 0, wait) < 0) {
  3122. chan_misdn_log(4, 0, "Waking up misdn_tasks thread\n");
  3123. }
  3124. ast_sched_runq(misdn_tasks);
  3125. }
  3126. return NULL;
  3127. }
  3128. static void misdn_tasks_init(void)
  3129. {
  3130. sem_t blocker;
  3131. int i = 5;
  3132. if (sem_init(&blocker, 0, 0)) {
  3133. perror("chan_misdn: Failed to initialize semaphore!");
  3134. exit(1);
  3135. }
  3136. chan_misdn_log(4, 0, "Starting misdn_tasks thread\n");
  3137. misdn_tasks = ast_sched_context_create();
  3138. pthread_create(&misdn_tasks_thread, NULL, misdn_tasks_thread_func, &blocker);
  3139. while (sem_wait(&blocker) && --i) {
  3140. }
  3141. sem_destroy(&blocker);
  3142. }
  3143. static void misdn_tasks_destroy(void)
  3144. {
  3145. if (misdn_tasks) {
  3146. chan_misdn_log(4, 0, "Killing misdn_tasks thread\n");
  3147. if (pthread_cancel(misdn_tasks_thread) == 0) {
  3148. cb_log(4, 0, "Joining misdn_tasks thread\n");
  3149. pthread_join(misdn_tasks_thread, NULL);
  3150. }
  3151. ast_sched_context_destroy(misdn_tasks);
  3152. }
  3153. }
  3154. static inline void misdn_tasks_wakeup(void)
  3155. {
  3156. pthread_kill(misdn_tasks_thread, SIGUSR1);
  3157. }
  3158. static inline int _misdn_tasks_add_variable(int timeout, ast_sched_cb callback, const void *data, int variable)
  3159. {
  3160. int task_id;
  3161. if (!misdn_tasks) {
  3162. misdn_tasks_init();
  3163. }
  3164. task_id = ast_sched_add_variable(misdn_tasks, timeout, callback, data, variable);
  3165. misdn_tasks_wakeup();
  3166. return task_id;
  3167. }
  3168. static int misdn_tasks_add(int timeout, ast_sched_cb callback, const void *data)
  3169. {
  3170. return _misdn_tasks_add_variable(timeout, callback, data, 0);
  3171. }
  3172. static int misdn_tasks_add_variable(int timeout, ast_sched_cb callback, const void *data)
  3173. {
  3174. return _misdn_tasks_add_variable(timeout, callback, data, 1);
  3175. }
  3176. static void misdn_tasks_remove(int task_id)
  3177. {
  3178. AST_SCHED_DEL(misdn_tasks, task_id);
  3179. }
  3180. static int misdn_l1_task(const void *vdata)
  3181. {
  3182. const int *data = vdata;
  3183. misdn_lib_isdn_l1watcher(*data);
  3184. chan_misdn_log(5, *data, "L1watcher timeout\n");
  3185. return 1;
  3186. }
  3187. static int misdn_overlap_dial_task(const void *data)
  3188. {
  3189. struct timeval tv_end, tv_now;
  3190. int diff;
  3191. struct chan_list *ch = (struct chan_list *) data;
  3192. char *dad;
  3193. chan_misdn_log(4, ch->bc->port, "overlap dial task, chan_state: %d\n", ch->state);
  3194. if (ch->state != MISDN_WAITING4DIGS) {
  3195. ch->overlap_dial_task = -1;
  3196. return 0;
  3197. }
  3198. ast_mutex_lock(&ch->overlap_tv_lock);
  3199. tv_end = ch->overlap_tv;
  3200. ast_mutex_unlock(&ch->overlap_tv_lock);
  3201. tv_end.tv_sec += ch->overlap_dial;
  3202. tv_now = ast_tvnow();
  3203. diff = ast_tvdiff_ms(tv_end, tv_now);
  3204. if (100 < diff) {
  3205. return diff;
  3206. }
  3207. /* if we are 100ms near the timeout, we are satisfied.. */
  3208. stop_indicate(ch);
  3209. if (ast_strlen_zero(ch->bc->dialed.number)) {
  3210. dad = "s";
  3211. ast_channel_exten_set(ch->ast, dad);
  3212. } else {
  3213. dad = ch->bc->dialed.number;
  3214. }
  3215. if (ast_exists_extension(ch->ast, ch->context, dad, 1, ch->bc->caller.number)) {
  3216. ch->state = MISDN_DIALING;
  3217. if (pbx_start_chan(ch) < 0) {
  3218. chan_misdn_log(-1, ch->bc->port, "ast_pbx_start returned < 0 in misdn_overlap_dial_task\n");
  3219. goto misdn_overlap_dial_task_disconnect;
  3220. }
  3221. } else {
  3222. misdn_overlap_dial_task_disconnect:
  3223. hanguptone_indicate(ch);
  3224. ch->bc->out_cause = AST_CAUSE_UNALLOCATED;
  3225. ch->state = MISDN_CLEANING;
  3226. misdn_lib_send_event(ch->bc, EVENT_DISCONNECT);
  3227. }
  3228. ch->overlap_dial_task = -1;
  3229. return 0;
  3230. }
  3231. static void send_digit_to_chan(struct chan_list *cl, char digit)
  3232. {
  3233. static const char * const dtmf_tones[] = {
  3234. /* *INDENT-OFF* */
  3235. "!941+1336/100,!0/100", /* 0 */
  3236. "!697+1209/100,!0/100", /* 1 */
  3237. "!697+1336/100,!0/100", /* 2 */
  3238. "!697+1477/100,!0/100", /* 3 */
  3239. "!770+1209/100,!0/100", /* 4 */
  3240. "!770+1336/100,!0/100", /* 5 */
  3241. "!770+1477/100,!0/100", /* 6 */
  3242. "!852+1209/100,!0/100", /* 7 */
  3243. "!852+1336/100,!0/100", /* 8 */
  3244. "!852+1477/100,!0/100", /* 9 */
  3245. "!697+1633/100,!0/100", /* A */
  3246. "!770+1633/100,!0/100", /* B */
  3247. "!852+1633/100,!0/100", /* C */
  3248. "!941+1633/100,!0/100", /* D */
  3249. "!941+1209/100,!0/100", /* * */
  3250. "!941+1477/100,!0/100", /* # */
  3251. /* *INDENT-ON* */
  3252. };
  3253. struct ast_channel *chan = cl->ast;
  3254. if (digit >= '0' && digit <='9') {
  3255. ast_playtones_start(chan, 0, dtmf_tones[digit - '0'], 0);
  3256. } else if (digit >= 'A' && digit <= 'D') {
  3257. ast_playtones_start(chan, 0, dtmf_tones[digit - 'A' + 10], 0);
  3258. } else if (digit == '*') {
  3259. ast_playtones_start(chan, 0, dtmf_tones[14], 0);
  3260. } else if (digit == '#') {
  3261. ast_playtones_start(chan, 0, dtmf_tones[15], 0);
  3262. } else {
  3263. /* not handled */
  3264. ast_debug(1, "Unable to handle DTMF tone '%c' for '%s'\n", digit, ast_channel_name(chan));
  3265. }
  3266. }
  3267. /*** CLI HANDLING ***/
  3268. static char *handle_cli_misdn_set_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3269. {
  3270. int level;
  3271. switch (cmd) {
  3272. case CLI_INIT:
  3273. e->command = "misdn set debug [on|off]";
  3274. e->usage =
  3275. "Usage: misdn set debug {on|off|<level>} [only] | [port <port> [only]]\n"
  3276. " Set the debug level of the mISDN channel.\n";
  3277. return NULL;
  3278. case CLI_GENERATE:
  3279. return complete_debug_port(a);
  3280. }
  3281. if (a->argc < 4 || a->argc > 7) {
  3282. return CLI_SHOWUSAGE;
  3283. }
  3284. if (!strcasecmp(a->argv[3], "on")) {
  3285. level = 1;
  3286. } else if (!strcasecmp(a->argv[3], "off")) {
  3287. level = 0;
  3288. } else if (isdigit(a->argv[3][0])) {
  3289. level = atoi(a->argv[3]);
  3290. } else {
  3291. return CLI_SHOWUSAGE;
  3292. }
  3293. switch (a->argc) {
  3294. case 4:
  3295. case 5:
  3296. {
  3297. int i;
  3298. int only = 0;
  3299. if (a->argc == 5) {
  3300. if (strncasecmp(a->argv[4], "only", strlen(a->argv[4]))) {
  3301. return CLI_SHOWUSAGE;
  3302. } else {
  3303. only = 1;
  3304. }
  3305. }
  3306. for (i = 0; i <= max_ports; i++) {
  3307. misdn_debug[i] = level;
  3308. misdn_debug_only[i] = only;
  3309. }
  3310. ast_cli(a->fd, "changing debug level for all ports to %d%s\n", misdn_debug[0], only ? " (only)" : "");
  3311. }
  3312. break;
  3313. case 6:
  3314. case 7:
  3315. {
  3316. int port;
  3317. if (strncasecmp(a->argv[4], "port", strlen(a->argv[4])))
  3318. return CLI_SHOWUSAGE;
  3319. port = atoi(a->argv[5]);
  3320. if (port <= 0 || port > max_ports) {
  3321. switch (max_ports) {
  3322. case 0:
  3323. ast_cli(a->fd, "port number not valid! no ports available so you won't get lucky with any number here...\n");
  3324. break;
  3325. case 1:
  3326. ast_cli(a->fd, "port number not valid! only port 1 is available.\n");
  3327. break;
  3328. default:
  3329. ast_cli(a->fd, "port number not valid! only ports 1 to %d are available.\n", max_ports);
  3330. }
  3331. return 0;
  3332. }
  3333. if (a->argc == 7) {
  3334. if (strncasecmp(a->argv[6], "only", strlen(a->argv[6]))) {
  3335. return CLI_SHOWUSAGE;
  3336. } else {
  3337. misdn_debug_only[port] = 1;
  3338. }
  3339. } else {
  3340. misdn_debug_only[port] = 0;
  3341. }
  3342. misdn_debug[port] = level;
  3343. ast_cli(a->fd, "changing debug level to %d%s for port %d\n", misdn_debug[port], misdn_debug_only[port] ? " (only)" : "", port);
  3344. }
  3345. }
  3346. return CLI_SUCCESS;
  3347. }
  3348. static char *handle_cli_misdn_set_crypt_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3349. {
  3350. switch (cmd) {
  3351. case CLI_INIT:
  3352. e->command = "misdn set crypt debug";
  3353. e->usage =
  3354. "Usage: misdn set crypt debug <level>\n"
  3355. " Set the crypt debug level of the mISDN channel. Level\n"
  3356. " must be 1 or 2.\n";
  3357. return NULL;
  3358. case CLI_GENERATE:
  3359. return NULL;
  3360. }
  3361. if (a->argc != 5) {
  3362. return CLI_SHOWUSAGE;
  3363. }
  3364. /* XXX Is this supposed to not do anything? XXX */
  3365. return CLI_SUCCESS;
  3366. }
  3367. static char *handle_cli_misdn_port_block(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3368. {
  3369. switch (cmd) {
  3370. case CLI_INIT:
  3371. e->command = "misdn port block";
  3372. e->usage =
  3373. "Usage: misdn port block <port>\n"
  3374. " Block the specified port by <port>.\n";
  3375. return NULL;
  3376. case CLI_GENERATE:
  3377. return NULL;
  3378. }
  3379. if (a->argc != 4) {
  3380. return CLI_SHOWUSAGE;
  3381. }
  3382. misdn_lib_port_block(atoi(a->argv[3]));
  3383. return CLI_SUCCESS;
  3384. }
  3385. static char *handle_cli_misdn_port_unblock(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3386. {
  3387. switch (cmd) {
  3388. case CLI_INIT:
  3389. e->command = "misdn port unblock";
  3390. e->usage =
  3391. "Usage: misdn port unblock <port>\n"
  3392. " Unblock the port specified by <port>.\n";
  3393. return NULL;
  3394. case CLI_GENERATE:
  3395. return NULL;
  3396. }
  3397. if (a->argc != 4) {
  3398. return CLI_SHOWUSAGE;
  3399. }
  3400. misdn_lib_port_unblock(atoi(a->argv[3]));
  3401. return CLI_SUCCESS;
  3402. }
  3403. static char *handle_cli_misdn_restart_port(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3404. {
  3405. switch (cmd) {
  3406. case CLI_INIT:
  3407. e->command = "misdn restart port";
  3408. e->usage =
  3409. "Usage: misdn restart port <port>\n"
  3410. " Restart the given port.\n";
  3411. return NULL;
  3412. case CLI_GENERATE:
  3413. return NULL;
  3414. }
  3415. if (a->argc != 4) {
  3416. return CLI_SHOWUSAGE;
  3417. }
  3418. misdn_lib_port_restart(atoi(a->argv[3]));
  3419. return CLI_SUCCESS;
  3420. }
  3421. static char *handle_cli_misdn_restart_pid(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3422. {
  3423. switch (cmd) {
  3424. case CLI_INIT:
  3425. e->command = "misdn restart pid";
  3426. e->usage =
  3427. "Usage: misdn restart pid <pid>\n"
  3428. " Restart the given pid\n";
  3429. return NULL;
  3430. case CLI_GENERATE:
  3431. return NULL;
  3432. }
  3433. if (a->argc != 4) {
  3434. return CLI_SHOWUSAGE;
  3435. }
  3436. misdn_lib_pid_restart(atoi(a->argv[3]));
  3437. return CLI_SUCCESS;
  3438. }
  3439. static char *handle_cli_misdn_port_up(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3440. {
  3441. switch (cmd) {
  3442. case CLI_INIT:
  3443. e->command = "misdn port up";
  3444. e->usage =
  3445. "Usage: misdn port up <port>\n"
  3446. " Try to establish L1 on the given port.\n";
  3447. return NULL;
  3448. case CLI_GENERATE:
  3449. return NULL;
  3450. }
  3451. if (a->argc != 4) {
  3452. return CLI_SHOWUSAGE;
  3453. }
  3454. misdn_lib_get_port_up(atoi(a->argv[3]));
  3455. return CLI_SUCCESS;
  3456. }
  3457. static char *handle_cli_misdn_port_down(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3458. {
  3459. switch (cmd) {
  3460. case CLI_INIT:
  3461. e->command = "misdn port down";
  3462. e->usage =
  3463. "Usage: misdn port down <port>\n"
  3464. " Try to deactivate the L1 on the given port.\n";
  3465. return NULL;
  3466. case CLI_GENERATE:
  3467. return NULL;
  3468. }
  3469. if (a->argc != 4) {
  3470. return CLI_SHOWUSAGE;
  3471. }
  3472. misdn_lib_get_port_down(atoi(a->argv[3]));
  3473. return CLI_SUCCESS;
  3474. }
  3475. static inline void show_config_description(int fd, enum misdn_cfg_elements elem)
  3476. {
  3477. char section[BUFFERSIZE];
  3478. char name[BUFFERSIZE];
  3479. char desc[BUFFERSIZE];
  3480. char def[BUFFERSIZE];
  3481. char tmp[BUFFERSIZE];
  3482. misdn_cfg_get_name(elem, tmp, sizeof(tmp));
  3483. term_color(name, tmp, COLOR_BRWHITE, 0, sizeof(tmp));
  3484. misdn_cfg_get_desc(elem, desc, sizeof(desc), def, sizeof(def));
  3485. if (elem < MISDN_CFG_LAST) {
  3486. term_color(section, "PORTS SECTION", COLOR_YELLOW, 0, sizeof(section));
  3487. } else {
  3488. term_color(section, "GENERAL SECTION", COLOR_YELLOW, 0, sizeof(section));
  3489. }
  3490. if (*def) {
  3491. ast_cli(fd, "[%s] %s (Default: %s)\n\t%s\n", section, name, def, desc);
  3492. } else {
  3493. ast_cli(fd, "[%s] %s\n\t%s\n", section, name, desc);
  3494. }
  3495. }
  3496. static char *handle_cli_misdn_show_config(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3497. {
  3498. char buffer[BUFFERSIZE];
  3499. enum misdn_cfg_elements elem;
  3500. int linebreak;
  3501. int onlyport = -1;
  3502. int ok = 0;
  3503. switch (cmd) {
  3504. case CLI_INIT:
  3505. e->command = "misdn show config";
  3506. e->usage =
  3507. "Usage: misdn show config [<port> | description <config element> | descriptions [general|ports]]\n"
  3508. " Use 0 for <port> to only print the general config.\n";
  3509. return NULL;
  3510. case CLI_GENERATE:
  3511. return complete_show_config(a);
  3512. }
  3513. if (a->argc >= 4) {
  3514. if (!strcmp(a->argv[3], "description")) {
  3515. if (a->argc == 5) {
  3516. enum misdn_cfg_elements elem = misdn_cfg_get_elem(a->argv[4]);
  3517. if (elem == MISDN_CFG_FIRST) {
  3518. ast_cli(a->fd, "Unknown element: %s\n", a->argv[4]);
  3519. } else {
  3520. show_config_description(a->fd, elem);
  3521. }
  3522. return CLI_SUCCESS;
  3523. }
  3524. return CLI_SHOWUSAGE;
  3525. } else if (!strcmp(a->argv[3], "descriptions")) {
  3526. if ((a->argc == 4) || ((a->argc == 5) && !strcmp(a->argv[4], "general"))) {
  3527. for (elem = MISDN_GEN_FIRST + 1; elem < MISDN_GEN_LAST; ++elem) {
  3528. show_config_description(a->fd, elem);
  3529. ast_cli(a->fd, "\n");
  3530. }
  3531. ok = 1;
  3532. }
  3533. if ((a->argc == 4) || ((a->argc == 5) && !strcmp(a->argv[4], "ports"))) {
  3534. for (elem = MISDN_CFG_FIRST + 1; elem < MISDN_CFG_LAST - 1 /* the ptp hack, remove the -1 when ptp is gone */; ++elem) {
  3535. show_config_description(a->fd, elem);
  3536. ast_cli(a->fd, "\n");
  3537. }
  3538. ok = 1;
  3539. }
  3540. return ok ? CLI_SUCCESS : CLI_SHOWUSAGE;
  3541. } else if (!sscanf(a->argv[3], "%5d", &onlyport) || onlyport < 0) {
  3542. ast_cli(a->fd, "Unknown option: %s\n", a->argv[3]);
  3543. return CLI_SHOWUSAGE;
  3544. }
  3545. }
  3546. if (a->argc == 3 || onlyport == 0) {
  3547. ast_cli(a->fd, "mISDN General-Config:\n");
  3548. for (elem = MISDN_GEN_FIRST + 1, linebreak = 1; elem < MISDN_GEN_LAST; elem++, linebreak++) {
  3549. misdn_cfg_get_config_string(0, elem, buffer, sizeof(buffer));
  3550. ast_cli(a->fd, "%-36s%s", buffer, !(linebreak % 2) ? "\n" : "");
  3551. }
  3552. ast_cli(a->fd, "\n");
  3553. }
  3554. if (onlyport < 0) {
  3555. int port = misdn_cfg_get_next_port(0);
  3556. for (; port > 0; port = misdn_cfg_get_next_port(port)) {
  3557. ast_cli(a->fd, "\n[PORT %d]\n", port);
  3558. for (elem = MISDN_CFG_FIRST + 1, linebreak = 1; elem < MISDN_CFG_LAST; elem++, linebreak++) {
  3559. misdn_cfg_get_config_string(port, elem, buffer, sizeof(buffer));
  3560. ast_cli(a->fd, "%-36s%s", buffer, !(linebreak % 2) ? "\n" : "");
  3561. }
  3562. ast_cli(a->fd, "\n");
  3563. }
  3564. }
  3565. if (onlyport > 0) {
  3566. if (misdn_cfg_is_port_valid(onlyport)) {
  3567. ast_cli(a->fd, "[PORT %d]\n", onlyport);
  3568. for (elem = MISDN_CFG_FIRST + 1, linebreak = 1; elem < MISDN_CFG_LAST; elem++, linebreak++) {
  3569. misdn_cfg_get_config_string(onlyport, elem, buffer, sizeof(buffer));
  3570. ast_cli(a->fd, "%-36s%s", buffer, !(linebreak % 2) ? "\n" : "");
  3571. }
  3572. ast_cli(a->fd, "\n");
  3573. } else {
  3574. ast_cli(a->fd, "Port %d is not active!\n", onlyport);
  3575. }
  3576. }
  3577. return CLI_SUCCESS;
  3578. }
  3579. struct state_struct {
  3580. enum misdn_chan_state state;
  3581. char txt[255];
  3582. };
  3583. static const struct state_struct state_array[] = {
  3584. /* *INDENT-OFF* */
  3585. { MISDN_NOTHING, "NOTHING" }, /* at beginning */
  3586. { MISDN_WAITING4DIGS, "WAITING4DIGS" }, /* when waiting for infos */
  3587. { MISDN_EXTCANTMATCH, "EXTCANTMATCH" }, /* when asterisk couldn't match our ext */
  3588. { MISDN_INCOMING_SETUP, "INCOMING SETUP" }, /* when pbx_start */
  3589. { MISDN_DIALING, "DIALING" }, /* when pbx_start */
  3590. { MISDN_PROGRESS, "PROGRESS" }, /* when pbx_start */
  3591. { MISDN_PROCEEDING, "PROCEEDING" }, /* when pbx_start */
  3592. { MISDN_CALLING, "CALLING" }, /* when misdn_call is called */
  3593. { MISDN_CALLING_ACKNOWLEDGE, "CALLING_ACKNOWLEDGE" }, /* when misdn_call is called */
  3594. { MISDN_ALERTING, "ALERTING" }, /* when Alerting */
  3595. { MISDN_BUSY, "BUSY" }, /* when BUSY */
  3596. { MISDN_CONNECTED, "CONNECTED" }, /* when connected */
  3597. { MISDN_DISCONNECTED, "DISCONNECTED" }, /* when connected */
  3598. { MISDN_CLEANING, "CLEANING" }, /* when hangup from * but we were connected before */
  3599. /* *INDENT-ON* */
  3600. };
  3601. static const char *misdn_get_ch_state(struct chan_list *p)
  3602. {
  3603. int i;
  3604. static char state[8];
  3605. if (!p) {
  3606. return NULL;
  3607. }
  3608. for (i = 0; i < ARRAY_LEN(state_array); i++) {
  3609. if (state_array[i].state == p->state) {
  3610. return state_array[i].txt;
  3611. }
  3612. }
  3613. snprintf(state, sizeof(state), "%d", p->state) ;
  3614. return state;
  3615. }
  3616. static void reload_config(void)
  3617. {
  3618. int i, cfg_debug;
  3619. if (!g_config_initialized) {
  3620. ast_log(LOG_WARNING, "chan_misdn is not initialized properly, still reloading ?\n");
  3621. return ;
  3622. }
  3623. free_robin_list();
  3624. misdn_cfg_reload();
  3625. misdn_cfg_update_ptp();
  3626. misdn_cfg_get(0, MISDN_GEN_TRACEFILE, global_tracefile, sizeof(global_tracefile));
  3627. misdn_cfg_get(0, MISDN_GEN_DEBUG, &cfg_debug, sizeof(cfg_debug));
  3628. for (i = 0; i <= max_ports; i++) {
  3629. misdn_debug[i] = cfg_debug;
  3630. misdn_debug_only[i] = 0;
  3631. }
  3632. }
  3633. static char *handle_cli_misdn_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3634. {
  3635. switch (cmd) {
  3636. case CLI_INIT:
  3637. e->command = "misdn reload";
  3638. e->usage =
  3639. "Usage: misdn reload\n"
  3640. " Reload internal mISDN config, read from the config\n"
  3641. " file.\n";
  3642. return NULL;
  3643. case CLI_GENERATE:
  3644. return NULL;
  3645. }
  3646. if (a->argc != 2) {
  3647. return CLI_SHOWUSAGE;
  3648. }
  3649. ast_cli(a->fd, "Reloading mISDN configuration\n");
  3650. reload_config();
  3651. return CLI_SUCCESS;
  3652. }
  3653. static void print_bc_info(int fd, struct chan_list *help, struct misdn_bchannel *bc)
  3654. {
  3655. struct ast_channel *ast = help->ast;
  3656. ast_cli(fd,
  3657. "* Pid:%d Port:%d Ch:%d Mode:%s Orig:%s dialed:%s\n"
  3658. " --> caller:\"%s\" <%s>\n"
  3659. " --> redirecting-from:\"%s\" <%s>\n"
  3660. " --> redirecting-to:\"%s\" <%s>\n"
  3661. " --> context:%s state:%s\n",
  3662. bc->pid,
  3663. bc->port,
  3664. bc->channel,
  3665. bc->nt ? "NT" : "TE",
  3666. help->originator == ORG_AST ? "*" : "I",
  3667. ast ? ast_channel_exten(ast) : "",
  3668. (ast && ast_channel_caller(ast)->id.name.valid && ast_channel_caller(ast)->id.name.str)
  3669. ? ast_channel_caller(ast)->id.name.str : "",
  3670. (ast && ast_channel_caller(ast)->id.number.valid && ast_channel_caller(ast)->id.number.str)
  3671. ? ast_channel_caller(ast)->id.number.str : "",
  3672. bc->redirecting.from.name,
  3673. bc->redirecting.from.number,
  3674. bc->redirecting.to.name,
  3675. bc->redirecting.to.number,
  3676. ast ? ast_channel_context(ast) : "",
  3677. misdn_get_ch_state(help));
  3678. if (misdn_debug[bc->port] > 0) {
  3679. ast_cli(fd,
  3680. " --> astname: %s\n"
  3681. " --> ch_l3id: %x\n"
  3682. " --> ch_addr: %x\n"
  3683. " --> bc_addr: %x\n"
  3684. " --> bc_l3id: %x\n"
  3685. " --> display: %s\n"
  3686. " --> activated: %d\n"
  3687. " --> state: %s\n"
  3688. " --> capability: %s\n"
  3689. #ifdef MISDN_1_2
  3690. " --> pipeline: %s\n"
  3691. #else
  3692. " --> echo_cancel: %d\n"
  3693. #endif
  3694. " --> notone : rx %d tx:%d\n"
  3695. " --> bc_hold: %d\n",
  3696. ast ? ast_channel_name(ast) : "",
  3697. help->l3id,
  3698. help->addr,
  3699. bc->addr,
  3700. bc->l3_id,
  3701. bc->display,
  3702. bc->active,
  3703. bc_state2str(bc->bc_state),
  3704. bearer2str(bc->capability),
  3705. #ifdef MISDN_1_2
  3706. bc->pipeline,
  3707. #else
  3708. bc->ec_enable,
  3709. #endif
  3710. help->norxtone, help->notxtone,
  3711. bc->holded);
  3712. }
  3713. }
  3714. static char *handle_cli_misdn_show_channels(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3715. {
  3716. struct chan_list *help;
  3717. switch (cmd) {
  3718. case CLI_INIT:
  3719. e->command = "misdn show channels";
  3720. e->usage =
  3721. "Usage: misdn show channels\n"
  3722. " Show the internal mISDN channel list\n";
  3723. return NULL;
  3724. case CLI_GENERATE:
  3725. return NULL;
  3726. }
  3727. if (a->argc != 3) {
  3728. return CLI_SHOWUSAGE;
  3729. }
  3730. ast_cli(a->fd, "Channel List: %p\n", cl_te);
  3731. /*
  3732. * Walking the list and dumping the channel information could
  3733. * take awhile. With the list locked for the duration, the
  3734. * channel driver cannot process signaling messages. However,
  3735. * since this is a CLI command it should not be run very often.
  3736. */
  3737. ast_mutex_lock(&cl_te_lock);
  3738. for (help = cl_te; help; help = help->next) {
  3739. struct misdn_bchannel *bc = help->bc;
  3740. struct ast_channel *ast = help->ast;
  3741. if (!ast) {
  3742. if (!bc) {
  3743. ast_cli(a->fd, "chan_list obj. with l3id:%x has no bc and no ast Leg\n", help->l3id);
  3744. continue;
  3745. }
  3746. ast_cli(a->fd, "bc with pid:%d has no Ast Leg\n", bc->pid);
  3747. }
  3748. if (misdn_debug[0] > 2) {
  3749. ast_cli(a->fd, "Bc:%p Ast:%p\n", bc, ast);
  3750. }
  3751. if (bc) {
  3752. print_bc_info(a->fd, help, bc);
  3753. } else {
  3754. if (help->hold.state != MISDN_HOLD_IDLE) {
  3755. ast_cli(a->fd, "ITS A HELD CALL BC:\n");
  3756. ast_cli(a->fd, " --> l3_id: %x\n"
  3757. " --> dialed:%s\n"
  3758. " --> caller:\"%s\" <%s>\n"
  3759. " --> hold_port: %d\n"
  3760. " --> hold_channel: %d\n",
  3761. help->l3id,
  3762. ast_channel_exten(ast),
  3763. S_COR(ast_channel_caller(ast)->id.name.valid, ast_channel_caller(ast)->id.name.str, ""),
  3764. S_COR(ast_channel_caller(ast)->id.number.valid, ast_channel_caller(ast)->id.number.str, ""),
  3765. help->hold.port,
  3766. help->hold.channel
  3767. );
  3768. } else {
  3769. ast_cli(a->fd, "* Channel in unknown STATE !!! Exten:%s, Callerid:%s\n",
  3770. ast_channel_exten(ast),
  3771. S_COR(ast_channel_caller(ast)->id.number.valid, ast_channel_caller(ast)->id.number.str, ""));
  3772. }
  3773. }
  3774. }
  3775. ast_mutex_unlock(&cl_te_lock);
  3776. misdn_dump_chanlist();
  3777. return CLI_SUCCESS;
  3778. }
  3779. static char *handle_cli_misdn_show_channel(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3780. {
  3781. struct chan_list *help;
  3782. switch (cmd) {
  3783. case CLI_INIT:
  3784. e->command = "misdn show channel";
  3785. e->usage =
  3786. "Usage: misdn show channel <channel>\n"
  3787. " Show an internal mISDN channel\n.";
  3788. return NULL;
  3789. case CLI_GENERATE:
  3790. return complete_ch(a);
  3791. }
  3792. if (a->argc != 4) {
  3793. return CLI_SHOWUSAGE;
  3794. }
  3795. ast_mutex_lock(&cl_te_lock);
  3796. for (help = cl_te; help; help = help->next) {
  3797. struct misdn_bchannel *bc = help->bc;
  3798. struct ast_channel *ast = help->ast;
  3799. if (bc && ast) {
  3800. if (!strcasecmp(ast_channel_name(ast), a->argv[3])) {
  3801. print_bc_info(a->fd, help, bc);
  3802. break;
  3803. }
  3804. }
  3805. }
  3806. ast_mutex_unlock(&cl_te_lock);
  3807. return CLI_SUCCESS;
  3808. }
  3809. static char *handle_cli_misdn_set_tics(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3810. {
  3811. switch (cmd) {
  3812. case CLI_INIT:
  3813. e->command = "misdn set tics";
  3814. e->usage =
  3815. "Usage: misdn set tics <value>\n";
  3816. return NULL;
  3817. case CLI_GENERATE:
  3818. return NULL;
  3819. }
  3820. if (a->argc != 4) {
  3821. return CLI_SHOWUSAGE;
  3822. }
  3823. /* XXX Wow, this does... a whole lot of nothing... XXX */
  3824. MAXTICS = atoi(a->argv[3]);
  3825. return CLI_SUCCESS;
  3826. }
  3827. static char *handle_cli_misdn_show_stacks(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3828. {
  3829. int port;
  3830. switch (cmd) {
  3831. case CLI_INIT:
  3832. e->command = "misdn show stacks";
  3833. e->usage =
  3834. "Usage: misdn show stacks\n"
  3835. " Show internal mISDN stack_list.\n";
  3836. return NULL;
  3837. case CLI_GENERATE:
  3838. return NULL;
  3839. }
  3840. if (a->argc != 3) {
  3841. return CLI_SHOWUSAGE;
  3842. }
  3843. ast_cli(a->fd, "BEGIN STACK_LIST:\n");
  3844. for (port = misdn_cfg_get_next_port(0); port > 0;
  3845. port = misdn_cfg_get_next_port(port)) {
  3846. char buf[128];
  3847. get_show_stack_details(port, buf);
  3848. ast_cli(a->fd, " %s Debug:%d%s\n", buf, misdn_debug[port], misdn_debug_only[port] ? "(only)" : "");
  3849. }
  3850. return CLI_SUCCESS;
  3851. }
  3852. static char *handle_cli_misdn_show_ports_stats(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3853. {
  3854. int port;
  3855. switch (cmd) {
  3856. case CLI_INIT:
  3857. e->command = "misdn show ports stats";
  3858. e->usage =
  3859. "Usage: misdn show ports stats\n"
  3860. " Show mISDNs channel's call statistics per port.\n";
  3861. return NULL;
  3862. case CLI_GENERATE:
  3863. return NULL;
  3864. }
  3865. if (a->argc != 4) {
  3866. return CLI_SHOWUSAGE;
  3867. }
  3868. ast_cli(a->fd, "Port\tin_calls\tout_calls\n");
  3869. for (port = misdn_cfg_get_next_port(0); port > 0;
  3870. port = misdn_cfg_get_next_port(port)) {
  3871. ast_cli(a->fd, "%d\t%d\t\t%d\n", port, misdn_in_calls[port], misdn_out_calls[port]);
  3872. }
  3873. ast_cli(a->fd, "\n");
  3874. return CLI_SUCCESS;
  3875. }
  3876. static char *handle_cli_misdn_show_port(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3877. {
  3878. int port;
  3879. char buf[128];
  3880. switch (cmd) {
  3881. case CLI_INIT:
  3882. e->command = "misdn show port";
  3883. e->usage =
  3884. "Usage: misdn show port <port>\n"
  3885. " Show detailed information for given port.\n";
  3886. return NULL;
  3887. case CLI_GENERATE:
  3888. return NULL;
  3889. }
  3890. if (a->argc != 4) {
  3891. return CLI_SHOWUSAGE;
  3892. }
  3893. port = atoi(a->argv[3]);
  3894. ast_cli(a->fd, "BEGIN STACK_LIST:\n");
  3895. get_show_stack_details(port, buf);
  3896. ast_cli(a->fd, " %s Debug:%d%s\n", buf, misdn_debug[port], misdn_debug_only[port] ? "(only)" : "");
  3897. return CLI_SUCCESS;
  3898. }
  3899. #if defined(AST_MISDN_ENHANCEMENTS) && defined(CCBS_TEST_MESSAGES)
  3900. static const struct FacParm Fac_Msgs[] = {
  3901. /* *INDENT-OFF* */
  3902. [0].Function = Fac_ERROR,
  3903. [0].u.ERROR.invokeId = 8,
  3904. [0].u.ERROR.errorValue = FacError_CCBS_AlreadyAccepted,
  3905. [1].Function = Fac_RESULT,
  3906. [1].u.RESULT.InvokeID = 9,
  3907. [2].Function = Fac_REJECT,
  3908. [2].u.REJECT.Code = FacReject_Gen_BadlyStructuredComponent,
  3909. [3].Function = Fac_REJECT,
  3910. [3].u.REJECT.InvokeIDPresent = 1,
  3911. [3].u.REJECT.InvokeID = 10,
  3912. [3].u.REJECT.Code = FacReject_Inv_InitiatorReleasing,
  3913. [4].Function = Fac_REJECT,
  3914. [4].u.REJECT.InvokeIDPresent = 1,
  3915. [4].u.REJECT.InvokeID = 11,
  3916. [4].u.REJECT.Code = FacReject_Res_MistypedResult,
  3917. [5].Function = Fac_REJECT,
  3918. [5].u.REJECT.InvokeIDPresent = 1,
  3919. [5].u.REJECT.InvokeID = 12,
  3920. [5].u.REJECT.Code = FacReject_Err_ErrorResponseUnexpected,
  3921. [6].Function = Fac_StatusRequest,
  3922. [6].u.StatusRequest.InvokeID = 13,
  3923. [6].u.StatusRequest.ComponentType = FacComponent_Invoke,
  3924. [6].u.StatusRequest.Component.Invoke.Q931ie.Bc.Length = 2,
  3925. [6].u.StatusRequest.Component.Invoke.Q931ie.Bc.Contents = "AB",
  3926. [6].u.StatusRequest.Component.Invoke.Q931ie.Llc.Length = 3,
  3927. [6].u.StatusRequest.Component.Invoke.Q931ie.Llc.Contents = "CDE",
  3928. [6].u.StatusRequest.Component.Invoke.Q931ie.Hlc.Length = 4,
  3929. [6].u.StatusRequest.Component.Invoke.Q931ie.Hlc.Contents = "FGHI",
  3930. [6].u.StatusRequest.Component.Invoke.CompatibilityMode = 1,
  3931. [7].Function = Fac_StatusRequest,
  3932. [7].u.StatusRequest.InvokeID = 14,
  3933. [7].u.StatusRequest.ComponentType = FacComponent_Result,
  3934. [7].u.StatusRequest.Component.Result.Status = 2,
  3935. [8].Function = Fac_CallInfoRetain,
  3936. [8].u.CallInfoRetain.InvokeID = 15,
  3937. [8].u.CallInfoRetain.CallLinkageID = 115,
  3938. [9].Function = Fac_EraseCallLinkageID,
  3939. [9].u.EraseCallLinkageID.InvokeID = 16,
  3940. [9].u.EraseCallLinkageID.CallLinkageID = 105,
  3941. [10].Function = Fac_CCBSDeactivate,
  3942. [10].u.CCBSDeactivate.InvokeID = 17,
  3943. [10].u.CCBSDeactivate.ComponentType = FacComponent_Invoke,
  3944. [10].u.CCBSDeactivate.Component.Invoke.CCBSReference = 2,
  3945. [11].Function = Fac_CCBSDeactivate,
  3946. [11].u.CCBSDeactivate.InvokeID = 18,
  3947. [11].u.CCBSDeactivate.ComponentType = FacComponent_Result,
  3948. [12].Function = Fac_CCBSErase,
  3949. [12].u.CCBSErase.InvokeID = 19,
  3950. [12].u.CCBSErase.Q931ie.Bc.Length = 2,
  3951. [12].u.CCBSErase.Q931ie.Bc.Contents = "JK",
  3952. [12].u.CCBSErase.AddressOfB.Party.Type = 0,
  3953. [12].u.CCBSErase.AddressOfB.Party.LengthOfNumber = 5,
  3954. [12].u.CCBSErase.AddressOfB.Party.Number = "33403",
  3955. [12].u.CCBSErase.AddressOfB.Subaddress.Type = 0,
  3956. [12].u.CCBSErase.AddressOfB.Subaddress.Length = 4,
  3957. [12].u.CCBSErase.AddressOfB.Subaddress.u.UserSpecified.Information = "3748",
  3958. [12].u.CCBSErase.RecallMode = 1,
  3959. [12].u.CCBSErase.CCBSReference = 102,
  3960. [12].u.CCBSErase.Reason = 3,
  3961. [13].Function = Fac_CCBSErase,
  3962. [13].u.CCBSErase.InvokeID = 20,
  3963. [13].u.CCBSErase.Q931ie.Bc.Length = 2,
  3964. [13].u.CCBSErase.Q931ie.Bc.Contents = "JK",
  3965. [13].u.CCBSErase.AddressOfB.Party.Type = 1,
  3966. [13].u.CCBSErase.AddressOfB.Party.LengthOfNumber = 11,
  3967. [13].u.CCBSErase.AddressOfB.Party.TypeOfNumber = 1,
  3968. [13].u.CCBSErase.AddressOfB.Party.Number = "18003020102",
  3969. [13].u.CCBSErase.AddressOfB.Subaddress.Type = 0,
  3970. [13].u.CCBSErase.AddressOfB.Subaddress.Length = 4,
  3971. [13].u.CCBSErase.AddressOfB.Subaddress.u.UserSpecified.OddCountPresent = 1,
  3972. [13].u.CCBSErase.AddressOfB.Subaddress.u.UserSpecified.OddCount = 1,
  3973. [13].u.CCBSErase.AddressOfB.Subaddress.u.UserSpecified.Information = "3748",
  3974. [13].u.CCBSErase.RecallMode = 1,
  3975. [13].u.CCBSErase.CCBSReference = 102,
  3976. [13].u.CCBSErase.Reason = 3,
  3977. [14].Function = Fac_CCBSErase,
  3978. [14].u.CCBSErase.InvokeID = 21,
  3979. [14].u.CCBSErase.Q931ie.Bc.Length = 2,
  3980. [14].u.CCBSErase.Q931ie.Bc.Contents = "JK",
  3981. [14].u.CCBSErase.AddressOfB.Party.Type = 2,
  3982. [14].u.CCBSErase.AddressOfB.Party.LengthOfNumber = 4,
  3983. [14].u.CCBSErase.AddressOfB.Party.Number = "1803",
  3984. [14].u.CCBSErase.AddressOfB.Subaddress.Type = 1,
  3985. [14].u.CCBSErase.AddressOfB.Subaddress.Length = 4,
  3986. [14].u.CCBSErase.AddressOfB.Subaddress.u.Nsap = "6492",
  3987. [14].u.CCBSErase.RecallMode = 1,
  3988. [14].u.CCBSErase.CCBSReference = 102,
  3989. [14].u.CCBSErase.Reason = 3,
  3990. [15].Function = Fac_CCBSErase,
  3991. [15].u.CCBSErase.InvokeID = 22,
  3992. [15].u.CCBSErase.Q931ie.Bc.Length = 2,
  3993. [15].u.CCBSErase.Q931ie.Bc.Contents = "JK",
  3994. [15].u.CCBSErase.AddressOfB.Party.Type = 3,
  3995. [15].u.CCBSErase.AddressOfB.Party.LengthOfNumber = 4,
  3996. [15].u.CCBSErase.AddressOfB.Party.Number = "1803",
  3997. [15].u.CCBSErase.RecallMode = 1,
  3998. [15].u.CCBSErase.CCBSReference = 102,
  3999. [15].u.CCBSErase.Reason = 3,
  4000. [16].Function = Fac_CCBSErase,
  4001. [16].u.CCBSErase.InvokeID = 23,
  4002. [16].u.CCBSErase.Q931ie.Bc.Length = 2,
  4003. [16].u.CCBSErase.Q931ie.Bc.Contents = "JK",
  4004. [16].u.CCBSErase.AddressOfB.Party.Type = 4,
  4005. [16].u.CCBSErase.AddressOfB.Party.LengthOfNumber = 4,
  4006. [16].u.CCBSErase.AddressOfB.Party.Number = "1803",
  4007. [16].u.CCBSErase.RecallMode = 1,
  4008. [16].u.CCBSErase.CCBSReference = 102,
  4009. [16].u.CCBSErase.Reason = 3,
  4010. [17].Function = Fac_CCBSErase,
  4011. [17].u.CCBSErase.InvokeID = 24,
  4012. [17].u.CCBSErase.Q931ie.Bc.Length = 2,
  4013. [17].u.CCBSErase.Q931ie.Bc.Contents = "JK",
  4014. [17].u.CCBSErase.AddressOfB.Party.Type = 5,
  4015. [17].u.CCBSErase.AddressOfB.Party.LengthOfNumber = 11,
  4016. [17].u.CCBSErase.AddressOfB.Party.TypeOfNumber = 4,
  4017. [17].u.CCBSErase.AddressOfB.Party.Number = "18003020102",
  4018. [17].u.CCBSErase.RecallMode = 1,
  4019. [17].u.CCBSErase.CCBSReference = 102,
  4020. [17].u.CCBSErase.Reason = 3,
  4021. [18].Function = Fac_CCBSErase,
  4022. [18].u.CCBSErase.InvokeID = 25,
  4023. [18].u.CCBSErase.Q931ie.Bc.Length = 2,
  4024. [18].u.CCBSErase.Q931ie.Bc.Contents = "JK",
  4025. [18].u.CCBSErase.AddressOfB.Party.Type = 8,
  4026. [18].u.CCBSErase.AddressOfB.Party.LengthOfNumber = 4,
  4027. [18].u.CCBSErase.AddressOfB.Party.Number = "1803",
  4028. [18].u.CCBSErase.RecallMode = 1,
  4029. [18].u.CCBSErase.CCBSReference = 102,
  4030. [18].u.CCBSErase.Reason = 3,
  4031. [19].Function = Fac_CCBSRemoteUserFree,
  4032. [19].u.CCBSRemoteUserFree.InvokeID = 26,
  4033. [19].u.CCBSRemoteUserFree.Q931ie.Bc.Length = 2,
  4034. [19].u.CCBSRemoteUserFree.Q931ie.Bc.Contents = "JK",
  4035. [19].u.CCBSRemoteUserFree.AddressOfB.Party.Type = 8,
  4036. [19].u.CCBSRemoteUserFree.AddressOfB.Party.LengthOfNumber = 4,
  4037. [19].u.CCBSRemoteUserFree.AddressOfB.Party.Number = "1803",
  4038. [19].u.CCBSRemoteUserFree.RecallMode = 1,
  4039. [19].u.CCBSRemoteUserFree.CCBSReference = 102,
  4040. [20].Function = Fac_CCBSCall,
  4041. [20].u.CCBSCall.InvokeID = 27,
  4042. [20].u.CCBSCall.CCBSReference = 115,
  4043. [21].Function = Fac_CCBSStatusRequest,
  4044. [21].u.CCBSStatusRequest.InvokeID = 28,
  4045. [21].u.CCBSStatusRequest.ComponentType = FacComponent_Invoke,
  4046. [21].u.CCBSStatusRequest.Component.Invoke.Q931ie.Bc.Length = 2,
  4047. [21].u.CCBSStatusRequest.Component.Invoke.Q931ie.Bc.Contents = "JK",
  4048. [21].u.CCBSStatusRequest.Component.Invoke.RecallMode = 1,
  4049. [21].u.CCBSStatusRequest.Component.Invoke.CCBSReference = 102,
  4050. [22].Function = Fac_CCBSStatusRequest,
  4051. [22].u.CCBSStatusRequest.InvokeID = 29,
  4052. [22].u.CCBSStatusRequest.ComponentType = FacComponent_Result,
  4053. [22].u.CCBSStatusRequest.Component.Result.Free = 1,
  4054. [23].Function = Fac_CCBSBFree,
  4055. [23].u.CCBSBFree.InvokeID = 30,
  4056. [23].u.CCBSBFree.Q931ie.Bc.Length = 2,
  4057. [23].u.CCBSBFree.Q931ie.Bc.Contents = "JK",
  4058. [23].u.CCBSBFree.AddressOfB.Party.Type = 8,
  4059. [23].u.CCBSBFree.AddressOfB.Party.LengthOfNumber = 4,
  4060. [23].u.CCBSBFree.AddressOfB.Party.Number = "1803",
  4061. [23].u.CCBSBFree.RecallMode = 1,
  4062. [23].u.CCBSBFree.CCBSReference = 14,
  4063. [24].Function = Fac_CCBSStopAlerting,
  4064. [24].u.CCBSStopAlerting.InvokeID = 31,
  4065. [24].u.CCBSStopAlerting.CCBSReference = 37,
  4066. [25].Function = Fac_CCBSRequest,
  4067. [25].u.CCBSRequest.InvokeID = 32,
  4068. [25].u.CCBSRequest.ComponentType = FacComponent_Invoke,
  4069. [25].u.CCBSRequest.Component.Invoke.CallLinkageID = 57,
  4070. [26].Function = Fac_CCBSRequest,
  4071. [26].u.CCBSRequest.InvokeID = 33,
  4072. [26].u.CCBSRequest.ComponentType = FacComponent_Result,
  4073. [26].u.CCBSRequest.Component.Result.RecallMode = 1,
  4074. [26].u.CCBSRequest.Component.Result.CCBSReference = 102,
  4075. [27].Function = Fac_CCBSInterrogate,
  4076. [27].u.CCBSInterrogate.InvokeID = 34,
  4077. [27].u.CCBSInterrogate.ComponentType = FacComponent_Invoke,
  4078. [27].u.CCBSInterrogate.Component.Invoke.AParty.Type = 8,
  4079. [27].u.CCBSInterrogate.Component.Invoke.AParty.LengthOfNumber = 4,
  4080. [27].u.CCBSInterrogate.Component.Invoke.AParty.Number = "1803",
  4081. [27].u.CCBSInterrogate.Component.Invoke.CCBSReferencePresent = 1,
  4082. [27].u.CCBSInterrogate.Component.Invoke.CCBSReference = 76,
  4083. [28].Function = Fac_CCBSInterrogate,
  4084. [28].u.CCBSInterrogate.InvokeID = 35,
  4085. [28].u.CCBSInterrogate.ComponentType = FacComponent_Invoke,
  4086. [28].u.CCBSInterrogate.Component.Invoke.AParty.Type = 8,
  4087. [28].u.CCBSInterrogate.Component.Invoke.AParty.LengthOfNumber = 4,
  4088. [28].u.CCBSInterrogate.Component.Invoke.AParty.Number = "1803",
  4089. [29].Function = Fac_CCBSInterrogate,
  4090. [29].u.CCBSInterrogate.InvokeID = 36,
  4091. [29].u.CCBSInterrogate.ComponentType = FacComponent_Invoke,
  4092. [29].u.CCBSInterrogate.Component.Invoke.CCBSReferencePresent = 1,
  4093. [29].u.CCBSInterrogate.Component.Invoke.CCBSReference = 76,
  4094. [30].Function = Fac_CCBSInterrogate,
  4095. [30].u.CCBSInterrogate.InvokeID = 37,
  4096. [30].u.CCBSInterrogate.ComponentType = FacComponent_Invoke,
  4097. [31].Function = Fac_CCBSInterrogate,
  4098. [31].u.CCBSInterrogate.InvokeID = 38,
  4099. [31].u.CCBSInterrogate.ComponentType = FacComponent_Result,
  4100. [31].u.CCBSInterrogate.Component.Result.RecallMode = 1,
  4101. [32].Function = Fac_CCBSInterrogate,
  4102. [32].u.CCBSInterrogate.InvokeID = 39,
  4103. [32].u.CCBSInterrogate.ComponentType = FacComponent_Result,
  4104. [32].u.CCBSInterrogate.Component.Result.RecallMode = 1,
  4105. [32].u.CCBSInterrogate.Component.Result.NumRecords = 1,
  4106. [32].u.CCBSInterrogate.Component.Result.CallDetails[0].CCBSReference = 12,
  4107. [32].u.CCBSInterrogate.Component.Result.CallDetails[0].Q931ie.Bc.Length = 2,
  4108. [32].u.CCBSInterrogate.Component.Result.CallDetails[0].Q931ie.Bc.Contents = "JK",
  4109. [32].u.CCBSInterrogate.Component.Result.CallDetails[0].AddressOfB.Party.Type = 8,
  4110. [32].u.CCBSInterrogate.Component.Result.CallDetails[0].AddressOfB.Party.LengthOfNumber = 4,
  4111. [32].u.CCBSInterrogate.Component.Result.CallDetails[0].AddressOfB.Party.Number = "1803",
  4112. [32].u.CCBSInterrogate.Component.Result.CallDetails[0].SubaddressOfA.Type = 1,
  4113. [32].u.CCBSInterrogate.Component.Result.CallDetails[0].SubaddressOfA.Length = 4,
  4114. [32].u.CCBSInterrogate.Component.Result.CallDetails[0].SubaddressOfA.u.Nsap = "6492",
  4115. [33].Function = Fac_CCBSInterrogate,
  4116. [33].u.CCBSInterrogate.InvokeID = 40,
  4117. [33].u.CCBSInterrogate.ComponentType = FacComponent_Result,
  4118. [33].u.CCBSInterrogate.Component.Result.RecallMode = 1,
  4119. [33].u.CCBSInterrogate.Component.Result.NumRecords = 2,
  4120. [33].u.CCBSInterrogate.Component.Result.CallDetails[0].CCBSReference = 12,
  4121. [33].u.CCBSInterrogate.Component.Result.CallDetails[0].Q931ie.Bc.Length = 2,
  4122. [33].u.CCBSInterrogate.Component.Result.CallDetails[0].Q931ie.Bc.Contents = "JK",
  4123. [33].u.CCBSInterrogate.Component.Result.CallDetails[0].AddressOfB.Party.Type = 8,
  4124. [33].u.CCBSInterrogate.Component.Result.CallDetails[0].AddressOfB.Party.LengthOfNumber = 4,
  4125. [33].u.CCBSInterrogate.Component.Result.CallDetails[0].AddressOfB.Party.Number = "1803",
  4126. [33].u.CCBSInterrogate.Component.Result.CallDetails[1].CCBSReference = 102,
  4127. [33].u.CCBSInterrogate.Component.Result.CallDetails[1].Q931ie.Bc.Length = 2,
  4128. [33].u.CCBSInterrogate.Component.Result.CallDetails[1].Q931ie.Bc.Contents = "LM",
  4129. [33].u.CCBSInterrogate.Component.Result.CallDetails[1].AddressOfB.Party.Type = 8,
  4130. [33].u.CCBSInterrogate.Component.Result.CallDetails[1].AddressOfB.Party.LengthOfNumber = 4,
  4131. [33].u.CCBSInterrogate.Component.Result.CallDetails[1].AddressOfB.Party.Number = "6229",
  4132. [33].u.CCBSInterrogate.Component.Result.CallDetails[1].AddressOfB.Subaddress.Type = 1,
  4133. [33].u.CCBSInterrogate.Component.Result.CallDetails[1].AddressOfB.Subaddress.Length = 4,
  4134. [33].u.CCBSInterrogate.Component.Result.CallDetails[1].AddressOfB.Subaddress.u.Nsap = "8592",
  4135. [33].u.CCBSInterrogate.Component.Result.CallDetails[1].SubaddressOfA.Type = 1,
  4136. [33].u.CCBSInterrogate.Component.Result.CallDetails[1].SubaddressOfA.Length = 4,
  4137. [33].u.CCBSInterrogate.Component.Result.CallDetails[1].SubaddressOfA.u.Nsap = "6492",
  4138. [34].Function = Fac_CCNRRequest,
  4139. [34].u.CCNRRequest.InvokeID = 512,
  4140. [34].u.CCNRRequest.ComponentType = FacComponent_Invoke,
  4141. [34].u.CCNRRequest.Component.Invoke.CallLinkageID = 57,
  4142. [35].Function = Fac_CCNRRequest,
  4143. [35].u.CCNRRequest.InvokeID = 150,
  4144. [35].u.CCNRRequest.ComponentType = FacComponent_Result,
  4145. [35].u.CCNRRequest.Component.Result.RecallMode = 1,
  4146. [35].u.CCNRRequest.Component.Result.CCBSReference = 102,
  4147. [36].Function = Fac_CCNRInterrogate,
  4148. [36].u.CCNRInterrogate.InvokeID = -129,
  4149. [36].u.CCNRInterrogate.ComponentType = FacComponent_Invoke,
  4150. [37].Function = Fac_CCNRInterrogate,
  4151. [37].u.CCNRInterrogate.InvokeID = -3,
  4152. [37].u.CCNRInterrogate.ComponentType = FacComponent_Result,
  4153. [37].u.CCNRInterrogate.Component.Result.RecallMode = 1,
  4154. [38].Function = Fac_CCBS_T_Call,
  4155. [38].u.EctExecute.InvokeID = 41,
  4156. [39].Function = Fac_CCBS_T_Suspend,
  4157. [39].u.EctExecute.InvokeID = 42,
  4158. [40].Function = Fac_CCBS_T_Resume,
  4159. [40].u.EctExecute.InvokeID = 43,
  4160. [41].Function = Fac_CCBS_T_RemoteUserFree,
  4161. [41].u.EctExecute.InvokeID = 44,
  4162. [42].Function = Fac_CCBS_T_Available,
  4163. [42].u.EctExecute.InvokeID = 45,
  4164. [43].Function = Fac_CCBS_T_Request,
  4165. [43].u.CCBS_T_Request.InvokeID = 46,
  4166. [43].u.CCBS_T_Request.ComponentType = FacComponent_Invoke,
  4167. [43].u.CCBS_T_Request.Component.Invoke.Destination.Party.Type = 8,
  4168. [43].u.CCBS_T_Request.Component.Invoke.Destination.Party.LengthOfNumber = 4,
  4169. [43].u.CCBS_T_Request.Component.Invoke.Destination.Party.Number = "6229",
  4170. [43].u.CCBS_T_Request.Component.Invoke.Q931ie.Bc.Length = 2,
  4171. [43].u.CCBS_T_Request.Component.Invoke.Q931ie.Bc.Contents = "LM",
  4172. [43].u.CCBS_T_Request.Component.Invoke.RetentionSupported = 1,
  4173. [43].u.CCBS_T_Request.Component.Invoke.PresentationAllowedIndicatorPresent = 1,
  4174. [43].u.CCBS_T_Request.Component.Invoke.PresentationAllowedIndicator = 1,
  4175. [43].u.CCBS_T_Request.Component.Invoke.Originating.Party.Type = 8,
  4176. [43].u.CCBS_T_Request.Component.Invoke.Originating.Party.LengthOfNumber = 4,
  4177. [43].u.CCBS_T_Request.Component.Invoke.Originating.Party.Number = "9864",
  4178. [44].Function = Fac_CCBS_T_Request,
  4179. [44].u.CCBS_T_Request.InvokeID = 47,
  4180. [44].u.CCBS_T_Request.ComponentType = FacComponent_Invoke,
  4181. [44].u.CCBS_T_Request.Component.Invoke.Destination.Party.Type = 8,
  4182. [44].u.CCBS_T_Request.Component.Invoke.Destination.Party.LengthOfNumber = 4,
  4183. [44].u.CCBS_T_Request.Component.Invoke.Destination.Party.Number = "6229",
  4184. [44].u.CCBS_T_Request.Component.Invoke.Q931ie.Bc.Length = 2,
  4185. [44].u.CCBS_T_Request.Component.Invoke.Q931ie.Bc.Contents = "LM",
  4186. [44].u.CCBS_T_Request.Component.Invoke.PresentationAllowedIndicatorPresent = 1,
  4187. [44].u.CCBS_T_Request.Component.Invoke.PresentationAllowedIndicator = 1,
  4188. [44].u.CCBS_T_Request.Component.Invoke.Originating.Party.Type = 8,
  4189. [44].u.CCBS_T_Request.Component.Invoke.Originating.Party.LengthOfNumber = 4,
  4190. [44].u.CCBS_T_Request.Component.Invoke.Originating.Party.Number = "9864",
  4191. [45].Function = Fac_CCBS_T_Request,
  4192. [45].u.CCBS_T_Request.InvokeID = 48,
  4193. [45].u.CCBS_T_Request.ComponentType = FacComponent_Invoke,
  4194. [45].u.CCBS_T_Request.Component.Invoke.Destination.Party.Type = 8,
  4195. [45].u.CCBS_T_Request.Component.Invoke.Destination.Party.LengthOfNumber = 4,
  4196. [45].u.CCBS_T_Request.Component.Invoke.Destination.Party.Number = "6229",
  4197. [45].u.CCBS_T_Request.Component.Invoke.Q931ie.Bc.Length = 2,
  4198. [45].u.CCBS_T_Request.Component.Invoke.Q931ie.Bc.Contents = "LM",
  4199. [45].u.CCBS_T_Request.Component.Invoke.Originating.Party.Type = 8,
  4200. [45].u.CCBS_T_Request.Component.Invoke.Originating.Party.LengthOfNumber = 4,
  4201. [45].u.CCBS_T_Request.Component.Invoke.Originating.Party.Number = "9864",
  4202. [46].Function = Fac_CCBS_T_Request,
  4203. [46].u.CCBS_T_Request.InvokeID = 49,
  4204. [46].u.CCBS_T_Request.ComponentType = FacComponent_Invoke,
  4205. [46].u.CCBS_T_Request.Component.Invoke.Destination.Party.Type = 8,
  4206. [46].u.CCBS_T_Request.Component.Invoke.Destination.Party.LengthOfNumber = 4,
  4207. [46].u.CCBS_T_Request.Component.Invoke.Destination.Party.Number = "6229",
  4208. [46].u.CCBS_T_Request.Component.Invoke.Q931ie.Bc.Length = 2,
  4209. [46].u.CCBS_T_Request.Component.Invoke.Q931ie.Bc.Contents = "LM",
  4210. [46].u.CCBS_T_Request.Component.Invoke.PresentationAllowedIndicatorPresent = 1,
  4211. [46].u.CCBS_T_Request.Component.Invoke.PresentationAllowedIndicator = 1,
  4212. [47].Function = Fac_CCBS_T_Request,
  4213. [47].u.CCBS_T_Request.InvokeID = 50,
  4214. [47].u.CCBS_T_Request.ComponentType = FacComponent_Invoke,
  4215. [47].u.CCBS_T_Request.Component.Invoke.Destination.Party.Type = 8,
  4216. [47].u.CCBS_T_Request.Component.Invoke.Destination.Party.LengthOfNumber = 4,
  4217. [47].u.CCBS_T_Request.Component.Invoke.Destination.Party.Number = "6229",
  4218. [47].u.CCBS_T_Request.Component.Invoke.Q931ie.Bc.Length = 2,
  4219. [47].u.CCBS_T_Request.Component.Invoke.Q931ie.Bc.Contents = "LM",
  4220. [48].Function = Fac_CCBS_T_Request,
  4221. [48].u.CCBS_T_Request.InvokeID = 51,
  4222. [48].u.CCBS_T_Request.ComponentType = FacComponent_Result,
  4223. [48].u.CCBS_T_Request.Component.Result.RetentionSupported = 1,
  4224. [49].Function = Fac_CCNR_T_Request,
  4225. [49].u.CCNR_T_Request.InvokeID = 52,
  4226. [49].u.CCNR_T_Request.ComponentType = FacComponent_Invoke,
  4227. [49].u.CCNR_T_Request.Component.Invoke.Destination.Party.Type = 8,
  4228. [49].u.CCNR_T_Request.Component.Invoke.Destination.Party.LengthOfNumber = 4,
  4229. [49].u.CCNR_T_Request.Component.Invoke.Destination.Party.Number = "6229",
  4230. [49].u.CCNR_T_Request.Component.Invoke.Q931ie.Bc.Length = 2,
  4231. [49].u.CCNR_T_Request.Component.Invoke.Q931ie.Bc.Contents = "LM",
  4232. [50].Function = Fac_CCNR_T_Request,
  4233. [50].u.CCNR_T_Request.InvokeID = 53,
  4234. [50].u.CCNR_T_Request.ComponentType = FacComponent_Result,
  4235. [50].u.CCNR_T_Request.Component.Result.RetentionSupported = 1,
  4236. [51].Function = Fac_EctExecute,
  4237. [51].u.EctExecute.InvokeID = 54,
  4238. [52].Function = Fac_ExplicitEctExecute,
  4239. [52].u.ExplicitEctExecute.InvokeID = 55,
  4240. [52].u.ExplicitEctExecute.LinkID = 23,
  4241. [53].Function = Fac_RequestSubaddress,
  4242. [53].u.RequestSubaddress.InvokeID = 56,
  4243. [54].Function = Fac_SubaddressTransfer,
  4244. [54].u.SubaddressTransfer.InvokeID = 57,
  4245. [54].u.SubaddressTransfer.Subaddress.Type = 1,
  4246. [54].u.SubaddressTransfer.Subaddress.Length = 4,
  4247. [54].u.SubaddressTransfer.Subaddress.u.Nsap = "6492",
  4248. [55].Function = Fac_EctLinkIdRequest,
  4249. [55].u.EctLinkIdRequest.InvokeID = 58,
  4250. [55].u.EctLinkIdRequest.ComponentType = FacComponent_Invoke,
  4251. [56].Function = Fac_EctLinkIdRequest,
  4252. [56].u.EctLinkIdRequest.InvokeID = 59,
  4253. [56].u.EctLinkIdRequest.ComponentType = FacComponent_Result,
  4254. [56].u.EctLinkIdRequest.Component.Result.LinkID = 76,
  4255. [57].Function = Fac_EctInform,
  4256. [57].u.EctInform.InvokeID = 60,
  4257. [57].u.EctInform.Status = 1,
  4258. [57].u.EctInform.RedirectionPresent = 1,
  4259. [57].u.EctInform.Redirection.Type = 0,
  4260. [57].u.EctInform.Redirection.Unscreened.Type = 8,
  4261. [57].u.EctInform.Redirection.Unscreened.LengthOfNumber = 4,
  4262. [57].u.EctInform.Redirection.Unscreened.Number = "6229",
  4263. [58].Function = Fac_EctInform,
  4264. [58].u.EctInform.InvokeID = 61,
  4265. [58].u.EctInform.Status = 1,
  4266. [58].u.EctInform.RedirectionPresent = 1,
  4267. [58].u.EctInform.Redirection.Type = 1,
  4268. [59].Function = Fac_EctInform,
  4269. [59].u.EctInform.InvokeID = 62,
  4270. [59].u.EctInform.Status = 1,
  4271. [59].u.EctInform.RedirectionPresent = 1,
  4272. [59].u.EctInform.Redirection.Type = 2,
  4273. [60].Function = Fac_EctInform,
  4274. [60].u.EctInform.InvokeID = 63,
  4275. [60].u.EctInform.Status = 1,
  4276. [60].u.EctInform.RedirectionPresent = 1,
  4277. [60].u.EctInform.Redirection.Type = 3,
  4278. [60].u.EctInform.Redirection.Unscreened.Type = 8,
  4279. [60].u.EctInform.Redirection.Unscreened.LengthOfNumber = 4,
  4280. [60].u.EctInform.Redirection.Unscreened.Number = "3340",
  4281. [61].Function = Fac_EctInform,
  4282. [61].u.EctInform.InvokeID = 64,
  4283. [61].u.EctInform.Status = 1,
  4284. [61].u.EctInform.RedirectionPresent = 0,
  4285. [62].Function = Fac_EctLoopTest,
  4286. [62].u.EctLoopTest.InvokeID = 65,
  4287. [62].u.EctLoopTest.ComponentType = FacComponent_Invoke,
  4288. [62].u.EctLoopTest.Component.Invoke.CallTransferID = 7,
  4289. [63].Function = Fac_EctLoopTest,
  4290. [63].u.EctLoopTest.InvokeID = 66,
  4291. [63].u.EctLoopTest.ComponentType = FacComponent_Result,
  4292. [63].u.EctLoopTest.Component.Result.LoopResult = 2,
  4293. [64].Function = Fac_ActivationDiversion,
  4294. [64].u.ActivationDiversion.InvokeID = 67,
  4295. [64].u.ActivationDiversion.ComponentType = FacComponent_Invoke,
  4296. [64].u.ActivationDiversion.Component.Invoke.Procedure = 2,
  4297. [64].u.ActivationDiversion.Component.Invoke.BasicService = 3,
  4298. [64].u.ActivationDiversion.Component.Invoke.ForwardedTo.Party.Type = 4,
  4299. [64].u.ActivationDiversion.Component.Invoke.ForwardedTo.Party.LengthOfNumber = 4,
  4300. [64].u.ActivationDiversion.Component.Invoke.ForwardedTo.Party.Number = "1803",
  4301. [64].u.ActivationDiversion.Component.Invoke.ServedUser.Type = 4,
  4302. [64].u.ActivationDiversion.Component.Invoke.ServedUser.LengthOfNumber = 4,
  4303. [64].u.ActivationDiversion.Component.Invoke.ServedUser.Number = "5398",
  4304. [65].Function = Fac_ActivationDiversion,
  4305. [65].u.ActivationDiversion.InvokeID = 68,
  4306. [65].u.ActivationDiversion.ComponentType = FacComponent_Invoke,
  4307. [65].u.ActivationDiversion.Component.Invoke.Procedure = 1,
  4308. [65].u.ActivationDiversion.Component.Invoke.BasicService = 5,
  4309. [65].u.ActivationDiversion.Component.Invoke.ForwardedTo.Party.Type = 4,
  4310. [65].u.ActivationDiversion.Component.Invoke.ForwardedTo.Party.LengthOfNumber = 4,
  4311. [65].u.ActivationDiversion.Component.Invoke.ForwardedTo.Party.Number = "1803",
  4312. [66].Function = Fac_ActivationDiversion,
  4313. [66].u.ActivationDiversion.InvokeID = 69,
  4314. [66].u.ActivationDiversion.ComponentType = FacComponent_Result,
  4315. [67].Function = Fac_DeactivationDiversion,
  4316. [67].u.DeactivationDiversion.InvokeID = 70,
  4317. [67].u.DeactivationDiversion.ComponentType = FacComponent_Invoke,
  4318. [67].u.DeactivationDiversion.Component.Invoke.Procedure = 1,
  4319. [67].u.DeactivationDiversion.Component.Invoke.BasicService = 5,
  4320. [68].Function = Fac_DeactivationDiversion,
  4321. [68].u.DeactivationDiversion.InvokeID = 71,
  4322. [68].u.DeactivationDiversion.ComponentType = FacComponent_Result,
  4323. [69].Function = Fac_ActivationStatusNotificationDiv,
  4324. [69].u.ActivationStatusNotificationDiv.InvokeID = 72,
  4325. [69].u.ActivationStatusNotificationDiv.Procedure = 1,
  4326. [69].u.ActivationStatusNotificationDiv.BasicService = 5,
  4327. [69].u.ActivationStatusNotificationDiv.ForwardedTo.Party.Type = 4,
  4328. [69].u.ActivationStatusNotificationDiv.ForwardedTo.Party.LengthOfNumber = 4,
  4329. [69].u.ActivationStatusNotificationDiv.ForwardedTo.Party.Number = "1803",
  4330. [70].Function = Fac_DeactivationStatusNotificationDiv,
  4331. [70].u.DeactivationStatusNotificationDiv.InvokeID = 73,
  4332. [70].u.DeactivationStatusNotificationDiv.Procedure = 1,
  4333. [70].u.DeactivationStatusNotificationDiv.BasicService = 5,
  4334. [71].Function = Fac_InterrogationDiversion,
  4335. [71].u.InterrogationDiversion.InvokeID = 74,
  4336. [71].u.InterrogationDiversion.ComponentType = FacComponent_Invoke,
  4337. [71].u.InterrogationDiversion.Component.Invoke.Procedure = 1,
  4338. [71].u.InterrogationDiversion.Component.Invoke.BasicService = 5,
  4339. [72].Function = Fac_InterrogationDiversion,
  4340. [72].u.InterrogationDiversion.InvokeID = 75,
  4341. [72].u.InterrogationDiversion.ComponentType = FacComponent_Invoke,
  4342. [72].u.InterrogationDiversion.Component.Invoke.Procedure = 1,
  4343. [73].Function = Fac_InterrogationDiversion,
  4344. [73].u.InterrogationDiversion.InvokeID = 76,
  4345. [73].u.InterrogationDiversion.ComponentType = FacComponent_Result,
  4346. [73].u.InterrogationDiversion.Component.Result.NumRecords = 2,
  4347. [73].u.InterrogationDiversion.Component.Result.List[0].Procedure = 2,
  4348. [73].u.InterrogationDiversion.Component.Result.List[0].BasicService = 5,
  4349. [73].u.InterrogationDiversion.Component.Result.List[0].ForwardedTo.Party.Type = 4,
  4350. [73].u.InterrogationDiversion.Component.Result.List[0].ForwardedTo.Party.LengthOfNumber = 4,
  4351. [73].u.InterrogationDiversion.Component.Result.List[0].ForwardedTo.Party.Number = "1803",
  4352. [73].u.InterrogationDiversion.Component.Result.List[1].Procedure = 1,
  4353. [73].u.InterrogationDiversion.Component.Result.List[1].BasicService = 3,
  4354. [73].u.InterrogationDiversion.Component.Result.List[1].ForwardedTo.Party.Type = 4,
  4355. [73].u.InterrogationDiversion.Component.Result.List[1].ForwardedTo.Party.LengthOfNumber = 4,
  4356. [73].u.InterrogationDiversion.Component.Result.List[1].ForwardedTo.Party.Number = "1903",
  4357. [73].u.InterrogationDiversion.Component.Result.List[1].ServedUser.Type = 4,
  4358. [73].u.InterrogationDiversion.Component.Result.List[1].ServedUser.LengthOfNumber = 4,
  4359. [73].u.InterrogationDiversion.Component.Result.List[1].ServedUser.Number = "5398",
  4360. [74].Function = Fac_DiversionInformation,
  4361. [74].u.DiversionInformation.InvokeID = 77,
  4362. [74].u.DiversionInformation.DiversionReason = 3,
  4363. [74].u.DiversionInformation.BasicService = 5,
  4364. [74].u.DiversionInformation.ServedUserSubaddress.Type = 1,
  4365. [74].u.DiversionInformation.ServedUserSubaddress.Length = 4,
  4366. [74].u.DiversionInformation.ServedUserSubaddress.u.Nsap = "6492",
  4367. [74].u.DiversionInformation.CallingAddressPresent = 1,
  4368. [74].u.DiversionInformation.CallingAddress.Type = 0,
  4369. [74].u.DiversionInformation.CallingAddress.Address.ScreeningIndicator = 3,
  4370. [74].u.DiversionInformation.CallingAddress.Address.Party.Type = 4,
  4371. [74].u.DiversionInformation.CallingAddress.Address.Party.LengthOfNumber = 4,
  4372. [74].u.DiversionInformation.CallingAddress.Address.Party.Number = "1803",
  4373. [74].u.DiversionInformation.OriginalCalledPresent = 1,
  4374. [74].u.DiversionInformation.OriginalCalled.Type = 1,
  4375. [74].u.DiversionInformation.LastDivertingPresent = 1,
  4376. [74].u.DiversionInformation.LastDiverting.Type = 2,
  4377. [74].u.DiversionInformation.LastDivertingReasonPresent = 1,
  4378. [74].u.DiversionInformation.LastDivertingReason = 3,
  4379. [74].u.DiversionInformation.UserInfo.Length = 5,
  4380. [74].u.DiversionInformation.UserInfo.Contents = "79828",
  4381. [75].Function = Fac_DiversionInformation,
  4382. [75].u.DiversionInformation.InvokeID = 78,
  4383. [75].u.DiversionInformation.DiversionReason = 3,
  4384. [75].u.DiversionInformation.BasicService = 5,
  4385. [75].u.DiversionInformation.CallingAddressPresent = 1,
  4386. [75].u.DiversionInformation.CallingAddress.Type = 1,
  4387. [75].u.DiversionInformation.OriginalCalledPresent = 1,
  4388. [75].u.DiversionInformation.OriginalCalled.Type = 2,
  4389. [75].u.DiversionInformation.LastDivertingPresent = 1,
  4390. [75].u.DiversionInformation.LastDiverting.Type = 1,
  4391. [76].Function = Fac_DiversionInformation,
  4392. [76].u.DiversionInformation.InvokeID = 79,
  4393. [76].u.DiversionInformation.DiversionReason = 2,
  4394. [76].u.DiversionInformation.BasicService = 3,
  4395. [76].u.DiversionInformation.CallingAddressPresent = 1,
  4396. [76].u.DiversionInformation.CallingAddress.Type = 2,
  4397. [77].Function = Fac_DiversionInformation,
  4398. [77].u.DiversionInformation.InvokeID = 80,
  4399. [77].u.DiversionInformation.DiversionReason = 3,
  4400. [77].u.DiversionInformation.BasicService = 5,
  4401. [77].u.DiversionInformation.CallingAddressPresent = 1,
  4402. [77].u.DiversionInformation.CallingAddress.Type = 3,
  4403. [77].u.DiversionInformation.CallingAddress.Address.ScreeningIndicator = 2,
  4404. [77].u.DiversionInformation.CallingAddress.Address.Party.Type = 4,
  4405. [77].u.DiversionInformation.CallingAddress.Address.Party.LengthOfNumber = 4,
  4406. [77].u.DiversionInformation.CallingAddress.Address.Party.Number = "1803",
  4407. [78].Function = Fac_DiversionInformation,
  4408. [78].u.DiversionInformation.InvokeID = 81,
  4409. [78].u.DiversionInformation.DiversionReason = 2,
  4410. [78].u.DiversionInformation.BasicService = 4,
  4411. [78].u.DiversionInformation.UserInfo.Length = 5,
  4412. [78].u.DiversionInformation.UserInfo.Contents = "79828",
  4413. [79].Function = Fac_DiversionInformation,
  4414. [79].u.DiversionInformation.InvokeID = 82,
  4415. [79].u.DiversionInformation.DiversionReason = 2,
  4416. [79].u.DiversionInformation.BasicService = 4,
  4417. [80].Function = Fac_CallDeflection,
  4418. [80].u.CallDeflection.InvokeID = 83,
  4419. [80].u.CallDeflection.ComponentType = FacComponent_Invoke,
  4420. [80].u.CallDeflection.Component.Invoke.Deflection.Party.Type = 4,
  4421. [80].u.CallDeflection.Component.Invoke.Deflection.Party.LengthOfNumber = 4,
  4422. [80].u.CallDeflection.Component.Invoke.Deflection.Party.Number = "1803",
  4423. [80].u.CallDeflection.Component.Invoke.PresentationAllowedToDivertedToUserPresent = 1,
  4424. [80].u.CallDeflection.Component.Invoke.PresentationAllowedToDivertedToUser = 1,
  4425. [81].Function = Fac_CallDeflection,
  4426. [81].u.CallDeflection.InvokeID = 84,
  4427. [81].u.CallDeflection.ComponentType = FacComponent_Invoke,
  4428. [81].u.CallDeflection.Component.Invoke.Deflection.Party.Type = 4,
  4429. [81].u.CallDeflection.Component.Invoke.Deflection.Party.LengthOfNumber = 4,
  4430. [81].u.CallDeflection.Component.Invoke.Deflection.Party.Number = "1803",
  4431. [81].u.CallDeflection.Component.Invoke.PresentationAllowedToDivertedToUserPresent = 1,
  4432. [81].u.CallDeflection.Component.Invoke.PresentationAllowedToDivertedToUser = 0,
  4433. [82].Function = Fac_CallDeflection,
  4434. [82].u.CallDeflection.InvokeID = 85,
  4435. [82].u.CallDeflection.ComponentType = FacComponent_Invoke,
  4436. [82].u.CallDeflection.Component.Invoke.Deflection.Party.Type = 4,
  4437. [82].u.CallDeflection.Component.Invoke.Deflection.Party.LengthOfNumber = 4,
  4438. [82].u.CallDeflection.Component.Invoke.Deflection.Party.Number = "1803",
  4439. [83].Function = Fac_CallDeflection,
  4440. [83].u.CallDeflection.InvokeID = 86,
  4441. [83].u.CallDeflection.ComponentType = FacComponent_Result,
  4442. [84].Function = Fac_CallRerouteing,
  4443. [84].u.CallRerouteing.InvokeID = 87,
  4444. [84].u.CallRerouteing.ComponentType = FacComponent_Invoke,
  4445. [84].u.CallRerouteing.Component.Invoke.ReroutingReason = 3,
  4446. [84].u.CallRerouteing.Component.Invoke.ReroutingCounter = 2,
  4447. [84].u.CallRerouteing.Component.Invoke.CalledAddress.Party.Type = 4,
  4448. [84].u.CallRerouteing.Component.Invoke.CalledAddress.Party.LengthOfNumber = 4,
  4449. [84].u.CallRerouteing.Component.Invoke.CalledAddress.Party.Number = "1803",
  4450. [84].u.CallRerouteing.Component.Invoke.Q931ie.Bc.Length = 2,
  4451. [84].u.CallRerouteing.Component.Invoke.Q931ie.Bc.Contents = "RT",
  4452. [84].u.CallRerouteing.Component.Invoke.Q931ie.Hlc.Length = 3,
  4453. [84].u.CallRerouteing.Component.Invoke.Q931ie.Hlc.Contents = "RTG",
  4454. [84].u.CallRerouteing.Component.Invoke.Q931ie.Llc.Length = 2,
  4455. [84].u.CallRerouteing.Component.Invoke.Q931ie.Llc.Contents = "MY",
  4456. [84].u.CallRerouteing.Component.Invoke.Q931ie.UserInfo.Length = 5,
  4457. [84].u.CallRerouteing.Component.Invoke.Q931ie.UserInfo.Contents = "YEHAW",
  4458. [84].u.CallRerouteing.Component.Invoke.LastRerouting.Type = 1,
  4459. [84].u.CallRerouteing.Component.Invoke.SubscriptionOption = 2,
  4460. [84].u.CallRerouteing.Component.Invoke.CallingPartySubaddress.Type = 1,
  4461. [84].u.CallRerouteing.Component.Invoke.CallingPartySubaddress.Length = 4,
  4462. [84].u.CallRerouteing.Component.Invoke.CallingPartySubaddress.u.Nsap = "6492",
  4463. [85].Function = Fac_CallRerouteing,
  4464. [85].u.CallRerouteing.InvokeID = 88,
  4465. [85].u.CallRerouteing.ComponentType = FacComponent_Invoke,
  4466. [85].u.CallRerouteing.Component.Invoke.ReroutingReason = 3,
  4467. [85].u.CallRerouteing.Component.Invoke.ReroutingCounter = 2,
  4468. [85].u.CallRerouteing.Component.Invoke.CalledAddress.Party.Type = 4,
  4469. [85].u.CallRerouteing.Component.Invoke.CalledAddress.Party.LengthOfNumber = 4,
  4470. [85].u.CallRerouteing.Component.Invoke.CalledAddress.Party.Number = "1803",
  4471. [85].u.CallRerouteing.Component.Invoke.Q931ie.Bc.Length = 2,
  4472. [85].u.CallRerouteing.Component.Invoke.Q931ie.Bc.Contents = "RT",
  4473. [85].u.CallRerouteing.Component.Invoke.LastRerouting.Type = 1,
  4474. [85].u.CallRerouteing.Component.Invoke.SubscriptionOption = 2,
  4475. [86].Function = Fac_CallRerouteing,
  4476. [86].u.CallRerouteing.InvokeID = 89,
  4477. [86].u.CallRerouteing.ComponentType = FacComponent_Invoke,
  4478. [86].u.CallRerouteing.Component.Invoke.ReroutingReason = 3,
  4479. [86].u.CallRerouteing.Component.Invoke.ReroutingCounter = 2,
  4480. [86].u.CallRerouteing.Component.Invoke.CalledAddress.Party.Type = 4,
  4481. [86].u.CallRerouteing.Component.Invoke.CalledAddress.Party.LengthOfNumber = 4,
  4482. [86].u.CallRerouteing.Component.Invoke.CalledAddress.Party.Number = "1803",
  4483. [86].u.CallRerouteing.Component.Invoke.Q931ie.Bc.Length = 2,
  4484. [86].u.CallRerouteing.Component.Invoke.Q931ie.Bc.Contents = "RT",
  4485. [86].u.CallRerouteing.Component.Invoke.LastRerouting.Type = 2,
  4486. [87].Function = Fac_CallRerouteing,
  4487. [87].u.CallRerouteing.InvokeID = 90,
  4488. [87].u.CallRerouteing.ComponentType = FacComponent_Result,
  4489. [88].Function = Fac_InterrogateServedUserNumbers,
  4490. [88].u.InterrogateServedUserNumbers.InvokeID = 91,
  4491. [88].u.InterrogateServedUserNumbers.ComponentType = FacComponent_Invoke,
  4492. [89].Function = Fac_InterrogateServedUserNumbers,
  4493. [89].u.InterrogateServedUserNumbers.InvokeID = 92,
  4494. [89].u.InterrogateServedUserNumbers.ComponentType = FacComponent_Result,
  4495. [89].u.InterrogateServedUserNumbers.Component.Result.NumRecords = 2,
  4496. [89].u.InterrogateServedUserNumbers.Component.Result.List[0].Type = 4,
  4497. [89].u.InterrogateServedUserNumbers.Component.Result.List[0].LengthOfNumber = 4,
  4498. [89].u.InterrogateServedUserNumbers.Component.Result.List[0].Number = "1803",
  4499. [89].u.InterrogateServedUserNumbers.Component.Result.List[1].Type = 4,
  4500. [89].u.InterrogateServedUserNumbers.Component.Result.List[1].LengthOfNumber = 4,
  4501. [89].u.InterrogateServedUserNumbers.Component.Result.List[1].Number = "5786",
  4502. [90].Function = Fac_DivertingLegInformation1,
  4503. [90].u.DivertingLegInformation1.InvokeID = 93,
  4504. [90].u.DivertingLegInformation1.DiversionReason = 4,
  4505. [90].u.DivertingLegInformation1.SubscriptionOption = 1,
  4506. [90].u.DivertingLegInformation1.DivertedToPresent = 1,
  4507. [90].u.DivertingLegInformation1.DivertedTo.Type = 2,
  4508. [91].Function = Fac_DivertingLegInformation1,
  4509. [91].u.DivertingLegInformation1.InvokeID = 94,
  4510. [91].u.DivertingLegInformation1.DiversionReason = 4,
  4511. [91].u.DivertingLegInformation1.SubscriptionOption = 1,
  4512. [92].Function = Fac_DivertingLegInformation2,
  4513. [92].u.DivertingLegInformation2.InvokeID = 95,
  4514. [92].u.DivertingLegInformation2.DiversionCounter = 3,
  4515. [92].u.DivertingLegInformation2.DiversionReason = 2,
  4516. [92].u.DivertingLegInformation2.DivertingPresent = 1,
  4517. [92].u.DivertingLegInformation2.Diverting.Type = 2,
  4518. [92].u.DivertingLegInformation2.OriginalCalledPresent = 1,
  4519. [92].u.DivertingLegInformation2.OriginalCalled.Type = 1,
  4520. [93].Function = Fac_DivertingLegInformation2,
  4521. [93].u.DivertingLegInformation2.InvokeID = 96,
  4522. [93].u.DivertingLegInformation2.DiversionCounter = 3,
  4523. [93].u.DivertingLegInformation2.DiversionReason = 2,
  4524. [93].u.DivertingLegInformation2.OriginalCalledPresent = 1,
  4525. [93].u.DivertingLegInformation2.OriginalCalled.Type = 1,
  4526. [94].Function = Fac_DivertingLegInformation2,
  4527. [94].u.DivertingLegInformation2.InvokeID = 97,
  4528. [94].u.DivertingLegInformation2.DiversionCounter = 1,
  4529. [94].u.DivertingLegInformation2.DiversionReason = 2,
  4530. [95].Function = Fac_DivertingLegInformation3,
  4531. [95].u.DivertingLegInformation3.InvokeID = 98,
  4532. [95].u.DivertingLegInformation3.PresentationAllowedIndicator = 1,
  4533. /* *INDENT-ON* */
  4534. };
  4535. #endif /* defined(AST_MISDN_ENHANCEMENTS) && defined(CCBS_TEST_MESSAGES) */
  4536. static char *handle_cli_misdn_send_facility(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  4537. {
  4538. const char *channame;
  4539. const char *nr;
  4540. struct chan_list *tmp;
  4541. int port;
  4542. const char *served_nr;
  4543. struct misdn_bchannel dummy, *bc=&dummy;
  4544. unsigned max_len;
  4545. switch (cmd) {
  4546. case CLI_INIT:
  4547. e->command = "misdn send facility";
  4548. e->usage = "Usage: misdn send facility <type> <channel|port> \"<args>\" \n"
  4549. "\t type is one of:\n"
  4550. "\t - calldeflect\n"
  4551. #if defined(AST_MISDN_ENHANCEMENTS)
  4552. "\t - callrerouting\n"
  4553. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  4554. "\t - CFActivate\n"
  4555. "\t - CFDeactivate\n";
  4556. return NULL;
  4557. case CLI_GENERATE:
  4558. return complete_ch(a);
  4559. }
  4560. if (a->argc < 5) {
  4561. return CLI_SHOWUSAGE;
  4562. }
  4563. if (strstr(a->argv[3], "calldeflect")) {
  4564. if (a->argc < 6) {
  4565. ast_verbose("calldeflect requires 1 arg: ToNumber\n\n");
  4566. return 0;
  4567. }
  4568. channame = a->argv[4];
  4569. nr = a->argv[5];
  4570. ast_verbose("Sending Calldeflection (%s) to %s\n", nr, channame);
  4571. tmp = get_chan_by_ast_name(channame);
  4572. if (!tmp) {
  4573. ast_verbose("Sending CD with nr %s to %s failed: Channel does not exist.\n", nr, channame);
  4574. return 0;
  4575. }
  4576. ao2_lock(tmp);
  4577. #if defined(AST_MISDN_ENHANCEMENTS)
  4578. max_len = sizeof(tmp->bc->fac_out.u.CallDeflection.Component.Invoke.Deflection.Party.Number) - 1;
  4579. if (max_len < strlen(nr)) {
  4580. ast_verbose("Sending CD with nr %s to %s failed: Number too long (up to %u digits are allowed).\n",
  4581. nr, channame, max_len);
  4582. ao2_unlock(tmp);
  4583. chan_list_unref(tmp, "Number too long");
  4584. return 0;
  4585. }
  4586. tmp->bc->fac_out.Function = Fac_CallDeflection;
  4587. tmp->bc->fac_out.u.CallDeflection.InvokeID = ++misdn_invoke_id;
  4588. tmp->bc->fac_out.u.CallDeflection.ComponentType = FacComponent_Invoke;
  4589. tmp->bc->fac_out.u.CallDeflection.Component.Invoke.PresentationAllowedToDivertedToUserPresent = 1;
  4590. tmp->bc->fac_out.u.CallDeflection.Component.Invoke.PresentationAllowedToDivertedToUser = 0;
  4591. tmp->bc->fac_out.u.CallDeflection.Component.Invoke.Deflection.Party.Type = 0;/* unknown */
  4592. tmp->bc->fac_out.u.CallDeflection.Component.Invoke.Deflection.Party.LengthOfNumber = strlen(nr);
  4593. strcpy((char *) tmp->bc->fac_out.u.CallDeflection.Component.Invoke.Deflection.Party.Number, nr);
  4594. tmp->bc->fac_out.u.CallDeflection.Component.Invoke.Deflection.Subaddress.Length = 0;
  4595. #else /* !defined(AST_MISDN_ENHANCEMENTS) */
  4596. max_len = sizeof(tmp->bc->fac_out.u.CDeflection.DeflectedToNumber) - 1;
  4597. if (max_len < strlen(nr)) {
  4598. ast_verbose("Sending CD with nr %s to %s failed: Number too long (up to %u digits are allowed).\n",
  4599. nr, channame, max_len);
  4600. ao2_unlock(tmp);
  4601. chan_list_unref(tmp, "Number too long");
  4602. return 0;
  4603. }
  4604. tmp->bc->fac_out.Function = Fac_CD;
  4605. tmp->bc->fac_out.u.CDeflection.PresentationAllowed = 0;
  4606. //tmp->bc->fac_out.u.CDeflection.DeflectedToSubaddress[0] = 0;
  4607. strcpy((char *) tmp->bc->fac_out.u.CDeflection.DeflectedToNumber, nr);
  4608. #endif /* !defined(AST_MISDN_ENHANCEMENTS) */
  4609. /* Send message */
  4610. print_facility(&tmp->bc->fac_out, tmp->bc);
  4611. ao2_unlock(tmp);
  4612. misdn_lib_send_event(tmp->bc, EVENT_FACILITY);
  4613. chan_list_unref(tmp, "Send facility complete");
  4614. #if defined(AST_MISDN_ENHANCEMENTS)
  4615. } else if (strstr(a->argv[3], "callrerouteing") || strstr(a->argv[3], "callrerouting")) {
  4616. if (a->argc < 6) {
  4617. ast_verbose("callrerouting requires 1 arg: ToNumber\n\n");
  4618. return 0;
  4619. }
  4620. channame = a->argv[4];
  4621. nr = a->argv[5];
  4622. ast_verbose("Sending Callrerouting (%s) to %s\n", nr, channame);
  4623. tmp = get_chan_by_ast_name(channame);
  4624. if (!tmp) {
  4625. ast_verbose("Sending Call Rerouting with nr %s to %s failed: Channel does not exist.\n", nr, channame);
  4626. return 0;
  4627. }
  4628. ao2_lock(tmp);
  4629. max_len = sizeof(tmp->bc->fac_out.u.CallRerouteing.Component.Invoke.CalledAddress.Party.Number) - 1;
  4630. if (max_len < strlen(nr)) {
  4631. ast_verbose("Sending Call Rerouting with nr %s to %s failed: Number too long (up to %u digits are allowed).\n",
  4632. nr, channame, max_len);
  4633. ao2_unlock(tmp);
  4634. chan_list_unref(tmp, "Number too long");
  4635. return 0;
  4636. }
  4637. tmp->bc->fac_out.Function = Fac_CallRerouteing;
  4638. tmp->bc->fac_out.u.CallRerouteing.InvokeID = ++misdn_invoke_id;
  4639. tmp->bc->fac_out.u.CallRerouteing.ComponentType = FacComponent_Invoke;
  4640. tmp->bc->fac_out.u.CallRerouteing.Component.Invoke.ReroutingReason = 0;/* unknown */
  4641. tmp->bc->fac_out.u.CallRerouteing.Component.Invoke.ReroutingCounter = 1;
  4642. tmp->bc->fac_out.u.CallRerouteing.Component.Invoke.CalledAddress.Party.Type = 0;/* unknown */
  4643. tmp->bc->fac_out.u.CallRerouteing.Component.Invoke.CalledAddress.Party.LengthOfNumber = strlen(nr);
  4644. strcpy((char *) tmp->bc->fac_out.u.CallRerouteing.Component.Invoke.CalledAddress.Party.Number, nr);
  4645. tmp->bc->fac_out.u.CallRerouteing.Component.Invoke.CalledAddress.Subaddress.Length = 0;
  4646. tmp->bc->fac_out.u.CallRerouteing.Component.Invoke.CallingPartySubaddress.Length = 0;
  4647. /* 0x90 0x90 0xa3 3.1 kHz audio, circuit mode, 64kbit/sec, level1/a-Law */
  4648. tmp->bc->fac_out.u.CallRerouteing.Component.Invoke.Q931ie.Bc.Length = 3;
  4649. tmp->bc->fac_out.u.CallRerouteing.Component.Invoke.Q931ie.Bc.Contents[0] = 0x90;
  4650. tmp->bc->fac_out.u.CallRerouteing.Component.Invoke.Q931ie.Bc.Contents[1] = 0x90;
  4651. tmp->bc->fac_out.u.CallRerouteing.Component.Invoke.Q931ie.Bc.Contents[2] = 0xa3;
  4652. tmp->bc->fac_out.u.CallRerouteing.Component.Invoke.Q931ie.Hlc.Length = 0;
  4653. tmp->bc->fac_out.u.CallRerouteing.Component.Invoke.Q931ie.Llc.Length = 0;
  4654. tmp->bc->fac_out.u.CallRerouteing.Component.Invoke.Q931ie.UserInfo.Length = 0;
  4655. tmp->bc->fac_out.u.CallRerouteing.Component.Invoke.LastRerouting.Type = 1;/* presentationRestricted */
  4656. tmp->bc->fac_out.u.CallRerouteing.Component.Invoke.SubscriptionOption = 0;/* no notification to caller */
  4657. /* Send message */
  4658. print_facility(&tmp->bc->fac_out, tmp->bc);
  4659. ao2_unlock(tmp);
  4660. misdn_lib_send_event(tmp->bc, EVENT_FACILITY);
  4661. chan_list_unref(tmp, "Send facility complete");
  4662. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  4663. } else if (strstr(a->argv[3], "CFActivate")) {
  4664. if (a->argc < 7) {
  4665. ast_verbose("CFActivate requires 2 args: 1.FromNumber, 2.ToNumber\n\n");
  4666. return 0;
  4667. }
  4668. port = atoi(a->argv[4]);
  4669. served_nr = a->argv[5];
  4670. nr = a->argv[6];
  4671. misdn_make_dummy(bc, port, 0, misdn_lib_port_is_nt(port), 0);
  4672. ast_verbose("Sending CFActivate Port:(%d) FromNr. (%s) to Nr. (%s)\n", port, served_nr, nr);
  4673. #if defined(AST_MISDN_ENHANCEMENTS)
  4674. bc->fac_out.Function = Fac_ActivationDiversion;
  4675. bc->fac_out.u.ActivationDiversion.InvokeID = ++misdn_invoke_id;
  4676. bc->fac_out.u.ActivationDiversion.ComponentType = FacComponent_Invoke;
  4677. bc->fac_out.u.ActivationDiversion.Component.Invoke.BasicService = 0;/* allServices */
  4678. bc->fac_out.u.ActivationDiversion.Component.Invoke.Procedure = 0;/* cfu (Call Forward Unconditional) */
  4679. ast_copy_string((char *) bc->fac_out.u.ActivationDiversion.Component.Invoke.ServedUser.Number,
  4680. served_nr, sizeof(bc->fac_out.u.ActivationDiversion.Component.Invoke.ServedUser.Number));
  4681. bc->fac_out.u.ActivationDiversion.Component.Invoke.ServedUser.LengthOfNumber =
  4682. strlen((char *) bc->fac_out.u.ActivationDiversion.Component.Invoke.ServedUser.Number);
  4683. bc->fac_out.u.ActivationDiversion.Component.Invoke.ServedUser.Type = 0;/* unknown */
  4684. ast_copy_string((char *) bc->fac_out.u.ActivationDiversion.Component.Invoke.ForwardedTo.Party.Number,
  4685. nr, sizeof(bc->fac_out.u.ActivationDiversion.Component.Invoke.ForwardedTo.Party.Number));
  4686. bc->fac_out.u.ActivationDiversion.Component.Invoke.ForwardedTo.Party.LengthOfNumber =
  4687. strlen((char *) bc->fac_out.u.ActivationDiversion.Component.Invoke.ForwardedTo.Party.Number);
  4688. bc->fac_out.u.ActivationDiversion.Component.Invoke.ForwardedTo.Party.Type = 0;/* unknown */
  4689. bc->fac_out.u.ActivationDiversion.Component.Invoke.ForwardedTo.Subaddress.Length = 0;
  4690. #else /* !defined(AST_MISDN_ENHANCEMENTS) */
  4691. bc->fac_out.Function = Fac_CFActivate;
  4692. bc->fac_out.u.CFActivate.BasicService = 0; /* All Services */
  4693. bc->fac_out.u.CFActivate.Procedure = 0; /* Unconditional */
  4694. ast_copy_string((char *) bc->fac_out.u.CFActivate.ServedUserNumber, served_nr, sizeof(bc->fac_out.u.CFActivate.ServedUserNumber));
  4695. ast_copy_string((char *) bc->fac_out.u.CFActivate.ForwardedToNumber, nr, sizeof(bc->fac_out.u.CFActivate.ForwardedToNumber));
  4696. #endif /* !defined(AST_MISDN_ENHANCEMENTS) */
  4697. /* Send message */
  4698. print_facility(&bc->fac_out, bc);
  4699. misdn_lib_send_event(bc, EVENT_FACILITY);
  4700. } else if (strstr(a->argv[3], "CFDeactivate")) {
  4701. if (a->argc < 6) {
  4702. ast_verbose("CFDeactivate requires 1 arg: FromNumber\n\n");
  4703. return 0;
  4704. }
  4705. port = atoi(a->argv[4]);
  4706. served_nr = a->argv[5];
  4707. misdn_make_dummy(bc, port, 0, misdn_lib_port_is_nt(port), 0);
  4708. ast_verbose("Sending CFDeactivate Port:(%d) FromNr. (%s)\n", port, served_nr);
  4709. #if defined(AST_MISDN_ENHANCEMENTS)
  4710. bc->fac_out.Function = Fac_DeactivationDiversion;
  4711. bc->fac_out.u.DeactivationDiversion.InvokeID = ++misdn_invoke_id;
  4712. bc->fac_out.u.DeactivationDiversion.ComponentType = FacComponent_Invoke;
  4713. bc->fac_out.u.DeactivationDiversion.Component.Invoke.BasicService = 0;/* allServices */
  4714. bc->fac_out.u.DeactivationDiversion.Component.Invoke.Procedure = 0;/* cfu (Call Forward Unconditional) */
  4715. ast_copy_string((char *) bc->fac_out.u.DeactivationDiversion.Component.Invoke.ServedUser.Number,
  4716. served_nr, sizeof(bc->fac_out.u.DeactivationDiversion.Component.Invoke.ServedUser.Number));
  4717. bc->fac_out.u.DeactivationDiversion.Component.Invoke.ServedUser.LengthOfNumber =
  4718. strlen((char *) bc->fac_out.u.DeactivationDiversion.Component.Invoke.ServedUser.Number);
  4719. bc->fac_out.u.DeactivationDiversion.Component.Invoke.ServedUser.Type = 0;/* unknown */
  4720. #else /* !defined(AST_MISDN_ENHANCEMENTS) */
  4721. bc->fac_out.Function = Fac_CFDeactivate;
  4722. bc->fac_out.u.CFDeactivate.BasicService = 0; /* All Services */
  4723. bc->fac_out.u.CFDeactivate.Procedure = 0; /* Unconditional */
  4724. ast_copy_string((char *) bc->fac_out.u.CFActivate.ServedUserNumber, served_nr, sizeof(bc->fac_out.u.CFActivate.ServedUserNumber));
  4725. #endif /* !defined(AST_MISDN_ENHANCEMENTS) */
  4726. /* Send message */
  4727. print_facility(&bc->fac_out, bc);
  4728. misdn_lib_send_event(bc, EVENT_FACILITY);
  4729. #if defined(AST_MISDN_ENHANCEMENTS) && defined(CCBS_TEST_MESSAGES)
  4730. } else if (strstr(a->argv[3], "test")) {
  4731. int msg_number;
  4732. if (a->argc < 5) {
  4733. ast_verbose("test (<port> [<msg#>]) | (<channel-name> <msg#>)\n\n");
  4734. return 0;
  4735. }
  4736. port = atoi(a->argv[4]);
  4737. channame = a->argv[4];
  4738. tmp = get_chan_by_ast_name(channame);
  4739. if (tmp) {
  4740. /* We are going to send this FACILITY message out on an existing connection */
  4741. msg_number = atoi(a->argv[5]);
  4742. if (msg_number < ARRAY_LEN(Fac_Msgs)) {
  4743. ao2_lock(tmp);
  4744. tmp->bc->fac_out = Fac_Msgs[msg_number];
  4745. /* Send message */
  4746. print_facility(&tmp->bc->fac_out, tmp->bc);
  4747. ao2_unlock(tmp);
  4748. misdn_lib_send_event(tmp->bc, EVENT_FACILITY);
  4749. } else {
  4750. ast_verbose("test <channel-name> <msg#>\n\n");
  4751. }
  4752. chan_list_unref(tmp, "Facility test done");
  4753. } else if (a->argc < 6) {
  4754. for (msg_number = 0; msg_number < ARRAY_LEN(Fac_Msgs); ++msg_number) {
  4755. misdn_make_dummy(bc, port, 0, misdn_lib_port_is_nt(port), 0);
  4756. bc->fac_out = Fac_Msgs[msg_number];
  4757. /* Send message */
  4758. print_facility(&bc->fac_out, bc);
  4759. misdn_lib_send_event(bc, EVENT_FACILITY);
  4760. sleep(1);
  4761. }
  4762. } else {
  4763. msg_number = atoi(a->argv[5]);
  4764. if (msg_number < ARRAY_LEN(Fac_Msgs)) {
  4765. misdn_make_dummy(bc, port, 0, misdn_lib_port_is_nt(port), 0);
  4766. bc->fac_out = Fac_Msgs[msg_number];
  4767. /* Send message */
  4768. print_facility(&bc->fac_out, bc);
  4769. misdn_lib_send_event(bc, EVENT_FACILITY);
  4770. } else {
  4771. ast_verbose("test <port> [<msg#>]\n\n");
  4772. }
  4773. }
  4774. } else if (strstr(a->argv[3], "register")) {
  4775. if (a->argc < 5) {
  4776. ast_verbose("register <port>\n\n");
  4777. return 0;
  4778. }
  4779. port = atoi(a->argv[4]);
  4780. bc = misdn_lib_get_register_bc(port);
  4781. if (!bc) {
  4782. ast_verbose("Could not allocate REGISTER bc struct\n\n");
  4783. return 0;
  4784. }
  4785. bc->fac_out = Fac_Msgs[45];
  4786. /* Send message */
  4787. print_facility(&bc->fac_out, bc);
  4788. misdn_lib_send_event(bc, EVENT_REGISTER);
  4789. #endif /* defined(AST_MISDN_ENHANCEMENTS) && defined(CCBS_TEST_MESSAGES) */
  4790. }
  4791. return CLI_SUCCESS;
  4792. }
  4793. static char *handle_cli_misdn_send_restart(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  4794. {
  4795. int port;
  4796. int channel;
  4797. switch (cmd) {
  4798. case CLI_INIT:
  4799. e->command = "misdn send restart";
  4800. e->usage =
  4801. "Usage: misdn send restart [port [channel]]\n"
  4802. " Send a restart for every bchannel on the given port.\n";
  4803. return NULL;
  4804. case CLI_GENERATE:
  4805. return NULL;
  4806. }
  4807. if (a->argc < 4 || a->argc > 5) {
  4808. return CLI_SHOWUSAGE;
  4809. }
  4810. port = atoi(a->argv[3]);
  4811. if (a->argc == 5) {
  4812. channel = atoi(a->argv[4]);
  4813. misdn_lib_send_restart(port, channel);
  4814. } else {
  4815. misdn_lib_send_restart(port, -1);
  4816. }
  4817. return CLI_SUCCESS;
  4818. }
  4819. static char *handle_cli_misdn_send_digit(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  4820. {
  4821. const char *channame;
  4822. const char *msg;
  4823. struct chan_list *tmp;
  4824. int i, msglen;
  4825. switch (cmd) {
  4826. case CLI_INIT:
  4827. e->command = "misdn send digit";
  4828. e->usage =
  4829. "Usage: misdn send digit <channel> \"<msg>\" \n"
  4830. " Send <digit> to <channel> as DTMF Tone\n"
  4831. " when channel is a mISDN channel\n";
  4832. return NULL;
  4833. case CLI_GENERATE:
  4834. return complete_ch(a);
  4835. }
  4836. if (a->argc != 5) {
  4837. return CLI_SHOWUSAGE;
  4838. }
  4839. channame = a->argv[3];
  4840. msg = a->argv[4];
  4841. msglen = strlen(msg);
  4842. ast_cli(a->fd, "Sending %s to %s\n", msg, channame);
  4843. tmp = get_chan_by_ast_name(channame);
  4844. if (!tmp) {
  4845. ast_cli(a->fd, "Sending %s to %s failed Channel does not exist\n", msg, channame);
  4846. return CLI_SUCCESS;
  4847. }
  4848. #if 1
  4849. for (i = 0; i < msglen; i++) {
  4850. if (!tmp->ast) {
  4851. break;
  4852. }
  4853. ast_cli(a->fd, "Sending: %c\n", msg[i]);
  4854. send_digit_to_chan(tmp, msg[i]);
  4855. /* res = ast_safe_sleep(tmp->ast, 250); */
  4856. usleep(250000);
  4857. /* res = ast_waitfor(tmp->ast,100); */
  4858. }
  4859. #else
  4860. if (tmp->ast) {
  4861. ast_dtmf_stream(tmp->ast, NULL, msg, 250);
  4862. }
  4863. #endif
  4864. chan_list_unref(tmp, "Digit(s) sent");
  4865. return CLI_SUCCESS;
  4866. }
  4867. static char *handle_cli_misdn_toggle_echocancel(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  4868. {
  4869. const char *channame;
  4870. struct chan_list *tmp;
  4871. switch (cmd) {
  4872. case CLI_INIT:
  4873. e->command = "misdn toggle echocancel";
  4874. e->usage =
  4875. "Usage: misdn toggle echocancel <channel>\n"
  4876. " Toggle EchoCancel on mISDN Channel.\n";
  4877. return NULL;
  4878. case CLI_GENERATE:
  4879. return complete_ch(a);
  4880. }
  4881. if (a->argc != 4) {
  4882. return CLI_SHOWUSAGE;
  4883. }
  4884. channame = a->argv[3];
  4885. ast_cli(a->fd, "Toggling EchoCancel on %s\n", channame);
  4886. tmp = get_chan_by_ast_name(channame);
  4887. if (!tmp) {
  4888. ast_cli(a->fd, "Toggling EchoCancel %s failed Channel does not exist\n", channame);
  4889. return CLI_SUCCESS;
  4890. }
  4891. tmp->toggle_ec = tmp->toggle_ec ? 0 : 1;
  4892. if (tmp->toggle_ec) {
  4893. #ifdef MISDN_1_2
  4894. update_pipeline_config(tmp->bc);
  4895. #else
  4896. update_ec_config(tmp->bc);
  4897. #endif
  4898. manager_ec_enable(tmp->bc);
  4899. } else {
  4900. manager_ec_disable(tmp->bc);
  4901. }
  4902. chan_list_unref(tmp, "Done toggling echo cancel");
  4903. return CLI_SUCCESS;
  4904. }
  4905. static char *handle_cli_misdn_send_display(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  4906. {
  4907. const char *channame;
  4908. const char *msg;
  4909. struct chan_list *tmp;
  4910. switch (cmd) {
  4911. case CLI_INIT:
  4912. e->command = "misdn send display";
  4913. e->usage =
  4914. "Usage: misdn send display <channel> \"<msg>\" \n"
  4915. " Send <msg> to <channel> as Display Message\n"
  4916. " when channel is a mISDN channel\n";
  4917. return NULL;
  4918. case CLI_GENERATE:
  4919. return complete_ch(a);
  4920. }
  4921. if (a->argc != 5) {
  4922. return CLI_SHOWUSAGE;
  4923. }
  4924. channame = a->argv[3];
  4925. msg = a->argv[4];
  4926. ast_cli(a->fd, "Sending %s to %s\n", msg, channame);
  4927. tmp = get_chan_by_ast_name(channame);
  4928. if (tmp && tmp->bc) {
  4929. ast_copy_string(tmp->bc->display, msg, sizeof(tmp->bc->display));
  4930. misdn_lib_send_event(tmp->bc, EVENT_INFORMATION);
  4931. chan_list_unref(tmp, "Done sending display");
  4932. } else {
  4933. if (tmp) {
  4934. chan_list_unref(tmp, "Display failed");
  4935. }
  4936. ast_cli(a->fd, "No such channel %s\n", channame);
  4937. return CLI_SUCCESS;
  4938. }
  4939. return CLI_SUCCESS;
  4940. }
  4941. static char *complete_ch(struct ast_cli_args *a)
  4942. {
  4943. return ast_complete_channels(a->line, a->word, a->pos, a->n, 3);
  4944. }
  4945. static char *complete_debug_port(struct ast_cli_args *a)
  4946. {
  4947. if (a->n) {
  4948. return NULL;
  4949. }
  4950. switch (a->pos) {
  4951. case 4:
  4952. if (a->word[0] == 'p') {
  4953. return ast_strdup("port");
  4954. } else if (a->word[0] == 'o') {
  4955. return ast_strdup("only");
  4956. }
  4957. break;
  4958. case 6:
  4959. if (a->word[0] == 'o') {
  4960. return ast_strdup("only");
  4961. }
  4962. break;
  4963. }
  4964. return NULL;
  4965. }
  4966. static char *complete_show_config(struct ast_cli_args *a)
  4967. {
  4968. char buffer[BUFFERSIZE];
  4969. enum misdn_cfg_elements elem;
  4970. int wordlen = strlen(a->word);
  4971. int which = 0;
  4972. int port = 0;
  4973. switch (a->pos) {
  4974. case 3:
  4975. if ((!strncmp(a->word, "description", wordlen)) && (++which > a->n)) {
  4976. return ast_strdup("description");
  4977. }
  4978. if ((!strncmp(a->word, "descriptions", wordlen)) && (++which > a->n)) {
  4979. return ast_strdup("descriptions");
  4980. }
  4981. if ((!strncmp(a->word, "0", wordlen)) && (++which > a->n)) {
  4982. return ast_strdup("0");
  4983. }
  4984. while ((port = misdn_cfg_get_next_port(port)) != -1) {
  4985. snprintf(buffer, sizeof(buffer), "%d", port);
  4986. if ((!strncmp(a->word, buffer, wordlen)) && (++which > a->n)) {
  4987. return ast_strdup(buffer);
  4988. }
  4989. }
  4990. break;
  4991. case 4:
  4992. if (strstr(a->line, "description ")) {
  4993. for (elem = MISDN_CFG_FIRST + 1; elem < MISDN_GEN_LAST; ++elem) {
  4994. if ((elem == MISDN_CFG_LAST) || (elem == MISDN_GEN_FIRST)) {
  4995. continue;
  4996. }
  4997. misdn_cfg_get_name(elem, buffer, sizeof(buffer));
  4998. if (!wordlen || !strncmp(a->word, buffer, wordlen)) {
  4999. if (++which > a->n) {
  5000. return ast_strdup(buffer);
  5001. }
  5002. }
  5003. }
  5004. } else if (strstr(a->line, "descriptions ")) {
  5005. if ((!wordlen || !strncmp(a->word, "general", wordlen)) && (++which > a->n)) {
  5006. return ast_strdup("general");
  5007. }
  5008. if ((!wordlen || !strncmp(a->word, "ports", wordlen)) && (++which > a->n)) {
  5009. return ast_strdup("ports");
  5010. }
  5011. }
  5012. break;
  5013. }
  5014. return NULL;
  5015. }
  5016. static struct ast_cli_entry chan_misdn_clis[] = {
  5017. /* *INDENT-OFF* */
  5018. AST_CLI_DEFINE(handle_cli_misdn_port_block, "Block the given port"),
  5019. AST_CLI_DEFINE(handle_cli_misdn_port_down, "Try to deactivate the L1 on the given port"),
  5020. AST_CLI_DEFINE(handle_cli_misdn_port_unblock, "Unblock the given port"),
  5021. AST_CLI_DEFINE(handle_cli_misdn_port_up, "Try to establish L1 on the given port"),
  5022. AST_CLI_DEFINE(handle_cli_misdn_reload, "Reload internal mISDN config, read from the config file"),
  5023. AST_CLI_DEFINE(handle_cli_misdn_restart_pid, "Restart the given pid"),
  5024. AST_CLI_DEFINE(handle_cli_misdn_restart_port, "Restart the given port"),
  5025. AST_CLI_DEFINE(handle_cli_misdn_show_channel, "Show an internal mISDN channel"),
  5026. AST_CLI_DEFINE(handle_cli_misdn_show_channels, "Show the internal mISDN channel list"),
  5027. AST_CLI_DEFINE(handle_cli_misdn_show_config, "Show internal mISDN config, read from the config file"),
  5028. AST_CLI_DEFINE(handle_cli_misdn_show_port, "Show detailed information for given port"),
  5029. AST_CLI_DEFINE(handle_cli_misdn_show_ports_stats, "Show mISDNs channel's call statistics per port"),
  5030. AST_CLI_DEFINE(handle_cli_misdn_show_stacks, "Show internal mISDN stack_list"),
  5031. AST_CLI_DEFINE(handle_cli_misdn_send_facility, "Sends a Facility Message to the mISDN Channel"),
  5032. AST_CLI_DEFINE(handle_cli_misdn_send_digit, "Send DTMF digit to mISDN Channel"),
  5033. AST_CLI_DEFINE(handle_cli_misdn_send_display, "Send Text to mISDN Channel"),
  5034. AST_CLI_DEFINE(handle_cli_misdn_send_restart, "Send a restart for every bchannel on the given port"),
  5035. AST_CLI_DEFINE(handle_cli_misdn_set_crypt_debug, "Set CryptDebuglevel of chan_misdn, at the moment, level={1,2}"),
  5036. AST_CLI_DEFINE(handle_cli_misdn_set_debug, "Set Debuglevel of chan_misdn"),
  5037. AST_CLI_DEFINE(handle_cli_misdn_set_tics, "???"),
  5038. AST_CLI_DEFINE(handle_cli_misdn_toggle_echocancel, "Toggle EchoCancel on mISDN Channel"),
  5039. /* *INDENT-ON* */
  5040. };
  5041. /*! \brief Updates caller ID information from config */
  5042. static void update_config(struct chan_list *ch)
  5043. {
  5044. struct ast_channel *ast;
  5045. struct misdn_bchannel *bc;
  5046. int port;
  5047. int hdlc = 0;
  5048. int pres;
  5049. int screen;
  5050. if (!ch) {
  5051. ast_log(LOG_WARNING, "Cannot configure without chanlist\n");
  5052. return;
  5053. }
  5054. ast = ch->ast;
  5055. bc = ch->bc;
  5056. if (! ast || ! bc) {
  5057. ast_log(LOG_WARNING, "Cannot configure without ast || bc\n");
  5058. return;
  5059. }
  5060. port = bc->port;
  5061. chan_misdn_log(7, port, "update_config: Getting Config\n");
  5062. misdn_cfg_get(port, MISDN_CFG_HDLC, &hdlc, sizeof(int));
  5063. if (hdlc) {
  5064. switch (bc->capability) {
  5065. case INFO_CAPABILITY_DIGITAL_UNRESTRICTED:
  5066. case INFO_CAPABILITY_DIGITAL_RESTRICTED:
  5067. chan_misdn_log(1, bc->port, " --> CONF HDLC\n");
  5068. bc->hdlc = 1;
  5069. break;
  5070. }
  5071. }
  5072. misdn_cfg_get(port, MISDN_CFG_PRES, &pres, sizeof(pres));
  5073. misdn_cfg_get(port, MISDN_CFG_SCREEN, &screen, sizeof(screen));
  5074. chan_misdn_log(2, port, " --> pres: %d screen: %d\n", pres, screen);
  5075. if (pres < 0 || screen < 0) {
  5076. chan_misdn_log(2, port, " --> pres: %x\n", ast_channel_connected(ast)->id.number.presentation);
  5077. bc->caller.presentation = ast_to_misdn_pres(ast_channel_connected(ast)->id.number.presentation);
  5078. chan_misdn_log(2, port, " --> PRES: %s(%d)\n", misdn_to_str_pres(bc->caller.presentation), bc->caller.presentation);
  5079. bc->caller.screening = ast_to_misdn_screen(ast_channel_connected(ast)->id.number.presentation);
  5080. chan_misdn_log(2, port, " --> SCREEN: %s(%d)\n", misdn_to_str_screen(bc->caller.screening), bc->caller.screening);
  5081. } else {
  5082. bc->caller.screening = screen;
  5083. bc->caller.presentation = pres;
  5084. }
  5085. }
  5086. static void config_jitterbuffer(struct chan_list *ch)
  5087. {
  5088. struct misdn_bchannel *bc = ch->bc;
  5089. int len = ch->jb_len;
  5090. int threshold = ch->jb_upper_threshold;
  5091. chan_misdn_log(5, bc->port, "config_jb: Called\n");
  5092. if (!len) {
  5093. chan_misdn_log(1, bc->port, "config_jb: Deactivating Jitterbuffer\n");
  5094. bc->nojitter = 1;
  5095. } else {
  5096. if (len <= 100 || len > 8000) {
  5097. chan_misdn_log(0, bc->port, "config_jb: Jitterbuffer out of Bounds, setting to 1000\n");
  5098. len = 1000;
  5099. }
  5100. if (threshold > len) {
  5101. chan_misdn_log(0, bc->port, "config_jb: Jitterbuffer Threshold > Jitterbuffer setting to Jitterbuffer -1\n");
  5102. }
  5103. if (ch->jb) {
  5104. cb_log(0, bc->port, "config_jb: We've got a Jitterbuffer Already on this port.\n");
  5105. misdn_jb_destroy(ch->jb);
  5106. ch->jb = NULL;
  5107. }
  5108. ch->jb = misdn_jb_init(len, threshold);
  5109. if (!ch->jb) {
  5110. bc->nojitter = 1;
  5111. }
  5112. }
  5113. }
  5114. void debug_numtype(int port, int numtype, char *type)
  5115. {
  5116. switch (numtype) {
  5117. case NUMTYPE_UNKNOWN:
  5118. chan_misdn_log(2, port, " --> %s: Unknown\n", type);
  5119. break;
  5120. case NUMTYPE_INTERNATIONAL:
  5121. chan_misdn_log(2, port, " --> %s: International\n", type);
  5122. break;
  5123. case NUMTYPE_NATIONAL:
  5124. chan_misdn_log(2, port, " --> %s: National\n", type);
  5125. break;
  5126. case NUMTYPE_NETWORK_SPECIFIC:
  5127. chan_misdn_log(2, port, " --> %s: Network Specific\n", type);
  5128. break;
  5129. case NUMTYPE_SUBSCRIBER:
  5130. chan_misdn_log(2, port, " --> %s: Subscriber\n", type);
  5131. break;
  5132. case NUMTYPE_ABBREVIATED:
  5133. chan_misdn_log(2, port, " --> %s: Abbreviated\n", type);
  5134. break;
  5135. /* Maybe we should cut off the prefix if present ? */
  5136. default:
  5137. chan_misdn_log(0, port, " --> !!!! Wrong dialplan setting, please see the misdn.conf sample file\n ");
  5138. break;
  5139. }
  5140. }
  5141. #ifdef MISDN_1_2
  5142. static int update_pipeline_config(struct misdn_bchannel *bc)
  5143. {
  5144. int ec;
  5145. misdn_cfg_get(bc->port, MISDN_CFG_PIPELINE, bc->pipeline, sizeof(bc->pipeline));
  5146. if (*bc->pipeline) {
  5147. return 0;
  5148. }
  5149. misdn_cfg_get(bc->port, MISDN_CFG_ECHOCANCEL, &ec, sizeof(ec));
  5150. if (ec == 1) {
  5151. ast_copy_string(bc->pipeline, "mg2ec", sizeof(bc->pipeline));
  5152. } else if (ec > 1) {
  5153. snprintf(bc->pipeline, sizeof(bc->pipeline), "mg2ec(deftaps=%d)", ec);
  5154. }
  5155. return 0;
  5156. }
  5157. #else
  5158. static int update_ec_config(struct misdn_bchannel *bc)
  5159. {
  5160. int ec;
  5161. int port = bc->port;
  5162. misdn_cfg_get(port, MISDN_CFG_ECHOCANCEL, &ec, sizeof(ec));
  5163. if (ec == 1) {
  5164. bc->ec_enable = 1;
  5165. } else if (ec > 1) {
  5166. bc->ec_enable = 1;
  5167. bc->ec_deftaps = ec;
  5168. }
  5169. return 0;
  5170. }
  5171. #endif
  5172. static int read_config(struct chan_list *ch)
  5173. {
  5174. struct ast_channel *ast;
  5175. struct misdn_bchannel *bc;
  5176. int port;
  5177. int hdlc = 0;
  5178. char lang[BUFFERSIZE + 1];
  5179. char faxdetect[BUFFERSIZE + 1];
  5180. char buf[256];
  5181. char buf2[256];
  5182. ast_group_t pg;
  5183. ast_group_t cg;
  5184. struct ast_namedgroups *npg;
  5185. struct ast_namedgroups *ncg;
  5186. struct ast_str *tmp_str;
  5187. if (!ch) {
  5188. ast_log(LOG_WARNING, "Cannot configure without chanlist\n");
  5189. return -1;
  5190. }
  5191. ast = ch->ast;
  5192. bc = ch->bc;
  5193. if (! ast || ! bc) {
  5194. ast_log(LOG_WARNING, "Cannot configure without ast || bc\n");
  5195. return -1;
  5196. }
  5197. port = bc->port;
  5198. chan_misdn_log(1, port, "read_config: Getting Config\n");
  5199. misdn_cfg_get(port, MISDN_CFG_LANGUAGE, lang, sizeof(lang));
  5200. ast_channel_lock(ast);
  5201. ast_channel_language_set(ast, lang);
  5202. ast_channel_unlock(ast);
  5203. misdn_cfg_get(port, MISDN_CFG_MUSICCLASS, ch->mohinterpret, sizeof(ch->mohinterpret));
  5204. misdn_cfg_get(port, MISDN_CFG_TXGAIN, &bc->txgain, sizeof(bc->txgain));
  5205. misdn_cfg_get(port, MISDN_CFG_RXGAIN, &bc->rxgain, sizeof(bc->rxgain));
  5206. misdn_cfg_get(port, MISDN_CFG_INCOMING_EARLY_AUDIO, &ch->incoming_early_audio, sizeof(ch->incoming_early_audio));
  5207. misdn_cfg_get(port, MISDN_CFG_SENDDTMF, &bc->send_dtmf, sizeof(bc->send_dtmf));
  5208. misdn_cfg_get(port, MISDN_CFG_ASTDTMF, &ch->ast_dsp, sizeof(int));
  5209. if (ch->ast_dsp) {
  5210. ch->ignore_dtmf = 1;
  5211. }
  5212. misdn_cfg_get(port, MISDN_CFG_NEED_MORE_INFOS, &bc->need_more_infos, sizeof(bc->need_more_infos));
  5213. misdn_cfg_get(port, MISDN_CFG_NTTIMEOUT, &ch->nttimeout, sizeof(ch->nttimeout));
  5214. misdn_cfg_get(port, MISDN_CFG_NOAUTORESPOND_ON_SETUP, &ch->noautorespond_on_setup, sizeof(ch->noautorespond_on_setup));
  5215. misdn_cfg_get(port, MISDN_CFG_FAR_ALERTING, &ch->far_alerting, sizeof(ch->far_alerting));
  5216. misdn_cfg_get(port, MISDN_CFG_ALLOWED_BEARERS, &ch->allowed_bearers, sizeof(ch->allowed_bearers));
  5217. misdn_cfg_get(port, MISDN_CFG_FAXDETECT, faxdetect, sizeof(faxdetect));
  5218. misdn_cfg_get(port, MISDN_CFG_HDLC, &hdlc, sizeof(hdlc));
  5219. if (hdlc) {
  5220. switch (bc->capability) {
  5221. case INFO_CAPABILITY_DIGITAL_UNRESTRICTED:
  5222. case INFO_CAPABILITY_DIGITAL_RESTRICTED:
  5223. chan_misdn_log(1, bc->port, " --> CONF HDLC\n");
  5224. bc->hdlc = 1;
  5225. break;
  5226. }
  5227. }
  5228. /*Initialize new Jitterbuffer*/
  5229. misdn_cfg_get(port, MISDN_CFG_JITTERBUFFER, &ch->jb_len, sizeof(ch->jb_len));
  5230. misdn_cfg_get(port, MISDN_CFG_JITTERBUFFER_UPPER_THRESHOLD, &ch->jb_upper_threshold, sizeof(ch->jb_upper_threshold));
  5231. config_jitterbuffer(ch);
  5232. misdn_cfg_get(bc->port, MISDN_CFG_CONTEXT, ch->context, sizeof(ch->context));
  5233. ast_channel_lock(ast);
  5234. ast_channel_context_set(ast, ch->context);
  5235. ast_channel_unlock(ast);
  5236. #ifdef MISDN_1_2
  5237. update_pipeline_config(bc);
  5238. #else
  5239. update_ec_config(bc);
  5240. #endif
  5241. misdn_cfg_get(bc->port, MISDN_CFG_EARLY_BCONNECT, &bc->early_bconnect, sizeof(bc->early_bconnect));
  5242. misdn_cfg_get(port, MISDN_CFG_DISPLAY_CONNECTED, &bc->display_connected, sizeof(bc->display_connected));
  5243. misdn_cfg_get(port, MISDN_CFG_DISPLAY_SETUP, &bc->display_setup, sizeof(bc->display_setup));
  5244. misdn_cfg_get(port, MISDN_CFG_OUTGOING_COLP, &bc->outgoing_colp, sizeof(bc->outgoing_colp));
  5245. misdn_cfg_get(port, MISDN_CFG_PICKUPGROUP, &pg, sizeof(pg));
  5246. misdn_cfg_get(port, MISDN_CFG_CALLGROUP, &cg, sizeof(cg));
  5247. chan_misdn_log(5, port, " --> * CallGrp:%s PickupGrp:%s\n", ast_print_group(buf, sizeof(buf), cg), ast_print_group(buf2, sizeof(buf2), pg));
  5248. ast_channel_lock(ast);
  5249. ast_channel_pickupgroup_set(ast, pg);
  5250. ast_channel_callgroup_set(ast, cg);
  5251. ast_channel_unlock(ast);
  5252. misdn_cfg_get(port, MISDN_CFG_NAMEDPICKUPGROUP, &npg, sizeof(npg));
  5253. misdn_cfg_get(port, MISDN_CFG_NAMEDCALLGROUP, &ncg, sizeof(ncg));
  5254. tmp_str = ast_str_create(1024);
  5255. if (tmp_str) {
  5256. chan_misdn_log(5, port, " --> * NamedCallGrp:%s\n", ast_print_namedgroups(&tmp_str, ncg));
  5257. ast_str_reset(tmp_str);
  5258. chan_misdn_log(5, port, " --> * NamedPickupGrp:%s\n", ast_print_namedgroups(&tmp_str, npg));
  5259. ast_free(tmp_str);
  5260. }
  5261. ast_channel_lock(ast);
  5262. ast_channel_named_pickupgroups_set(ast, npg);
  5263. ast_channel_named_callgroups_set(ast, ncg);
  5264. ast_channel_unlock(ast);
  5265. if (ch->originator == ORG_AST) {
  5266. char callerid[BUFFERSIZE + 1];
  5267. /* ORIGINATOR Asterisk (outgoing call) */
  5268. misdn_cfg_get(port, MISDN_CFG_TE_CHOOSE_CHANNEL, &(bc->te_choose_channel), sizeof(bc->te_choose_channel));
  5269. if (strstr(faxdetect, "outgoing") || strstr(faxdetect, "both")) {
  5270. ch->faxdetect = strstr(faxdetect, "nojump") ? 2 : 1;
  5271. }
  5272. misdn_cfg_get(port, MISDN_CFG_CALLERID, callerid, sizeof(callerid));
  5273. if (!ast_strlen_zero(callerid)) {
  5274. char *cid_name = NULL;
  5275. char *cid_num = NULL;
  5276. ast_callerid_parse(callerid, &cid_name, &cid_num);
  5277. if (cid_name) {
  5278. ast_copy_string(bc->caller.name, cid_name, sizeof(bc->caller.name));
  5279. } else {
  5280. bc->caller.name[0] = '\0';
  5281. }
  5282. if (cid_num) {
  5283. ast_copy_string(bc->caller.number, cid_num, sizeof(bc->caller.number));
  5284. } else {
  5285. bc->caller.number[0] = '\0';
  5286. }
  5287. chan_misdn_log(1, port, " --> * Setting caller to \"%s\" <%s>\n", bc->caller.name, bc->caller.number);
  5288. }
  5289. misdn_cfg_get(port, MISDN_CFG_DIALPLAN, &bc->dialed.number_type, sizeof(bc->dialed.number_type));
  5290. bc->dialed.number_plan = NUMPLAN_ISDN;
  5291. debug_numtype(port, bc->dialed.number_type, "TON");
  5292. ch->overlap_dial = 0;
  5293. } else {
  5294. /* ORIGINATOR MISDN (incoming call) */
  5295. if (strstr(faxdetect, "incoming") || strstr(faxdetect, "both")) {
  5296. ch->faxdetect = (strstr(faxdetect, "nojump")) ? 2 : 1;
  5297. }
  5298. /* Add configured prefix to caller.number */
  5299. misdn_add_number_prefix(bc->port, bc->caller.number_type, bc->caller.number, sizeof(bc->caller.number));
  5300. if (ast_strlen_zero(bc->dialed.number) && !ast_strlen_zero(bc->keypad)) {
  5301. ast_copy_string(bc->dialed.number, bc->keypad, sizeof(bc->dialed.number));
  5302. }
  5303. /* Add configured prefix to dialed.number */
  5304. misdn_add_number_prefix(bc->port, bc->dialed.number_type, bc->dialed.number, sizeof(bc->dialed.number));
  5305. ast_channel_lock(ast);
  5306. ast_channel_exten_set(ast, bc->dialed.number);
  5307. ast_channel_unlock(ast);
  5308. misdn_cfg_get(bc->port, MISDN_CFG_OVERLAP_DIAL, &ch->overlap_dial, sizeof(ch->overlap_dial));
  5309. ast_mutex_init(&ch->overlap_tv_lock);
  5310. } /* ORIG MISDN END */
  5311. misdn_cfg_get(port, MISDN_CFG_INCOMING_CALLERID_TAG, bc->incoming_cid_tag, sizeof(bc->incoming_cid_tag));
  5312. if (!ast_strlen_zero(bc->incoming_cid_tag)) {
  5313. chan_misdn_log(1, port, " --> * Setting incoming caller id tag to \"%s\"\n", bc->incoming_cid_tag);
  5314. }
  5315. ch->overlap_dial_task = -1;
  5316. if (ch->faxdetect || ch->ast_dsp) {
  5317. misdn_cfg_get(port, MISDN_CFG_FAXDETECT_TIMEOUT, &ch->faxdetect_timeout, sizeof(ch->faxdetect_timeout));
  5318. if (!ch->dsp) {
  5319. ch->dsp = ast_dsp_new();
  5320. }
  5321. if (ch->dsp) {
  5322. ast_dsp_set_features(ch->dsp, DSP_FEATURE_DIGIT_DETECT | (ch->faxdetect ? DSP_FEATURE_FAX_DETECT : 0));
  5323. }
  5324. }
  5325. /* AOCD initialization */
  5326. bc->AOCDtype = Fac_None;
  5327. return 0;
  5328. }
  5329. /*!
  5330. * \internal
  5331. * \brief Send a connected line update to the other channel
  5332. *
  5333. * \param ast Current Asterisk channel
  5334. * \param id Party id information to send to the other side
  5335. * \param source Why are we sending this update
  5336. * \param cid_tag User tag to apply to the party id.
  5337. *
  5338. * \return Nothing
  5339. */
  5340. static void misdn_queue_connected_line_update(struct ast_channel *ast, const struct misdn_party_id *id, enum AST_CONNECTED_LINE_UPDATE_SOURCE source, char *cid_tag)
  5341. {
  5342. struct ast_party_connected_line connected;
  5343. struct ast_set_party_connected_line update_connected;
  5344. ast_party_connected_line_init(&connected);
  5345. memset(&update_connected, 0, sizeof(update_connected));
  5346. update_connected.id.number = 1;
  5347. connected.id.number.valid = 1;
  5348. connected.id.number.str = (char *) id->number;
  5349. connected.id.number.plan = misdn_to_ast_ton(id->number_type)
  5350. | misdn_to_ast_plan(id->number_plan);
  5351. connected.id.number.presentation = misdn_to_ast_pres(id->presentation)
  5352. | misdn_to_ast_screen(id->screening);
  5353. /*
  5354. * Make sure that any earlier private connected id
  5355. * representation at the remote end is invalidated
  5356. */
  5357. ast_set_party_id_all(&update_connected.priv);
  5358. connected.id.tag = cid_tag;
  5359. connected.source = source;
  5360. ast_channel_queue_connected_line_update(ast, &connected, &update_connected);
  5361. }
  5362. /*!
  5363. * \internal
  5364. * \brief Update the caller id party on this channel.
  5365. *
  5366. * \param ast Current Asterisk channel
  5367. * \param id Remote party id information to update.
  5368. * \param cid_tag User tag to apply to the party id.
  5369. *
  5370. * \return Nothing
  5371. */
  5372. static void misdn_update_caller_id(struct ast_channel *ast, const struct misdn_party_id *id, char *cid_tag)
  5373. {
  5374. struct ast_party_caller caller;
  5375. struct ast_set_party_caller update_caller;
  5376. memset(&update_caller, 0, sizeof(update_caller));
  5377. update_caller.id.number = 1;
  5378. update_caller.ani.number = 1;
  5379. ast_channel_lock(ast);
  5380. ast_party_caller_set_init(&caller, ast_channel_caller(ast));
  5381. caller.id.number.valid = 1;
  5382. caller.id.number.str = (char *) id->number;
  5383. caller.id.number.plan = misdn_to_ast_ton(id->number_type)
  5384. | misdn_to_ast_plan(id->number_plan);
  5385. caller.id.number.presentation = misdn_to_ast_pres(id->presentation)
  5386. | misdn_to_ast_screen(id->screening);
  5387. caller.ani.number = caller.id.number;
  5388. caller.id.tag = cid_tag;
  5389. caller.ani.tag = cid_tag;
  5390. ast_channel_set_caller_event(ast, &caller, &update_caller);
  5391. ast_channel_unlock(ast);
  5392. }
  5393. /*!
  5394. * \internal
  5395. * \brief Update the remote party id information.
  5396. *
  5397. * \param ast Current Asterisk channel
  5398. * \param id Remote party id information to update.
  5399. * \param source Why are we sending this update
  5400. * \param cid_tag User tag to apply to the party id.
  5401. *
  5402. * \return Nothing
  5403. */
  5404. static void misdn_update_remote_party(struct ast_channel *ast, const struct misdn_party_id *id, enum AST_CONNECTED_LINE_UPDATE_SOURCE source, char *cid_tag)
  5405. {
  5406. misdn_update_caller_id(ast, id, cid_tag);
  5407. misdn_queue_connected_line_update(ast, id, source, cid_tag);
  5408. }
  5409. /*!
  5410. * \internal
  5411. * \brief Get the connected line information out of the Asterisk channel.
  5412. *
  5413. * \param ast Current Asterisk channel
  5414. * \param bc Associated B channel
  5415. * \param originator Who originally created this channel. ORG_AST or ORG_MISDN
  5416. *
  5417. * \return Nothing
  5418. */
  5419. static void misdn_get_connected_line(struct ast_channel *ast, struct misdn_bchannel *bc, int originator)
  5420. {
  5421. int number_type;
  5422. struct ast_party_id connected_id = ast_channel_connected_effective_id(ast);
  5423. if (originator == ORG_MISDN) {
  5424. /* ORIGINATOR MISDN (incoming call) */
  5425. ast_copy_string(bc->connected.name,
  5426. S_COR(connected_id.name.valid, connected_id.name.str, ""),
  5427. sizeof(bc->connected.name));
  5428. if (connected_id.number.valid) {
  5429. ast_copy_string(bc->connected.number, S_OR(connected_id.number.str, ""),
  5430. sizeof(bc->connected.number));
  5431. bc->connected.presentation = ast_to_misdn_pres(connected_id.number.presentation);
  5432. bc->connected.screening = ast_to_misdn_screen(connected_id.number.presentation);
  5433. bc->connected.number_type = ast_to_misdn_ton(connected_id.number.plan);
  5434. bc->connected.number_plan = ast_to_misdn_plan(connected_id.number.plan);
  5435. } else {
  5436. bc->connected.number[0] = '\0';
  5437. bc->connected.presentation = 0;/* Allowed */
  5438. bc->connected.screening = 0;/* Unscreened */
  5439. bc->connected.number_type = NUMTYPE_UNKNOWN;
  5440. bc->connected.number_plan = NUMPLAN_UNKNOWN;
  5441. }
  5442. misdn_cfg_get(bc->port, MISDN_CFG_CPNDIALPLAN, &number_type, sizeof(number_type));
  5443. if (0 <= number_type) {
  5444. /* Force us to send in CONNECT message */
  5445. bc->connected.number_type = number_type;
  5446. bc->connected.number_plan = NUMPLAN_ISDN;
  5447. }
  5448. debug_numtype(bc->port, bc->connected.number_type, "CTON");
  5449. } else {
  5450. /* ORIGINATOR Asterisk (outgoing call) */
  5451. ast_copy_string(bc->caller.name,
  5452. S_COR(connected_id.name.valid, connected_id.name.str, ""),
  5453. sizeof(bc->caller.name));
  5454. if (connected_id.number.valid) {
  5455. ast_copy_string(bc->caller.number, S_OR(connected_id.number.str, ""),
  5456. sizeof(bc->caller.number));
  5457. bc->caller.presentation = ast_to_misdn_pres(connected_id.number.presentation);
  5458. bc->caller.screening = ast_to_misdn_screen(connected_id.number.presentation);
  5459. bc->caller.number_type = ast_to_misdn_ton(connected_id.number.plan);
  5460. bc->caller.number_plan = ast_to_misdn_plan(connected_id.number.plan);
  5461. } else {
  5462. bc->caller.number[0] = '\0';
  5463. bc->caller.presentation = 0;/* Allowed */
  5464. bc->caller.screening = 0;/* Unscreened */
  5465. bc->caller.number_type = NUMTYPE_UNKNOWN;
  5466. bc->caller.number_plan = NUMPLAN_UNKNOWN;
  5467. }
  5468. misdn_cfg_get(bc->port, MISDN_CFG_LOCALDIALPLAN, &number_type, sizeof(number_type));
  5469. if (0 <= number_type) {
  5470. /* Force us to send in SETUP message */
  5471. bc->caller.number_type = number_type;
  5472. bc->caller.number_plan = NUMPLAN_ISDN;
  5473. }
  5474. debug_numtype(bc->port, bc->caller.number_type, "LTON");
  5475. }
  5476. }
  5477. /*!
  5478. * \internal
  5479. * \brief Notify peer that the connected line has changed.
  5480. *
  5481. * \param ast Current Asterisk channel
  5482. * \param bc Associated B channel
  5483. * \param originator Who originally created this channel. ORG_AST or ORG_MISDN
  5484. *
  5485. * \return Nothing
  5486. */
  5487. static void misdn_update_connected_line(struct ast_channel *ast, struct misdn_bchannel *bc, int originator)
  5488. {
  5489. struct chan_list *ch;
  5490. misdn_get_connected_line(ast, bc, originator);
  5491. if (originator == ORG_MISDN) {
  5492. bc->redirecting.to = bc->connected;
  5493. } else {
  5494. bc->redirecting.to = bc->caller;
  5495. }
  5496. switch (bc->outgoing_colp) {
  5497. case 1:/* restricted */
  5498. bc->redirecting.to.presentation = 1;/* restricted */
  5499. break;
  5500. case 2:/* blocked */
  5501. /* Don't tell the remote party that the call was transferred. */
  5502. return;
  5503. default:
  5504. break;
  5505. }
  5506. ch = MISDN_ASTERISK_TECH_PVT(ast);
  5507. if (ch->state == MISDN_CONNECTED
  5508. || originator != ORG_MISDN) {
  5509. int is_ptmp;
  5510. is_ptmp = !misdn_lib_is_ptp(bc->port);
  5511. if (is_ptmp) {
  5512. /*
  5513. * We should not send these messages to the network if we are
  5514. * the CPE side since phones do not transfer calls within
  5515. * themselves. Well... If you consider handing the handset to
  5516. * someone else a transfer then how is the network to know?
  5517. */
  5518. if (!misdn_lib_port_is_nt(bc->port)) {
  5519. return;
  5520. }
  5521. if (ch->state != MISDN_CONNECTED) {
  5522. /* Send NOTIFY(Nie(transfer-active), RDNie(redirecting.to data)) */
  5523. bc->redirecting.to_changed = 1;
  5524. bc->notify_description_code = mISDN_NOTIFY_CODE_CALL_TRANSFER_ACTIVE;
  5525. misdn_lib_send_event(bc, EVENT_NOTIFY);
  5526. #if defined(AST_MISDN_ENHANCEMENTS)
  5527. } else {
  5528. /* Send FACILITY(Fie(RequestSubaddress), Nie(transfer-active), RDNie(redirecting.to data)) */
  5529. bc->redirecting.to_changed = 1;
  5530. bc->notify_description_code = mISDN_NOTIFY_CODE_CALL_TRANSFER_ACTIVE;
  5531. bc->fac_out.Function = Fac_RequestSubaddress;
  5532. bc->fac_out.u.RequestSubaddress.InvokeID = ++misdn_invoke_id;
  5533. /* Send message */
  5534. print_facility(&bc->fac_out, bc);
  5535. misdn_lib_send_event(bc, EVENT_FACILITY);
  5536. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  5537. }
  5538. #if defined(AST_MISDN_ENHANCEMENTS)
  5539. } else {
  5540. /* Send FACILITY(Fie(EctInform(transfer-active, redirecting.to data))) */
  5541. bc->fac_out.Function = Fac_EctInform;
  5542. bc->fac_out.u.EctInform.InvokeID = ++misdn_invoke_id;
  5543. bc->fac_out.u.EctInform.Status = 1;/* active */
  5544. bc->fac_out.u.EctInform.RedirectionPresent = 1;/* Must be present when status is active */
  5545. misdn_PresentedNumberUnscreened_fill(&bc->fac_out.u.EctInform.Redirection,
  5546. &bc->redirecting.to);
  5547. /* Send message */
  5548. print_facility(&bc->fac_out, bc);
  5549. misdn_lib_send_event(bc, EVENT_FACILITY);
  5550. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  5551. }
  5552. }
  5553. }
  5554. /*!
  5555. * \internal
  5556. * \brief Copy the redirecting information out of the Asterisk channel
  5557. *
  5558. * \param bc Associated B channel
  5559. * \param ast Current Asterisk channel
  5560. *
  5561. * \return Nothing
  5562. */
  5563. static void misdn_copy_redirecting_from_ast(struct misdn_bchannel *bc, struct ast_channel *ast)
  5564. {
  5565. struct ast_party_id from_id = ast_channel_redirecting_effective_from(ast);
  5566. struct ast_party_id to_id = ast_channel_redirecting_effective_to(ast);
  5567. ast_copy_string(bc->redirecting.from.name,
  5568. S_COR(from_id.name.valid, from_id.name.str, ""),
  5569. sizeof(bc->redirecting.from.name));
  5570. if (from_id.number.valid) {
  5571. ast_copy_string(bc->redirecting.from.number, S_OR(from_id.number.str, ""),
  5572. sizeof(bc->redirecting.from.number));
  5573. bc->redirecting.from.presentation = ast_to_misdn_pres(from_id.number.presentation);
  5574. bc->redirecting.from.screening = ast_to_misdn_screen(from_id.number.presentation);
  5575. bc->redirecting.from.number_type = ast_to_misdn_ton(from_id.number.plan);
  5576. bc->redirecting.from.number_plan = ast_to_misdn_plan(from_id.number.plan);
  5577. } else {
  5578. bc->redirecting.from.number[0] = '\0';
  5579. bc->redirecting.from.presentation = 0;/* Allowed */
  5580. bc->redirecting.from.screening = 0;/* Unscreened */
  5581. bc->redirecting.from.number_type = NUMTYPE_UNKNOWN;
  5582. bc->redirecting.from.number_plan = NUMPLAN_UNKNOWN;
  5583. }
  5584. ast_copy_string(bc->redirecting.to.name,
  5585. S_COR(to_id.name.valid, to_id.name.str, ""),
  5586. sizeof(bc->redirecting.to.name));
  5587. if (to_id.number.valid) {
  5588. ast_copy_string(bc->redirecting.to.number, S_OR(to_id.number.str, ""),
  5589. sizeof(bc->redirecting.to.number));
  5590. bc->redirecting.to.presentation = ast_to_misdn_pres(to_id.number.presentation);
  5591. bc->redirecting.to.screening = ast_to_misdn_screen(to_id.number.presentation);
  5592. bc->redirecting.to.number_type = ast_to_misdn_ton(to_id.number.plan);
  5593. bc->redirecting.to.number_plan = ast_to_misdn_plan(to_id.number.plan);
  5594. } else {
  5595. bc->redirecting.to.number[0] = '\0';
  5596. bc->redirecting.to.presentation = 0;/* Allowed */
  5597. bc->redirecting.to.screening = 0;/* Unscreened */
  5598. bc->redirecting.to.number_type = NUMTYPE_UNKNOWN;
  5599. bc->redirecting.to.number_plan = NUMPLAN_UNKNOWN;
  5600. }
  5601. bc->redirecting.reason = ast_to_misdn_reason(ast_channel_redirecting(ast)->reason.code);
  5602. bc->redirecting.count = ast_channel_redirecting(ast)->count;
  5603. }
  5604. /*!
  5605. * \internal
  5606. * \brief Copy the redirecting info into the Asterisk channel
  5607. *
  5608. * \param ast Current Asterisk channel
  5609. * \param redirect Associated B channel redirecting info
  5610. * \param tag Caller ID tag to set in the redirecting party fields
  5611. *
  5612. * \return Nothing
  5613. */
  5614. static void misdn_copy_redirecting_to_ast(struct ast_channel *ast, const struct misdn_party_redirecting *redirect, char *tag)
  5615. {
  5616. struct ast_party_redirecting redirecting;
  5617. struct ast_set_party_redirecting update_redirecting;
  5618. ast_party_redirecting_set_init(&redirecting, ast_channel_redirecting(ast));
  5619. memset(&update_redirecting, 0, sizeof(update_redirecting));
  5620. update_redirecting.from.number = 1;
  5621. redirecting.from.number.valid = 1;
  5622. redirecting.from.number.str = (char *) redirect->from.number;
  5623. redirecting.from.number.plan =
  5624. misdn_to_ast_ton(redirect->from.number_type)
  5625. | misdn_to_ast_plan(redirect->from.number_plan);
  5626. redirecting.from.number.presentation =
  5627. misdn_to_ast_pres(redirect->from.presentation)
  5628. | misdn_to_ast_screen(redirect->from.screening);
  5629. redirecting.from.tag = tag;
  5630. update_redirecting.to.number = 1;
  5631. redirecting.to.number.valid = 1;
  5632. redirecting.to.number.str = (char *) redirect->to.number;
  5633. redirecting.to.number.plan =
  5634. misdn_to_ast_ton(redirect->to.number_type)
  5635. | misdn_to_ast_plan(redirect->to.number_plan);
  5636. redirecting.to.number.presentation =
  5637. misdn_to_ast_pres(redirect->to.presentation)
  5638. | misdn_to_ast_screen(redirect->to.screening);
  5639. redirecting.to.tag = tag;
  5640. redirecting.reason.code = misdn_to_ast_reason(redirect->reason);
  5641. redirecting.count = redirect->count;
  5642. ast_channel_set_redirecting(ast, &redirecting, &update_redirecting);
  5643. }
  5644. /*!
  5645. * \internal
  5646. * \brief Notify peer that the redirecting information has changed.
  5647. *
  5648. * \param ast Current Asterisk channel
  5649. * \param bc Associated B channel
  5650. * \param originator Who originally created this channel. ORG_AST or ORG_MISDN
  5651. *
  5652. * \return Nothing
  5653. */
  5654. static void misdn_update_redirecting(struct ast_channel *ast, struct misdn_bchannel *bc, int originator)
  5655. {
  5656. int is_ptmp;
  5657. misdn_copy_redirecting_from_ast(bc, ast);
  5658. switch (bc->outgoing_colp) {
  5659. case 1:/* restricted */
  5660. bc->redirecting.to.presentation = 1;/* restricted */
  5661. break;
  5662. case 2:/* blocked */
  5663. /* Don't tell the remote party that the call was redirected. */
  5664. return;
  5665. default:
  5666. break;
  5667. }
  5668. if (originator != ORG_MISDN) {
  5669. return;
  5670. }
  5671. is_ptmp = !misdn_lib_is_ptp(bc->port);
  5672. if (is_ptmp) {
  5673. /*
  5674. * We should not send these messages to the network if we are
  5675. * the CPE side since phones do not redirect calls within
  5676. * themselves. Well... If you consider someone else picking up
  5677. * the handset a redirection then how is the network to know?
  5678. */
  5679. if (!misdn_lib_port_is_nt(bc->port)) {
  5680. return;
  5681. }
  5682. /* Send NOTIFY(call-is-diverting, redirecting.to data) */
  5683. bc->redirecting.to_changed = 1;
  5684. bc->notify_description_code = mISDN_NOTIFY_CODE_CALL_IS_DIVERTING;
  5685. misdn_lib_send_event(bc, EVENT_NOTIFY);
  5686. #if defined(AST_MISDN_ENHANCEMENTS)
  5687. } else {
  5688. int match; /* TRUE if the dialed number matches the redirecting to number */
  5689. match = (strcmp(ast_channel_exten(ast), bc->redirecting.to.number) == 0) ? 1 : 0;
  5690. if (!bc->div_leg_3_tx_pending
  5691. || !match) {
  5692. /* Send DivertingLegInformation1 */
  5693. bc->fac_out.Function = Fac_DivertingLegInformation1;
  5694. bc->fac_out.u.DivertingLegInformation1.InvokeID = ++misdn_invoke_id;
  5695. bc->fac_out.u.DivertingLegInformation1.DiversionReason =
  5696. misdn_to_diversion_reason(bc->redirecting.reason);
  5697. bc->fac_out.u.DivertingLegInformation1.SubscriptionOption = 2;/* notificationWithDivertedToNr */
  5698. bc->fac_out.u.DivertingLegInformation1.DivertedToPresent = 1;
  5699. misdn_PresentedNumberUnscreened_fill(&bc->fac_out.u.DivertingLegInformation1.DivertedTo, &bc->redirecting.to);
  5700. print_facility(&bc->fac_out, bc);
  5701. misdn_lib_send_event(bc, EVENT_FACILITY);
  5702. }
  5703. bc->div_leg_3_tx_pending = 0;
  5704. /* Send DivertingLegInformation3 */
  5705. bc->fac_out.Function = Fac_DivertingLegInformation3;
  5706. bc->fac_out.u.DivertingLegInformation3.InvokeID = ++misdn_invoke_id;
  5707. bc->fac_out.u.DivertingLegInformation3.PresentationAllowedIndicator =
  5708. bc->redirecting.to.presentation == 0 ? 1 : 0;
  5709. print_facility(&bc->fac_out, bc);
  5710. misdn_lib_send_event(bc, EVENT_FACILITY);
  5711. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  5712. }
  5713. }
  5714. /*****************************/
  5715. /*** AST Indications Start ***/
  5716. /*****************************/
  5717. static int misdn_call(struct ast_channel *ast, const char *dest, int timeout)
  5718. {
  5719. int port = 0;
  5720. int r;
  5721. int exceed;
  5722. int number_type;
  5723. struct chan_list *ch;
  5724. struct misdn_bchannel *newbc;
  5725. char *dest_cp;
  5726. int append_msn = 0;
  5727. AST_DECLARE_APP_ARGS(args,
  5728. AST_APP_ARG(intf); /* The interface token is discarded. */
  5729. AST_APP_ARG(ext); /* extension token */
  5730. AST_APP_ARG(opts); /* options token */
  5731. );
  5732. if (!ast) {
  5733. ast_log(LOG_WARNING, " --> ! misdn_call called on ast_channel *ast where ast == NULL\n");
  5734. return -1;
  5735. }
  5736. if (((ast_channel_state(ast) != AST_STATE_DOWN) && (ast_channel_state(ast) != AST_STATE_RESERVED)) || !dest) {
  5737. ast_log(LOG_WARNING, " --> ! misdn_call called on %s, neither down nor reserved (or dest==NULL)\n", ast_channel_name(ast));
  5738. ast_channel_hangupcause_set(ast, AST_CAUSE_NORMAL_TEMPORARY_FAILURE);
  5739. ast_setstate(ast, AST_STATE_DOWN);
  5740. return -1;
  5741. }
  5742. ch = MISDN_ASTERISK_TECH_PVT(ast);
  5743. if (!ch) {
  5744. ast_log(LOG_WARNING, " --> ! misdn_call called on %s, chan_list *ch==NULL\n", ast_channel_name(ast));
  5745. ast_channel_hangupcause_set(ast, AST_CAUSE_NORMAL_TEMPORARY_FAILURE);
  5746. ast_setstate(ast, AST_STATE_DOWN);
  5747. return -1;
  5748. }
  5749. newbc = ch->bc;
  5750. if (!newbc) {
  5751. ast_log(LOG_WARNING, " --> ! misdn_call called on %s, newbc==NULL\n", ast_channel_name(ast));
  5752. ast_channel_hangupcause_set(ast, AST_CAUSE_NORMAL_TEMPORARY_FAILURE);
  5753. ast_setstate(ast, AST_STATE_DOWN);
  5754. return -1;
  5755. }
  5756. port = newbc->port;
  5757. #if defined(AST_MISDN_ENHANCEMENTS)
  5758. if ((ch->peer = misdn_cc_caller_get(ast))) {
  5759. chan_misdn_log(3, port, " --> Found CC caller data, peer:%s\n",
  5760. ch->peer->chan ? "available" : "NULL");
  5761. }
  5762. if (ch->record_id != -1) {
  5763. struct misdn_cc_record *cc_record;
  5764. /* This is a call completion retry call */
  5765. AST_LIST_LOCK(&misdn_cc_records_db);
  5766. cc_record = misdn_cc_find_by_id(ch->record_id);
  5767. if (!cc_record) {
  5768. AST_LIST_UNLOCK(&misdn_cc_records_db);
  5769. ast_log(LOG_WARNING, " --> ! misdn_call called on %s, cc_record==NULL\n", ast_channel_name(ast));
  5770. ast_channel_hangupcause_set(ast, AST_CAUSE_NORMAL_TEMPORARY_FAILURE);
  5771. ast_setstate(ast, AST_STATE_DOWN);
  5772. return -1;
  5773. }
  5774. /* Setup calling parameters to retry the call. */
  5775. newbc->dialed = cc_record->redial.dialed;
  5776. newbc->caller = cc_record->redial.caller;
  5777. memset(&newbc->redirecting, 0, sizeof(newbc->redirecting));
  5778. newbc->capability = cc_record->redial.capability;
  5779. newbc->hdlc = cc_record->redial.hdlc;
  5780. newbc->sending_complete = 1;
  5781. if (cc_record->ptp) {
  5782. newbc->fac_out.Function = Fac_CCBS_T_Call;
  5783. newbc->fac_out.u.CCBS_T_Call.InvokeID = ++misdn_invoke_id;
  5784. } else {
  5785. newbc->fac_out.Function = Fac_CCBSCall;
  5786. newbc->fac_out.u.CCBSCall.InvokeID = ++misdn_invoke_id;
  5787. newbc->fac_out.u.CCBSCall.CCBSReference = cc_record->mode.ptmp.reference_id;
  5788. }
  5789. AST_LIST_UNLOCK(&misdn_cc_records_db);
  5790. ast_channel_exten_set(ast, newbc->dialed.number);
  5791. chan_misdn_log(1, port, "* Call completion to: %s\n", newbc->dialed.number);
  5792. chan_misdn_log(2, port, " --> * tech:%s context:%s\n", ast_channel_name(ast), ast_channel_context(ast));
  5793. } else
  5794. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  5795. {
  5796. struct ast_party_id connected_id = ast_channel_connected_effective_id(ast);
  5797. /*
  5798. * dest is ---v
  5799. * Dial(mISDN/g:group_name[/extension[/options]])
  5800. * Dial(mISDN/port[:preselected_channel][/extension[/options]])
  5801. *
  5802. * The dial extension could be empty if you are using MISDN_KEYPAD
  5803. * to control ISDN provider features.
  5804. */
  5805. dest_cp = ast_strdupa(dest);
  5806. AST_NONSTANDARD_APP_ARGS(args, dest_cp, '/');
  5807. if (!args.ext) {
  5808. args.ext = "";
  5809. }
  5810. chan_misdn_log(1, port, "* CALL: %s\n", dest);
  5811. chan_misdn_log(2, port, " --> * dialed:%s tech:%s context:%s\n", args.ext, ast_channel_name(ast), ast_channel_context(ast));
  5812. ast_channel_exten_set(ast, args.ext);
  5813. ast_copy_string(newbc->dialed.number, args.ext, sizeof(newbc->dialed.number));
  5814. if (ast_strlen_zero(newbc->caller.name)
  5815. && connected_id.name.valid
  5816. && !ast_strlen_zero(connected_id.name.str)) {
  5817. ast_copy_string(newbc->caller.name, connected_id.name.str, sizeof(newbc->caller.name));
  5818. chan_misdn_log(3, port, " --> * set caller:\"%s\" <%s>\n", newbc->caller.name, newbc->caller.number);
  5819. }
  5820. if (ast_strlen_zero(newbc->caller.number)
  5821. && connected_id.number.valid
  5822. && !ast_strlen_zero(connected_id.number.str)) {
  5823. ast_copy_string(newbc->caller.number, connected_id.number.str, sizeof(newbc->caller.number));
  5824. chan_misdn_log(3, port, " --> * set caller:\"%s\" <%s>\n", newbc->caller.name, newbc->caller.number);
  5825. }
  5826. misdn_cfg_get(port, MISDN_CFG_APPEND_MSN_TO_CALLERID_TAG, &append_msn, sizeof(append_msn));
  5827. if (append_msn) {
  5828. strncat(newbc->incoming_cid_tag, "_", sizeof(newbc->incoming_cid_tag) - strlen(newbc->incoming_cid_tag) - 1);
  5829. strncat(newbc->incoming_cid_tag, newbc->caller.number, sizeof(newbc->incoming_cid_tag) - strlen(newbc->incoming_cid_tag) - 1);
  5830. }
  5831. ast_channel_caller(ast)->id.tag = ast_strdup(newbc->incoming_cid_tag);
  5832. misdn_cfg_get(port, MISDN_CFG_LOCALDIALPLAN, &number_type, sizeof(number_type));
  5833. if (number_type < 0) {
  5834. if (connected_id.number.valid) {
  5835. newbc->caller.number_type = ast_to_misdn_ton(connected_id.number.plan);
  5836. newbc->caller.number_plan = ast_to_misdn_plan(connected_id.number.plan);
  5837. } else {
  5838. newbc->caller.number_type = NUMTYPE_UNKNOWN;
  5839. newbc->caller.number_plan = NUMPLAN_ISDN;
  5840. }
  5841. } else {
  5842. /* Force us to send in SETUP message */
  5843. newbc->caller.number_type = number_type;
  5844. newbc->caller.number_plan = NUMPLAN_ISDN;
  5845. }
  5846. debug_numtype(port, newbc->caller.number_type, "LTON");
  5847. newbc->capability = ast_channel_transfercapability(ast);
  5848. pbx_builtin_setvar_helper(ast, "TRANSFERCAPABILITY", ast_transfercapability2str(newbc->capability));
  5849. if (ast_channel_transfercapability(ast) == INFO_CAPABILITY_DIGITAL_UNRESTRICTED) {
  5850. chan_misdn_log(2, port, " --> * Call with flag Digital\n");
  5851. }
  5852. /* update caller screening and presentation */
  5853. update_config(ch);
  5854. /* fill in some ies from channel dialplan variables */
  5855. import_ch(ast, newbc, ch);
  5856. /* Finally The Options Override Everything */
  5857. if (!ast_strlen_zero(args.opts)) {
  5858. misdn_set_opt_exec(ast, args.opts);
  5859. } else {
  5860. chan_misdn_log(2, port, "NO OPTS GIVEN\n");
  5861. }
  5862. if (newbc->set_presentation) {
  5863. newbc->caller.presentation = newbc->presentation;
  5864. }
  5865. misdn_copy_redirecting_from_ast(newbc, ast);
  5866. switch (newbc->outgoing_colp) {
  5867. case 1:/* restricted */
  5868. case 2:/* blocked */
  5869. newbc->redirecting.from.presentation = 1;/* restricted */
  5870. break;
  5871. default:
  5872. break;
  5873. }
  5874. #if defined(AST_MISDN_ENHANCEMENTS)
  5875. if (newbc->redirecting.from.number[0] && misdn_lib_is_ptp(port)) {
  5876. if (newbc->redirecting.count < 1) {
  5877. newbc->redirecting.count = 1;
  5878. }
  5879. /* Create DivertingLegInformation2 facility */
  5880. newbc->fac_out.Function = Fac_DivertingLegInformation2;
  5881. newbc->fac_out.u.DivertingLegInformation2.InvokeID = ++misdn_invoke_id;
  5882. newbc->fac_out.u.DivertingLegInformation2.DivertingPresent = 1;
  5883. misdn_PresentedNumberUnscreened_fill(
  5884. &newbc->fac_out.u.DivertingLegInformation2.Diverting,
  5885. &newbc->redirecting.from);
  5886. switch (newbc->outgoing_colp) {
  5887. case 2:/* blocked */
  5888. /* Block the number going out */
  5889. newbc->fac_out.u.DivertingLegInformation2.Diverting.Type = 1;/* presentationRestricted */
  5890. /* Don't tell about any previous diversions or why for that matter. */
  5891. newbc->fac_out.u.DivertingLegInformation2.DiversionCounter = 1;
  5892. newbc->fac_out.u.DivertingLegInformation2.DiversionReason = 0;/* unknown */
  5893. break;
  5894. default:
  5895. newbc->fac_out.u.DivertingLegInformation2.DiversionCounter =
  5896. newbc->redirecting.count;
  5897. newbc->fac_out.u.DivertingLegInformation2.DiversionReason =
  5898. misdn_to_diversion_reason(newbc->redirecting.reason);
  5899. break;
  5900. }
  5901. newbc->fac_out.u.DivertingLegInformation2.OriginalCalledPresent = 0;
  5902. if (1 < newbc->fac_out.u.DivertingLegInformation2.DiversionCounter) {
  5903. newbc->fac_out.u.DivertingLegInformation2.OriginalCalledPresent = 1;
  5904. newbc->fac_out.u.DivertingLegInformation2.OriginalCalled.Type = 2;/* numberNotAvailableDueToInterworking */
  5905. }
  5906. /*
  5907. * Expect a DivertingLegInformation3 to update the COLR of the
  5908. * redirecting-to party we are attempting to call now.
  5909. */
  5910. newbc->div_leg_3_rx_wanted = 1;
  5911. }
  5912. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  5913. }
  5914. exceed = add_out_calls(port);
  5915. if (exceed != 0) {
  5916. char tmp[16];
  5917. snprintf(tmp, sizeof(tmp), "%d", exceed);
  5918. pbx_builtin_setvar_helper(ast, "MAX_OVERFLOW", tmp);
  5919. ast_channel_hangupcause_set(ast, AST_CAUSE_NORMAL_TEMPORARY_FAILURE);
  5920. ast_setstate(ast, AST_STATE_DOWN);
  5921. return -1;
  5922. }
  5923. #if defined(AST_MISDN_ENHANCEMENTS)
  5924. if (newbc->fac_out.Function != Fac_None) {
  5925. print_facility(&newbc->fac_out, newbc);
  5926. }
  5927. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  5928. r = misdn_lib_send_event(newbc, EVENT_SETUP);
  5929. /** we should have l3id after sending setup **/
  5930. ch->l3id = newbc->l3_id;
  5931. if (r == -ENOCHAN) {
  5932. chan_misdn_log(0, port, " --> * Theres no Channel at the moment .. !\n");
  5933. chan_misdn_log(1, port, " --> * SEND: State Down pid:%d\n", newbc ? newbc->pid : -1);
  5934. ast_channel_hangupcause_set(ast, AST_CAUSE_NORMAL_CIRCUIT_CONGESTION);
  5935. ast_setstate(ast, AST_STATE_DOWN);
  5936. return -1;
  5937. }
  5938. chan_misdn_log(2, port, " --> * SEND: State Dialing pid:%d\n", newbc ? newbc->pid : 1);
  5939. ast_setstate(ast, AST_STATE_DIALING);
  5940. ast_channel_hangupcause_set(ast, AST_CAUSE_NORMAL_CLEARING);
  5941. if (newbc->nt) {
  5942. stop_bc_tones(ch);
  5943. }
  5944. ch->state = MISDN_CALLING;
  5945. return 0;
  5946. }
  5947. static int misdn_answer(struct ast_channel *ast)
  5948. {
  5949. struct chan_list *p;
  5950. const char *tmp;
  5951. if (!ast || !(p = MISDN_ASTERISK_TECH_PVT(ast))) {
  5952. return -1;
  5953. }
  5954. chan_misdn_log(1, p ? (p->bc ? p->bc->port : 0) : 0, "* ANSWER:\n");
  5955. if (!p) {
  5956. ast_log(LOG_WARNING, " --> Channel not connected ??\n");
  5957. ast_queue_hangup_with_cause(ast, AST_CAUSE_NETWORK_OUT_OF_ORDER);
  5958. }
  5959. if (!p->bc) {
  5960. chan_misdn_log(1, 0, " --> Got Answer, but there is no bc obj ??\n");
  5961. ast_queue_hangup_with_cause(ast, AST_CAUSE_PROTOCOL_ERROR);
  5962. }
  5963. ast_channel_lock(ast);
  5964. tmp = pbx_builtin_getvar_helper(ast, "CRYPT_KEY");
  5965. if (!ast_strlen_zero(tmp)) {
  5966. chan_misdn_log(1, p->bc->port, " --> Connection will be BF crypted\n");
  5967. ast_copy_string(p->bc->crypt_key, tmp, sizeof(p->bc->crypt_key));
  5968. } else {
  5969. chan_misdn_log(3, p->bc->port, " --> Connection is without BF encryption\n");
  5970. }
  5971. tmp = pbx_builtin_getvar_helper(ast, "MISDN_DIGITAL_TRANS");
  5972. if (!ast_strlen_zero(tmp) && ast_true(tmp)) {
  5973. chan_misdn_log(1, p->bc->port, " --> Connection is transparent digital\n");
  5974. p->bc->nodsp = 1;
  5975. p->bc->hdlc = 0;
  5976. p->bc->nojitter = 1;
  5977. }
  5978. ast_channel_unlock(ast);
  5979. p->state = MISDN_CONNECTED;
  5980. stop_indicate(p);
  5981. if (ast_strlen_zero(p->bc->connected.number)) {
  5982. chan_misdn_log(2,p->bc->port," --> empty connected number using dialed number\n");
  5983. ast_copy_string(p->bc->connected.number, p->bc->dialed.number, sizeof(p->bc->connected.number));
  5984. /*
  5985. * Use the misdn_set_opt() application to set the presentation
  5986. * before we answer or you can use the CONECTEDLINE() function
  5987. * to set everything before using the Answer() application.
  5988. */
  5989. p->bc->connected.presentation = p->bc->presentation;
  5990. p->bc->connected.screening = 0; /* unscreened */
  5991. p->bc->connected.number_type = p->bc->dialed.number_type;
  5992. p->bc->connected.number_plan = p->bc->dialed.number_plan;
  5993. }
  5994. switch (p->bc->outgoing_colp) {
  5995. case 1:/* restricted */
  5996. case 2:/* blocked */
  5997. p->bc->connected.presentation = 1;/* restricted */
  5998. break;
  5999. default:
  6000. break;
  6001. }
  6002. #if defined(AST_MISDN_ENHANCEMENTS)
  6003. if (p->bc->div_leg_3_tx_pending) {
  6004. p->bc->div_leg_3_tx_pending = 0;
  6005. /* Send DivertingLegInformation3 */
  6006. p->bc->fac_out.Function = Fac_DivertingLegInformation3;
  6007. p->bc->fac_out.u.DivertingLegInformation3.InvokeID = ++misdn_invoke_id;
  6008. p->bc->fac_out.u.DivertingLegInformation3.PresentationAllowedIndicator =
  6009. (p->bc->connected.presentation == 0) ? 1 : 0;
  6010. print_facility(&p->bc->fac_out, p->bc);
  6011. }
  6012. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  6013. misdn_lib_send_event(p->bc, EVENT_CONNECT);
  6014. start_bc_tones(p);
  6015. return 0;
  6016. }
  6017. static int misdn_digit_begin(struct ast_channel *chan, char digit)
  6018. {
  6019. /* XXX Modify this callback to support Asterisk controlling the length of DTMF */
  6020. return 0;
  6021. }
  6022. static int misdn_digit_end(struct ast_channel *ast, char digit, unsigned int duration)
  6023. {
  6024. struct chan_list *p;
  6025. struct misdn_bchannel *bc;
  6026. char buf[2] = { digit, 0 };
  6027. if (!ast || !(p = MISDN_ASTERISK_TECH_PVT(ast))) {
  6028. return -1;
  6029. }
  6030. bc = p->bc;
  6031. chan_misdn_log(1, bc ? bc->port : 0, "* IND : Digit %c\n", digit);
  6032. if (!bc) {
  6033. ast_log(LOG_WARNING, " --> !! Got Digit Event without having bchannel Object\n");
  6034. return -1;
  6035. }
  6036. switch (p->state) {
  6037. case MISDN_CALLING:
  6038. if (strlen(bc->infos_pending) < sizeof(bc->infos_pending) - 1) {
  6039. strncat(bc->infos_pending, buf, sizeof(bc->infos_pending) - strlen(bc->infos_pending) - 1);
  6040. }
  6041. break;
  6042. case MISDN_CALLING_ACKNOWLEDGE:
  6043. ast_copy_string(bc->info_dad, buf, sizeof(bc->info_dad));
  6044. if (strlen(bc->dialed.number) < sizeof(bc->dialed.number) - 1) {
  6045. strncat(bc->dialed.number, buf, sizeof(bc->dialed.number) - strlen(bc->dialed.number) - 1);
  6046. }
  6047. ast_channel_exten_set(p->ast, bc->dialed.number);
  6048. misdn_lib_send_event(bc, EVENT_INFORMATION);
  6049. break;
  6050. default:
  6051. if (bc->send_dtmf) {
  6052. send_digit_to_chan(p, digit);
  6053. }
  6054. break;
  6055. }
  6056. return 0;
  6057. }
  6058. static int misdn_fixup(struct ast_channel *oldast, struct ast_channel *ast)
  6059. {
  6060. struct chan_list *p;
  6061. if (!ast || !(p = MISDN_ASTERISK_TECH_PVT(ast))) {
  6062. return -1;
  6063. }
  6064. chan_misdn_log(1, p->bc ? p->bc->port : 0, "* IND: Got Fixup State:%s L3id:%x\n", misdn_get_ch_state(p), p->l3id);
  6065. p->ast = ast;
  6066. return 0;
  6067. }
  6068. static int misdn_indication(struct ast_channel *ast, int cond, const void *data, size_t datalen)
  6069. {
  6070. struct chan_list *p;
  6071. if (!ast || !(p = MISDN_ASTERISK_TECH_PVT(ast))) {
  6072. ast_log(LOG_WARNING, "Returned -1 in misdn_indication\n");
  6073. return -1;
  6074. }
  6075. if (!p->bc) {
  6076. if (p->hold.state == MISDN_HOLD_IDLE) {
  6077. chan_misdn_log(1, 0, "* IND : Indication [%d] ignored on %s\n", cond,
  6078. ast_channel_name(ast));
  6079. ast_log(LOG_WARNING, "Private Pointer but no bc ?\n");
  6080. } else {
  6081. chan_misdn_log(1, 0, "* IND : Indication [%d] ignored on hold %s\n",
  6082. cond, ast_channel_name(ast));
  6083. }
  6084. return -1;
  6085. }
  6086. chan_misdn_log(5, p->bc->port, "* IND : Indication [%d] on %s\n", cond, ast_channel_name(ast));
  6087. switch (cond) {
  6088. case AST_CONTROL_BUSY:
  6089. chan_misdn_log(1, p->bc->port, "* IND :\tbusy pid:%d\n", p->bc->pid);
  6090. ast_setstate(ast, AST_STATE_BUSY);
  6091. p->bc->out_cause = AST_CAUSE_USER_BUSY;
  6092. if (p->state != MISDN_CONNECTED) {
  6093. start_bc_tones(p);
  6094. misdn_lib_send_event(p->bc, EVENT_DISCONNECT);
  6095. }
  6096. return -1;
  6097. case AST_CONTROL_RING:
  6098. chan_misdn_log(1, p->bc->port, "* IND :\tring pid:%d\n", p->bc->pid);
  6099. return -1;
  6100. case AST_CONTROL_RINGING:
  6101. chan_misdn_log(1, p->bc->port, "* IND :\tringing pid:%d\n", p->bc->pid);
  6102. switch (p->state) {
  6103. case MISDN_ALERTING:
  6104. chan_misdn_log(2, p->bc->port, " --> * IND :\tringing pid:%d but I was Ringing before, so ignoring it\n", p->bc->pid);
  6105. break;
  6106. case MISDN_CONNECTED:
  6107. chan_misdn_log(2, p->bc->port, " --> * IND :\tringing pid:%d but Connected, so just send TONE_ALERTING without state changes \n", p->bc->pid);
  6108. return -1;
  6109. default:
  6110. p->state = MISDN_ALERTING;
  6111. chan_misdn_log(2, p->bc->port, " --> * IND :\tringing pid:%d\n", p->bc->pid);
  6112. misdn_lib_send_event(p->bc, EVENT_ALERTING);
  6113. chan_misdn_log(3, p->bc->port, " --> * SEND: State Ring pid:%d\n", p->bc->pid);
  6114. ast_setstate(ast, AST_STATE_RING);
  6115. if (!p->bc->nt && (p->originator == ORG_MISDN) && !p->incoming_early_audio) {
  6116. chan_misdn_log(2, p->bc->port, " --> incoming_early_audio off\n");
  6117. } else {
  6118. return -1;
  6119. }
  6120. }
  6121. break;
  6122. case AST_CONTROL_ANSWER:
  6123. chan_misdn_log(1, p->bc->port, " --> * IND :\tanswer pid:%d\n", p->bc->pid);
  6124. start_bc_tones(p);
  6125. break;
  6126. case AST_CONTROL_TAKEOFFHOOK:
  6127. chan_misdn_log(1, p->bc->port, " --> *\ttakeoffhook pid:%d\n", p->bc->pid);
  6128. return -1;
  6129. case AST_CONTROL_OFFHOOK:
  6130. chan_misdn_log(1, p->bc->port, " --> *\toffhook pid:%d\n", p->bc->pid);
  6131. return -1;
  6132. case AST_CONTROL_FLASH:
  6133. chan_misdn_log(1, p->bc->port, " --> *\tflash pid:%d\n", p->bc->pid);
  6134. break;
  6135. case AST_CONTROL_PROGRESS:
  6136. chan_misdn_log(1, p->bc->port, " --> * IND :\tprogress pid:%d\n", p->bc->pid);
  6137. misdn_lib_send_event(p->bc, EVENT_PROGRESS);
  6138. break;
  6139. case AST_CONTROL_PROCEEDING:
  6140. chan_misdn_log(1, p->bc->port, " --> * IND :\tproceeding pid:%d\n", p->bc->pid);
  6141. misdn_lib_send_event(p->bc, EVENT_PROCEEDING);
  6142. break;
  6143. case AST_CONTROL_INCOMPLETE:
  6144. chan_misdn_log(1, p->bc->port, " --> *\tincomplete pid:%d\n", p->bc->pid);
  6145. if (!p->overlap_dial) {
  6146. /* Overlapped dialing not enabled - send hangup */
  6147. p->bc->out_cause = AST_CAUSE_INVALID_NUMBER_FORMAT;
  6148. start_bc_tones(p);
  6149. misdn_lib_send_event(p->bc, EVENT_DISCONNECT);
  6150. if (p->bc->nt) {
  6151. hanguptone_indicate(p);
  6152. }
  6153. }
  6154. break;
  6155. case AST_CONTROL_CONGESTION:
  6156. chan_misdn_log(1, p->bc->port, " --> * IND :\tcongestion pid:%d\n", p->bc->pid);
  6157. p->bc->out_cause = AST_CAUSE_SWITCH_CONGESTION;
  6158. start_bc_tones(p);
  6159. misdn_lib_send_event(p->bc, EVENT_DISCONNECT);
  6160. if (p->bc->nt) {
  6161. hanguptone_indicate(p);
  6162. }
  6163. break;
  6164. case -1 :
  6165. chan_misdn_log(1, p->bc->port, " --> * IND :\t-1! (stop indication) pid:%d\n", p->bc->pid);
  6166. stop_indicate(p);
  6167. if (p->state == MISDN_CONNECTED) {
  6168. start_bc_tones(p);
  6169. }
  6170. break;
  6171. case AST_CONTROL_HOLD:
  6172. ast_moh_start(ast, data, p->mohinterpret);
  6173. chan_misdn_log(1, p->bc->port, " --> *\tHOLD pid:%d\n", p->bc->pid);
  6174. break;
  6175. case AST_CONTROL_UNHOLD:
  6176. ast_moh_stop(ast);
  6177. chan_misdn_log(1, p->bc->port, " --> *\tUNHOLD pid:%d\n", p->bc->pid);
  6178. break;
  6179. case AST_CONTROL_CONNECTED_LINE:
  6180. chan_misdn_log(1, p->bc->port, "* IND :\tconnected line update pid:%d\n", p->bc->pid);
  6181. misdn_update_connected_line(ast, p->bc, p->originator);
  6182. break;
  6183. case AST_CONTROL_REDIRECTING:
  6184. chan_misdn_log(1, p->bc->port, "* IND :\tredirecting info update pid:%d\n", p->bc->pid);
  6185. misdn_update_redirecting(ast, p->bc, p->originator);
  6186. break;
  6187. default:
  6188. chan_misdn_log(1, p->bc->port, " --> * Unknown Indication:%d pid:%d\n", cond, p->bc->pid);
  6189. /* fallthrough */
  6190. case AST_CONTROL_PVT_CAUSE_CODE:
  6191. case AST_CONTROL_MASQUERADE_NOTIFY:
  6192. return -1;
  6193. }
  6194. return 0;
  6195. }
  6196. static int misdn_hangup(struct ast_channel *ast)
  6197. {
  6198. struct chan_list *p;
  6199. struct misdn_bchannel *bc;
  6200. const char *var;
  6201. if (!ast) {
  6202. return -1;
  6203. }
  6204. ast_debug(1, "misdn_hangup(%s)\n", ast_channel_name(ast));
  6205. /* Take the ast_channel's tech_pvt reference. */
  6206. ast_mutex_lock(&release_lock);
  6207. p = MISDN_ASTERISK_TECH_PVT(ast);
  6208. if (!p) {
  6209. ast_mutex_unlock(&release_lock);
  6210. return -1;
  6211. }
  6212. MISDN_ASTERISK_TECH_PVT_SET(ast, NULL);
  6213. if (!misdn_chan_is_valid(p)) {
  6214. ast_mutex_unlock(&release_lock);
  6215. chan_list_unref(p, "Release ast_channel reference. Was not active?");
  6216. return 0;
  6217. }
  6218. if (p->hold.state == MISDN_HOLD_IDLE) {
  6219. bc = p->bc;
  6220. } else {
  6221. p->hold.state = MISDN_HOLD_DISCONNECT;
  6222. bc = misdn_lib_find_held_bc(p->hold.port, p->l3id);
  6223. if (!bc) {
  6224. chan_misdn_log(4, p->hold.port,
  6225. "misdn_hangup: Could not find held bc for (%s)\n", ast_channel_name(ast));
  6226. release_chan_early(p);
  6227. ast_mutex_unlock(&release_lock);
  6228. chan_list_unref(p, "Release ast_channel reference");
  6229. return 0;
  6230. }
  6231. }
  6232. if (ast_channel_state(ast) == AST_STATE_RESERVED || p->state == MISDN_NOTHING) {
  6233. /* between request and call */
  6234. ast_debug(1, "State Reserved (or nothing) => chanIsAvail\n");
  6235. release_chan_early(p);
  6236. if (bc) {
  6237. misdn_lib_release(bc);
  6238. }
  6239. ast_mutex_unlock(&release_lock);
  6240. chan_list_unref(p, "Release ast_channel reference");
  6241. return 0;
  6242. }
  6243. if (!bc) {
  6244. ast_log(LOG_WARNING, "Hangup with private but no bc ? state:%s l3id:%x\n",
  6245. misdn_get_ch_state(p), p->l3id);
  6246. release_chan_early(p);
  6247. ast_mutex_unlock(&release_lock);
  6248. chan_list_unref(p, "Release ast_channel reference");
  6249. return 0;
  6250. }
  6251. p->ast = NULL;
  6252. p->need_hangup = 0;
  6253. p->need_queue_hangup = 0;
  6254. p->need_busy = 0;
  6255. if (!bc->nt) {
  6256. stop_bc_tones(p);
  6257. }
  6258. bc->out_cause = ast_channel_hangupcause(ast) ? ast_channel_hangupcause(ast) : AST_CAUSE_NORMAL_CLEARING;
  6259. /* Channel lock is already held when we are called. */
  6260. //ast_channel_lock(ast);
  6261. var = pbx_builtin_getvar_helper(ast, "HANGUPCAUSE");
  6262. if (!var) {
  6263. var = pbx_builtin_getvar_helper(ast, "PRI_CAUSE");
  6264. }
  6265. if (var) {
  6266. int tmpcause;
  6267. tmpcause = atoi(var);
  6268. bc->out_cause = tmpcause ? tmpcause : AST_CAUSE_NORMAL_CLEARING;
  6269. }
  6270. var = pbx_builtin_getvar_helper(ast, "MISDN_USERUSER");
  6271. if (var) {
  6272. ast_log(LOG_NOTICE, "MISDN_USERUSER: %s\n", var);
  6273. ast_copy_string(bc->uu, var, sizeof(bc->uu));
  6274. bc->uulen = strlen(bc->uu);
  6275. }
  6276. //ast_channel_unlock(ast);
  6277. chan_misdn_log(1, bc->port,
  6278. "* IND : HANGUP\tpid:%d context:%s dialed:%s caller:\"%s\" <%s> State:%s\n",
  6279. bc->pid,
  6280. ast_channel_context(ast),
  6281. ast_channel_exten(ast),
  6282. (ast_channel_caller(ast)->id.name.valid && ast_channel_caller(ast)->id.name.str)
  6283. ? ast_channel_caller(ast)->id.name.str : "",
  6284. (ast_channel_caller(ast)->id.number.valid && ast_channel_caller(ast)->id.number.str)
  6285. ? ast_channel_caller(ast)->id.number.str : "",
  6286. misdn_get_ch_state(p));
  6287. chan_misdn_log(3, bc->port, " --> l3id:%x\n", p->l3id);
  6288. chan_misdn_log(3, bc->port, " --> cause:%d\n", bc->cause);
  6289. chan_misdn_log(2, bc->port, " --> out_cause:%d\n", bc->out_cause);
  6290. switch (p->state) {
  6291. case MISDN_INCOMING_SETUP:
  6292. /*
  6293. * This is the only place in misdn_hangup, where we
  6294. * can call release_chan, else it might create a lot of trouble.
  6295. */
  6296. ast_log(LOG_NOTICE, "release channel, in INCOMING_SETUP state.. no other events happened\n");
  6297. release_chan(p, bc);
  6298. misdn_lib_send_event(bc, EVENT_RELEASE_COMPLETE);
  6299. ast_mutex_unlock(&release_lock);
  6300. chan_list_unref(p, "Release ast_channel reference");
  6301. return 0;
  6302. case MISDN_DIALING:
  6303. if (p->hold.state == MISDN_HOLD_IDLE) {
  6304. start_bc_tones(p);
  6305. hanguptone_indicate(p);
  6306. }
  6307. if (bc->need_disconnect) {
  6308. misdn_lib_send_event(bc, EVENT_DISCONNECT);
  6309. }
  6310. break;
  6311. case MISDN_CALLING_ACKNOWLEDGE:
  6312. if (p->hold.state == MISDN_HOLD_IDLE) {
  6313. start_bc_tones(p);
  6314. hanguptone_indicate(p);
  6315. }
  6316. if (bc->need_disconnect) {
  6317. misdn_lib_send_event(bc, EVENT_DISCONNECT);
  6318. }
  6319. break;
  6320. case MISDN_CALLING:
  6321. case MISDN_ALERTING:
  6322. case MISDN_PROGRESS:
  6323. case MISDN_PROCEEDING:
  6324. if (p->originator != ORG_AST && p->hold.state == MISDN_HOLD_IDLE) {
  6325. hanguptone_indicate(p);
  6326. }
  6327. if (bc->need_disconnect) {
  6328. misdn_lib_send_event(bc, EVENT_DISCONNECT);
  6329. }
  6330. break;
  6331. case MISDN_CONNECTED:
  6332. /* Alerting or Disconnect */
  6333. if (bc->nt && p->hold.state == MISDN_HOLD_IDLE) {
  6334. start_bc_tones(p);
  6335. hanguptone_indicate(p);
  6336. bc->progress_indicator = INFO_PI_INBAND_AVAILABLE;
  6337. }
  6338. if (bc->need_disconnect) {
  6339. misdn_lib_send_event(bc, EVENT_DISCONNECT);
  6340. }
  6341. break;
  6342. case MISDN_DISCONNECTED:
  6343. if (bc->need_release) {
  6344. misdn_lib_send_event(bc, EVENT_RELEASE);
  6345. }
  6346. break;
  6347. case MISDN_CLEANING:
  6348. ast_mutex_unlock(&release_lock);
  6349. chan_list_unref(p, "Release ast_channel reference");
  6350. return 0;
  6351. case MISDN_BUSY:
  6352. break;
  6353. default:
  6354. if (bc->nt) {
  6355. bc->out_cause = -1;
  6356. if (bc->need_release) {
  6357. misdn_lib_send_event(bc, EVENT_RELEASE);
  6358. }
  6359. } else {
  6360. if (bc->need_disconnect) {
  6361. misdn_lib_send_event(bc, EVENT_DISCONNECT);
  6362. }
  6363. }
  6364. break;
  6365. }
  6366. p->state = MISDN_CLEANING;
  6367. chan_misdn_log(3, bc->port, " --> Channel: %s hungup new state:%s\n", ast_channel_name(ast),
  6368. misdn_get_ch_state(p));
  6369. ast_mutex_unlock(&release_lock);
  6370. chan_list_unref(p, "Release ast_channel reference");
  6371. return 0;
  6372. }
  6373. static struct ast_frame *process_ast_dsp(struct chan_list *tmp, struct ast_frame *frame)
  6374. {
  6375. struct ast_frame *f;
  6376. if (tmp->dsp) {
  6377. f = ast_dsp_process(tmp->ast, tmp->dsp, frame);
  6378. } else {
  6379. chan_misdn_log(0, tmp->bc->port, "No DSP-Path found\n");
  6380. return NULL;
  6381. }
  6382. if (!f || (f->frametype != AST_FRAME_DTMF)) {
  6383. return f;
  6384. }
  6385. ast_debug(1, "Detected inband DTMF digit: %c\n", f->subclass.integer);
  6386. if (tmp->faxdetect && (f->subclass.integer == 'f')) {
  6387. /* Fax tone -- Handle and return NULL */
  6388. if (!tmp->faxhandled) {
  6389. struct ast_channel *ast = tmp->ast;
  6390. tmp->faxhandled++;
  6391. chan_misdn_log(0, tmp->bc->port, "Fax detected, preparing %s for fax transfer.\n", ast_channel_name(ast));
  6392. tmp->bc->rxgain = 0;
  6393. isdn_lib_update_rxgain(tmp->bc);
  6394. tmp->bc->txgain = 0;
  6395. isdn_lib_update_txgain(tmp->bc);
  6396. #ifdef MISDN_1_2
  6397. *tmp->bc->pipeline = 0;
  6398. #else
  6399. tmp->bc->ec_enable = 0;
  6400. #endif
  6401. isdn_lib_update_ec(tmp->bc);
  6402. isdn_lib_stop_dtmf(tmp->bc);
  6403. switch (tmp->faxdetect) {
  6404. case 1:
  6405. if (strcmp(ast_channel_exten(ast), "fax")) {
  6406. const char *context;
  6407. char context_tmp[BUFFERSIZE];
  6408. misdn_cfg_get(tmp->bc->port, MISDN_CFG_FAXDETECT_CONTEXT, &context_tmp, sizeof(context_tmp));
  6409. context = S_OR(context_tmp, S_OR(ast_channel_macrocontext(ast), ast_channel_context(ast)));
  6410. if (ast_exists_extension(ast, context, "fax", 1,
  6411. S_COR(ast_channel_caller(ast)->id.number.valid, ast_channel_caller(ast)->id.number.str, NULL))) {
  6412. ast_verb(3, "Redirecting %s to fax extension (context:%s)\n", ast_channel_name(ast), context);
  6413. /* Save the DID/DNIS when we transfer the fax call to a "fax" extension */
  6414. pbx_builtin_setvar_helper(ast,"FAXEXTEN",ast_channel_exten(ast));
  6415. if (ast_async_goto(ast, context, "fax", 1)) {
  6416. ast_log(LOG_WARNING, "Failed to async goto '%s' into fax of '%s'\n", ast_channel_name(ast), context);
  6417. }
  6418. } else {
  6419. ast_log(LOG_NOTICE, "Fax detected but no fax extension, context:%s exten:%s\n", context, ast_channel_exten(ast));
  6420. }
  6421. } else {
  6422. ast_debug(1, "Already in a fax extension, not redirecting\n");
  6423. }
  6424. break;
  6425. case 2:
  6426. ast_verb(3, "Not redirecting %s to fax extension, nojump is set.\n", ast_channel_name(ast));
  6427. break;
  6428. default:
  6429. break;
  6430. }
  6431. } else {
  6432. ast_debug(1, "Fax already handled\n");
  6433. }
  6434. }
  6435. if (tmp->ast_dsp && (f->subclass.integer != 'f')) {
  6436. chan_misdn_log(2, tmp->bc->port, " --> * SEND: DTMF (AST_DSP) :%c\n", f->subclass.integer);
  6437. }
  6438. return f;
  6439. }
  6440. static struct ast_frame *misdn_read(struct ast_channel *ast)
  6441. {
  6442. struct chan_list *tmp;
  6443. int len, t;
  6444. struct pollfd pfd = { .fd = -1, .events = POLLIN };
  6445. if (!ast) {
  6446. chan_misdn_log(1, 0, "misdn_read called without ast\n");
  6447. return NULL;
  6448. }
  6449. if (!(tmp = MISDN_ASTERISK_TECH_PVT(ast))) {
  6450. chan_misdn_log(1, 0, "misdn_read called without ast->pvt\n");
  6451. return NULL;
  6452. }
  6453. if (!tmp->bc && tmp->hold.state == MISDN_HOLD_IDLE) {
  6454. chan_misdn_log(1, 0, "misdn_read called without bc\n");
  6455. return NULL;
  6456. }
  6457. pfd.fd = tmp->pipe[0];
  6458. t = ast_poll(&pfd, 1, 20);
  6459. if (t < 0) {
  6460. chan_misdn_log(-1, tmp->bc->port, "poll() error (err=%s)\n", strerror(errno));
  6461. return NULL;
  6462. }
  6463. if (!t) {
  6464. chan_misdn_log(3, tmp->bc->port, "poll() timed out\n");
  6465. len = 160;
  6466. } else if (pfd.revents & POLLIN) {
  6467. len = read(tmp->pipe[0], tmp->ast_rd_buf, sizeof(tmp->ast_rd_buf));
  6468. if (len <= 0) {
  6469. /* we hangup here, since our pipe is closed */
  6470. chan_misdn_log(2, tmp->bc->port, "misdn_read: Pipe closed, hanging up\n");
  6471. return NULL;
  6472. }
  6473. } else {
  6474. return NULL;
  6475. }
  6476. tmp->frame.frametype = AST_FRAME_VOICE;
  6477. tmp->frame.subclass.format = ast_format_alaw;
  6478. tmp->frame.datalen = len;
  6479. tmp->frame.samples = len;
  6480. tmp->frame.mallocd = 0;
  6481. tmp->frame.offset = 0;
  6482. tmp->frame.delivery = ast_tv(0, 0);
  6483. tmp->frame.src = NULL;
  6484. tmp->frame.data.ptr = tmp->ast_rd_buf;
  6485. if (tmp->faxdetect && !tmp->faxhandled) {
  6486. if (tmp->faxdetect_timeout) {
  6487. if (ast_tvzero(tmp->faxdetect_tv)) {
  6488. tmp->faxdetect_tv = ast_tvnow();
  6489. chan_misdn_log(2, tmp->bc->port, "faxdetect: starting detection with timeout: %ds ...\n", tmp->faxdetect_timeout);
  6490. return process_ast_dsp(tmp, &tmp->frame);
  6491. } else {
  6492. struct timeval tv_now = ast_tvnow();
  6493. int diff = ast_tvdiff_ms(tv_now, tmp->faxdetect_tv);
  6494. if (diff <= (tmp->faxdetect_timeout * 1000)) {
  6495. chan_misdn_log(5, tmp->bc->port, "faxdetect: detecting ...\n");
  6496. return process_ast_dsp(tmp, &tmp->frame);
  6497. } else {
  6498. chan_misdn_log(2, tmp->bc->port, "faxdetect: stopping detection (time ran out) ...\n");
  6499. tmp->faxdetect = 0;
  6500. return &tmp->frame;
  6501. }
  6502. }
  6503. } else {
  6504. chan_misdn_log(5, tmp->bc->port, "faxdetect: detecting ... (no timeout)\n");
  6505. return process_ast_dsp(tmp, &tmp->frame);
  6506. }
  6507. } else {
  6508. if (tmp->ast_dsp) {
  6509. return process_ast_dsp(tmp, &tmp->frame);
  6510. } else {
  6511. return &tmp->frame;
  6512. }
  6513. }
  6514. }
  6515. static int misdn_write(struct ast_channel *ast, struct ast_frame *frame)
  6516. {
  6517. struct chan_list *ch;
  6518. if (!ast || !(ch = MISDN_ASTERISK_TECH_PVT(ast))) {
  6519. return -1;
  6520. }
  6521. if (ch->hold.state != MISDN_HOLD_IDLE) {
  6522. chan_misdn_log(7, 0, "misdn_write: Returning because hold active\n");
  6523. return 0;
  6524. }
  6525. if (!ch->bc) {
  6526. ast_log(LOG_WARNING, "private but no bc\n");
  6527. return -1;
  6528. }
  6529. if (ch->notxtone) {
  6530. chan_misdn_log(7, ch->bc->port, "misdn_write: Returning because notxtone\n");
  6531. return 0;
  6532. }
  6533. if (!frame->subclass.format) {
  6534. chan_misdn_log(4, ch->bc->port, "misdn_write: * prods us\n");
  6535. return 0;
  6536. }
  6537. if (ast_format_cmp(frame->subclass.format, ast_format_alaw) == AST_FORMAT_CMP_NOT_EQUAL) {
  6538. chan_misdn_log(-1, ch->bc->port, "Got Unsupported Frame with Format:%s\n",
  6539. ast_format_get_name(frame->subclass.format));
  6540. return 0;
  6541. }
  6542. if (!frame->samples) {
  6543. chan_misdn_log(4, ch->bc->port, "misdn_write: zero write\n");
  6544. if (!strcmp(frame->src,"ast_prod")) {
  6545. chan_misdn_log(1, ch->bc->port, "misdn_write: state (%s) prodded.\n", misdn_get_ch_state(ch));
  6546. if (ch->ts) {
  6547. chan_misdn_log(4, ch->bc->port, "Starting Playtones\n");
  6548. misdn_lib_tone_generator_start(ch->bc);
  6549. }
  6550. return 0;
  6551. }
  6552. return -1;
  6553. }
  6554. if (!ch->bc->addr) {
  6555. chan_misdn_log(8, ch->bc->port, "misdn_write: no addr for bc dropping:%d\n", frame->samples);
  6556. return 0;
  6557. }
  6558. #ifdef MISDN_DEBUG
  6559. {
  6560. int i;
  6561. int max = 5 > frame->samples ? frame->samples : 5;
  6562. ast_debug(1, "write2mISDN %p %d bytes: ", p, frame->samples);
  6563. for (i = 0; i < max; i++) {
  6564. ast_debug(1, "%02hhx ", ((unsigned char *) frame->data.ptr)[i]);
  6565. }
  6566. }
  6567. #endif
  6568. switch (ch->bc->bc_state) {
  6569. case BCHAN_ACTIVATED:
  6570. case BCHAN_BRIDGED:
  6571. break;
  6572. default:
  6573. if (!ch->dropped_frame_cnt) {
  6574. chan_misdn_log(5, ch->bc->port,
  6575. "BC not active (nor bridged) dropping: %d frames addr:%x exten:%s cid:%s ch->state:%s bc_state:%d l3id:%x\n",
  6576. frame->samples, ch->bc->addr, ast_channel_exten(ast),
  6577. S_COR(ast_channel_caller(ast)->id.number.valid, ast_channel_caller(ast)->id.number.str, ""),
  6578. misdn_get_ch_state(ch), ch->bc->bc_state, ch->bc->l3_id);
  6579. }
  6580. if (++ch->dropped_frame_cnt > 100) {
  6581. ch->dropped_frame_cnt = 0;
  6582. chan_misdn_log(5, ch->bc->port, "BC not active (nor bridged) dropping: %d frames addr:%x dropped > 100 frames!\n", frame->samples, ch->bc->addr);
  6583. }
  6584. return 0;
  6585. }
  6586. chan_misdn_log(9, ch->bc->port, "Sending :%d bytes to MISDN\n", frame->samples);
  6587. if (!ch->bc->nojitter && misdn_cap_is_speech(ch->bc->capability)) {
  6588. /* Buffered Transmit (triggered by read from isdn side)*/
  6589. if (misdn_jb_fill(ch->jb, frame->data.ptr, frame->samples) < 0) {
  6590. if (ch->bc->active) {
  6591. cb_log(0, ch->bc->port, "Misdn Jitterbuffer Overflow.\n");
  6592. }
  6593. }
  6594. } else {
  6595. /* transmit without jitterbuffer */
  6596. misdn_lib_tx2misdn_frm(ch->bc, frame->data.ptr, frame->samples);
  6597. }
  6598. return 0;
  6599. }
  6600. #if defined(mISDN_NATIVE_BRIDGING)
  6601. static enum ast_bridge_result misdn_bridge(struct ast_channel *c0,
  6602. struct ast_channel *c1, int flags,
  6603. struct ast_frame **fo,
  6604. struct ast_channel **rc,
  6605. int timeoutms)
  6606. {
  6607. struct chan_list *ch1, *ch2;
  6608. struct ast_channel *carr[2], *who;
  6609. int to = -1;
  6610. struct ast_frame *f;
  6611. int p1_b, p2_b;
  6612. int bridging;
  6613. misdn_cfg_get(0, MISDN_GEN_BRIDGING, &bridging, sizeof(bridging));
  6614. if (!bridging) {
  6615. /* Native mISDN bridging globally disabled. */
  6616. return AST_BRIDGE_FAILED_NOWARN;
  6617. }
  6618. ch1 = get_chan_by_ast(c0);
  6619. if (!ch1) {
  6620. return AST_BRIDGE_FAILED;
  6621. }
  6622. ch2 = get_chan_by_ast(c1);
  6623. if (!ch2) {
  6624. chan_list_unref(ch1, "Failed to find ch2");
  6625. return AST_BRIDGE_FAILED;
  6626. }
  6627. carr[0] = c0;
  6628. carr[1] = c1;
  6629. misdn_cfg_get(ch1->bc->port, MISDN_CFG_BRIDGING, &p1_b, sizeof(p1_b));
  6630. misdn_cfg_get(ch2->bc->port, MISDN_CFG_BRIDGING, &p2_b, sizeof(p2_b));
  6631. if (!p1_b || !p2_b) {
  6632. ast_log(LOG_NOTICE, "Falling back to Asterisk bridging\n");
  6633. chan_list_unref(ch1, "Bridge fallback ch1");
  6634. chan_list_unref(ch2, "Bridge fallback ch2");
  6635. return AST_BRIDGE_FAILED_NOWARN;
  6636. }
  6637. /* make a mISDN_dsp conference */
  6638. chan_misdn_log(1, ch1->bc->port, "I SEND: Making conference with Number:%d\n", ch1->bc->pid + 1);
  6639. misdn_lib_bridge(ch1->bc, ch2->bc);
  6640. ast_verb(3, "Native bridging %s and %s\n", ast_channel_name(c0), ast_channel_name(c1));
  6641. chan_misdn_log(1, ch1->bc->port, "* Making Native Bridge between \"%s\" <%s> and \"%s\" <%s>\n",
  6642. ch1->bc->caller.name,
  6643. ch1->bc->caller.number,
  6644. ch2->bc->caller.name,
  6645. ch2->bc->caller.number);
  6646. if (!(flags & AST_BRIDGE_DTMF_CHANNEL_0)) {
  6647. ch1->ignore_dtmf = 1;
  6648. }
  6649. if (!(flags & AST_BRIDGE_DTMF_CHANNEL_1)) {
  6650. ch2->ignore_dtmf = 1;
  6651. }
  6652. for (;/*ever*/;) {
  6653. to = -1;
  6654. who = ast_waitfor_n(carr, 2, &to);
  6655. if (!who) {
  6656. ast_log(LOG_NOTICE, "misdn_bridge: empty read, breaking out\n");
  6657. break;
  6658. }
  6659. f = ast_read(who);
  6660. if (!f || (f->frametype == AST_FRAME_CONTROL && f->subclass.integer != AST_CONTROL_PVT_CAUSE_CODE)) {
  6661. /* got hangup .. */
  6662. if (!f) {
  6663. chan_misdn_log(4, ch1->bc->port, "Read Null Frame\n");
  6664. } else {
  6665. chan_misdn_log(4, ch1->bc->port, "Read Frame Control class:%d\n", f->subclass.integer);
  6666. }
  6667. *fo = f;
  6668. *rc = who;
  6669. break;
  6670. }
  6671. if (f->frametype == AST_FRAME_DTMF) {
  6672. chan_misdn_log(1, 0, "Read DTMF %d from %s\n", f->subclass.integer, ast_channel_exten(who));
  6673. *fo = f;
  6674. *rc = who;
  6675. break;
  6676. }
  6677. #if 0
  6678. if (f->frametype == AST_FRAME_VOICE) {
  6679. chan_misdn_log(1, ch1->bc->port, "I SEND: Splitting conference with Number:%d\n", ch1->bc->pid +1);
  6680. continue;
  6681. }
  6682. #endif
  6683. if (f->frametype == AST_FRAME_CONTROL && f->subclass.integer == AST_CONTROL_PVT_CAUSE_CODE) {
  6684. ast_channel_hangupcause_hash_set((who == c0) ? c1 : c0, f->data.ptr, f->datalen);
  6685. } else {
  6686. ast_write((who == c0) ? c1 : c0, f);
  6687. }
  6688. }
  6689. chan_misdn_log(1, ch1->bc->port, "I SEND: Splitting conference with Number:%d\n", ch1->bc->pid + 1);
  6690. misdn_lib_split_bridge(ch1->bc, ch2->bc);
  6691. chan_list_unref(ch1, "Bridge complete ch1");
  6692. chan_list_unref(ch2, "Bridge complete ch2");
  6693. return AST_BRIDGE_COMPLETE;
  6694. }
  6695. #endif /* defined(mISDN_NATIVE_BRIDGING) */
  6696. /** AST INDICATIONS END **/
  6697. static int dialtone_indicate(struct chan_list *cl)
  6698. {
  6699. struct ast_channel *ast = cl->ast;
  6700. int nd = 0;
  6701. if (!ast) {
  6702. chan_misdn_log(0, cl->bc->port, "No Ast in dialtone_indicate\n");
  6703. return -1;
  6704. }
  6705. misdn_cfg_get(cl->bc->port, MISDN_CFG_NODIALTONE, &nd, sizeof(nd));
  6706. if (nd) {
  6707. chan_misdn_log(1, cl->bc->port, "Not sending Dialtone, because config wants it\n");
  6708. return 0;
  6709. }
  6710. chan_misdn_log(3, cl->bc->port, " --> Dial\n");
  6711. cl->ts = ast_get_indication_tone(ast_channel_zone(ast), "dial");
  6712. if (cl->ts) {
  6713. cl->notxtone = 0;
  6714. cl->norxtone = 0;
  6715. /* This prods us in misdn_write */
  6716. ast_playtones_start(ast, 0, cl->ts->data, 0);
  6717. }
  6718. return 0;
  6719. }
  6720. static void hanguptone_indicate(struct chan_list *cl)
  6721. {
  6722. misdn_lib_send_tone(cl->bc, TONE_HANGUP);
  6723. }
  6724. static int stop_indicate(struct chan_list *cl)
  6725. {
  6726. struct ast_channel *ast = cl->ast;
  6727. if (!ast) {
  6728. chan_misdn_log(0, cl->bc->port, "No Ast in stop_indicate\n");
  6729. return -1;
  6730. }
  6731. chan_misdn_log(3, cl->bc->port, " --> None\n");
  6732. misdn_lib_tone_generator_stop(cl->bc);
  6733. ast_playtones_stop(ast);
  6734. if (cl->ts) {
  6735. cl->ts = ast_tone_zone_sound_unref(cl->ts);
  6736. }
  6737. return 0;
  6738. }
  6739. static int start_bc_tones(struct chan_list* cl)
  6740. {
  6741. misdn_lib_tone_generator_stop(cl->bc);
  6742. cl->notxtone = 0;
  6743. cl->norxtone = 0;
  6744. return 0;
  6745. }
  6746. static int stop_bc_tones(struct chan_list *cl)
  6747. {
  6748. if (!cl) {
  6749. return -1;
  6750. }
  6751. cl->notxtone = 1;
  6752. cl->norxtone = 1;
  6753. return 0;
  6754. }
  6755. /*!
  6756. * \internal
  6757. * \brief Destroy the chan_list object.
  6758. *
  6759. * \param obj chan_list object to destroy.
  6760. *
  6761. * \return Nothing
  6762. */
  6763. static void chan_list_destructor(void *obj)
  6764. {
  6765. struct chan_list *ch = obj;
  6766. #if defined(AST_MISDN_ENHANCEMENTS)
  6767. if (ch->peer) {
  6768. ao2_ref(ch->peer, -1);
  6769. ch->peer = NULL;
  6770. }
  6771. #endif /* AST_MISDN_ENHANCEMENTS */
  6772. if (ch->dsp) {
  6773. ast_dsp_free(ch->dsp);
  6774. ch->dsp = NULL;
  6775. }
  6776. /* releasing jitterbuffer */
  6777. if (ch->jb) {
  6778. misdn_jb_destroy(ch->jb);
  6779. ch->jb = NULL;
  6780. }
  6781. if (ch->overlap_dial) {
  6782. if (ch->overlap_dial_task != -1) {
  6783. misdn_tasks_remove(ch->overlap_dial_task);
  6784. ch->overlap_dial_task = -1;
  6785. }
  6786. ast_mutex_destroy(&ch->overlap_tv_lock);
  6787. }
  6788. if (-1 < ch->pipe[0]) {
  6789. close(ch->pipe[0]);
  6790. }
  6791. if (-1 < ch->pipe[1]) {
  6792. close(ch->pipe[1]);
  6793. }
  6794. }
  6795. /*! Returns a reference to the new chan_list. */
  6796. static struct chan_list *chan_list_init(int orig)
  6797. {
  6798. struct chan_list *cl;
  6799. cl = ao2_alloc(sizeof(*cl), chan_list_destructor);
  6800. if (!cl) {
  6801. chan_misdn_log(-1, 0, "misdn_request: malloc failed!");
  6802. return NULL;
  6803. }
  6804. cl->originator = orig;
  6805. cl->need_queue_hangup = 1;
  6806. cl->need_hangup = 1;
  6807. cl->need_busy = 1;
  6808. cl->overlap_dial_task = -1;
  6809. #if defined(AST_MISDN_ENHANCEMENTS)
  6810. cl->record_id = -1;
  6811. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  6812. cl->pipe[0] = -1;
  6813. cl->pipe[1] = -1;
  6814. return cl;
  6815. }
  6816. static struct ast_channel *misdn_request(const char *type, struct ast_format_cap *cap, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *data, int *cause)
  6817. {
  6818. struct ast_channel *ast;
  6819. char group[BUFFERSIZE + 1] = "";
  6820. char dial_str[128];
  6821. char *dest_cp;
  6822. char *p = NULL;
  6823. int channel = 0;
  6824. int port = 0;
  6825. struct misdn_bchannel *newbc = NULL;
  6826. int dec = 0;
  6827. #if defined(AST_MISDN_ENHANCEMENTS)
  6828. int cc_retry_call = 0; /* TRUE if this is a call completion retry call */
  6829. long record_id = -1;
  6830. struct misdn_cc_record *cc_record;
  6831. const char *err_msg;
  6832. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  6833. struct chan_list *cl;
  6834. AST_DECLARE_APP_ARGS(args,
  6835. AST_APP_ARG(intf); /* interface token */
  6836. AST_APP_ARG(ext); /* extension token */
  6837. AST_APP_ARG(opts); /* options token */
  6838. );
  6839. snprintf(dial_str, sizeof(dial_str), "%s/%s", misdn_type, data);
  6840. /*
  6841. * data is ---v
  6842. * Dial(mISDN/g:group_name[/extension[/options]])
  6843. * Dial(mISDN/port[:preselected_channel][/extension[/options]])
  6844. * Dial(mISDN/cc/cc-record-id)
  6845. *
  6846. * The dial extension could be empty if you are using MISDN_KEYPAD
  6847. * to control ISDN provider features.
  6848. */
  6849. dest_cp = ast_strdupa(data);
  6850. AST_NONSTANDARD_APP_ARGS(args, dest_cp, '/');
  6851. if (!args.ext) {
  6852. args.ext = "";
  6853. }
  6854. if (!ast_strlen_zero(args.intf)) {
  6855. if (args.intf[0] == 'g' && args.intf[1] == ':') {
  6856. /* We make a group call lets checkout which ports are in my group */
  6857. args.intf += 2;
  6858. ast_copy_string(group, args.intf, sizeof(group));
  6859. chan_misdn_log(2, 0, " --> Group Call group: %s\n", group);
  6860. #if defined(AST_MISDN_ENHANCEMENTS)
  6861. } else if (strcmp(args.intf, "cc") == 0) {
  6862. cc_retry_call = 1;
  6863. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  6864. } else if ((p = strchr(args.intf, ':'))) {
  6865. /* we have a preselected channel */
  6866. *p++ = 0;
  6867. channel = atoi(p);
  6868. port = atoi(args.intf);
  6869. chan_misdn_log(2, port, " --> Call on preselected Channel (%d).\n", channel);
  6870. } else {
  6871. port = atoi(args.intf);
  6872. }
  6873. } else {
  6874. ast_log(LOG_WARNING, " --> ! IND : Dial(%s) WITHOUT Port or Group, check extensions.conf\n", dial_str);
  6875. return NULL;
  6876. }
  6877. #if defined(AST_MISDN_ENHANCEMENTS)
  6878. if (cc_retry_call) {
  6879. if (ast_strlen_zero(args.ext)) {
  6880. ast_log(LOG_WARNING, " --> ! IND : Dial(%s) WITHOUT cc-record-id, check extensions.conf\n", dial_str);
  6881. return NULL;
  6882. }
  6883. if (!isdigit(*args.ext)) {
  6884. ast_log(LOG_WARNING, " --> ! IND : Dial(%s) cc-record-id must be a number.\n", dial_str);
  6885. return NULL;
  6886. }
  6887. record_id = atol(args.ext);
  6888. AST_LIST_LOCK(&misdn_cc_records_db);
  6889. cc_record = misdn_cc_find_by_id(record_id);
  6890. if (!cc_record) {
  6891. AST_LIST_UNLOCK(&misdn_cc_records_db);
  6892. err_msg = misdn_cc_record_not_found;
  6893. ast_log(LOG_WARNING, " --> ! IND : Dial(%s) %s.\n", dial_str, err_msg);
  6894. return NULL;
  6895. }
  6896. if (!cc_record->activated) {
  6897. AST_LIST_UNLOCK(&misdn_cc_records_db);
  6898. err_msg = "Call completion has not been activated";
  6899. ast_log(LOG_WARNING, " --> ! IND : Dial(%s) %s.\n", dial_str, err_msg);
  6900. return NULL;
  6901. }
  6902. port = cc_record->port;
  6903. AST_LIST_UNLOCK(&misdn_cc_records_db);
  6904. }
  6905. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  6906. if (misdn_cfg_is_group_method(group, METHOD_STANDARD_DEC)) {
  6907. chan_misdn_log(4, port, " --> STARTING STANDARD DEC...\n");
  6908. dec = 1;
  6909. }
  6910. if (!ast_strlen_zero(group)) {
  6911. char cfg_group[BUFFERSIZE + 1];
  6912. struct robin_list *rr = NULL;
  6913. /* Group dial */
  6914. if (misdn_cfg_is_group_method(group, METHOD_ROUND_ROBIN)) {
  6915. chan_misdn_log(4, port, " --> STARTING ROUND ROBIN...\n");
  6916. rr = get_robin_position(group);
  6917. }
  6918. if (rr) {
  6919. int port_start;
  6920. int bchan_start;
  6921. int port_up;
  6922. int check;
  6923. int maxbchans;
  6924. int wraped = 0;
  6925. if (!rr->port) {
  6926. rr->port = misdn_cfg_get_next_port_spin(0);
  6927. }
  6928. if (!rr->channel) {
  6929. rr->channel = 1;
  6930. }
  6931. bchan_start = rr->channel;
  6932. port_start = rr->port;
  6933. do {
  6934. misdn_cfg_get(rr->port, MISDN_CFG_GROUPNAME, cfg_group, sizeof(cfg_group));
  6935. if (strcasecmp(cfg_group, group)) {
  6936. wraped = 1;
  6937. rr->port = misdn_cfg_get_next_port_spin(rr->port);
  6938. rr->channel = 1;
  6939. continue;
  6940. }
  6941. misdn_cfg_get(rr->port, MISDN_CFG_PMP_L1_CHECK, &check, sizeof(check));
  6942. port_up = misdn_lib_port_up(rr->port, check);
  6943. if (!port_up) {
  6944. chan_misdn_log(1, rr->port, "L1 is not Up on this Port\n");
  6945. rr->port = misdn_cfg_get_next_port_spin(rr->port);
  6946. rr->channel = 1;
  6947. } else if (port_up < 0) {
  6948. ast_log(LOG_WARNING, "This port (%d) is blocked\n", rr->port);
  6949. rr->port = misdn_cfg_get_next_port_spin(rr->port);
  6950. rr->channel = 1;
  6951. } else {
  6952. chan_misdn_log(4, rr->port, "portup\n");
  6953. maxbchans = misdn_lib_get_maxchans(rr->port);
  6954. for (;rr->channel <= maxbchans;rr->channel++) {
  6955. /* ive come full circle and can stop now */
  6956. if (wraped && (rr->port == port_start) && (rr->channel == bchan_start)) {
  6957. break;
  6958. }
  6959. chan_misdn_log(4, rr->port, "Checking channel %d\n", rr->channel);
  6960. if ((newbc = misdn_lib_get_free_bc(rr->port, rr->channel, 0, 0))) {
  6961. chan_misdn_log(4, rr->port, " Success! Found port:%d channel:%d\n", newbc->port, newbc->channel);
  6962. rr->channel++;
  6963. break;
  6964. }
  6965. }
  6966. if (wraped && (rr->port == port_start) && (rr->channel <= bchan_start)) {
  6967. break;
  6968. } else if (!newbc || (rr->channel == maxbchans)) {
  6969. rr->port = misdn_cfg_get_next_port_spin(rr->port);
  6970. rr->channel = 1;
  6971. }
  6972. }
  6973. wraped = 1;
  6974. } while (!newbc && (rr->port > 0));
  6975. } else {
  6976. for (port = misdn_cfg_get_next_port(0); port > 0;
  6977. port = misdn_cfg_get_next_port(port)) {
  6978. misdn_cfg_get(port, MISDN_CFG_GROUPNAME, cfg_group, sizeof(cfg_group));
  6979. chan_misdn_log(3, port, "Group [%s] Port [%d]\n", group, port);
  6980. if (!strcasecmp(cfg_group, group)) {
  6981. int port_up;
  6982. int check;
  6983. misdn_cfg_get(port, MISDN_CFG_PMP_L1_CHECK, &check, sizeof(check));
  6984. port_up = misdn_lib_port_up(port, check);
  6985. chan_misdn_log(4, port, "portup:%d\n", port_up);
  6986. if (port_up > 0) {
  6987. newbc = misdn_lib_get_free_bc(port, 0, 0, dec);
  6988. if (newbc) {
  6989. break;
  6990. }
  6991. }
  6992. }
  6993. }
  6994. }
  6995. /* Group dial failed ?*/
  6996. if (!newbc) {
  6997. ast_log(LOG_WARNING,
  6998. "Could not Dial out on group '%s'.\n"
  6999. "\tEither the L2 and L1 on all of these ports where DOWN (see 'show application misdn_check_l2l1')\n"
  7000. "\tOr there was no free channel on none of the ports\n\n",
  7001. group);
  7002. return NULL;
  7003. }
  7004. } else {
  7005. /* 'Normal' Port dial * Port dial */
  7006. if (channel) {
  7007. chan_misdn_log(1, port, " --> preselected_channel: %d\n", channel);
  7008. }
  7009. newbc = misdn_lib_get_free_bc(port, channel, 0, dec);
  7010. if (!newbc) {
  7011. ast_log(LOG_WARNING, "Could not create channel on port:%d for Dial(%s)\n", port, dial_str);
  7012. return NULL;
  7013. }
  7014. }
  7015. /* create ast_channel and link all the objects together */
  7016. cl = chan_list_init(ORG_AST);
  7017. if (!cl) {
  7018. misdn_lib_release(newbc);
  7019. ast_log(LOG_ERROR, "Could not create call record for Dial(%s)\n", dial_str);
  7020. return NULL;
  7021. }
  7022. cl->bc = newbc;
  7023. ast = misdn_new(cl, AST_STATE_RESERVED, args.ext, NULL, cap, assignedids, requestor, port, channel);
  7024. if (!ast) {
  7025. chan_list_unref(cl, "Failed to create a new channel");
  7026. misdn_lib_release(newbc);
  7027. ast_log(LOG_ERROR, "Could not create Asterisk channel for Dial(%s)\n", dial_str);
  7028. return NULL;
  7029. }
  7030. #if defined(AST_MISDN_ENHANCEMENTS)
  7031. cl->record_id = record_id;
  7032. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  7033. /* register chan in local list */
  7034. cl_queue_chan(cl);
  7035. /* fill in the config into the objects */
  7036. read_config(cl);
  7037. /* important */
  7038. cl->need_hangup = 0;
  7039. chan_list_unref(cl, "Successful misdn_request()");
  7040. return ast;
  7041. }
  7042. static int misdn_send_text(struct ast_channel *chan, const char *text)
  7043. {
  7044. struct chan_list *tmp = MISDN_ASTERISK_TECH_PVT(chan);
  7045. if (tmp && tmp->bc) {
  7046. ast_copy_string(tmp->bc->display, text, sizeof(tmp->bc->display));
  7047. misdn_lib_send_event(tmp->bc, EVENT_INFORMATION);
  7048. } else {
  7049. ast_log(LOG_WARNING, "No chan_list but send_text request?\n");
  7050. return -1;
  7051. }
  7052. return 0;
  7053. }
  7054. static struct ast_channel_tech misdn_tech = {
  7055. .type = misdn_type,
  7056. .description = "Channel driver for mISDN Support (Bri/Pri)",
  7057. .requester = misdn_request,
  7058. .send_digit_begin = misdn_digit_begin,
  7059. .send_digit_end = misdn_digit_end,
  7060. .call = misdn_call,
  7061. .hangup = misdn_hangup,
  7062. .answer = misdn_answer,
  7063. .read = misdn_read,
  7064. .write = misdn_write,
  7065. .indicate = misdn_indication,
  7066. .fixup = misdn_fixup,
  7067. .send_text = misdn_send_text,
  7068. .properties = 0,
  7069. };
  7070. static int glob_channel = 0;
  7071. static void update_name(struct ast_channel *tmp, int port, int c)
  7072. {
  7073. int chan_offset = 0;
  7074. int tmp_port = misdn_cfg_get_next_port(0);
  7075. char newname[255];
  7076. for (; tmp_port > 0; tmp_port = misdn_cfg_get_next_port(tmp_port)) {
  7077. if (tmp_port == port) {
  7078. break;
  7079. }
  7080. chan_offset += misdn_lib_port_is_pri(tmp_port) ? 30 : 2;
  7081. }
  7082. if (c < 0) {
  7083. c = 0;
  7084. }
  7085. snprintf(newname, sizeof(newname), "%s/%d-", misdn_type, chan_offset + c);
  7086. if (strncmp(ast_channel_name(tmp), newname, strlen(newname))) {
  7087. snprintf(newname, sizeof(newname), "%s/%d-u%d", misdn_type, chan_offset + c, glob_channel++);
  7088. ast_change_name(tmp, newname);
  7089. chan_misdn_log(3, port, " --> updating channel name to [%s]\n", ast_channel_name(tmp));
  7090. }
  7091. }
  7092. static struct ast_channel *misdn_new(struct chan_list *chlist, int state, char *exten, char *callerid, struct ast_format_cap *cap, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, int port, int c)
  7093. {
  7094. struct ast_format_cap *native;
  7095. struct ast_channel *tmp;
  7096. char *cid_name = NULL;
  7097. char *cid_num = NULL;
  7098. int chan_offset = 0;
  7099. int tmp_port = misdn_cfg_get_next_port(0);
  7100. struct ast_format *tmpfmt;
  7101. native = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
  7102. if (!native) {
  7103. return NULL;
  7104. }
  7105. for (; tmp_port > 0; tmp_port = misdn_cfg_get_next_port(tmp_port)) {
  7106. if (tmp_port == port) {
  7107. break;
  7108. }
  7109. chan_offset += misdn_lib_port_is_pri(tmp_port) ? 30 : 2;
  7110. }
  7111. if (c < 0) {
  7112. c = 0;
  7113. }
  7114. if (callerid) {
  7115. ast_callerid_parse(callerid, &cid_name, &cid_num);
  7116. }
  7117. tmp = ast_channel_alloc(1, state, cid_num, cid_name, "", exten, "", assignedids, requestor, 0, "%s/%s%d-u%d", misdn_type, c ? "" : "tmp", chan_offset + c, glob_channel++);
  7118. if (tmp) {
  7119. chan_misdn_log(2, port, " --> * NEW CHANNEL dialed:%s caller:%s\n", exten, callerid);
  7120. tmpfmt = ast_format_cap_get_format(cap, 0);
  7121. ast_format_cap_append(native, ast_format_alaw, 0);
  7122. ast_channel_nativeformats_set(tmp, native);
  7123. ast_channel_set_writeformat(tmp, tmpfmt);
  7124. ast_channel_set_rawwriteformat(tmp, tmpfmt);
  7125. ast_channel_set_readformat(tmp, tmpfmt);
  7126. ast_channel_set_rawreadformat(tmp, tmpfmt);
  7127. ao2_ref(tmpfmt, -1);
  7128. /* Link the channel and private together */
  7129. chan_list_ref(chlist, "Give a reference to ast_channel");
  7130. MISDN_ASTERISK_TECH_PVT_SET(tmp, chlist);
  7131. chlist->ast = tmp;
  7132. ast_channel_tech_set(tmp, &misdn_tech);
  7133. ast_channel_priority_set(tmp, 1);
  7134. if (exten) {
  7135. ast_channel_exten_set(tmp, exten);
  7136. } else {
  7137. chan_misdn_log(1, 0, "misdn_new: no exten given.\n");
  7138. }
  7139. if (!ast_strlen_zero(cid_num)) {
  7140. /* Don't use ast_set_callerid() here because it will
  7141. * generate a needless NewCallerID event */
  7142. ast_channel_caller(tmp)->ani.number.valid = 1;
  7143. ast_channel_caller(tmp)->ani.number.str = ast_strdup(cid_num);
  7144. }
  7145. if (pipe(chlist->pipe) < 0) {
  7146. ast_log(LOG_ERROR, "Pipe failed\n");
  7147. }
  7148. ast_channel_set_fd(tmp, 0, chlist->pipe[0]);
  7149. ast_channel_rings_set(tmp, (state == AST_STATE_RING) ? 1 : 0);
  7150. ast_jb_configure(tmp, misdn_get_global_jbconf());
  7151. ast_channel_unlock(tmp);
  7152. } else {
  7153. chan_misdn_log(-1, 0, "Unable to allocate channel structure\n");
  7154. }
  7155. ao2_ref(native, -1);
  7156. return tmp;
  7157. }
  7158. /*! Returns a reference to the found chan_list. */
  7159. static struct chan_list *find_chan_by_bc(struct misdn_bchannel *bc)
  7160. {
  7161. struct chan_list *help;
  7162. ast_mutex_lock(&cl_te_lock);
  7163. for (help = cl_te; help; help = help->next) {
  7164. if (help->bc == bc) {
  7165. chan_list_ref(help, "Found chan_list by bc");
  7166. ast_mutex_unlock(&cl_te_lock);
  7167. return help;
  7168. }
  7169. }
  7170. ast_mutex_unlock(&cl_te_lock);
  7171. chan_misdn_log(6, bc->port,
  7172. "$$$ find_chan_by_bc: No channel found for dialed:%s caller:\"%s\" <%s>\n",
  7173. bc->dialed.number,
  7174. bc->caller.name,
  7175. bc->caller.number);
  7176. return NULL;
  7177. }
  7178. /*! Returns a reference to the found chan_list. */
  7179. static struct chan_list *find_hold_call(struct misdn_bchannel *bc)
  7180. {
  7181. struct chan_list *help;
  7182. if (bc->pri) {
  7183. return NULL;
  7184. }
  7185. chan_misdn_log(6, bc->port, "$$$ find_hold_call: channel:%d dialed:%s caller:\"%s\" <%s>\n",
  7186. bc->channel,
  7187. bc->dialed.number,
  7188. bc->caller.name,
  7189. bc->caller.number);
  7190. ast_mutex_lock(&cl_te_lock);
  7191. for (help = cl_te; help; help = help->next) {
  7192. chan_misdn_log(4, bc->port, "$$$ find_hold_call: --> hold:%d channel:%d\n", help->hold.state, help->hold.channel);
  7193. if (help->hold.state == MISDN_HOLD_ACTIVE && help->hold.port == bc->port) {
  7194. chan_list_ref(help, "Found chan_list hold call");
  7195. ast_mutex_unlock(&cl_te_lock);
  7196. return help;
  7197. }
  7198. }
  7199. ast_mutex_unlock(&cl_te_lock);
  7200. chan_misdn_log(6, bc->port,
  7201. "$$$ find_hold_call: No channel found for dialed:%s caller:\"%s\" <%s>\n",
  7202. bc->dialed.number,
  7203. bc->caller.name,
  7204. bc->caller.number);
  7205. return NULL;
  7206. }
  7207. /*! Returns a reference to the found chan_list. */
  7208. static struct chan_list *find_hold_call_l3(unsigned long l3_id)
  7209. {
  7210. struct chan_list *help;
  7211. ast_mutex_lock(&cl_te_lock);
  7212. for (help = cl_te; help; help = help->next) {
  7213. if (help->hold.state != MISDN_HOLD_IDLE && help->l3id == l3_id) {
  7214. chan_list_ref(help, "Found chan_list hold call l3");
  7215. ast_mutex_unlock(&cl_te_lock);
  7216. return help;
  7217. }
  7218. }
  7219. ast_mutex_unlock(&cl_te_lock);
  7220. return NULL;
  7221. }
  7222. #define TRANSFER_ON_HELD_CALL_HANGUP 1
  7223. #if defined(TRANSFER_ON_HELD_CALL_HANGUP)
  7224. /*!
  7225. * \internal
  7226. * \brief Find a suitable active call to go with a held call so we could try a transfer.
  7227. *
  7228. * \param bc B channel record.
  7229. *
  7230. * \return Found call record or NULL.
  7231. *
  7232. * \note Returns a reference to the found chan_list.
  7233. *
  7234. * \note There could be a possibility where we find the wrong active call to transfer.
  7235. * This concern is mitigated by the fact that there could be at most one other call
  7236. * on a PTMP BRI link to another device. Maybe the l3_id could help in locating an
  7237. * active call on the same TEI?
  7238. */
  7239. static struct chan_list *find_hold_active_call(struct misdn_bchannel *bc)
  7240. {
  7241. struct chan_list *list;
  7242. ast_mutex_lock(&cl_te_lock);
  7243. for (list = cl_te; list; list = list->next) {
  7244. if (list->hold.state == MISDN_HOLD_IDLE && list->bc && list->bc->port == bc->port
  7245. && list->ast) {
  7246. switch (list->state) {
  7247. case MISDN_PROCEEDING:
  7248. case MISDN_PROGRESS:
  7249. case MISDN_ALERTING:
  7250. case MISDN_CONNECTED:
  7251. chan_list_ref(list, "Found chan_list hold active call");
  7252. ast_mutex_unlock(&cl_te_lock);
  7253. return list;
  7254. default:
  7255. break;
  7256. }
  7257. }
  7258. }
  7259. ast_mutex_unlock(&cl_te_lock);
  7260. return NULL;
  7261. }
  7262. #endif /* defined(TRANSFER_ON_HELD_CALL_HANGUP) */
  7263. static void cl_queue_chan(struct chan_list *chan)
  7264. {
  7265. chan_misdn_log(4, chan->bc ? chan->bc->port : 0, "* Queuing chan %p\n", chan);
  7266. chan_list_ref(chan, "Adding chan_list to list");
  7267. ast_mutex_lock(&cl_te_lock);
  7268. chan->next = NULL;
  7269. if (!cl_te) {
  7270. /* List is empty, make head of list. */
  7271. cl_te = chan;
  7272. } else {
  7273. struct chan_list *help;
  7274. /* Put at end of list. */
  7275. for (help = cl_te; help->next; help = help->next) {
  7276. }
  7277. help->next = chan;
  7278. }
  7279. ast_mutex_unlock(&cl_te_lock);
  7280. }
  7281. static int cl_dequeue_chan(struct chan_list *chan)
  7282. {
  7283. int found_it;
  7284. struct chan_list *help;
  7285. ast_mutex_lock(&cl_te_lock);
  7286. if (!cl_te) {
  7287. /* List is empty. */
  7288. ast_mutex_unlock(&cl_te_lock);
  7289. return 0;
  7290. }
  7291. if (cl_te == chan) {
  7292. /* What we want is the head of the list. */
  7293. cl_te = cl_te->next;
  7294. ast_mutex_unlock(&cl_te_lock);
  7295. chan_list_unref(chan, "Removed chan_list from list head");
  7296. return 1;
  7297. }
  7298. found_it = 0;
  7299. for (help = cl_te; help->next; help = help->next) {
  7300. if (help->next == chan) {
  7301. /* Found it in the list. */
  7302. help->next = help->next->next;
  7303. found_it = 1;
  7304. break;
  7305. }
  7306. }
  7307. ast_mutex_unlock(&cl_te_lock);
  7308. if (found_it) {
  7309. chan_list_unref(chan, "Removed chan_list from list");
  7310. }
  7311. return found_it;
  7312. }
  7313. /** Channel Queue End **/
  7314. static int pbx_start_chan(struct chan_list *ch)
  7315. {
  7316. int ret = ast_pbx_start(ch->ast);
  7317. ch->need_hangup = (ret >= 0) ? 0 : 1;
  7318. return ret;
  7319. }
  7320. static void hangup_chan(struct chan_list *ch, struct misdn_bchannel *bc)
  7321. {
  7322. int port = bc->port;
  7323. if (!ch) {
  7324. cb_log(1, port, "Cannot hangup chan, no ch\n");
  7325. return;
  7326. }
  7327. cb_log(5, port, "hangup_chan called\n");
  7328. if (ch->need_hangup) {
  7329. cb_log(2, port, " --> hangup\n");
  7330. ch->need_hangup = 0;
  7331. ch->need_queue_hangup = 0;
  7332. if (ch->ast && send_cause2ast(ch->ast, bc, ch)) {
  7333. ast_hangup(ch->ast);
  7334. }
  7335. return;
  7336. }
  7337. if (!ch->need_queue_hangup) {
  7338. cb_log(2, port, " --> No need to queue hangup\n");
  7339. return;
  7340. }
  7341. ch->need_queue_hangup = 0;
  7342. if (ch->ast) {
  7343. if (send_cause2ast(ch->ast, bc, ch)) {
  7344. ast_queue_hangup_with_cause(ch->ast, bc->cause);
  7345. cb_log(2, port, " --> queue_hangup\n");
  7346. }
  7347. } else {
  7348. cb_log(1, port, "Cannot hangup chan, no ast\n");
  7349. }
  7350. }
  7351. /*!
  7352. * \internal
  7353. * \brief ISDN asked us to release channel, pendant to misdn_hangup.
  7354. *
  7355. * \param ch Call channel record to release.
  7356. * \param bc Current B channel record associated with ch.
  7357. *
  7358. * \return Nothing
  7359. *
  7360. * \note The only valid thing to do with ch after calling is to chan_list_unref(ch, "").
  7361. */
  7362. static void release_chan(struct chan_list *ch, struct misdn_bchannel *bc)
  7363. {
  7364. struct ast_channel *ast;
  7365. chan_misdn_log(5, bc->port, "release_chan: bc with pid:%d l3id: %x\n", bc->pid, bc->l3_id);
  7366. ast_mutex_lock(&release_lock);
  7367. for (;;) {
  7368. ast = ch->ast;
  7369. if (!ast || !ast_channel_trylock(ast)) {
  7370. break;
  7371. }
  7372. DEADLOCK_AVOIDANCE(&release_lock);
  7373. }
  7374. if (!cl_dequeue_chan(ch)) {
  7375. /* Someone already released it. */
  7376. if (ast) {
  7377. ast_channel_unlock(ast);
  7378. }
  7379. ast_mutex_unlock(&release_lock);
  7380. return;
  7381. }
  7382. ch->state = MISDN_CLEANING;
  7383. ch->ast = NULL;
  7384. if (ast) {
  7385. struct chan_list *ast_ch;
  7386. ast_ch = MISDN_ASTERISK_TECH_PVT(ast);
  7387. MISDN_ASTERISK_TECH_PVT_SET(ast, NULL);
  7388. chan_misdn_log(1, bc->port,
  7389. "* RELEASING CHANNEL pid:%d context:%s dialed:%s caller:\"%s\" <%s>\n",
  7390. bc->pid,
  7391. ast_channel_context(ast),
  7392. ast_channel_exten(ast),
  7393. S_COR(ast_channel_caller(ast)->id.name.valid, ast_channel_caller(ast)->id.name.str, ""),
  7394. S_COR(ast_channel_caller(ast)->id.number.valid, ast_channel_caller(ast)->id.number.str, ""));
  7395. if (ast_channel_state(ast) != AST_STATE_RESERVED) {
  7396. chan_misdn_log(3, bc->port, " --> Setting AST State to down\n");
  7397. ast_setstate(ast, AST_STATE_DOWN);
  7398. }
  7399. ast_channel_unlock(ast);
  7400. if (ast_ch) {
  7401. chan_list_unref(ast_ch, "Release ast_channel reference.");
  7402. }
  7403. }
  7404. if (ch->originator == ORG_AST) {
  7405. --misdn_out_calls[bc->port];
  7406. } else {
  7407. --misdn_in_calls[bc->port];
  7408. }
  7409. ast_mutex_unlock(&release_lock);
  7410. }
  7411. /*!
  7412. * \internal
  7413. * \brief Do everything in release_chan() that makes sense without a bc.
  7414. *
  7415. * \param ch Call channel record to release.
  7416. *
  7417. * \return Nothing
  7418. *
  7419. * \note The only valid thing to do with ch after calling is to chan_list_unref(ch, "").
  7420. */
  7421. static void release_chan_early(struct chan_list *ch)
  7422. {
  7423. struct ast_channel *ast;
  7424. ast_mutex_lock(&release_lock);
  7425. for (;;) {
  7426. ast = ch->ast;
  7427. if (!ast || !ast_channel_trylock(ast)) {
  7428. break;
  7429. }
  7430. DEADLOCK_AVOIDANCE(&release_lock);
  7431. }
  7432. if (!cl_dequeue_chan(ch)) {
  7433. /* Someone already released it. */
  7434. if (ast) {
  7435. ast_channel_unlock(ast);
  7436. }
  7437. ast_mutex_unlock(&release_lock);
  7438. return;
  7439. }
  7440. ch->state = MISDN_CLEANING;
  7441. ch->ast = NULL;
  7442. if (ast) {
  7443. struct chan_list *ast_ch;
  7444. ast_ch = MISDN_ASTERISK_TECH_PVT(ast);
  7445. MISDN_ASTERISK_TECH_PVT_SET(ast, NULL);
  7446. if (ast_channel_state(ast) != AST_STATE_RESERVED) {
  7447. ast_setstate(ast, AST_STATE_DOWN);
  7448. }
  7449. ast_channel_unlock(ast);
  7450. if (ast_ch) {
  7451. chan_list_unref(ast_ch, "Release ast_channel reference.");
  7452. }
  7453. }
  7454. if (ch->hold.state != MISDN_HOLD_IDLE) {
  7455. if (ch->originator == ORG_AST) {
  7456. --misdn_out_calls[ch->hold.port];
  7457. } else {
  7458. --misdn_in_calls[ch->hold.port];
  7459. }
  7460. }
  7461. ast_mutex_unlock(&release_lock);
  7462. }
  7463. /*!
  7464. * \internal
  7465. * \brief Attempt to transfer the active channel party to the held channel party.
  7466. *
  7467. * \param active_ch Channel currently connected.
  7468. * \param held_ch Channel currently on hold.
  7469. *
  7470. * \retval 0 on success.
  7471. * \retval -1 on error.
  7472. */
  7473. static int misdn_attempt_transfer(struct chan_list *active_ch, struct chan_list *held_ch)
  7474. {
  7475. int retval;
  7476. enum ast_transfer_result xfer_res;
  7477. struct ast_channel *to_target;
  7478. struct ast_channel *to_transferee;
  7479. switch (active_ch->state) {
  7480. case MISDN_PROCEEDING:
  7481. case MISDN_PROGRESS:
  7482. case MISDN_ALERTING:
  7483. case MISDN_CONNECTED:
  7484. break;
  7485. default:
  7486. return -1;
  7487. }
  7488. ast_channel_lock_both(held_ch->ast, active_ch->ast);
  7489. to_target = active_ch->ast;
  7490. to_transferee = held_ch->ast;
  7491. chan_misdn_log(1, held_ch->hold.port, "TRANSFERRING %s to %s\n",
  7492. ast_channel_name(to_transferee), ast_channel_name(to_target));
  7493. held_ch->hold.state = MISDN_HOLD_TRANSFER;
  7494. ast_channel_ref(to_target);
  7495. ast_channel_ref(to_transferee);
  7496. ast_channel_unlock(to_target);
  7497. ast_channel_unlock(to_transferee);
  7498. retval = 0;
  7499. xfer_res = ast_bridge_transfer_attended(to_transferee, to_target);
  7500. if (xfer_res != AST_BRIDGE_TRANSFER_SUCCESS) {
  7501. retval = -1;
  7502. }
  7503. ast_channel_unref(to_target);
  7504. ast_channel_unref(to_transferee);
  7505. return retval;
  7506. }
  7507. static void do_immediate_setup(struct misdn_bchannel *bc, struct chan_list *ch, struct ast_channel *ast)
  7508. {
  7509. char *predial;
  7510. struct ast_frame fr;
  7511. predial = ast_strdupa(ast_channel_exten(ast));
  7512. ch->state = MISDN_DIALING;
  7513. if (!ch->noautorespond_on_setup) {
  7514. if (bc->nt) {
  7515. misdn_lib_send_event(bc, EVENT_SETUP_ACKNOWLEDGE);
  7516. } else {
  7517. if (misdn_lib_is_ptp(bc->port)) {
  7518. misdn_lib_send_event(bc, EVENT_SETUP_ACKNOWLEDGE);
  7519. } else {
  7520. misdn_lib_send_event(bc, EVENT_PROCEEDING);
  7521. }
  7522. }
  7523. } else {
  7524. ch->state = MISDN_INCOMING_SETUP;
  7525. }
  7526. chan_misdn_log(1, bc->port,
  7527. "* Starting Ast context:%s dialed:%s caller:\"%s\" <%s> with 's' extension\n",
  7528. ast_channel_context(ast),
  7529. ast_channel_exten(ast),
  7530. (ast_channel_caller(ast)->id.name.valid && ast_channel_caller(ast)->id.name.str)
  7531. ? ast_channel_caller(ast)->id.name.str : "",
  7532. (ast_channel_caller(ast)->id.number.valid && ast_channel_caller(ast)->id.number.str)
  7533. ? ast_channel_caller(ast)->id.number.str : "");
  7534. ast_channel_exten_set(ast, "s");
  7535. if (!ast_canmatch_extension(ast, ast_channel_context(ast), ast_channel_exten(ast), 1, bc->caller.number) || pbx_start_chan(ch) < 0) {
  7536. ast = NULL;
  7537. bc->out_cause = AST_CAUSE_UNALLOCATED;
  7538. hangup_chan(ch, bc);
  7539. hanguptone_indicate(ch);
  7540. misdn_lib_send_event(bc, bc->nt ? EVENT_RELEASE_COMPLETE : EVENT_DISCONNECT);
  7541. }
  7542. while (!ast_strlen_zero(predial)) {
  7543. fr.frametype = AST_FRAME_DTMF;
  7544. fr.subclass.integer = *predial;
  7545. fr.src = NULL;
  7546. fr.data.ptr = NULL;
  7547. fr.datalen = 0;
  7548. fr.samples = 0;
  7549. fr.mallocd = 0;
  7550. fr.offset = 0;
  7551. fr.delivery = ast_tv(0,0);
  7552. if (ch->ast && MISDN_ASTERISK_TECH_PVT(ch->ast)) {
  7553. ast_queue_frame(ch->ast, &fr);
  7554. }
  7555. predial++;
  7556. }
  7557. }
  7558. /*!
  7559. * \retval -1 if can hangup after calling.
  7560. * \retval 0 if cannot hangup after calling.
  7561. */
  7562. static int send_cause2ast(struct ast_channel *ast, struct misdn_bchannel *bc, struct chan_list *ch)
  7563. {
  7564. int can_hangup;
  7565. if (!ast) {
  7566. chan_misdn_log(1, 0, "send_cause2ast: No Ast\n");
  7567. return 0;
  7568. }
  7569. if (!bc) {
  7570. chan_misdn_log(1, 0, "send_cause2ast: No BC\n");
  7571. return 0;
  7572. }
  7573. if (!ch) {
  7574. chan_misdn_log(1, 0, "send_cause2ast: No Ch\n");
  7575. return 0;
  7576. }
  7577. ast_channel_hangupcause_set(ast, bc->cause);
  7578. can_hangup = -1;
  7579. switch (bc->cause) {
  7580. case AST_CAUSE_UNALLOCATED:
  7581. case AST_CAUSE_NO_ROUTE_TRANSIT_NET:
  7582. case AST_CAUSE_NO_ROUTE_DESTINATION:
  7583. case 4: /* Send special information tone */
  7584. case AST_CAUSE_NUMBER_CHANGED:
  7585. case AST_CAUSE_DESTINATION_OUT_OF_ORDER:
  7586. /* Congestion Cases */
  7587. /*
  7588. * Not Queueing the Congestion anymore, since we want to hear
  7589. * the inband message
  7590. *
  7591. chan_misdn_log(1, bc ? bc->port : 0, " --> * SEND: Queue Congestion pid:%d\n", bc ? bc->pid : -1);
  7592. ch->state = MISDN_BUSY;
  7593. ast_queue_control(ast, AST_CONTROL_CONGESTION);
  7594. */
  7595. break;
  7596. case AST_CAUSE_CALL_REJECTED:
  7597. case AST_CAUSE_USER_BUSY:
  7598. ch->state = MISDN_BUSY;
  7599. if (!ch->need_busy) {
  7600. chan_misdn_log(1, bc ? bc->port : 0, "Queued busy already\n");
  7601. break;
  7602. }
  7603. ch->need_busy = 0;
  7604. chan_misdn_log(1, bc ? bc->port : 0, " --> * SEND: Queue Busy pid:%d\n", bc ? bc->pid : -1);
  7605. ast_queue_control(ast, AST_CONTROL_BUSY);
  7606. /* The BUSY is likely to cause a hangup or the user needs to hear it. */
  7607. can_hangup = 0;
  7608. break;
  7609. }
  7610. return can_hangup;
  7611. }
  7612. /*! \brief Import parameters from the dialplan environment variables */
  7613. void import_ch(struct ast_channel *chan, struct misdn_bchannel *bc, struct chan_list *ch)
  7614. {
  7615. const char *tmp;
  7616. ast_channel_lock(chan);
  7617. tmp = pbx_builtin_getvar_helper(chan, "MISDN_ADDRESS_COMPLETE");
  7618. if (tmp && (atoi(tmp) == 1)) {
  7619. bc->sending_complete = 1;
  7620. }
  7621. tmp = pbx_builtin_getvar_helper(chan, "MISDN_USERUSER");
  7622. if (tmp) {
  7623. ast_log(LOG_NOTICE, "MISDN_USERUSER: %s\n", tmp);
  7624. ast_copy_string(bc->uu, tmp, sizeof(bc->uu));
  7625. bc->uulen = strlen(bc->uu);
  7626. }
  7627. tmp = pbx_builtin_getvar_helper(chan, "MISDN_KEYPAD");
  7628. if (tmp) {
  7629. ast_copy_string(bc->keypad, tmp, sizeof(bc->keypad));
  7630. }
  7631. ast_channel_unlock(chan);
  7632. }
  7633. /*! \brief Export parameters to the dialplan environment variables */
  7634. void export_ch(struct ast_channel *chan, struct misdn_bchannel *bc, struct chan_list *ch)
  7635. {
  7636. char tmp[32];
  7637. /*
  7638. * The only use for MISDN_PID is if there is a problem and you
  7639. * have to use the "misdn restart pid" CLI command. Otherwise,
  7640. * the pid is not used by anyone. The internal use of MISDN_PID
  7641. * has been deleted.
  7642. */
  7643. chan_misdn_log(3, bc->port, " --> EXPORT_PID: pid:%d\n", bc->pid);
  7644. snprintf(tmp, sizeof(tmp), "%d", bc->pid);
  7645. pbx_builtin_setvar_helper(chan, "_MISDN_PID", tmp);
  7646. if (bc->sending_complete) {
  7647. snprintf(tmp, sizeof(tmp), "%d", bc->sending_complete);
  7648. pbx_builtin_setvar_helper(chan, "MISDN_ADDRESS_COMPLETE", tmp);
  7649. }
  7650. if (bc->urate) {
  7651. snprintf(tmp, sizeof(tmp), "%d", bc->urate);
  7652. pbx_builtin_setvar_helper(chan, "MISDN_URATE", tmp);
  7653. }
  7654. if (bc->uulen) {
  7655. pbx_builtin_setvar_helper(chan, "MISDN_USERUSER", bc->uu);
  7656. }
  7657. if (!ast_strlen_zero(bc->keypad)) {
  7658. pbx_builtin_setvar_helper(chan, "MISDN_KEYPAD", bc->keypad);
  7659. }
  7660. }
  7661. int add_in_calls(int port)
  7662. {
  7663. int max_in_calls;
  7664. misdn_cfg_get(port, MISDN_CFG_MAX_IN, &max_in_calls, sizeof(max_in_calls));
  7665. misdn_in_calls[port]++;
  7666. if (max_in_calls >= 0 && max_in_calls < misdn_in_calls[port]) {
  7667. ast_log(LOG_NOTICE, "Marking Incoming Call on port[%d]\n", port);
  7668. return misdn_in_calls[port] - max_in_calls;
  7669. }
  7670. return 0;
  7671. }
  7672. int add_out_calls(int port)
  7673. {
  7674. int max_out_calls;
  7675. misdn_cfg_get(port, MISDN_CFG_MAX_OUT, &max_out_calls, sizeof(max_out_calls));
  7676. if (max_out_calls >= 0 && max_out_calls <= misdn_out_calls[port]) {
  7677. ast_log(LOG_NOTICE, "Rejecting Outgoing Call on port[%d]\n", port);
  7678. return (misdn_out_calls[port] + 1) - max_out_calls;
  7679. }
  7680. misdn_out_calls[port]++;
  7681. return 0;
  7682. }
  7683. static void start_pbx(struct chan_list *ch, struct misdn_bchannel *bc, struct ast_channel *chan)
  7684. {
  7685. if (pbx_start_chan(ch) < 0) {
  7686. hangup_chan(ch, bc);
  7687. chan_misdn_log(-1, bc->port, "ast_pbx_start returned <0 in SETUP\n");
  7688. if (bc->nt) {
  7689. hanguptone_indicate(ch);
  7690. misdn_lib_send_event(bc, EVENT_RELEASE_COMPLETE);
  7691. } else {
  7692. misdn_lib_send_event(bc, EVENT_RELEASE);
  7693. }
  7694. }
  7695. }
  7696. static void wait_for_digits(struct chan_list *ch, struct misdn_bchannel *bc, struct ast_channel *chan)
  7697. {
  7698. ch->state = MISDN_WAITING4DIGS;
  7699. misdn_lib_send_event(bc, EVENT_SETUP_ACKNOWLEDGE);
  7700. if (bc->nt && !bc->dialed.number[0]) {
  7701. dialtone_indicate(ch);
  7702. }
  7703. }
  7704. #if defined(AST_MISDN_ENHANCEMENTS)
  7705. /*!
  7706. * \internal
  7707. * \brief Handle the FACILITY CCBSStatusRequest message.
  7708. *
  7709. * \param port Logical port number.
  7710. * \param facility Facility ie contents.
  7711. *
  7712. * \return Nothing
  7713. */
  7714. static void misdn_cc_handle_ccbs_status_request(int port, const struct FacParm *facility)
  7715. {
  7716. struct misdn_cc_record *cc_record;
  7717. struct misdn_bchannel dummy;
  7718. switch (facility->u.CCBSStatusRequest.ComponentType) {
  7719. case FacComponent_Invoke:
  7720. /* Build message */
  7721. misdn_make_dummy(&dummy, port, 0, misdn_lib_port_is_nt(port), 0);
  7722. dummy.fac_out.Function = Fac_CCBSStatusRequest;
  7723. dummy.fac_out.u.CCBSStatusRequest.InvokeID = facility->u.CCBSStatusRequest.InvokeID;
  7724. dummy.fac_out.u.CCBSStatusRequest.ComponentType = FacComponent_Result;
  7725. /* Answer User-A free question */
  7726. AST_LIST_LOCK(&misdn_cc_records_db);
  7727. cc_record = misdn_cc_find_by_reference(port, facility->u.CCBSStatusRequest.Component.Invoke.CCBSReference);
  7728. if (cc_record) {
  7729. dummy.fac_out.u.CCBSStatusRequest.Component.Result.Free = cc_record->party_a_free;
  7730. } else {
  7731. /* No record so say User-A is free */
  7732. dummy.fac_out.u.CCBSStatusRequest.Component.Result.Free = 1;
  7733. }
  7734. AST_LIST_UNLOCK(&misdn_cc_records_db);
  7735. /* Send message */
  7736. print_facility(&dummy.fac_out, &dummy);
  7737. misdn_lib_send_event(&dummy, EVENT_FACILITY);
  7738. break;
  7739. default:
  7740. chan_misdn_log(0, port, " --> not yet handled: facility type:0x%04X\n", facility->Function);
  7741. break;
  7742. }
  7743. }
  7744. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  7745. #if defined(AST_MISDN_ENHANCEMENTS)
  7746. /*!
  7747. * \internal
  7748. * \brief Start a PBX to notify that User-B is available.
  7749. *
  7750. * \param record_id Call completion record ID
  7751. * \param notify Dialplan location to start processing.
  7752. *
  7753. * \return Nothing
  7754. */
  7755. static void misdn_cc_pbx_notify(long record_id, const struct misdn_cc_notify *notify)
  7756. {
  7757. struct ast_channel *chan;
  7758. char id_str[32];
  7759. static unsigned short sequence = 0;
  7760. /* Create a channel to notify with */
  7761. snprintf(id_str, sizeof(id_str), "%ld", record_id);
  7762. chan = ast_channel_alloc(0, AST_STATE_DOWN, id_str, NULL, NULL,
  7763. notify->exten, notify->context, NULL, 0,
  7764. "mISDN-CC/%ld-%X", record_id, (unsigned) ++sequence);
  7765. if (!chan) {
  7766. ast_log(LOG_ERROR, "Unable to allocate channel!\n");
  7767. return;
  7768. }
  7769. ast_channel_priority_set(chan, notify->priority);
  7770. ast_free(ast_channel_dialed(chan)->number.str);
  7771. ast_channel_dialed(chan)->number.str = ast_strdup(notify->exten);
  7772. ast_channel_unlock(chan);
  7773. if (ast_pbx_start(chan)) {
  7774. ast_log(LOG_WARNING, "Unable to start pbx channel %s!\n", ast_channel_name(chan));
  7775. ast_channel_release(chan);
  7776. } else {
  7777. ast_verb(1, "Started pbx for call completion notify channel %s\n", ast_channel_name(chan));
  7778. }
  7779. }
  7780. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  7781. #if defined(AST_MISDN_ENHANCEMENTS)
  7782. /*!
  7783. * \internal
  7784. * \brief Handle the FACILITY CCBS_T_RemoteUserFree message.
  7785. *
  7786. * \param bc B channel control structure message came in on
  7787. *
  7788. * \return Nothing
  7789. */
  7790. static void misdn_cc_handle_T_remote_user_free(struct misdn_bchannel *bc)
  7791. {
  7792. struct misdn_cc_record *cc_record;
  7793. struct misdn_cc_notify notify;
  7794. long record_id;
  7795. AST_LIST_LOCK(&misdn_cc_records_db);
  7796. cc_record = misdn_cc_find_by_bc(bc);
  7797. if (cc_record) {
  7798. if (cc_record->party_a_free) {
  7799. notify = cc_record->remote_user_free;
  7800. } else {
  7801. /* Send CCBS_T_Suspend message */
  7802. bc->fac_out.Function = Fac_CCBS_T_Suspend;
  7803. bc->fac_out.u.CCBS_T_Suspend.InvokeID = ++misdn_invoke_id;
  7804. print_facility(&bc->fac_out, bc);
  7805. misdn_lib_send_event(bc, EVENT_FACILITY);
  7806. notify = cc_record->b_free;
  7807. }
  7808. record_id = cc_record->record_id;
  7809. AST_LIST_UNLOCK(&misdn_cc_records_db);
  7810. if (notify.context[0]) {
  7811. /* Party A is free or B-Free notify has been setup. */
  7812. misdn_cc_pbx_notify(record_id, &notify);
  7813. }
  7814. } else {
  7815. AST_LIST_UNLOCK(&misdn_cc_records_db);
  7816. }
  7817. }
  7818. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  7819. #if defined(AST_MISDN_ENHANCEMENTS)
  7820. /*!
  7821. * \internal
  7822. * \brief Handle the FACILITY CCBSRemoteUserFree message.
  7823. *
  7824. * \param port Logical port number.
  7825. * \param facility Facility ie contents.
  7826. *
  7827. * \return Nothing
  7828. */
  7829. static void misdn_cc_handle_remote_user_free(int port, const struct FacParm *facility)
  7830. {
  7831. struct misdn_cc_record *cc_record;
  7832. struct misdn_cc_notify notify;
  7833. long record_id;
  7834. AST_LIST_LOCK(&misdn_cc_records_db);
  7835. cc_record = misdn_cc_find_by_reference(port, facility->u.CCBSRemoteUserFree.CCBSReference);
  7836. if (cc_record) {
  7837. notify = cc_record->remote_user_free;
  7838. record_id = cc_record->record_id;
  7839. AST_LIST_UNLOCK(&misdn_cc_records_db);
  7840. misdn_cc_pbx_notify(record_id, &notify);
  7841. } else {
  7842. AST_LIST_UNLOCK(&misdn_cc_records_db);
  7843. }
  7844. }
  7845. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  7846. #if defined(AST_MISDN_ENHANCEMENTS)
  7847. /*!
  7848. * \internal
  7849. * \brief Handle the FACILITY CCBSBFree message.
  7850. *
  7851. * \param port Logical port number.
  7852. * \param facility Facility ie contents.
  7853. *
  7854. * \return Nothing
  7855. */
  7856. static void misdn_cc_handle_b_free(int port, const struct FacParm *facility)
  7857. {
  7858. struct misdn_cc_record *cc_record;
  7859. struct misdn_cc_notify notify;
  7860. long record_id;
  7861. AST_LIST_LOCK(&misdn_cc_records_db);
  7862. cc_record = misdn_cc_find_by_reference(port, facility->u.CCBSBFree.CCBSReference);
  7863. if (cc_record && cc_record->b_free.context[0]) {
  7864. /* B-Free notify has been setup. */
  7865. notify = cc_record->b_free;
  7866. record_id = cc_record->record_id;
  7867. AST_LIST_UNLOCK(&misdn_cc_records_db);
  7868. misdn_cc_pbx_notify(record_id, &notify);
  7869. } else {
  7870. AST_LIST_UNLOCK(&misdn_cc_records_db);
  7871. }
  7872. }
  7873. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  7874. /*!
  7875. * \internal
  7876. * \brief Handle the incoming facility ie contents
  7877. *
  7878. * \param event Message type facility ie came in on
  7879. * \param bc B channel control structure message came in on
  7880. * \param ch Associated channel call record
  7881. *
  7882. * \return Nothing
  7883. */
  7884. static void misdn_facility_ie_handler(enum event_e event, struct misdn_bchannel *bc, struct chan_list *ch)
  7885. {
  7886. #if defined(AST_MISDN_ENHANCEMENTS)
  7887. const char *diagnostic_msg;
  7888. struct misdn_cc_record *cc_record;
  7889. char buf[32];
  7890. struct misdn_party_id party_id;
  7891. long new_record_id;
  7892. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  7893. print_facility(&bc->fac_in, bc);
  7894. switch (bc->fac_in.Function) {
  7895. #if defined(AST_MISDN_ENHANCEMENTS)
  7896. case Fac_ActivationDiversion:
  7897. switch (bc->fac_in.u.ActivationDiversion.ComponentType) {
  7898. case FacComponent_Result:
  7899. /* Positive ACK to activation */
  7900. /* We don't handle this yet */
  7901. break;
  7902. default:
  7903. chan_misdn_log(0, bc->port," --> not yet handled: facility type:0x%04X\n",
  7904. bc->fac_in.Function);
  7905. break;
  7906. }
  7907. break;
  7908. case Fac_DeactivationDiversion:
  7909. switch (bc->fac_in.u.DeactivationDiversion.ComponentType) {
  7910. case FacComponent_Result:
  7911. /* Positive ACK to deactivation */
  7912. /* We don't handle this yet */
  7913. break;
  7914. default:
  7915. chan_misdn_log(0, bc->port," --> not yet handled: facility type:0x%04X\n",
  7916. bc->fac_in.Function);
  7917. break;
  7918. }
  7919. break;
  7920. case Fac_ActivationStatusNotificationDiv:
  7921. /* Sent to other MSN numbers on the line when a user activates call forwarding. */
  7922. /* Sent in the first call control message of an outgoing call from the served user. */
  7923. /* We do not have anything to do for this message. */
  7924. break;
  7925. case Fac_DeactivationStatusNotificationDiv:
  7926. /* Sent to other MSN numbers on the line when a user deactivates call forwarding. */
  7927. /* We do not have anything to do for this message. */
  7928. break;
  7929. #if 0 /* We don't handle this yet */
  7930. case Fac_InterrogationDiversion:
  7931. /* We don't handle this yet */
  7932. break;
  7933. case Fac_InterrogateServedUserNumbers:
  7934. /* We don't handle this yet */
  7935. break;
  7936. #endif /* We don't handle this yet */
  7937. case Fac_DiversionInformation:
  7938. /* Sent to the served user when a call is forwarded. */
  7939. /* We do not have anything to do for this message. */
  7940. break;
  7941. case Fac_CallDeflection:
  7942. if (ch && ch->ast) {
  7943. switch (bc->fac_in.u.CallDeflection.ComponentType) {
  7944. case FacComponent_Invoke:
  7945. ast_copy_string(bc->redirecting.from.number, bc->dialed.number,
  7946. sizeof(bc->redirecting.from.number));
  7947. bc->redirecting.from.name[0] = 0;
  7948. bc->redirecting.from.number_plan = bc->dialed.number_plan;
  7949. bc->redirecting.from.number_type = bc->dialed.number_type;
  7950. bc->redirecting.from.screening = 0;/* Unscreened */
  7951. if (bc->fac_in.u.CallDeflection.Component.Invoke.PresentationAllowedToDivertedToUserPresent) {
  7952. bc->redirecting.from.presentation =
  7953. bc->fac_in.u.CallDeflection.Component.Invoke.PresentationAllowedToDivertedToUser
  7954. ? 0 /* Allowed */ : 1 /* Restricted */;
  7955. } else {
  7956. bc->redirecting.from.presentation = 0;/* Allowed */
  7957. }
  7958. /* Add configured prefix to the call deflection number */
  7959. memset(&party_id, 0, sizeof(party_id));
  7960. misdn_PartyNumber_extract(&party_id,
  7961. &bc->fac_in.u.CallDeflection.Component.Invoke.Deflection.Party);
  7962. misdn_add_number_prefix(bc->port, party_id.number_type,
  7963. party_id.number, sizeof(party_id.number));
  7964. //party_id.presentation = 0;/* Allowed */
  7965. //party_id.screening = 0;/* Unscreened */
  7966. bc->redirecting.to = party_id;
  7967. ++bc->redirecting.count;
  7968. bc->redirecting.reason = mISDN_REDIRECTING_REASON_DEFLECTION;
  7969. misdn_copy_redirecting_to_ast(ch->ast, &bc->redirecting, bc->incoming_cid_tag);
  7970. ast_channel_call_forward_set(ch->ast, bc->redirecting.to.number);
  7971. /* Send back positive ACK */
  7972. #if 1
  7973. /*
  7974. * Since there are no return result arguments it must be a
  7975. * generic result message. ETSI 300-196
  7976. */
  7977. bc->fac_out.Function = Fac_RESULT;
  7978. bc->fac_out.u.RESULT.InvokeID = bc->fac_in.u.CallDeflection.InvokeID;
  7979. #else
  7980. bc->fac_out.Function = Fac_CallDeflection;
  7981. bc->fac_out.u.CallDeflection.InvokeID = bc->fac_in.u.CallDeflection.InvokeID;
  7982. bc->fac_out.u.CallDeflection.ComponentType = FacComponent_Result;
  7983. #endif
  7984. print_facility(&bc->fac_out, bc);
  7985. misdn_lib_send_event(bc, EVENT_DISCONNECT);
  7986. /* This line is BUSY to further attempts by this dialing attempt. */
  7987. ast_queue_control(ch->ast, AST_CONTROL_BUSY);
  7988. break;
  7989. case FacComponent_Result:
  7990. /* Positive ACK to call deflection */
  7991. /*
  7992. * Sent in DISCONNECT or FACILITY message depending upon network option.
  7993. * It is in the FACILITY message if the call is still offered to the user
  7994. * while trying to alert the deflected to party.
  7995. */
  7996. /* Ignore the ACK */
  7997. break;
  7998. default:
  7999. break;
  8000. }
  8001. }
  8002. break;
  8003. #if 0 /* We don't handle this yet */
  8004. case Fac_CallRerouteing:
  8005. /* Private-Public ISDN interworking message */
  8006. /* We don't handle this yet */
  8007. break;
  8008. #endif /* We don't handle this yet */
  8009. case Fac_DivertingLegInformation1:
  8010. /* Private-Public ISDN interworking message */
  8011. bc->div_leg_3_rx_wanted = 0;
  8012. if (ch && ch->ast) {
  8013. bc->redirecting.reason =
  8014. diversion_reason_to_misdn(bc->fac_in.u.DivertingLegInformation1.DiversionReason);
  8015. if (bc->fac_in.u.DivertingLegInformation1.DivertedToPresent) {
  8016. misdn_PresentedNumberUnscreened_extract(&bc->redirecting.to,
  8017. &bc->fac_in.u.DivertingLegInformation1.DivertedTo);
  8018. /* Add configured prefix to redirecting.to.number */
  8019. misdn_add_number_prefix(bc->port, bc->redirecting.to.number_type,
  8020. bc->redirecting.to.number, sizeof(bc->redirecting.to.number));
  8021. } else {
  8022. bc->redirecting.to.number[0] = '\0';
  8023. bc->redirecting.to.number_plan = NUMPLAN_ISDN;
  8024. bc->redirecting.to.number_type = NUMTYPE_UNKNOWN;
  8025. bc->redirecting.to.presentation = 1;/* restricted */
  8026. bc->redirecting.to.screening = 0;/* unscreened */
  8027. }
  8028. misdn_copy_redirecting_to_ast(ch->ast, &bc->redirecting, bc->incoming_cid_tag);
  8029. bc->div_leg_3_rx_wanted = 1;
  8030. }
  8031. break;
  8032. case Fac_DivertingLegInformation2:
  8033. /* Private-Public ISDN interworking message */
  8034. switch (event) {
  8035. case EVENT_SETUP:
  8036. /* Comes in on a SETUP with redirecting.from information */
  8037. bc->div_leg_3_tx_pending = 1;
  8038. if (ch && ch->ast) {
  8039. /*
  8040. * Setup the redirecting.to informtion so we can identify
  8041. * if the user wants to manually supply the COLR for this
  8042. * redirected to number if further redirects could happen.
  8043. *
  8044. * All the user needs to do is set the REDIRECTING(to-pres)
  8045. * to the COLR and REDIRECTING(to-num) = ${EXTEN} to be safe
  8046. * after determining that the incoming call was redirected by
  8047. * checking if there is a REDIRECTING(from-num).
  8048. */
  8049. ast_copy_string(bc->redirecting.to.number, bc->dialed.number,
  8050. sizeof(bc->redirecting.to.number));
  8051. bc->redirecting.to.number_plan = bc->dialed.number_plan;
  8052. bc->redirecting.to.number_type = bc->dialed.number_type;
  8053. bc->redirecting.to.presentation = 1;/* restricted */
  8054. bc->redirecting.to.screening = 0;/* unscreened */
  8055. bc->redirecting.reason =
  8056. diversion_reason_to_misdn(bc->fac_in.u.DivertingLegInformation2.DiversionReason);
  8057. bc->redirecting.count = bc->fac_in.u.DivertingLegInformation2.DiversionCounter;
  8058. if (bc->fac_in.u.DivertingLegInformation2.DivertingPresent) {
  8059. /* This information is redundant if there was a redirecting ie in the SETUP. */
  8060. misdn_PresentedNumberUnscreened_extract(&bc->redirecting.from,
  8061. &bc->fac_in.u.DivertingLegInformation2.Diverting);
  8062. /* Add configured prefix to redirecting.from.number */
  8063. misdn_add_number_prefix(bc->port, bc->redirecting.from.number_type,
  8064. bc->redirecting.from.number, sizeof(bc->redirecting.from.number));
  8065. }
  8066. #if 0
  8067. if (bc->fac_in.u.DivertingLegInformation2.OriginalCalledPresent) {
  8068. /* We have no place to put the OriginalCalled number */
  8069. }
  8070. #endif
  8071. misdn_copy_redirecting_to_ast(ch->ast, &bc->redirecting, bc->incoming_cid_tag);
  8072. }
  8073. break;
  8074. default:
  8075. chan_misdn_log(0, bc->port," --> Expected in a SETUP message: facility type:0x%04X\n",
  8076. bc->fac_in.Function);
  8077. break;
  8078. }
  8079. break;
  8080. case Fac_DivertingLegInformation3:
  8081. /* Private-Public ISDN interworking message */
  8082. if (bc->div_leg_3_rx_wanted) {
  8083. bc->div_leg_3_rx_wanted = 0;
  8084. if (ch && ch->ast) {
  8085. struct ast_party_redirecting redirecting;
  8086. ast_channel_redirecting(ch->ast)->to.number.presentation =
  8087. bc->fac_in.u.DivertingLegInformation3.PresentationAllowedIndicator
  8088. ? AST_PRES_ALLOWED | AST_PRES_USER_NUMBER_UNSCREENED
  8089. : AST_PRES_RESTRICTED | AST_PRES_USER_NUMBER_UNSCREENED;
  8090. ast_party_redirecting_init(&redirecting);
  8091. ast_party_redirecting_copy(&redirecting, ast_channel_redirecting(ch->ast));
  8092. /*
  8093. * Reset any earlier private redirecting id representations and
  8094. * make sure that it is invalidated at the remote end.
  8095. */
  8096. ast_party_id_reset(&redirecting.priv_orig);
  8097. ast_party_id_reset(&redirecting.priv_from);
  8098. ast_party_id_reset(&redirecting.priv_to);
  8099. ast_channel_queue_redirecting_update(ch->ast, &redirecting, NULL);
  8100. ast_party_redirecting_free(&redirecting);
  8101. }
  8102. }
  8103. break;
  8104. #else /* !defined(AST_MISDN_ENHANCEMENTS) */
  8105. case Fac_CD:
  8106. if (ch && ch->ast) {
  8107. ast_copy_string(bc->redirecting.from.number, bc->dialed.number,
  8108. sizeof(bc->redirecting.from.number));
  8109. bc->redirecting.from.name[0] = 0;
  8110. bc->redirecting.from.number_plan = bc->dialed.number_plan;
  8111. bc->redirecting.from.number_type = bc->dialed.number_type;
  8112. bc->redirecting.from.screening = 0;/* Unscreened */
  8113. bc->redirecting.from.presentation =
  8114. bc->fac_in.u.CDeflection.PresentationAllowed
  8115. ? 0 /* Allowed */ : 1 /* Restricted */;
  8116. ast_copy_string(bc->redirecting.to.number,
  8117. (char *) bc->fac_in.u.CDeflection.DeflectedToNumber,
  8118. sizeof(bc->redirecting.to.number));
  8119. bc->redirecting.to.name[0] = 0;
  8120. bc->redirecting.to.number_plan = NUMPLAN_UNKNOWN;
  8121. bc->redirecting.to.number_type = NUMTYPE_UNKNOWN;
  8122. bc->redirecting.to.presentation = 0;/* Allowed */
  8123. bc->redirecting.to.screening = 0;/* Unscreened */
  8124. ++bc->redirecting.count;
  8125. bc->redirecting.reason = mISDN_REDIRECTING_REASON_DEFLECTION;
  8126. misdn_copy_redirecting_to_ast(ch->ast, &bc->redirecting, bc->incoming_cid_tag);
  8127. ast_channel_call_forward_set(ch->ast, bc->redirecting.to.number);
  8128. misdn_lib_send_event(bc, EVENT_DISCONNECT);
  8129. /* This line is BUSY to further attempts by this dialing attempt. */
  8130. ast_queue_control(ch->ast, AST_CONTROL_BUSY);
  8131. }
  8132. break;
  8133. #endif /* !defined(AST_MISDN_ENHANCEMENTS) */
  8134. case Fac_AOCDCurrency:
  8135. if (ch && ch->ast) {
  8136. bc->AOCDtype = Fac_AOCDCurrency;
  8137. memcpy(&bc->AOCD.currency, &bc->fac_in.u.AOCDcur, sizeof(bc->AOCD.currency));
  8138. bc->AOCD_need_export = 1;
  8139. export_aoc_vars(ch->originator, ch->ast, bc);
  8140. }
  8141. break;
  8142. case Fac_AOCDChargingUnit:
  8143. if (ch && ch->ast) {
  8144. bc->AOCDtype = Fac_AOCDChargingUnit;
  8145. memcpy(&bc->AOCD.chargingUnit, &bc->fac_in.u.AOCDchu, sizeof(bc->AOCD.chargingUnit));
  8146. bc->AOCD_need_export = 1;
  8147. export_aoc_vars(ch->originator, ch->ast, bc);
  8148. }
  8149. break;
  8150. #if defined(AST_MISDN_ENHANCEMENTS)
  8151. case Fac_ERROR:
  8152. diagnostic_msg = misdn_to_str_error_code(bc->fac_in.u.ERROR.errorValue);
  8153. chan_misdn_log(1, bc->port, " --> Facility error code: %s\n", diagnostic_msg);
  8154. switch (event) {
  8155. case EVENT_DISCONNECT:
  8156. case EVENT_RELEASE:
  8157. case EVENT_RELEASE_COMPLETE:
  8158. /* Possible call failure as a result of Fac_CCBSCall/Fac_CCBS_T_Call */
  8159. if (ch && ch->peer) {
  8160. misdn_cc_set_peer_var(ch->peer, MISDN_ERROR_MSG, diagnostic_msg);
  8161. }
  8162. break;
  8163. default:
  8164. break;
  8165. }
  8166. AST_LIST_LOCK(&misdn_cc_records_db);
  8167. cc_record = misdn_cc_find_by_invoke(bc->port, bc->fac_in.u.ERROR.invokeId);
  8168. if (cc_record) {
  8169. cc_record->outstanding_message = 0;
  8170. cc_record->error_code = bc->fac_in.u.ERROR.errorValue;
  8171. }
  8172. AST_LIST_UNLOCK(&misdn_cc_records_db);
  8173. break;
  8174. case Fac_REJECT:
  8175. diagnostic_msg = misdn_to_str_reject_code(bc->fac_in.u.REJECT.Code);
  8176. chan_misdn_log(1, bc->port, " --> Facility reject code: %s\n", diagnostic_msg);
  8177. switch (event) {
  8178. case EVENT_DISCONNECT:
  8179. case EVENT_RELEASE:
  8180. case EVENT_RELEASE_COMPLETE:
  8181. /* Possible call failure as a result of Fac_CCBSCall/Fac_CCBS_T_Call */
  8182. if (ch && ch->peer) {
  8183. misdn_cc_set_peer_var(ch->peer, MISDN_ERROR_MSG, diagnostic_msg);
  8184. }
  8185. break;
  8186. default:
  8187. break;
  8188. }
  8189. if (bc->fac_in.u.REJECT.InvokeIDPresent) {
  8190. AST_LIST_LOCK(&misdn_cc_records_db);
  8191. cc_record = misdn_cc_find_by_invoke(bc->port, bc->fac_in.u.REJECT.InvokeID);
  8192. if (cc_record) {
  8193. cc_record->outstanding_message = 0;
  8194. cc_record->reject_code = bc->fac_in.u.REJECT.Code;
  8195. }
  8196. AST_LIST_UNLOCK(&misdn_cc_records_db);
  8197. }
  8198. break;
  8199. case Fac_RESULT:
  8200. AST_LIST_LOCK(&misdn_cc_records_db);
  8201. cc_record = misdn_cc_find_by_invoke(bc->port, bc->fac_in.u.RESULT.InvokeID);
  8202. if (cc_record) {
  8203. cc_record->outstanding_message = 0;
  8204. }
  8205. AST_LIST_UNLOCK(&misdn_cc_records_db);
  8206. break;
  8207. #if 0 /* We don't handle this yet */
  8208. case Fac_EctExecute:
  8209. /* We don't handle this yet */
  8210. break;
  8211. case Fac_ExplicitEctExecute:
  8212. /* We don't handle this yet */
  8213. break;
  8214. case Fac_EctLinkIdRequest:
  8215. /* We don't handle this yet */
  8216. break;
  8217. #endif /* We don't handle this yet */
  8218. case Fac_SubaddressTransfer:
  8219. /* We do not have anything to do for this message since we do not handle subaddresses. */
  8220. break;
  8221. case Fac_RequestSubaddress:
  8222. /*
  8223. * We do not have anything to do for this message since we do not handle subaddresses.
  8224. * However, we do care about some other ie's that should be present.
  8225. */
  8226. if (bc->redirecting.to_changed) {
  8227. /* Add configured prefix to redirecting.to.number */
  8228. misdn_add_number_prefix(bc->port, bc->redirecting.to.number_type,
  8229. bc->redirecting.to.number, sizeof(bc->redirecting.to.number));
  8230. }
  8231. switch (bc->notify_description_code) {
  8232. case mISDN_NOTIFY_CODE_INVALID:
  8233. /* Notify ie was not present. */
  8234. bc->redirecting.to_changed = 0;
  8235. break;
  8236. case mISDN_NOTIFY_CODE_CALL_TRANSFER_ALERTING:
  8237. /*
  8238. * It would be preferable to update the connected line information
  8239. * only when the message callStatus is active. However, the
  8240. * optional redirection number may not be present in the active
  8241. * message if an alerting message were received earlier.
  8242. *
  8243. * The consequences if we wind up sending two updates is benign.
  8244. * The other end will think that it got transferred twice.
  8245. */
  8246. if (!bc->redirecting.to_changed) {
  8247. break;
  8248. }
  8249. bc->redirecting.to_changed = 0;
  8250. if (!ch || !ch->ast) {
  8251. break;
  8252. }
  8253. misdn_update_remote_party(ch->ast, &bc->redirecting.to,
  8254. AST_CONNECTED_LINE_UPDATE_SOURCE_TRANSFER_ALERTING,
  8255. bc->incoming_cid_tag);
  8256. break;
  8257. case mISDN_NOTIFY_CODE_CALL_TRANSFER_ACTIVE:
  8258. if (!bc->redirecting.to_changed) {
  8259. break;
  8260. }
  8261. bc->redirecting.to_changed = 0;
  8262. if (!ch || !ch->ast) {
  8263. break;
  8264. }
  8265. misdn_update_remote_party(ch->ast, &bc->redirecting.to,
  8266. AST_CONNECTED_LINE_UPDATE_SOURCE_TRANSFER, bc->incoming_cid_tag);
  8267. break;
  8268. default:
  8269. bc->redirecting.to_changed = 0;
  8270. chan_misdn_log(0, bc->port," --> not yet handled: notify code:0x%02X\n",
  8271. bc->notify_description_code);
  8272. break;
  8273. }
  8274. bc->notify_description_code = mISDN_NOTIFY_CODE_INVALID;
  8275. break;
  8276. case Fac_EctInform:
  8277. /* Private-Public ISDN interworking message */
  8278. if (ch && ch->ast && bc->fac_in.u.EctInform.RedirectionPresent) {
  8279. /* Add configured prefix to the redirection number */
  8280. memset(&party_id, 0, sizeof(party_id));
  8281. misdn_PresentedNumberUnscreened_extract(&party_id,
  8282. &bc->fac_in.u.EctInform.Redirection);
  8283. misdn_add_number_prefix(bc->port, party_id.number_type,
  8284. party_id.number, sizeof(party_id.number));
  8285. /*
  8286. * It would be preferable to update the connected line information
  8287. * only when the message callStatus is active. However, the
  8288. * optional redirection number may not be present in the active
  8289. * message if an alerting message were received earlier.
  8290. *
  8291. * The consequences if we wind up sending two updates is benign.
  8292. * The other end will think that it got transferred twice.
  8293. */
  8294. misdn_update_remote_party(ch->ast, &party_id,
  8295. (bc->fac_in.u.EctInform.Status == 0 /* alerting */)
  8296. ? AST_CONNECTED_LINE_UPDATE_SOURCE_TRANSFER_ALERTING
  8297. : AST_CONNECTED_LINE_UPDATE_SOURCE_TRANSFER,
  8298. bc->incoming_cid_tag);
  8299. }
  8300. break;
  8301. #if 0 /* We don't handle this yet */
  8302. case Fac_EctLoopTest:
  8303. /* The use of this message is unclear on how it works to detect loops. */
  8304. /* We don't handle this yet */
  8305. break;
  8306. #endif /* We don't handle this yet */
  8307. case Fac_CallInfoRetain:
  8308. switch (event) {
  8309. case EVENT_ALERTING:
  8310. case EVENT_DISCONNECT:
  8311. /* CCBS/CCNR is available */
  8312. if (ch && ch->peer) {
  8313. AST_LIST_LOCK(&misdn_cc_records_db);
  8314. if (ch->record_id == -1) {
  8315. cc_record = misdn_cc_new();
  8316. } else {
  8317. /*
  8318. * We are doing a call-completion attempt
  8319. * or the switch is sending us extra call-completion
  8320. * availability indications (erroneously?).
  8321. *
  8322. * Assume that the network request retention option
  8323. * is not on and that the current call-completion
  8324. * request is disabled.
  8325. */
  8326. cc_record = misdn_cc_find_by_id(ch->record_id);
  8327. if (cc_record) {
  8328. if (cc_record->ptp && cc_record->mode.ptp.bc) {
  8329. /*
  8330. * What? We are getting mixed messages from the
  8331. * switch. We are currently setup for
  8332. * point-to-point. Now we are switching to
  8333. * point-to-multipoint.
  8334. *
  8335. * Close the call-completion signaling link
  8336. */
  8337. cc_record->mode.ptp.bc->fac_out.Function = Fac_None;
  8338. cc_record->mode.ptp.bc->out_cause = AST_CAUSE_NORMAL_CLEARING;
  8339. misdn_lib_send_event(cc_record->mode.ptp.bc, EVENT_RELEASE_COMPLETE);
  8340. }
  8341. /*
  8342. * Resetup the existing record for a possible new
  8343. * call-completion request.
  8344. */
  8345. new_record_id = misdn_cc_record_id_new();
  8346. if (new_record_id < 0) {
  8347. /* Looks like we must keep the old id anyway. */
  8348. } else {
  8349. cc_record->record_id = new_record_id;
  8350. ch->record_id = new_record_id;
  8351. }
  8352. cc_record->ptp = 0;
  8353. cc_record->port = bc->port;
  8354. memset(&cc_record->mode, 0, sizeof(cc_record->mode));
  8355. cc_record->mode.ptmp.linkage_id = bc->fac_in.u.CallInfoRetain.CallLinkageID;
  8356. cc_record->invoke_id = ++misdn_invoke_id;
  8357. cc_record->activated = 0;
  8358. cc_record->outstanding_message = 0;
  8359. cc_record->activation_requested = 0;
  8360. cc_record->error_code = FacError_None;
  8361. cc_record->reject_code = FacReject_None;
  8362. memset(&cc_record->remote_user_free, 0, sizeof(cc_record->remote_user_free));
  8363. memset(&cc_record->b_free, 0, sizeof(cc_record->b_free));
  8364. cc_record->time_created = time(NULL);
  8365. cc_record = NULL;
  8366. } else {
  8367. /*
  8368. * Where did the record go? We will have to recapture
  8369. * the call setup information. Unfortunately, some
  8370. * setup information may have been changed.
  8371. */
  8372. ch->record_id = -1;
  8373. cc_record = misdn_cc_new();
  8374. }
  8375. }
  8376. if (cc_record) {
  8377. ch->record_id = cc_record->record_id;
  8378. cc_record->ptp = 0;
  8379. cc_record->port = bc->port;
  8380. cc_record->mode.ptmp.linkage_id = bc->fac_in.u.CallInfoRetain.CallLinkageID;
  8381. /* Record call information for possible call-completion attempt. */
  8382. cc_record->redial.caller = bc->caller;
  8383. cc_record->redial.dialed = bc->dialed;
  8384. cc_record->redial.setup_bc_hlc_llc = bc->setup_bc_hlc_llc;
  8385. cc_record->redial.capability = bc->capability;
  8386. cc_record->redial.hdlc = bc->hdlc;
  8387. }
  8388. AST_LIST_UNLOCK(&misdn_cc_records_db);
  8389. /* Set MISDN_CC_RECORD_ID in original channel */
  8390. if (ch->record_id != -1) {
  8391. snprintf(buf, sizeof(buf), "%ld", ch->record_id);
  8392. } else {
  8393. buf[0] = 0;
  8394. }
  8395. misdn_cc_set_peer_var(ch->peer, MISDN_CC_RECORD_ID, buf);
  8396. }
  8397. break;
  8398. default:
  8399. chan_misdn_log(0, bc->port,
  8400. " --> Expected in a DISCONNECT or ALERTING message: facility type:0x%04X\n",
  8401. bc->fac_in.Function);
  8402. break;
  8403. }
  8404. break;
  8405. case Fac_CCBS_T_Call:
  8406. case Fac_CCBSCall:
  8407. switch (event) {
  8408. case EVENT_SETUP:
  8409. /*
  8410. * This is a call completion retry call.
  8411. * If we had anything to do we would do it here.
  8412. */
  8413. break;
  8414. default:
  8415. chan_misdn_log(0, bc->port, " --> Expected in a SETUP message: facility type:0x%04X\n",
  8416. bc->fac_in.Function);
  8417. break;
  8418. }
  8419. break;
  8420. case Fac_CCBSDeactivate:
  8421. switch (bc->fac_in.u.CCBSDeactivate.ComponentType) {
  8422. case FacComponent_Result:
  8423. AST_LIST_LOCK(&misdn_cc_records_db);
  8424. cc_record = misdn_cc_find_by_invoke(bc->port, bc->fac_in.u.CCBSDeactivate.InvokeID);
  8425. if (cc_record) {
  8426. cc_record->outstanding_message = 0;
  8427. }
  8428. AST_LIST_UNLOCK(&misdn_cc_records_db);
  8429. break;
  8430. default:
  8431. chan_misdn_log(0, bc->port, " --> not yet handled: facility type:0x%04X\n",
  8432. bc->fac_in.Function);
  8433. break;
  8434. }
  8435. break;
  8436. case Fac_CCBSErase:
  8437. AST_LIST_LOCK(&misdn_cc_records_db);
  8438. cc_record = misdn_cc_find_by_reference(bc->port, bc->fac_in.u.CCBSErase.CCBSReference);
  8439. if (cc_record) {
  8440. misdn_cc_delete(cc_record);
  8441. }
  8442. AST_LIST_UNLOCK(&misdn_cc_records_db);
  8443. break;
  8444. case Fac_CCBSRemoteUserFree:
  8445. misdn_cc_handle_remote_user_free(bc->port, &bc->fac_in);
  8446. break;
  8447. case Fac_CCBSBFree:
  8448. misdn_cc_handle_b_free(bc->port, &bc->fac_in);
  8449. break;
  8450. case Fac_CCBSStatusRequest:
  8451. misdn_cc_handle_ccbs_status_request(bc->port, &bc->fac_in);
  8452. break;
  8453. case Fac_EraseCallLinkageID:
  8454. AST_LIST_LOCK(&misdn_cc_records_db);
  8455. cc_record = misdn_cc_find_by_linkage(bc->port,
  8456. bc->fac_in.u.EraseCallLinkageID.CallLinkageID);
  8457. if (cc_record && !cc_record->activation_requested) {
  8458. /*
  8459. * The T-RETENTION timer expired before we requested
  8460. * call completion activation. Call completion is no
  8461. * longer available.
  8462. */
  8463. misdn_cc_delete(cc_record);
  8464. }
  8465. AST_LIST_UNLOCK(&misdn_cc_records_db);
  8466. break;
  8467. case Fac_CCBSStopAlerting:
  8468. /* We do not have anything to do for this message. */
  8469. break;
  8470. case Fac_CCBSRequest:
  8471. case Fac_CCNRRequest:
  8472. switch (bc->fac_in.u.CCBSRequest.ComponentType) {
  8473. case FacComponent_Result:
  8474. AST_LIST_LOCK(&misdn_cc_records_db);
  8475. cc_record = misdn_cc_find_by_invoke(bc->port, bc->fac_in.u.CCBSRequest.InvokeID);
  8476. if (cc_record && !cc_record->ptp) {
  8477. cc_record->outstanding_message = 0;
  8478. cc_record->activated = 1;
  8479. cc_record->mode.ptmp.recall_mode = bc->fac_in.u.CCBSRequest.Component.Result.RecallMode;
  8480. cc_record->mode.ptmp.reference_id = bc->fac_in.u.CCBSRequest.Component.Result.CCBSReference;
  8481. }
  8482. AST_LIST_UNLOCK(&misdn_cc_records_db);
  8483. break;
  8484. default:
  8485. chan_misdn_log(0, bc->port, " --> not yet handled: facility type:0x%04X\n",
  8486. bc->fac_in.Function);
  8487. break;
  8488. }
  8489. break;
  8490. #if 0 /* We don't handle this yet */
  8491. case Fac_CCBSInterrogate:
  8492. case Fac_CCNRInterrogate:
  8493. /* We don't handle this yet */
  8494. break;
  8495. case Fac_StatusRequest:
  8496. /* We don't handle this yet */
  8497. break;
  8498. #endif /* We don't handle this yet */
  8499. #if 0 /* We don't handle this yet */
  8500. case Fac_CCBS_T_Suspend:
  8501. case Fac_CCBS_T_Resume:
  8502. /* We don't handle this yet */
  8503. break;
  8504. #endif /* We don't handle this yet */
  8505. case Fac_CCBS_T_RemoteUserFree:
  8506. misdn_cc_handle_T_remote_user_free(bc);
  8507. break;
  8508. case Fac_CCBS_T_Available:
  8509. switch (event) {
  8510. case EVENT_ALERTING:
  8511. case EVENT_DISCONNECT:
  8512. /* CCBS-T/CCNR-T is available */
  8513. if (ch && ch->peer) {
  8514. int set_id = 1;
  8515. AST_LIST_LOCK(&misdn_cc_records_db);
  8516. if (ch->record_id == -1) {
  8517. cc_record = misdn_cc_new();
  8518. } else {
  8519. /*
  8520. * We are doing a call-completion attempt
  8521. * or the switch is sending us extra call-completion
  8522. * availability indications (erroneously?).
  8523. */
  8524. cc_record = misdn_cc_find_by_id(ch->record_id);
  8525. if (cc_record) {
  8526. if (cc_record->ptp && cc_record->mode.ptp.retention_enabled) {
  8527. /*
  8528. * Call-completion is still activated.
  8529. * The user does not have to request it again.
  8530. */
  8531. chan_misdn_log(1, bc->port, " --> Call-completion request retention option is enabled\n");
  8532. set_id = 0;
  8533. } else {
  8534. if (cc_record->ptp && cc_record->mode.ptp.bc) {
  8535. /*
  8536. * The network request retention option
  8537. * is not on and the current call-completion
  8538. * request is to be disabled.
  8539. *
  8540. * We should get here only if EVENT_DISCONNECT
  8541. *
  8542. * Close the call-completion signaling link
  8543. */
  8544. cc_record->mode.ptp.bc->fac_out.Function = Fac_None;
  8545. cc_record->mode.ptp.bc->out_cause = AST_CAUSE_NORMAL_CLEARING;
  8546. misdn_lib_send_event(cc_record->mode.ptp.bc, EVENT_RELEASE_COMPLETE);
  8547. }
  8548. /*
  8549. * Resetup the existing record for a possible new
  8550. * call-completion request.
  8551. */
  8552. new_record_id = misdn_cc_record_id_new();
  8553. if (new_record_id < 0) {
  8554. /* Looks like we must keep the old id anyway. */
  8555. } else {
  8556. cc_record->record_id = new_record_id;
  8557. ch->record_id = new_record_id;
  8558. }
  8559. cc_record->ptp = 1;
  8560. cc_record->port = bc->port;
  8561. memset(&cc_record->mode, 0, sizeof(cc_record->mode));
  8562. cc_record->invoke_id = ++misdn_invoke_id;
  8563. cc_record->activated = 0;
  8564. cc_record->outstanding_message = 0;
  8565. cc_record->activation_requested = 0;
  8566. cc_record->error_code = FacError_None;
  8567. cc_record->reject_code = FacReject_None;
  8568. memset(&cc_record->remote_user_free, 0, sizeof(cc_record->remote_user_free));
  8569. memset(&cc_record->b_free, 0, sizeof(cc_record->b_free));
  8570. cc_record->time_created = time(NULL);
  8571. }
  8572. cc_record = NULL;
  8573. } else {
  8574. /*
  8575. * Where did the record go? We will have to recapture
  8576. * the call setup information. Unfortunately, some
  8577. * setup information may have been changed.
  8578. */
  8579. ch->record_id = -1;
  8580. cc_record = misdn_cc_new();
  8581. }
  8582. }
  8583. if (cc_record) {
  8584. ch->record_id = cc_record->record_id;
  8585. cc_record->ptp = 1;
  8586. cc_record->port = bc->port;
  8587. /* Record call information for possible call-completion attempt. */
  8588. cc_record->redial.caller = bc->caller;
  8589. cc_record->redial.dialed = bc->dialed;
  8590. cc_record->redial.setup_bc_hlc_llc = bc->setup_bc_hlc_llc;
  8591. cc_record->redial.capability = bc->capability;
  8592. cc_record->redial.hdlc = bc->hdlc;
  8593. }
  8594. AST_LIST_UNLOCK(&misdn_cc_records_db);
  8595. /* Set MISDN_CC_RECORD_ID in original channel */
  8596. if (ch->record_id != -1 && set_id) {
  8597. snprintf(buf, sizeof(buf), "%ld", ch->record_id);
  8598. } else {
  8599. buf[0] = 0;
  8600. }
  8601. misdn_cc_set_peer_var(ch->peer, MISDN_CC_RECORD_ID, buf);
  8602. }
  8603. break;
  8604. default:
  8605. chan_misdn_log(0, bc->port,
  8606. " --> Expected in a DISCONNECT or ALERTING message: facility type:0x%04X\n",
  8607. bc->fac_in.Function);
  8608. break;
  8609. }
  8610. break;
  8611. case Fac_CCBS_T_Request:
  8612. case Fac_CCNR_T_Request:
  8613. switch (bc->fac_in.u.CCBS_T_Request.ComponentType) {
  8614. case FacComponent_Result:
  8615. AST_LIST_LOCK(&misdn_cc_records_db);
  8616. cc_record = misdn_cc_find_by_invoke(bc->port, bc->fac_in.u.CCBS_T_Request.InvokeID);
  8617. if (cc_record && cc_record->ptp) {
  8618. cc_record->outstanding_message = 0;
  8619. cc_record->activated = 1;
  8620. cc_record->mode.ptp.retention_enabled =
  8621. cc_record->mode.ptp.requested_retention
  8622. ? bc->fac_in.u.CCBS_T_Request.Component.Result.RetentionSupported
  8623. ? 1 : 0
  8624. : 0;
  8625. }
  8626. AST_LIST_UNLOCK(&misdn_cc_records_db);
  8627. break;
  8628. case FacComponent_Invoke:
  8629. /* We cannot be User-B in ptp mode. */
  8630. default:
  8631. chan_misdn_log(0, bc->port, " --> not yet handled: facility type:0x%04X\n",
  8632. bc->fac_in.Function);
  8633. break;
  8634. }
  8635. break;
  8636. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  8637. case Fac_None:
  8638. break;
  8639. default:
  8640. chan_misdn_log(0, bc->port, " --> not yet handled: facility type:0x%04X\n",
  8641. bc->fac_in.Function);
  8642. break;
  8643. }
  8644. }
  8645. /*!
  8646. * \internal
  8647. * \brief Determine if the given dialed party matches our MSN.
  8648. * \since 1.8
  8649. *
  8650. * \param port ISDN port
  8651. * \param dialed Dialed party information of incoming call.
  8652. *
  8653. * \retval non-zero if MSN is valid.
  8654. * \retval 0 if MSN invalid.
  8655. */
  8656. static int misdn_is_msn_valid(int port, const struct misdn_party_dialing *dialed)
  8657. {
  8658. char number[sizeof(dialed->number)];
  8659. ast_copy_string(number, dialed->number, sizeof(number));
  8660. misdn_add_number_prefix(port, dialed->number_type, number, sizeof(number));
  8661. return misdn_cfg_is_msn_valid(port, number);
  8662. }
  8663. /************************************************************/
  8664. /* Receive Events from isdn_lib here */
  8665. /************************************************************/
  8666. static enum event_response_e
  8667. cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
  8668. {
  8669. #if defined(AST_MISDN_ENHANCEMENTS)
  8670. struct misdn_cc_record *cc_record;
  8671. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  8672. struct chan_list *held_ch;
  8673. struct chan_list *ch = find_chan_by_bc(bc);
  8674. if (event != EVENT_BCHAN_DATA && event != EVENT_TONE_GENERATE) {
  8675. int debuglevel = 1;
  8676. /* Debug Only Non-Bchan */
  8677. if (event == EVENT_CLEANUP && !user_data) {
  8678. debuglevel = 5;
  8679. }
  8680. chan_misdn_log(debuglevel, bc->port,
  8681. "I IND :%s caller:\"%s\" <%s> dialed:%s pid:%d state:%s\n",
  8682. manager_isdn_get_info(event),
  8683. bc->caller.name,
  8684. bc->caller.number,
  8685. bc->dialed.number,
  8686. bc->pid,
  8687. ch ? misdn_get_ch_state(ch) : "none");
  8688. if (debuglevel == 1) {
  8689. misdn_lib_log_ies(bc);
  8690. chan_misdn_log(4, bc->port, " --> bc_state:%s\n", bc_state2str(bc->bc_state));
  8691. }
  8692. }
  8693. if (!ch) {
  8694. switch(event) {
  8695. case EVENT_SETUP:
  8696. case EVENT_DISCONNECT:
  8697. case EVENT_RELEASE:
  8698. case EVENT_RELEASE_COMPLETE:
  8699. case EVENT_PORT_ALARM:
  8700. case EVENT_RETRIEVE:
  8701. case EVENT_NEW_BC:
  8702. case EVENT_FACILITY:
  8703. case EVENT_REGISTER:
  8704. break;
  8705. case EVENT_CLEANUP:
  8706. case EVENT_TONE_GENERATE:
  8707. case EVENT_BCHAN_DATA:
  8708. return -1;
  8709. default:
  8710. chan_misdn_log(1, bc->port, "Chan not existing at the moment bc->l3id:%x bc:%p event:%s port:%d channel:%d\n", bc->l3_id, bc, manager_isdn_get_info(event), bc->port, bc->channel);
  8711. return -1;
  8712. }
  8713. } else {
  8714. switch (event) {
  8715. case EVENT_TONE_GENERATE:
  8716. break;
  8717. case EVENT_DISCONNECT:
  8718. case EVENT_RELEASE:
  8719. case EVENT_RELEASE_COMPLETE:
  8720. case EVENT_CLEANUP:
  8721. case EVENT_TIMEOUT:
  8722. if (!ch->ast) {
  8723. chan_misdn_log(3, bc->port, "ast_hangup already called, so we have no ast ptr anymore in event(%s)\n", manager_isdn_get_info(event));
  8724. }
  8725. break;
  8726. default:
  8727. if (!ch->ast || !MISDN_ASTERISK_TECH_PVT(ch->ast)) {
  8728. if (event != EVENT_BCHAN_DATA) {
  8729. ast_log(LOG_NOTICE, "No Ast or No private Pointer in Event (%d:%s)\n", event, manager_isdn_get_info(event));
  8730. }
  8731. chan_list_unref(ch, "No Ast or Ast private pointer");
  8732. return -1;
  8733. }
  8734. break;
  8735. }
  8736. }
  8737. switch (event) {
  8738. case EVENT_PORT_ALARM:
  8739. {
  8740. int boa = 0;
  8741. misdn_cfg_get(bc->port, MISDN_CFG_ALARM_BLOCK, &boa, sizeof(boa));
  8742. if (boa) {
  8743. cb_log(1, bc->port, " --> blocking\n");
  8744. misdn_lib_port_block(bc->port);
  8745. }
  8746. }
  8747. break;
  8748. case EVENT_BCHAN_ACTIVATED:
  8749. break;
  8750. case EVENT_NEW_CHANNEL:
  8751. update_name(ch->ast,bc->port,bc->channel);
  8752. break;
  8753. case EVENT_NEW_L3ID:
  8754. ch->l3id=bc->l3_id;
  8755. ch->addr=bc->addr;
  8756. break;
  8757. case EVENT_NEW_BC:
  8758. if (!ch) {
  8759. ch = find_hold_call(bc);
  8760. }
  8761. if (!ch) {
  8762. ast_log(LOG_WARNING, "NEW_BC without chan_list?\n");
  8763. break;
  8764. }
  8765. if (bc) {
  8766. ch->bc = (struct misdn_bchannel *) user_data;
  8767. }
  8768. break;
  8769. case EVENT_DTMF_TONE:
  8770. {
  8771. /* sending INFOS as DTMF-Frames :) */
  8772. struct ast_frame fr;
  8773. memset(&fr, 0, sizeof(fr));
  8774. fr.frametype = AST_FRAME_DTMF;
  8775. fr.subclass.integer = bc->dtmf ;
  8776. fr.src = NULL;
  8777. fr.data.ptr = NULL;
  8778. fr.datalen = 0;
  8779. fr.samples = 0;
  8780. fr.mallocd = 0;
  8781. fr.offset = 0;
  8782. fr.delivery = ast_tv(0,0);
  8783. if (!ch->ignore_dtmf) {
  8784. chan_misdn_log(2, bc->port, " --> DTMF:%c\n", bc->dtmf);
  8785. ast_queue_frame(ch->ast, &fr);
  8786. } else {
  8787. chan_misdn_log(2, bc->port, " --> Ignoring DTMF:%c due to bridge flags\n", bc->dtmf);
  8788. }
  8789. break;
  8790. }
  8791. case EVENT_STATUS:
  8792. break;
  8793. case EVENT_INFORMATION:
  8794. if (ch->state != MISDN_CONNECTED) {
  8795. stop_indicate(ch);
  8796. }
  8797. if (!ch->ast) {
  8798. break;
  8799. }
  8800. if (ch->state == MISDN_WAITING4DIGS) {
  8801. RAII_VAR(struct ast_features_pickup_config *, pickup_cfg, NULL, ao2_cleanup);
  8802. const char *pickupexten;
  8803. /* Ok, incomplete Setup, waiting till extension exists */
  8804. if (ast_strlen_zero(bc->info_dad) && ! ast_strlen_zero(bc->keypad)) {
  8805. chan_misdn_log(1, bc->port, " --> using keypad as info\n");
  8806. ast_copy_string(bc->info_dad, bc->keypad, sizeof(bc->info_dad));
  8807. }
  8808. strncat(bc->dialed.number, bc->info_dad, sizeof(bc->dialed.number) - strlen(bc->dialed.number) - 1);
  8809. ast_channel_exten_set(ch->ast, bc->dialed.number);
  8810. ast_channel_lock(ch->ast);
  8811. pickup_cfg = ast_get_chan_features_pickup_config(ch->ast);
  8812. if (!pickup_cfg) {
  8813. ast_log(LOG_ERROR, "Unable to retrieve pickup configuration options. Unable to detect call pickup extension\n");
  8814. pickupexten = "";
  8815. } else {
  8816. pickupexten = ast_strdupa(pickup_cfg->pickupexten);
  8817. }
  8818. ast_channel_unlock(ch->ast);
  8819. /* Check for Pickup Request first */
  8820. if (!strcmp(ast_channel_exten(ch->ast), pickupexten)) {
  8821. if (ast_pickup_call(ch->ast)) {
  8822. hangup_chan(ch, bc);
  8823. } else {
  8824. ch->state = MISDN_CALLING_ACKNOWLEDGE;
  8825. hangup_chan(ch, bc);
  8826. ch->ast = NULL;
  8827. break;
  8828. }
  8829. }
  8830. if (!ast_canmatch_extension(ch->ast, ch->context, bc->dialed.number, 1, bc->caller.number)) {
  8831. if (ast_exists_extension(ch->ast, ch->context, "i", 1, bc->caller.number)) {
  8832. ast_log(LOG_WARNING,
  8833. "Extension '%s@%s' can never match. Jumping to 'i' extension. port:%d\n",
  8834. bc->dialed.number, ch->context, bc->port);
  8835. pbx_builtin_setvar_helper(ch->ast, "INVALID_EXTEN", bc->dialed.number);
  8836. ast_channel_exten_set(ch->ast, "i");
  8837. ch->state = MISDN_DIALING;
  8838. start_pbx(ch, bc, ch->ast);
  8839. break;
  8840. }
  8841. ast_log(LOG_WARNING,
  8842. "Extension '%s@%s' can never match. Disconnecting. port:%d\n"
  8843. "\tMaybe you want to add an 'i' extension to catch this case.\n",
  8844. bc->dialed.number, ch->context, bc->port);
  8845. if (bc->nt) {
  8846. hanguptone_indicate(ch);
  8847. }
  8848. ch->state = MISDN_EXTCANTMATCH;
  8849. bc->out_cause = AST_CAUSE_UNALLOCATED;
  8850. misdn_lib_send_event(bc, EVENT_DISCONNECT);
  8851. break;
  8852. }
  8853. if (ch->overlap_dial) {
  8854. ast_mutex_lock(&ch->overlap_tv_lock);
  8855. ch->overlap_tv = ast_tvnow();
  8856. ast_mutex_unlock(&ch->overlap_tv_lock);
  8857. if (ch->overlap_dial_task == -1) {
  8858. ch->overlap_dial_task =
  8859. misdn_tasks_add_variable(ch->overlap_dial, misdn_overlap_dial_task, ch);
  8860. }
  8861. break;
  8862. }
  8863. if (ast_exists_extension(ch->ast, ch->context, bc->dialed.number, 1, bc->caller.number)) {
  8864. ch->state = MISDN_DIALING;
  8865. start_pbx(ch, bc, ch->ast);
  8866. }
  8867. } else {
  8868. /* sending INFOS as DTMF-Frames :) */
  8869. struct ast_frame fr;
  8870. int digits;
  8871. memset(&fr, 0, sizeof(fr));
  8872. fr.frametype = AST_FRAME_DTMF;
  8873. fr.subclass.integer = bc->info_dad[0] ;
  8874. fr.src = NULL;
  8875. fr.data.ptr = NULL;
  8876. fr.datalen = 0;
  8877. fr.samples = 0;
  8878. fr.mallocd = 0;
  8879. fr.offset = 0;
  8880. fr.delivery = ast_tv(0,0);
  8881. misdn_cfg_get(0, MISDN_GEN_APPEND_DIGITS2EXTEN, &digits, sizeof(digits));
  8882. if (ch->state != MISDN_CONNECTED) {
  8883. if (digits) {
  8884. strncat(bc->dialed.number, bc->info_dad, sizeof(bc->dialed.number) - strlen(bc->dialed.number) - 1);
  8885. ast_channel_exten_set(ch->ast, bc->dialed.number);
  8886. }
  8887. ast_queue_frame(ch->ast, &fr);
  8888. }
  8889. }
  8890. break;
  8891. case EVENT_SETUP:
  8892. {
  8893. struct ast_channel *chan;
  8894. int exceed;
  8895. int ai;
  8896. int im;
  8897. int append_msn = 0;
  8898. RAII_VAR(struct ast_features_pickup_config *, pickup_cfg, NULL, ao2_cleanup);
  8899. const char *pickupexten;
  8900. if (ch) {
  8901. switch (ch->state) {
  8902. case MISDN_NOTHING:
  8903. chan_list_unref(ch, "Ignore found ch. Is it for an outgoing call?");
  8904. ch = NULL;
  8905. break;
  8906. default:
  8907. chan_list_unref(ch, "Already have a call.");
  8908. chan_misdn_log(1, bc->port, " --> Ignoring Call we have already one\n");
  8909. return RESPONSE_IGNORE_SETUP_WITHOUT_CLOSE; /* Ignore MSNs which are not in our List */
  8910. }
  8911. }
  8912. if (!bc->nt && !misdn_is_msn_valid(bc->port, &bc->dialed)) {
  8913. chan_misdn_log(1, bc->port, " --> Ignoring Call, its not in our MSN List\n");
  8914. return RESPONSE_IGNORE_SETUP; /* Ignore MSNs which are not in our List */
  8915. }
  8916. if (bc->cw) {
  8917. int cause;
  8918. chan_misdn_log(0, bc->port, " --> Call Waiting on PMP sending RELEASE_COMPLETE\n");
  8919. misdn_cfg_get(bc->port, MISDN_CFG_REJECT_CAUSE, &cause, sizeof(cause));
  8920. bc->out_cause = cause ? cause : AST_CAUSE_NORMAL_CLEARING;
  8921. return RESPONSE_RELEASE_SETUP;
  8922. }
  8923. print_bearer(bc);
  8924. ch = chan_list_init(ORG_MISDN);
  8925. if (!ch) {
  8926. chan_misdn_log(-1, bc->port, "cb_events: malloc for chan_list failed!\n");
  8927. return RESPONSE_RELEASE_SETUP;
  8928. }
  8929. ch->bc = bc;
  8930. ch->l3id = bc->l3_id;
  8931. ch->addr = bc->addr;
  8932. {
  8933. struct ast_format_cap *cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
  8934. if (!(cap)) {
  8935. return RESPONSE_ERR;
  8936. }
  8937. ast_format_cap_append(cap, ast_format_alaw, 0);
  8938. chan = misdn_new(ch, AST_STATE_RESERVED, bc->dialed.number, bc->caller.number, cap, NULL, NULL, bc->port, bc->channel);
  8939. ao2_ref(cap, -1);
  8940. }
  8941. if (!chan) {
  8942. chan_list_unref(ch, "Failed to create a new channel");
  8943. ast_log(LOG_ERROR, "cb_events: misdn_new failed!\n");
  8944. return RESPONSE_RELEASE_SETUP;
  8945. }
  8946. ast_channel_lock(chan);
  8947. pickup_cfg = ast_get_chan_features_pickup_config(chan);
  8948. if (!pickup_cfg) {
  8949. ast_log(LOG_ERROR, "Unable to retrieve pickup configuration options. Unable to detect call pickup extension\n");
  8950. pickupexten = "";
  8951. } else {
  8952. pickupexten = ast_strdupa(pickup_cfg->pickupexten);
  8953. }
  8954. ast_channel_unlock(chan);
  8955. if ((exceed = add_in_calls(bc->port))) {
  8956. char tmp[16];
  8957. snprintf(tmp, sizeof(tmp), "%d", exceed);
  8958. pbx_builtin_setvar_helper(chan, "MAX_OVERFLOW", tmp);
  8959. }
  8960. read_config(ch);
  8961. export_ch(chan, bc, ch);
  8962. ast_channel_lock(ch->ast);
  8963. ast_channel_rings_set(ch->ast, 1);
  8964. ast_setstate(ch->ast, AST_STATE_RINGING);
  8965. ast_channel_unlock(ch->ast);
  8966. /* Update asterisk channel caller information */
  8967. chan_misdn_log(2, bc->port, " --> TON: %s(%d)\n", misdn_to_str_ton(bc->caller.number_type), bc->caller.number_type);
  8968. chan_misdn_log(2, bc->port, " --> PLAN: %s(%d)\n", misdn_to_str_plan(bc->caller.number_plan), bc->caller.number_plan);
  8969. ast_channel_caller(chan)->id.number.plan = misdn_to_ast_ton(bc->caller.number_type)
  8970. | misdn_to_ast_plan(bc->caller.number_plan);
  8971. chan_misdn_log(2, bc->port, " --> PRES: %s(%d)\n", misdn_to_str_pres(bc->caller.presentation), bc->caller.presentation);
  8972. chan_misdn_log(2, bc->port, " --> SCREEN: %s(%d)\n", misdn_to_str_screen(bc->caller.screening), bc->caller.screening);
  8973. ast_channel_caller(chan)->id.number.presentation = misdn_to_ast_pres(bc->caller.presentation)
  8974. | misdn_to_ast_screen(bc->caller.screening);
  8975. ast_set_callerid(chan, bc->caller.number, NULL, bc->caller.number);
  8976. misdn_cfg_get(bc->port, MISDN_CFG_APPEND_MSN_TO_CALLERID_TAG, &append_msn, sizeof(append_msn));
  8977. if (append_msn) {
  8978. strncat(bc->incoming_cid_tag, "_", sizeof(bc->incoming_cid_tag) - strlen(bc->incoming_cid_tag) - 1);
  8979. strncat(bc->incoming_cid_tag, bc->dialed.number, sizeof(bc->incoming_cid_tag) - strlen(bc->incoming_cid_tag) - 1);
  8980. }
  8981. ast_channel_lock(chan);
  8982. ast_channel_caller(chan)->id.tag = ast_strdup(bc->incoming_cid_tag);
  8983. ast_channel_unlock(chan);
  8984. if (!ast_strlen_zero(bc->redirecting.from.number)) {
  8985. /* Add configured prefix to redirecting.from.number */
  8986. misdn_add_number_prefix(bc->port, bc->redirecting.from.number_type, bc->redirecting.from.number, sizeof(bc->redirecting.from.number));
  8987. /* Update asterisk channel redirecting information */
  8988. misdn_copy_redirecting_to_ast(chan, &bc->redirecting, bc->incoming_cid_tag);
  8989. }
  8990. pbx_builtin_setvar_helper(chan, "TRANSFERCAPABILITY", ast_transfercapability2str(bc->capability));
  8991. ast_channel_transfercapability_set(chan, bc->capability);
  8992. switch (bc->capability) {
  8993. case INFO_CAPABILITY_DIGITAL_UNRESTRICTED:
  8994. pbx_builtin_setvar_helper(chan, "CALLTYPE", "DIGITAL");
  8995. break;
  8996. default:
  8997. pbx_builtin_setvar_helper(chan, "CALLTYPE", "SPEECH");
  8998. break;
  8999. }
  9000. if (!strstr(ch->allowed_bearers, "all")) {
  9001. int i;
  9002. for (i = 0; i < ARRAY_LEN(allowed_bearers_array); ++i) {
  9003. if (allowed_bearers_array[i].cap == bc->capability) {
  9004. if (strstr(ch->allowed_bearers, allowed_bearers_array[i].name)) {
  9005. /* The bearer capability is allowed */
  9006. if (allowed_bearers_array[i].deprecated) {
  9007. chan_misdn_log(0, bc->port, "%s in allowed_bearers list is deprecated\n",
  9008. allowed_bearers_array[i].name);
  9009. }
  9010. break;
  9011. }
  9012. }
  9013. }
  9014. if (i == ARRAY_LEN(allowed_bearers_array)) {
  9015. /* We did not find the bearer capability */
  9016. chan_misdn_log(0, bc->port, "Bearer capability not allowed: %s(%d)\n",
  9017. bearer2str(bc->capability), bc->capability);
  9018. ch->state = MISDN_EXTCANTMATCH;
  9019. chan_list_unref(ch, "BC not allowed, releasing call");
  9020. bc->out_cause = AST_CAUSE_INCOMPATIBLE_DESTINATION;
  9021. return RESPONSE_RELEASE_SETUP;
  9022. }
  9023. }
  9024. /** queue new chan **/
  9025. cl_queue_chan(ch);
  9026. if (bc->fac_in.Function != Fac_None) {
  9027. misdn_facility_ie_handler(event, bc, ch);
  9028. }
  9029. /* Check for Pickup Request first */
  9030. if (!strcmp(ast_channel_exten(chan), pickupexten)) {
  9031. if (!ch->noautorespond_on_setup) {
  9032. /* Sending SETUP_ACK */
  9033. misdn_lib_send_event(bc, EVENT_SETUP_ACKNOWLEDGE);
  9034. } else {
  9035. ch->state = MISDN_INCOMING_SETUP;
  9036. }
  9037. if (ast_pickup_call(chan)) {
  9038. hangup_chan(ch, bc);
  9039. } else {
  9040. ch->state = MISDN_CALLING_ACKNOWLEDGE;
  9041. hangup_chan(ch, bc);
  9042. ch->ast = NULL;
  9043. break;
  9044. }
  9045. }
  9046. /*
  9047. * added support for s extension hope it will help those poor cretains
  9048. * which haven't overlap dial.
  9049. */
  9050. misdn_cfg_get(bc->port, MISDN_CFG_ALWAYS_IMMEDIATE, &ai, sizeof(ai));
  9051. if (ai) {
  9052. do_immediate_setup(bc, ch, chan);
  9053. break;
  9054. }
  9055. /* check if we should jump into s when we have no dialed.number */
  9056. misdn_cfg_get(bc->port, MISDN_CFG_IMMEDIATE, &im, sizeof(im));
  9057. if (im && ast_strlen_zero(bc->dialed.number)) {
  9058. do_immediate_setup(bc, ch, chan);
  9059. break;
  9060. }
  9061. chan_misdn_log(5, bc->port, "CONTEXT:%s\n", ch->context);
  9062. if (!ast_canmatch_extension(ch->ast, ch->context, bc->dialed.number, 1, bc->caller.number)) {
  9063. if (ast_exists_extension(ch->ast, ch->context, "i", 1, bc->caller.number)) {
  9064. ast_log(LOG_WARNING,
  9065. "Extension '%s@%s' can never match. Jumping to 'i' extension. port:%d\n",
  9066. bc->dialed.number, ch->context, bc->port);
  9067. pbx_builtin_setvar_helper(ch->ast, "INVALID_EXTEN", bc->dialed.number);
  9068. ast_channel_exten_set(ch->ast, "i");
  9069. misdn_lib_send_event(bc, EVENT_SETUP_ACKNOWLEDGE);
  9070. ch->state = MISDN_DIALING;
  9071. start_pbx(ch, bc, chan);
  9072. break;
  9073. }
  9074. ast_log(LOG_WARNING,
  9075. "Extension '%s@%s' can never match. Disconnecting. port:%d\n"
  9076. "\tMaybe you want to add an 'i' extension to catch this case.\n",
  9077. bc->dialed.number, ch->context, bc->port);
  9078. if (bc->nt) {
  9079. hanguptone_indicate(ch);
  9080. }
  9081. ch->state = MISDN_EXTCANTMATCH;
  9082. bc->out_cause = AST_CAUSE_UNALLOCATED;
  9083. misdn_lib_send_event(bc, bc->nt ? EVENT_RELEASE_COMPLETE : EVENT_RELEASE);
  9084. break;
  9085. }
  9086. /* Whatever happens, when sending_complete is set or we are PTMP TE, we will definitely
  9087. * jump into the dialplan, when the dialed extension does not exist, the 's' extension
  9088. * will be used by Asterisk automatically. */
  9089. if (bc->sending_complete || (!bc->nt && !misdn_lib_is_ptp(bc->port))) {
  9090. if (!ch->noautorespond_on_setup) {
  9091. ch->state=MISDN_DIALING;
  9092. misdn_lib_send_event(bc, EVENT_PROCEEDING);
  9093. } else {
  9094. ch->state = MISDN_INCOMING_SETUP;
  9095. }
  9096. start_pbx(ch, bc, chan);
  9097. break;
  9098. }
  9099. /*
  9100. * When we are NT and overlapdial is set and if
  9101. * the number is empty, we wait for the ISDN timeout
  9102. * instead of our own timer.
  9103. */
  9104. if (ch->overlap_dial && bc->nt && !bc->dialed.number[0]) {
  9105. wait_for_digits(ch, bc, chan);
  9106. break;
  9107. }
  9108. /*
  9109. * If overlapdial we will definitely send a SETUP_ACKNOWLEDGE and wait for more
  9110. * Infos with a Interdigit Timeout.
  9111. * */
  9112. if (ch->overlap_dial) {
  9113. ast_mutex_lock(&ch->overlap_tv_lock);
  9114. ch->overlap_tv = ast_tvnow();
  9115. ast_mutex_unlock(&ch->overlap_tv_lock);
  9116. wait_for_digits(ch, bc, chan);
  9117. if (ch->overlap_dial_task == -1) {
  9118. ch->overlap_dial_task =
  9119. misdn_tasks_add_variable(ch->overlap_dial, misdn_overlap_dial_task, ch);
  9120. }
  9121. break;
  9122. }
  9123. /* If the extension does not exist and we're not TE_PTMP we wait for more digits
  9124. * without interdigit timeout.
  9125. * */
  9126. if (!ast_exists_extension(ch->ast, ch->context, bc->dialed.number, 1, bc->caller.number)) {
  9127. wait_for_digits(ch, bc, chan);
  9128. break;
  9129. }
  9130. /*
  9131. * If the extension exists let's just jump into it.
  9132. * */
  9133. if (ast_exists_extension(ch->ast, ch->context, bc->dialed.number, 1, bc->caller.number)) {
  9134. misdn_lib_send_event(bc, bc->need_more_infos ? EVENT_SETUP_ACKNOWLEDGE : EVENT_PROCEEDING);
  9135. ch->state = MISDN_DIALING;
  9136. start_pbx(ch, bc, chan);
  9137. break;
  9138. }
  9139. break;
  9140. }
  9141. #if defined(AST_MISDN_ENHANCEMENTS)
  9142. case EVENT_REGISTER:
  9143. if (bc->fac_in.Function != Fac_None) {
  9144. misdn_facility_ie_handler(event, bc, ch);
  9145. }
  9146. /*
  9147. * Shut down this connection immediately.
  9148. * The current design of chan_misdn data structures
  9149. * does not allow the proper handling of inbound call records
  9150. * without an assigned B channel. Therefore, we cannot
  9151. * be the CCBS User-B party in a point-to-point setup.
  9152. */
  9153. bc->fac_out.Function = Fac_None;
  9154. bc->out_cause = AST_CAUSE_NORMAL_CLEARING;
  9155. misdn_lib_send_event(bc, EVENT_RELEASE_COMPLETE);
  9156. break;
  9157. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  9158. case EVENT_SETUP_ACKNOWLEDGE:
  9159. ch->state = MISDN_CALLING_ACKNOWLEDGE;
  9160. if (bc->channel) {
  9161. update_name(ch->ast,bc->port,bc->channel);
  9162. }
  9163. if (bc->fac_in.Function != Fac_None) {
  9164. misdn_facility_ie_handler(event, bc, ch);
  9165. }
  9166. if (!ast_strlen_zero(bc->infos_pending)) {
  9167. /* TX Pending Infos */
  9168. strncat(bc->dialed.number, bc->infos_pending, sizeof(bc->dialed.number) - strlen(bc->dialed.number) - 1);
  9169. if (!ch->ast) {
  9170. break;
  9171. }
  9172. ast_channel_exten_set(ch->ast, bc->dialed.number);
  9173. ast_copy_string(bc->info_dad, bc->infos_pending, sizeof(bc->info_dad));
  9174. ast_copy_string(bc->infos_pending, "", sizeof(bc->infos_pending));
  9175. misdn_lib_send_event(bc, EVENT_INFORMATION);
  9176. }
  9177. break;
  9178. case EVENT_PROCEEDING:
  9179. if (misdn_cap_is_speech(bc->capability) &&
  9180. misdn_inband_avail(bc)) {
  9181. start_bc_tones(ch);
  9182. }
  9183. ch->state = MISDN_PROCEEDING;
  9184. if (bc->fac_in.Function != Fac_None) {
  9185. misdn_facility_ie_handler(event, bc, ch);
  9186. }
  9187. if (!ch->ast) {
  9188. break;
  9189. }
  9190. ast_queue_control(ch->ast, AST_CONTROL_PROCEEDING);
  9191. break;
  9192. case EVENT_PROGRESS:
  9193. if (bc->channel) {
  9194. update_name(ch->ast, bc->port, bc->channel);
  9195. }
  9196. if (bc->fac_in.Function != Fac_None) {
  9197. misdn_facility_ie_handler(event, bc, ch);
  9198. }
  9199. if (!bc->nt) {
  9200. if (misdn_cap_is_speech(bc->capability) &&
  9201. misdn_inband_avail(bc)) {
  9202. start_bc_tones(ch);
  9203. }
  9204. ch->state = MISDN_PROGRESS;
  9205. if (!ch->ast) {
  9206. break;
  9207. }
  9208. ast_queue_control(ch->ast, AST_CONTROL_PROGRESS);
  9209. }
  9210. break;
  9211. case EVENT_ALERTING:
  9212. ch->state = MISDN_ALERTING;
  9213. if (!ch->ast) {
  9214. break;
  9215. }
  9216. if (bc->fac_in.Function != Fac_None) {
  9217. misdn_facility_ie_handler(event, bc, ch);
  9218. }
  9219. ast_queue_control(ch->ast, AST_CONTROL_RINGING);
  9220. ast_channel_lock(ch->ast);
  9221. ast_setstate(ch->ast, AST_STATE_RINGING);
  9222. ast_channel_unlock(ch->ast);
  9223. cb_log(7, bc->port, " --> Set State Ringing\n");
  9224. if (misdn_cap_is_speech(bc->capability) && misdn_inband_avail(bc)) {
  9225. cb_log(1, bc->port, "Starting Tones, we have inband Data\n");
  9226. start_bc_tones(ch);
  9227. } else {
  9228. cb_log(3, bc->port, " --> We have no inband Data, the other end must create ringing\n");
  9229. if (ch->far_alerting) {
  9230. cb_log(1, bc->port, " --> The other end can not do ringing eh ?.. we must do all ourself..");
  9231. start_bc_tones(ch);
  9232. /*tone_indicate(ch, TONE_FAR_ALERTING);*/
  9233. }
  9234. }
  9235. break;
  9236. case EVENT_CONNECT:
  9237. if (bc->fac_in.Function != Fac_None) {
  9238. misdn_facility_ie_handler(event, bc, ch);
  9239. }
  9240. #if defined(AST_MISDN_ENHANCEMENTS)
  9241. if (bc->div_leg_3_rx_wanted) {
  9242. bc->div_leg_3_rx_wanted = 0;
  9243. if (ch->ast) {
  9244. struct ast_party_redirecting redirecting;
  9245. ast_channel_redirecting(ch->ast)->to.number.presentation =
  9246. AST_PRES_RESTRICTED | AST_PRES_USER_NUMBER_UNSCREENED;
  9247. ast_party_redirecting_init(&redirecting);
  9248. ast_party_redirecting_copy(&redirecting, ast_channel_redirecting(ch->ast));
  9249. /*
  9250. * Reset any earlier private redirecting id representations and
  9251. * make sure that it is invalidated at the remote end.
  9252. */
  9253. ast_party_id_reset(&redirecting.priv_orig);
  9254. ast_party_id_reset(&redirecting.priv_from);
  9255. ast_party_id_reset(&redirecting.priv_to);
  9256. ast_channel_queue_redirecting_update(ch->ast, &redirecting, NULL);
  9257. ast_party_redirecting_free(&redirecting);
  9258. }
  9259. }
  9260. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  9261. /* we answer when we've got our very new L3 ID from the NT stack */
  9262. misdn_lib_send_event(bc, EVENT_CONNECT_ACKNOWLEDGE);
  9263. if (!ch->ast) {
  9264. break;
  9265. }
  9266. stop_indicate(ch);
  9267. #if defined(AST_MISDN_ENHANCEMENTS)
  9268. if (ch->record_id != -1) {
  9269. /*
  9270. * We will delete the associated call completion
  9271. * record since we now have a completed call.
  9272. * We will not wait/depend on the network to tell
  9273. * us to delete it.
  9274. */
  9275. AST_LIST_LOCK(&misdn_cc_records_db);
  9276. cc_record = misdn_cc_find_by_id(ch->record_id);
  9277. if (cc_record) {
  9278. if (cc_record->ptp && cc_record->mode.ptp.bc) {
  9279. /* Close the call-completion signaling link */
  9280. cc_record->mode.ptp.bc->fac_out.Function = Fac_None;
  9281. cc_record->mode.ptp.bc->out_cause = AST_CAUSE_NORMAL_CLEARING;
  9282. misdn_lib_send_event(cc_record->mode.ptp.bc, EVENT_RELEASE_COMPLETE);
  9283. }
  9284. misdn_cc_delete(cc_record);
  9285. }
  9286. AST_LIST_UNLOCK(&misdn_cc_records_db);
  9287. ch->record_id = -1;
  9288. if (ch->peer) {
  9289. misdn_cc_set_peer_var(ch->peer, MISDN_CC_RECORD_ID, "");
  9290. ao2_ref(ch->peer, -1);
  9291. ch->peer = NULL;
  9292. }
  9293. }
  9294. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  9295. if (!ast_strlen_zero(bc->connected.number)) {
  9296. /* Add configured prefix to connected.number */
  9297. misdn_add_number_prefix(bc->port, bc->connected.number_type, bc->connected.number, sizeof(bc->connected.number));
  9298. /* Update the connected line information on the other channel */
  9299. misdn_update_remote_party(ch->ast, &bc->connected, AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER, bc->incoming_cid_tag);
  9300. }
  9301. ch->l3id = bc->l3_id;
  9302. ch->addr = bc->addr;
  9303. start_bc_tones(ch);
  9304. ch->state = MISDN_CONNECTED;
  9305. ast_queue_control(ch->ast, AST_CONTROL_ANSWER);
  9306. break;
  9307. case EVENT_CONNECT_ACKNOWLEDGE:
  9308. ch->l3id = bc->l3_id;
  9309. ch->addr = bc->addr;
  9310. start_bc_tones(ch);
  9311. ch->state = MISDN_CONNECTED;
  9312. break;
  9313. case EVENT_DISCONNECT:
  9314. /* we might not have an ch->ast ptr here anymore */
  9315. if (ch) {
  9316. if (bc->fac_in.Function != Fac_None) {
  9317. misdn_facility_ie_handler(event, bc, ch);
  9318. }
  9319. chan_misdn_log(3, bc->port, " --> org:%d nt:%d, inbandavail:%d state:%d\n", ch->originator, bc->nt, misdn_inband_avail(bc), ch->state);
  9320. if (ch->originator == ORG_AST && !bc->nt && misdn_inband_avail(bc) && ch->state != MISDN_CONNECTED) {
  9321. /* If there's inband information available (e.g. a
  9322. recorded message saying what was wrong with the
  9323. dialled number, or perhaps even giving an
  9324. alternative number, then play it instead of
  9325. immediately releasing the call */
  9326. chan_misdn_log(1, bc->port, " --> Inband Info Avail, not sending RELEASE\n");
  9327. ch->state = MISDN_DISCONNECTED;
  9328. start_bc_tones(ch);
  9329. if (ch->ast) {
  9330. ast_channel_hangupcause_set(ch->ast, bc->cause);
  9331. if (bc->cause == AST_CAUSE_USER_BUSY) {
  9332. ast_queue_control(ch->ast, AST_CONTROL_BUSY);
  9333. }
  9334. }
  9335. ch->need_busy = 0;
  9336. break;
  9337. }
  9338. bc->need_disconnect = 0;
  9339. stop_bc_tones(ch);
  9340. /* Check for held channel, to implement transfer */
  9341. held_ch = find_hold_call(bc);
  9342. if (!held_ch || !ch->ast || misdn_attempt_transfer(ch, held_ch)) {
  9343. hangup_chan(ch, bc);
  9344. }
  9345. } else {
  9346. held_ch = find_hold_call_l3(bc->l3_id);
  9347. if (held_ch) {
  9348. if (bc->fac_in.Function != Fac_None) {
  9349. misdn_facility_ie_handler(event, bc, held_ch);
  9350. }
  9351. if (held_ch->hold.state == MISDN_HOLD_ACTIVE) {
  9352. bc->need_disconnect = 0;
  9353. #if defined(TRANSFER_ON_HELD_CALL_HANGUP)
  9354. /*
  9355. * Some phones disconnect the held call and the active call at the
  9356. * same time to do the transfer. Unfortunately, either call could
  9357. * be disconnected first.
  9358. */
  9359. ch = find_hold_active_call(bc);
  9360. if (!ch || misdn_attempt_transfer(ch, held_ch)) {
  9361. held_ch->hold.state = MISDN_HOLD_DISCONNECT;
  9362. hangup_chan(held_ch, bc);
  9363. }
  9364. #else
  9365. hangup_chan(held_ch, bc);
  9366. #endif /* defined(TRANSFER_ON_HELD_CALL_HANGUP) */
  9367. }
  9368. }
  9369. }
  9370. if (held_ch) {
  9371. chan_list_unref(held_ch, "Done with held call");
  9372. }
  9373. bc->out_cause = -1;
  9374. if (bc->need_release) {
  9375. misdn_lib_send_event(bc, EVENT_RELEASE);
  9376. }
  9377. break;
  9378. case EVENT_RELEASE:
  9379. if (!ch) {
  9380. ch = find_hold_call_l3(bc->l3_id);
  9381. if (!ch) {
  9382. chan_misdn_log(1, bc->port,
  9383. " --> no Ch, so we've already released. (%s)\n",
  9384. manager_isdn_get_info(event));
  9385. return -1;
  9386. }
  9387. }
  9388. if (bc->fac_in.Function != Fac_None) {
  9389. misdn_facility_ie_handler(event, bc, ch);
  9390. }
  9391. bc->need_disconnect = 0;
  9392. bc->need_release = 0;
  9393. hangup_chan(ch, bc);
  9394. release_chan(ch, bc);
  9395. break;
  9396. case EVENT_RELEASE_COMPLETE:
  9397. if (!ch) {
  9398. ch = find_hold_call_l3(bc->l3_id);
  9399. }
  9400. bc->need_disconnect = 0;
  9401. bc->need_release = 0;
  9402. bc->need_release_complete = 0;
  9403. if (ch) {
  9404. if (bc->fac_in.Function != Fac_None) {
  9405. misdn_facility_ie_handler(event, bc, ch);
  9406. }
  9407. stop_bc_tones(ch);
  9408. hangup_chan(ch, bc);
  9409. release_chan(ch, bc);
  9410. } else {
  9411. #if defined(AST_MISDN_ENHANCEMENTS)
  9412. /*
  9413. * A call-completion signaling link established with
  9414. * REGISTER does not have a struct chan_list record
  9415. * associated with it.
  9416. */
  9417. AST_LIST_LOCK(&misdn_cc_records_db);
  9418. cc_record = misdn_cc_find_by_bc(bc);
  9419. if (cc_record) {
  9420. /* The call-completion signaling link is closed. */
  9421. misdn_cc_delete(cc_record);
  9422. }
  9423. AST_LIST_UNLOCK(&misdn_cc_records_db);
  9424. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  9425. chan_misdn_log(1, bc->port,
  9426. " --> no Ch, so we've already released. (%s)\n",
  9427. manager_isdn_get_info(event));
  9428. }
  9429. break;
  9430. case EVENT_BCHAN_ERROR:
  9431. case EVENT_CLEANUP:
  9432. stop_bc_tones(ch);
  9433. switch (ch->state) {
  9434. case MISDN_CALLING:
  9435. bc->cause = AST_CAUSE_DESTINATION_OUT_OF_ORDER;
  9436. break;
  9437. default:
  9438. break;
  9439. }
  9440. hangup_chan(ch, bc);
  9441. release_chan(ch, bc);
  9442. break;
  9443. case EVENT_TONE_GENERATE:
  9444. {
  9445. int tone_len = bc->tone_cnt;
  9446. struct ast_channel *ast = ch->ast;
  9447. void *tmp;
  9448. int res;
  9449. int (*generate)(struct ast_channel *chan, void *tmp, int datalen, int samples);
  9450. chan_misdn_log(9, bc->port, "TONE_GEN: len:%d\n", tone_len);
  9451. if (!ast) {
  9452. break;
  9453. }
  9454. if (!ast_channel_generator(ast)) {
  9455. break;
  9456. }
  9457. tmp = ast_channel_generatordata(ast);
  9458. ast_channel_generatordata_set(ast, NULL);
  9459. generate = ast_channel_generator(ast)->generate;
  9460. if (tone_len < 0 || tone_len > 512) {
  9461. ast_log(LOG_NOTICE, "TONE_GEN: len was %d, set to 128\n", tone_len);
  9462. tone_len = 128;
  9463. }
  9464. res = generate(ast, tmp, tone_len, tone_len);
  9465. ast_channel_generatordata_set(ast, tmp);
  9466. if (res) {
  9467. ast_log(LOG_WARNING, "Auto-deactivating generator\n");
  9468. ast_deactivate_generator(ast);
  9469. } else {
  9470. bc->tone_cnt = 0;
  9471. }
  9472. break;
  9473. }
  9474. case EVENT_BCHAN_DATA:
  9475. if (ch->bc->AOCD_need_export) {
  9476. export_aoc_vars(ch->originator, ch->ast, ch->bc);
  9477. }
  9478. if (!misdn_cap_is_speech(ch->bc->capability)) {
  9479. struct ast_frame frame;
  9480. /* In Data Modes we queue frames */
  9481. memset(&frame, 0, sizeof(frame));
  9482. frame.frametype = AST_FRAME_VOICE; /* we have no data frames yet */
  9483. frame.subclass.format = ast_format_alaw;
  9484. frame.datalen = bc->bframe_len;
  9485. frame.samples = bc->bframe_len;
  9486. frame.mallocd = 0;
  9487. frame.offset = 0;
  9488. frame.delivery = ast_tv(0, 0);
  9489. frame.src = NULL;
  9490. frame.data.ptr = bc->bframe;
  9491. if (ch->ast) {
  9492. ast_queue_frame(ch->ast, &frame);
  9493. }
  9494. } else {
  9495. struct pollfd pfd = { .fd = ch->pipe[1], .events = POLLOUT };
  9496. int t;
  9497. t = ast_poll(&pfd, 1, 0);
  9498. if (t < 0) {
  9499. chan_misdn_log(-1, bc->port, "poll() error (err=%s)\n", strerror(errno));
  9500. break;
  9501. }
  9502. if (!t) {
  9503. chan_misdn_log(9, bc->port, "poll() timed out\n");
  9504. break;
  9505. }
  9506. if (pfd.revents & POLLOUT) {
  9507. chan_misdn_log(9, bc->port, "writing %d bytes to asterisk\n", bc->bframe_len);
  9508. if (write(ch->pipe[1], bc->bframe, bc->bframe_len) <= 0) {
  9509. chan_misdn_log(0, bc->port, "Write returned <=0 (err=%s) --> hanging up channel\n", strerror(errno));
  9510. stop_bc_tones(ch);
  9511. hangup_chan(ch, bc);
  9512. release_chan(ch, bc);
  9513. }
  9514. } else {
  9515. chan_misdn_log(1, bc->port, "Write Pipe full!\n");
  9516. }
  9517. }
  9518. break;
  9519. case EVENT_TIMEOUT:
  9520. if (ch && bc) {
  9521. chan_misdn_log(1, bc->port, "--> state: %s\n", misdn_get_ch_state(ch));
  9522. }
  9523. switch (ch->state) {
  9524. case MISDN_DIALING:
  9525. case MISDN_PROGRESS:
  9526. if (bc->nt && !ch->nttimeout) {
  9527. break;
  9528. }
  9529. /* fall-through */
  9530. case MISDN_CALLING:
  9531. case MISDN_ALERTING:
  9532. case MISDN_PROCEEDING:
  9533. case MISDN_CALLING_ACKNOWLEDGE:
  9534. if (bc->nt) {
  9535. bc->progress_indicator = INFO_PI_INBAND_AVAILABLE;
  9536. hanguptone_indicate(ch);
  9537. }
  9538. bc->out_cause = AST_CAUSE_UNALLOCATED;
  9539. misdn_lib_send_event(bc, EVENT_DISCONNECT);
  9540. break;
  9541. case MISDN_WAITING4DIGS:
  9542. if (bc->nt) {
  9543. bc->progress_indicator = INFO_PI_INBAND_AVAILABLE;
  9544. bc->out_cause = AST_CAUSE_UNALLOCATED;
  9545. hanguptone_indicate(ch);
  9546. misdn_lib_send_event(bc, EVENT_DISCONNECT);
  9547. } else {
  9548. bc->out_cause = AST_CAUSE_NORMAL_CLEARING;
  9549. misdn_lib_send_event(bc, EVENT_RELEASE);
  9550. }
  9551. break;
  9552. case MISDN_CLEANING:
  9553. chan_misdn_log(1, bc->port, " --> in state cleaning .. so ignoring, the stack should clean it for us\n");
  9554. break;
  9555. default:
  9556. misdn_lib_send_event(bc, EVENT_RELEASE_COMPLETE);
  9557. break;
  9558. }
  9559. break;
  9560. /****************************/
  9561. /** Supplementary Services **/
  9562. /****************************/
  9563. case EVENT_RETRIEVE:
  9564. if (!ch) {
  9565. chan_misdn_log(4, bc->port, " --> no CH, searching for held call\n");
  9566. ch = find_hold_call_l3(bc->l3_id);
  9567. if (!ch || ch->hold.state != MISDN_HOLD_ACTIVE) {
  9568. ast_log(LOG_WARNING, "No held call found, cannot Retrieve\n");
  9569. misdn_lib_send_event(bc, EVENT_RETRIEVE_REJECT);
  9570. break;
  9571. }
  9572. }
  9573. /* remember the channel again */
  9574. ch->bc = bc;
  9575. ch->hold.state = MISDN_HOLD_IDLE;
  9576. ch->hold.port = 0;
  9577. ch->hold.channel = 0;
  9578. ast_queue_unhold(ch->ast);
  9579. if (misdn_lib_send_event(bc, EVENT_RETRIEVE_ACKNOWLEDGE) < 0) {
  9580. chan_misdn_log(4, bc->port, " --> RETRIEVE_ACK failed\n");
  9581. misdn_lib_send_event(bc, EVENT_RETRIEVE_REJECT);
  9582. }
  9583. break;
  9584. case EVENT_HOLD:
  9585. {
  9586. int hold_allowed;
  9587. RAII_VAR(struct ast_channel *, bridged, NULL, ast_channel_cleanup);
  9588. misdn_cfg_get(bc->port, MISDN_CFG_HOLD_ALLOWED, &hold_allowed, sizeof(hold_allowed));
  9589. if (!hold_allowed) {
  9590. chan_misdn_log(-1, bc->port, "Hold not allowed this port.\n");
  9591. misdn_lib_send_event(bc, EVENT_HOLD_REJECT);
  9592. break;
  9593. }
  9594. bridged = ast_channel_bridge_peer(ch->ast);
  9595. if (bridged) {
  9596. chan_misdn_log(2, bc->port, "Bridge Partner is of type: %s\n", ast_channel_tech(bridged)->type);
  9597. ch->l3id = bc->l3_id;
  9598. /* forget the channel now */
  9599. ch->bc = NULL;
  9600. ch->hold.state = MISDN_HOLD_ACTIVE;
  9601. ch->hold.port = bc->port;
  9602. ch->hold.channel = bc->channel;
  9603. ast_queue_hold(ch->ast, NULL);
  9604. misdn_lib_send_event(bc, EVENT_HOLD_ACKNOWLEDGE);
  9605. } else {
  9606. misdn_lib_send_event(bc, EVENT_HOLD_REJECT);
  9607. chan_misdn_log(0, bc->port, "We aren't bridged to anybody\n");
  9608. }
  9609. break;
  9610. }
  9611. case EVENT_NOTIFY:
  9612. if (bc->redirecting.to_changed) {
  9613. /* Add configured prefix to redirecting.to.number */
  9614. misdn_add_number_prefix(bc->port, bc->redirecting.to.number_type,
  9615. bc->redirecting.to.number, sizeof(bc->redirecting.to.number));
  9616. }
  9617. switch (bc->notify_description_code) {
  9618. case mISDN_NOTIFY_CODE_DIVERSION_ACTIVATED:
  9619. /* Ignore for now. */
  9620. bc->redirecting.to_changed = 0;
  9621. break;
  9622. case mISDN_NOTIFY_CODE_CALL_IS_DIVERTING:
  9623. {
  9624. struct ast_party_redirecting redirecting;
  9625. if (!bc->redirecting.to_changed) {
  9626. break;
  9627. }
  9628. bc->redirecting.to_changed = 0;
  9629. if (!ch || !ch->ast) {
  9630. break;
  9631. }
  9632. switch (ch->state) {
  9633. case MISDN_ALERTING:
  9634. /* Call is deflecting after we have seen an ALERTING message */
  9635. bc->redirecting.reason = mISDN_REDIRECTING_REASON_NO_REPLY;
  9636. break;
  9637. default:
  9638. /* Call is deflecting for call forwarding unconditional or busy reason. */
  9639. bc->redirecting.reason = mISDN_REDIRECTING_REASON_UNKNOWN;
  9640. break;
  9641. }
  9642. misdn_copy_redirecting_to_ast(ch->ast, &bc->redirecting, bc->incoming_cid_tag);
  9643. ast_party_redirecting_init(&redirecting);
  9644. ast_party_redirecting_copy(&redirecting, ast_channel_redirecting(ch->ast));
  9645. /*
  9646. * Reset any earlier private redirecting id representations and
  9647. * make sure that it is invalidated at the remote end.
  9648. */
  9649. ast_party_id_reset(&redirecting.priv_orig);
  9650. ast_party_id_reset(&redirecting.priv_from);
  9651. ast_party_id_reset(&redirecting.priv_to);
  9652. ast_channel_queue_redirecting_update(ch->ast, &redirecting, NULL);
  9653. ast_party_redirecting_free(&redirecting);
  9654. break;
  9655. }
  9656. case mISDN_NOTIFY_CODE_CALL_TRANSFER_ALERTING:
  9657. /*
  9658. * It would be preferable to update the connected line information
  9659. * only when the message callStatus is active. However, the
  9660. * optional redirection number may not be present in the active
  9661. * message if an alerting message were received earlier.
  9662. *
  9663. * The consequences if we wind up sending two updates is benign.
  9664. * The other end will think that it got transferred twice.
  9665. */
  9666. if (!bc->redirecting.to_changed) {
  9667. break;
  9668. }
  9669. bc->redirecting.to_changed = 0;
  9670. if (!ch || !ch->ast) {
  9671. break;
  9672. }
  9673. misdn_update_remote_party(ch->ast, &bc->redirecting.to,
  9674. AST_CONNECTED_LINE_UPDATE_SOURCE_TRANSFER_ALERTING,
  9675. bc->incoming_cid_tag);
  9676. break;
  9677. case mISDN_NOTIFY_CODE_CALL_TRANSFER_ACTIVE:
  9678. if (!bc->redirecting.to_changed) {
  9679. break;
  9680. }
  9681. bc->redirecting.to_changed = 0;
  9682. if (!ch || !ch->ast) {
  9683. break;
  9684. }
  9685. misdn_update_remote_party(ch->ast, &bc->redirecting.to,
  9686. AST_CONNECTED_LINE_UPDATE_SOURCE_TRANSFER, bc->incoming_cid_tag);
  9687. break;
  9688. default:
  9689. bc->redirecting.to_changed = 0;
  9690. chan_misdn_log(0, bc->port," --> not yet handled: notify code:0x%02X\n",
  9691. bc->notify_description_code);
  9692. break;
  9693. }
  9694. bc->notify_description_code = mISDN_NOTIFY_CODE_INVALID;
  9695. break;
  9696. case EVENT_FACILITY:
  9697. if (bc->fac_in.Function == Fac_None) {
  9698. /* This is a FACILITY message so we MUST have a facility ie */
  9699. chan_misdn_log(0, bc->port," --> Missing facility ie or unknown facility ie contents.\n");
  9700. } else {
  9701. misdn_facility_ie_handler(event, bc, ch);
  9702. }
  9703. /* In case it came in on a FACILITY message and we did not handle it. */
  9704. bc->redirecting.to_changed = 0;
  9705. bc->notify_description_code = mISDN_NOTIFY_CODE_INVALID;
  9706. break;
  9707. case EVENT_RESTART:
  9708. if (!bc->dummy) {
  9709. stop_bc_tones(ch);
  9710. release_chan(ch, bc);
  9711. }
  9712. break;
  9713. default:
  9714. chan_misdn_log(1, 0, "Got Unknown Event\n");
  9715. break;
  9716. }
  9717. if (ch) {
  9718. chan_list_unref(ch, "cb_event complete OK");
  9719. }
  9720. return RESPONSE_OK;
  9721. }
  9722. /** TE STUFF END **/
  9723. #if defined(AST_MISDN_ENHANCEMENTS)
  9724. /*!
  9725. * \internal
  9726. * \brief Get call completion record information.
  9727. *
  9728. * \param chan Asterisk channel to operate upon. (Not used)
  9729. * \param function_name Name of the function that called us.
  9730. * \param function_args Argument string passed to function (Could be NULL)
  9731. * \param buf Buffer to put returned string.
  9732. * \param size Size of the supplied buffer including the null terminator.
  9733. *
  9734. * \retval 0 on success.
  9735. * \retval -1 on error.
  9736. */
  9737. static int misdn_cc_read(struct ast_channel *chan, const char *function_name,
  9738. char *function_args, char *buf, size_t size)
  9739. {
  9740. char *parse;
  9741. struct misdn_cc_record *cc_record;
  9742. AST_DECLARE_APP_ARGS(args,
  9743. AST_APP_ARG(cc_id); /* Call completion record ID value. */
  9744. AST_APP_ARG(get_name); /* Name of what to get */
  9745. AST_APP_ARG(other); /* Any extraneous garbage arguments */
  9746. );
  9747. /* Ensure that the buffer is empty */
  9748. *buf = 0;
  9749. if (ast_strlen_zero(function_args)) {
  9750. ast_log(LOG_ERROR, "Function '%s' requires arguments.\n", function_name);
  9751. return -1;
  9752. }
  9753. parse = ast_strdupa(function_args);
  9754. AST_STANDARD_APP_ARGS(args, parse);
  9755. if (!args.argc || ast_strlen_zero(args.cc_id)) {
  9756. ast_log(LOG_ERROR, "Function '%s' missing call completion record ID.\n",
  9757. function_name);
  9758. return -1;
  9759. }
  9760. if (!isdigit(*args.cc_id)) {
  9761. ast_log(LOG_ERROR, "Function '%s' call completion record ID must be numeric.\n",
  9762. function_name);
  9763. return -1;
  9764. }
  9765. if (ast_strlen_zero(args.get_name)) {
  9766. ast_log(LOG_ERROR, "Function '%s' missing what-to-get parameter.\n",
  9767. function_name);
  9768. return -1;
  9769. }
  9770. AST_LIST_LOCK(&misdn_cc_records_db);
  9771. cc_record = misdn_cc_find_by_id(atoi(args.cc_id));
  9772. if (cc_record) {
  9773. if (!strcasecmp("a-all", args.get_name)) {
  9774. snprintf(buf, size, "\"%s\" <%s>", cc_record->redial.caller.name,
  9775. cc_record->redial.caller.number);
  9776. } else if (!strcasecmp("a-name", args.get_name)) {
  9777. ast_copy_string(buf, cc_record->redial.caller.name, size);
  9778. } else if (!strncasecmp("a-num", args.get_name, 5)) {
  9779. ast_copy_string(buf, cc_record->redial.caller.number, size);
  9780. } else if (!strcasecmp("a-ton", args.get_name)) {
  9781. snprintf(buf, size, "%d",
  9782. misdn_to_ast_plan(cc_record->redial.caller.number_plan)
  9783. | misdn_to_ast_ton(cc_record->redial.caller.number_type));
  9784. } else if (!strncasecmp("a-pres", args.get_name, 6)) {
  9785. ast_copy_string(buf, ast_named_caller_presentation(
  9786. misdn_to_ast_pres(cc_record->redial.caller.presentation)
  9787. | misdn_to_ast_screen(cc_record->redial.caller.screening)), size);
  9788. } else if (!strcasecmp("a-busy", args.get_name)) {
  9789. ast_copy_string(buf, cc_record->party_a_free ? "no" : "yes", size);
  9790. } else if (!strncasecmp("b-num", args.get_name, 5)) {
  9791. ast_copy_string(buf, cc_record->redial.dialed.number, size);
  9792. } else if (!strcasecmp("b-ton", args.get_name)) {
  9793. snprintf(buf, size, "%d",
  9794. misdn_to_ast_plan(cc_record->redial.dialed.number_plan)
  9795. | misdn_to_ast_ton(cc_record->redial.dialed.number_type));
  9796. } else if (!strcasecmp("port", args.get_name)) {
  9797. snprintf(buf, size, "%d", cc_record->port);
  9798. } else if (!strcasecmp("available-notify-priority", args.get_name)) {
  9799. snprintf(buf, size, "%d", cc_record->remote_user_free.priority);
  9800. } else if (!strcasecmp("available-notify-exten", args.get_name)) {
  9801. ast_copy_string(buf, cc_record->remote_user_free.exten, size);
  9802. } else if (!strcasecmp("available-notify-context", args.get_name)) {
  9803. ast_copy_string(buf, cc_record->remote_user_free.context, size);
  9804. } else if (!strcasecmp("busy-notify-priority", args.get_name)) {
  9805. snprintf(buf, size, "%d", cc_record->b_free.priority);
  9806. } else if (!strcasecmp("busy-notify-exten", args.get_name)) {
  9807. ast_copy_string(buf, cc_record->b_free.exten, size);
  9808. } else if (!strcasecmp("busy-notify-context", args.get_name)) {
  9809. ast_copy_string(buf, cc_record->b_free.context, size);
  9810. } else {
  9811. AST_LIST_UNLOCK(&misdn_cc_records_db);
  9812. ast_log(LOG_ERROR, "Function '%s': Unknown what-to-get '%s'.\n", function_name, args.get_name);
  9813. return -1;
  9814. }
  9815. }
  9816. AST_LIST_UNLOCK(&misdn_cc_records_db);
  9817. return 0;
  9818. }
  9819. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  9820. #if defined(AST_MISDN_ENHANCEMENTS)
  9821. static struct ast_custom_function misdn_cc_function = {
  9822. .name = "mISDN_CC",
  9823. .synopsis = "Get call completion record information.",
  9824. .syntax = "mISDN_CC(${MISDN_CC_RECORD_ID},<what-to-get>)",
  9825. .desc =
  9826. "mISDN_CC(${MISDN_CC_RECORD_ID},<what-to-get>)\n"
  9827. "The following can be retrieved:\n"
  9828. "\"a-num\", \"a-name\", \"a-all\", \"a-ton\", \"a-pres\", \"a-busy\",\n"
  9829. "\"b-num\", \"b-ton\", \"port\",\n"
  9830. " User-A is available for call completion:\n"
  9831. " \"available-notify-priority\",\n"
  9832. " \"available-notify-exten\",\n"
  9833. " \"available-notify-context\",\n"
  9834. " User-A is busy:\n"
  9835. " \"busy-notify-priority\",\n"
  9836. " \"busy-notify-exten\",\n"
  9837. " \"busy-notify-context\"\n",
  9838. .read = misdn_cc_read,
  9839. };
  9840. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  9841. /******************************************
  9842. *
  9843. * Asterisk Channel Endpoint END
  9844. *
  9845. *
  9846. *******************************************/
  9847. static int unload_module(void)
  9848. {
  9849. /* First, take us out of the channel loop */
  9850. ast_verb(0, "-- Unregistering mISDN Channel Driver --\n");
  9851. misdn_tasks_destroy();
  9852. if (!g_config_initialized) {
  9853. return 0;
  9854. }
  9855. ast_cli_unregister_multiple(chan_misdn_clis, sizeof(chan_misdn_clis) / sizeof(struct ast_cli_entry));
  9856. /* ast_unregister_application("misdn_crypt"); */
  9857. ast_unregister_application("misdn_set_opt");
  9858. ast_unregister_application("misdn_facility");
  9859. ast_unregister_application("misdn_check_l2l1");
  9860. #if defined(AST_MISDN_ENHANCEMENTS)
  9861. ast_unregister_application(misdn_command_name);
  9862. ast_custom_function_unregister(&misdn_cc_function);
  9863. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  9864. ast_channel_unregister(&misdn_tech);
  9865. free_robin_list();
  9866. misdn_cfg_destroy();
  9867. misdn_lib_destroy();
  9868. ast_free(misdn_out_calls);
  9869. ast_free(misdn_in_calls);
  9870. ast_free(misdn_debug_only);
  9871. ast_free(misdn_ports);
  9872. ast_free(misdn_debug);
  9873. #if defined(AST_MISDN_ENHANCEMENTS)
  9874. misdn_cc_destroy();
  9875. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  9876. ao2_cleanup(misdn_tech.capabilities);
  9877. misdn_tech.capabilities = NULL;
  9878. return 0;
  9879. }
  9880. /*!
  9881. * \brief Load the module
  9882. *
  9883. * Module loading including tests for configuration or dependencies.
  9884. * This function can return AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_DECLINE,
  9885. * or AST_MODULE_LOAD_SUCCESS. If a dependency or environment variable fails
  9886. * tests return AST_MODULE_LOAD_FAILURE. If the module can not load the
  9887. * configuration file or other non-critical problem return
  9888. * AST_MODULE_LOAD_DECLINE. On success return AST_MODULE_LOAD_SUCCESS.
  9889. */
  9890. static int load_module(void)
  9891. {
  9892. int i, port;
  9893. int ntflags = 0, ntkc = 0;
  9894. char ports[256] = "";
  9895. char tempbuf[BUFFERSIZE + 1];
  9896. char ntfile[BUFFERSIZE + 1];
  9897. struct misdn_lib_iface iface = {
  9898. .cb_event = cb_events,
  9899. .cb_log = chan_misdn_log,
  9900. .cb_jb_empty = chan_misdn_jb_empty,
  9901. };
  9902. if (!(misdn_tech.capabilities = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
  9903. return AST_MODULE_LOAD_DECLINE;
  9904. }
  9905. ast_format_cap_append(misdn_tech.capabilities, ast_format_alaw, 0);
  9906. max_ports = misdn_lib_maxports_get();
  9907. if (max_ports <= 0) {
  9908. ast_log(LOG_ERROR, "Unable to initialize mISDN\n");
  9909. return AST_MODULE_LOAD_DECLINE;
  9910. }
  9911. if (misdn_cfg_init(max_ports, 0)) {
  9912. ast_log(LOG_ERROR, "Unable to initialize misdn_config.\n");
  9913. return AST_MODULE_LOAD_DECLINE;
  9914. }
  9915. g_config_initialized = 1;
  9916. #if defined(AST_MISDN_ENHANCEMENTS)
  9917. misdn_cc_init();
  9918. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  9919. misdn_debug = ast_malloc(sizeof(int) * (max_ports + 1));
  9920. if (!misdn_debug) {
  9921. ast_log(LOG_ERROR, "Out of memory for misdn_debug\n");
  9922. return AST_MODULE_LOAD_DECLINE;
  9923. }
  9924. misdn_ports = ast_malloc(sizeof(int) * (max_ports + 1));
  9925. if (!misdn_ports) {
  9926. ast_free(misdn_debug);
  9927. ast_log(LOG_ERROR, "Out of memory for misdn_ports\n");
  9928. return AST_MODULE_LOAD_DECLINE;
  9929. }
  9930. misdn_cfg_get(0, MISDN_GEN_DEBUG, &misdn_debug[0], sizeof(misdn_debug[0]));
  9931. for (i = 1; i <= max_ports; i++) {
  9932. misdn_debug[i] = misdn_debug[0];
  9933. misdn_ports[i] = i;
  9934. }
  9935. *misdn_ports = 0;
  9936. misdn_debug_only = ast_calloc(max_ports + 1, sizeof(int));
  9937. if (!misdn_debug_only) {
  9938. ast_free(misdn_ports);
  9939. ast_free(misdn_debug);
  9940. ast_log(LOG_ERROR, "Out of memory for misdn_debug_only\n");
  9941. return AST_MODULE_LOAD_DECLINE;
  9942. }
  9943. misdn_cfg_get(0, MISDN_GEN_TRACEFILE, tempbuf, sizeof(tempbuf));
  9944. if (!ast_strlen_zero(tempbuf)) {
  9945. tracing = 1;
  9946. }
  9947. misdn_in_calls = ast_malloc(sizeof(int) * (max_ports + 1));
  9948. if (!misdn_in_calls) {
  9949. ast_free(misdn_debug_only);
  9950. ast_free(misdn_ports);
  9951. ast_free(misdn_debug);
  9952. ast_log(LOG_ERROR, "Out of memory for misdn_in_calls\n");
  9953. return AST_MODULE_LOAD_DECLINE;
  9954. }
  9955. misdn_out_calls = ast_malloc(sizeof(int) * (max_ports + 1));
  9956. if (!misdn_out_calls) {
  9957. ast_free(misdn_in_calls);
  9958. ast_free(misdn_debug_only);
  9959. ast_free(misdn_ports);
  9960. ast_free(misdn_debug);
  9961. ast_log(LOG_ERROR, "Out of memory for misdn_out_calls\n");
  9962. return AST_MODULE_LOAD_DECLINE;
  9963. }
  9964. for (i = 1; i <= max_ports; i++) {
  9965. misdn_in_calls[i] = 0;
  9966. misdn_out_calls[i] = 0;
  9967. }
  9968. ast_mutex_init(&cl_te_lock);
  9969. ast_mutex_init(&release_lock);
  9970. misdn_cfg_update_ptp();
  9971. misdn_cfg_get_ports_string(ports);
  9972. if (!ast_strlen_zero(ports)) {
  9973. chan_misdn_log(0, 0, "Got: %s from get_ports\n", ports);
  9974. }
  9975. if (misdn_lib_init(ports, &iface, NULL)) {
  9976. chan_misdn_log(0, 0, "No te ports initialized\n");
  9977. }
  9978. misdn_cfg_get(0, MISDN_GEN_NTDEBUGFLAGS, &ntflags, sizeof(ntflags));
  9979. misdn_cfg_get(0, MISDN_GEN_NTDEBUGFILE, &ntfile, sizeof(ntfile));
  9980. misdn_cfg_get(0, MISDN_GEN_NTKEEPCALLS, &ntkc, sizeof(ntkc));
  9981. misdn_lib_nt_keepcalls(ntkc);
  9982. misdn_lib_nt_debug_init(ntflags, ntfile);
  9983. if (ast_channel_register(&misdn_tech)) {
  9984. ast_log(LOG_ERROR, "Unable to register channel class %s\n", misdn_type);
  9985. unload_module();
  9986. return AST_MODULE_LOAD_DECLINE;
  9987. }
  9988. ast_cli_register_multiple(chan_misdn_clis, sizeof(chan_misdn_clis) / sizeof(struct ast_cli_entry));
  9989. ast_register_application("misdn_set_opt", misdn_set_opt_exec, "misdn_set_opt",
  9990. "misdn_set_opt(:<opt><optarg>:<opt><optarg>...):\n"
  9991. "Sets mISDN opts. and optargs\n"
  9992. "\n"
  9993. "The available options are:\n"
  9994. " a - Have Asterisk detect DTMF tones on called channel\n"
  9995. " c - Make crypted outgoing call, optarg is keyindex\n"
  9996. " d - Send display text to called phone, text is the optarg\n"
  9997. " e - Perform echo cancellation on this channel,\n"
  9998. " takes taps as optarg (32,64,128,256)\n"
  9999. " e! - Disable echo cancellation on this channel\n"
  10000. " f - Enable fax detection\n"
  10001. " h - Make digital outgoing call\n"
  10002. " h1 - Make HDLC mode digital outgoing call\n"
  10003. " i - Ignore detected DTMF tones, don't signal them to Asterisk,\n"
  10004. " they will be transported inband.\n"
  10005. " jb - Set jitter buffer length, optarg is length\n"
  10006. " jt - Set jitter buffer upper threshold, optarg is threshold\n"
  10007. " jn - Disable jitter buffer\n"
  10008. " n - Disable mISDN DSP on channel.\n"
  10009. " Disables: echo cancel, DTMF detection, and volume control.\n"
  10010. " p - Caller ID presentation,\n"
  10011. " optarg is either 'allowed' or 'restricted'\n"
  10012. " s - Send Non-inband DTMF as inband\n"
  10013. " vr - Rx gain control, optarg is gain\n"
  10014. " vt - Tx gain control, optarg is gain\n"
  10015. );
  10016. ast_register_application("misdn_facility", misdn_facility_exec, "misdn_facility",
  10017. "misdn_facility(<FACILITY_TYPE>|<ARG1>|..)\n"
  10018. "Sends the Facility Message FACILITY_TYPE with \n"
  10019. "the given Arguments to the current ISDN Channel\n"
  10020. "Supported Facilities are:\n"
  10021. "\n"
  10022. "type=calldeflect args=Nr where to deflect\n"
  10023. #if defined(AST_MISDN_ENHANCEMENTS)
  10024. "type=callrerouting args=Nr where to deflect\n"
  10025. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  10026. );
  10027. ast_register_application("misdn_check_l2l1", misdn_check_l2l1, "misdn_check_l2l1",
  10028. "misdn_check_l2l1(<port>||g:<groupname>,timeout)\n"
  10029. "Checks if the L2 and L1 are up on either the given <port> or\n"
  10030. "on the ports in the group with <groupname>\n"
  10031. "If the L1/L2 are down, check_l2l1 gets up the L1/L2 and waits\n"
  10032. "for <timeout> seconds that this happens. Otherwise, nothing happens\n"
  10033. "\n"
  10034. "This application, ensures the L1/L2 state of the Ports in a group\n"
  10035. "it is intended to make the pmp_l1_check option redundant and to\n"
  10036. "fix a buggy switch config from your provider\n"
  10037. "\n"
  10038. "a sample dialplan would look like:\n\n"
  10039. "exten => _X.,1,misdn_check_l2l1(g:out|2)\n"
  10040. "exten => _X.,n,dial(mISDN/g:out/${EXTEN})\n"
  10041. );
  10042. #if defined(AST_MISDN_ENHANCEMENTS)
  10043. ast_register_application(misdn_command_name, misdn_command_exec, misdn_command_name,
  10044. "misdn_command(<command>[,<options>])\n"
  10045. "The following commands are defined:\n"
  10046. "cc-initialize\n"
  10047. " Setup mISDN support for call completion\n"
  10048. " Must call before doing any Dial() involving call completion.\n"
  10049. "ccnr-request,${MISDN_CC_RECORD_ID},<notify-context>,<user-a-extension>,<priority>\n"
  10050. " Request Call Completion No Reply activation\n"
  10051. "ccbs-request,${MISDN_CC_RECORD_ID},<notify-context>,<user-a-extension>,<priority>\n"
  10052. " Request Call Completion Busy Subscriber activation\n"
  10053. "cc-b-free,${MISDN_CC_RECORD_ID},<notify-context>,<user-a-extension>,<priority>\n"
  10054. " Set the dialplan location to notify when User-B is available but User-A is busy.\n"
  10055. " Setting this dialplan location is optional.\n"
  10056. "cc-a-busy,${MISDN_CC_RECORD_ID},<yes/no>\n"
  10057. " Set the busy status of call completion User-A\n"
  10058. "cc-deactivate,${MISDN_CC_RECORD_ID}\n"
  10059. " Deactivate the identified call completion request\n"
  10060. "\n"
  10061. "MISDN_CC_RECORD_ID is set when Dial() returns and call completion is possible\n"
  10062. "MISDN_CC_STATUS is set to ACTIVATED or ERROR after the call completion\n"
  10063. "activation request.\n"
  10064. "MISDN_ERROR_MSG is set to a descriptive message on error.\n"
  10065. );
  10066. ast_custom_function_register(&misdn_cc_function);
  10067. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  10068. misdn_cfg_get(0, MISDN_GEN_TRACEFILE, global_tracefile, sizeof(global_tracefile));
  10069. /* start the l1 watchers */
  10070. for (port = misdn_cfg_get_next_port(0); port >= 0; port = misdn_cfg_get_next_port(port)) {
  10071. int l1timeout;
  10072. misdn_cfg_get(port, MISDN_CFG_L1_TIMEOUT, &l1timeout, sizeof(l1timeout));
  10073. if (l1timeout) {
  10074. chan_misdn_log(4, 0, "Adding L1watcher task: port:%d timeout:%ds\n", port, l1timeout);
  10075. misdn_tasks_add(l1timeout * 1000, misdn_l1_task, &misdn_ports[port]);
  10076. }
  10077. }
  10078. chan_misdn_log(0, 0, "-- mISDN Channel Driver Registered --\n");
  10079. return 0;
  10080. }
  10081. static int reload(void)
  10082. {
  10083. reload_config();
  10084. return 0;
  10085. }
  10086. /*** SOME APPS ;)***/
  10087. #if defined(AST_MISDN_ENHANCEMENTS)
  10088. /*!
  10089. * \brief misdn_command arguments container.
  10090. */
  10091. AST_DEFINE_APP_ARGS_TYPE(misdn_command_args,
  10092. AST_APP_ARG(name); /* Subcommand name */
  10093. AST_APP_ARG(arg)[10 + 1]; /* Subcommand arguments */
  10094. );
  10095. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  10096. #if defined(AST_MISDN_ENHANCEMENTS)
  10097. static void misdn_cc_caller_destroy(void *obj)
  10098. {
  10099. /* oh snap! */
  10100. }
  10101. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  10102. #if defined(AST_MISDN_ENHANCEMENTS)
  10103. static struct misdn_cc_caller *misdn_cc_caller_alloc(struct ast_channel *chan)
  10104. {
  10105. struct misdn_cc_caller *cc_caller;
  10106. if (!(cc_caller = ao2_alloc(sizeof(*cc_caller), misdn_cc_caller_destroy))) {
  10107. return NULL;
  10108. }
  10109. cc_caller->chan = chan;
  10110. return cc_caller;
  10111. }
  10112. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  10113. #if defined(AST_MISDN_ENHANCEMENTS)
  10114. /*!
  10115. * \internal
  10116. * \brief misdn_command(cc-initialize) subcommand handler
  10117. *
  10118. * \param chan Asterisk channel to operate upon.
  10119. * \param subcommand Arguments for the subcommand
  10120. *
  10121. * \retval 0 on success.
  10122. * \retval -1 on error.
  10123. */
  10124. static int misdn_command_cc_initialize(struct ast_channel *chan, struct misdn_command_args *subcommand)
  10125. {
  10126. struct misdn_cc_caller *cc_caller;
  10127. struct ast_datastore *datastore;
  10128. if (!(cc_caller = misdn_cc_caller_alloc(chan))) {
  10129. return -1;
  10130. }
  10131. if (!(datastore = ast_datastore_alloc(&misdn_cc_ds_info, NULL))) {
  10132. ao2_ref(cc_caller, -1);
  10133. return -1;
  10134. }
  10135. ast_channel_lock(chan);
  10136. /* Inherit reference */
  10137. datastore->data = cc_caller;
  10138. cc_caller = NULL;
  10139. datastore->inheritance = DATASTORE_INHERIT_FOREVER;
  10140. ast_channel_datastore_add(chan, datastore);
  10141. ast_channel_unlock(chan);
  10142. return 0;
  10143. }
  10144. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  10145. #if defined(AST_MISDN_ENHANCEMENTS)
  10146. /*!
  10147. * \internal
  10148. * \brief misdn_command(cc-deactivate) subcommand handler
  10149. *
  10150. * \details
  10151. * misdn_command(cc-deactivate,${MISDN_CC_RECORD_ID})
  10152. * Deactivate a call completion service instance.
  10153. *
  10154. * \param chan Asterisk channel to operate upon.
  10155. * \param subcommand Arguments for the subcommand
  10156. *
  10157. * \retval 0 on success.
  10158. * \retval -1 on error.
  10159. */
  10160. static int misdn_command_cc_deactivate(struct ast_channel *chan, struct misdn_command_args *subcommand)
  10161. {
  10162. long record_id;
  10163. const char *error_str;
  10164. struct misdn_cc_record *cc_record;
  10165. struct misdn_bchannel *bc;
  10166. struct misdn_bchannel dummy;
  10167. static const char cmd_help[] = "%s(%s,${MISDN_CC_RECORD_ID})\n";
  10168. if (ast_strlen_zero(subcommand->arg[0]) || !isdigit(*subcommand->arg[0])) {
  10169. ast_log(LOG_WARNING, cmd_help, misdn_command_name, subcommand->name);
  10170. return -1;
  10171. }
  10172. record_id = atol(subcommand->arg[0]);
  10173. AST_LIST_LOCK(&misdn_cc_records_db);
  10174. cc_record = misdn_cc_find_by_id(record_id);
  10175. if (cc_record && 0 <= cc_record->port) {
  10176. if (cc_record->ptp) {
  10177. if (cc_record->mode.ptp.bc) {
  10178. /* Close the call-completion signaling link */
  10179. bc = cc_record->mode.ptp.bc;
  10180. bc->fac_out.Function = Fac_None;
  10181. bc->out_cause = AST_CAUSE_NORMAL_CLEARING;
  10182. misdn_lib_send_event(bc, EVENT_RELEASE_COMPLETE);
  10183. }
  10184. misdn_cc_delete(cc_record);
  10185. } else if (cc_record->activated) {
  10186. cc_record->error_code = FacError_None;
  10187. cc_record->reject_code = FacReject_None;
  10188. cc_record->invoke_id = ++misdn_invoke_id;
  10189. cc_record->outstanding_message = 1;
  10190. /* Build message */
  10191. misdn_make_dummy(&dummy, cc_record->port, 0, misdn_lib_port_is_nt(cc_record->port), 0);
  10192. dummy.fac_out.Function = Fac_CCBSDeactivate;
  10193. dummy.fac_out.u.CCBSDeactivate.InvokeID = cc_record->invoke_id;
  10194. dummy.fac_out.u.CCBSDeactivate.ComponentType = FacComponent_Invoke;
  10195. dummy.fac_out.u.CCBSDeactivate.Component.Invoke.CCBSReference = cc_record->mode.ptmp.reference_id;
  10196. /* Send message */
  10197. print_facility(&dummy.fac_out, &dummy);
  10198. misdn_lib_send_event(&dummy, EVENT_FACILITY);
  10199. }
  10200. }
  10201. AST_LIST_UNLOCK(&misdn_cc_records_db);
  10202. /* Wait for the response to the call completion deactivation request. */
  10203. misdn_cc_response_wait(chan, MISDN_CC_REQUEST_WAIT_MAX, record_id);
  10204. AST_LIST_LOCK(&misdn_cc_records_db);
  10205. cc_record = misdn_cc_find_by_id(record_id);
  10206. if (cc_record) {
  10207. if (cc_record->port < 0) {
  10208. /* The network did not tell us that call completion was available. */
  10209. error_str = NULL;
  10210. } else if (cc_record->outstanding_message) {
  10211. cc_record->outstanding_message = 0;
  10212. error_str = misdn_no_response_from_network;
  10213. } else if (cc_record->reject_code != FacReject_None) {
  10214. error_str = misdn_to_str_reject_code(cc_record->reject_code);
  10215. } else if (cc_record->error_code != FacError_None) {
  10216. error_str = misdn_to_str_error_code(cc_record->error_code);
  10217. } else {
  10218. error_str = NULL;
  10219. }
  10220. misdn_cc_delete(cc_record);
  10221. } else {
  10222. error_str = NULL;
  10223. }
  10224. AST_LIST_UNLOCK(&misdn_cc_records_db);
  10225. if (error_str) {
  10226. ast_verb(1, "%s(%s) diagnostic '%s' on channel %s\n",
  10227. misdn_command_name, subcommand->name, error_str, ast_channel_name(chan));
  10228. pbx_builtin_setvar_helper(chan, MISDN_ERROR_MSG, error_str);
  10229. }
  10230. return 0;
  10231. }
  10232. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  10233. #if defined(AST_MISDN_ENHANCEMENTS)
  10234. /*!
  10235. * \internal
  10236. * \brief misdn_command(cc-a-busy) subcommand handler
  10237. *
  10238. * \details
  10239. * misdn_command(cc-a-busy,${MISDN_CC_RECORD_ID},<yes/no>)
  10240. * Set the status of User-A for a call completion service instance.
  10241. *
  10242. * \param chan Asterisk channel to operate upon.
  10243. * \param subcommand Arguments for the subcommand
  10244. *
  10245. * \retval 0 on success.
  10246. * \retval -1 on error.
  10247. */
  10248. static int misdn_command_cc_a_busy(struct ast_channel *chan, struct misdn_command_args *subcommand)
  10249. {
  10250. long record_id;
  10251. int party_a_free;
  10252. struct misdn_cc_record *cc_record;
  10253. struct misdn_bchannel *bc;
  10254. static const char cmd_help[] = "%s(%s,${MISDN_CC_RECORD_ID},<yes/no>)\n";
  10255. if (ast_strlen_zero(subcommand->arg[0]) || !isdigit(*subcommand->arg[0])) {
  10256. ast_log(LOG_WARNING, cmd_help, misdn_command_name, subcommand->name);
  10257. return -1;
  10258. }
  10259. record_id = atol(subcommand->arg[0]);
  10260. if (ast_true(subcommand->arg[1])) {
  10261. party_a_free = 0;
  10262. } else if (ast_false(subcommand->arg[1])) {
  10263. party_a_free = 1;
  10264. } else {
  10265. ast_log(LOG_WARNING, cmd_help, misdn_command_name, subcommand->name);
  10266. return -1;
  10267. }
  10268. AST_LIST_LOCK(&misdn_cc_records_db);
  10269. cc_record = misdn_cc_find_by_id(record_id);
  10270. if (cc_record && cc_record->party_a_free != party_a_free) {
  10271. /* User-A's status has changed */
  10272. cc_record->party_a_free = party_a_free;
  10273. if (cc_record->ptp && cc_record->mode.ptp.bc) {
  10274. cc_record->error_code = FacError_None;
  10275. cc_record->reject_code = FacReject_None;
  10276. /* Build message */
  10277. bc = cc_record->mode.ptp.bc;
  10278. if (cc_record->party_a_free) {
  10279. bc->fac_out.Function = Fac_CCBS_T_Resume;
  10280. bc->fac_out.u.CCBS_T_Resume.InvokeID = ++misdn_invoke_id;
  10281. } else {
  10282. bc->fac_out.Function = Fac_CCBS_T_Suspend;
  10283. bc->fac_out.u.CCBS_T_Suspend.InvokeID = ++misdn_invoke_id;
  10284. }
  10285. /* Send message */
  10286. print_facility(&bc->fac_out, bc);
  10287. misdn_lib_send_event(bc, EVENT_FACILITY);
  10288. }
  10289. }
  10290. AST_LIST_UNLOCK(&misdn_cc_records_db);
  10291. return 0;
  10292. }
  10293. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  10294. #if defined(AST_MISDN_ENHANCEMENTS)
  10295. /*!
  10296. * \internal
  10297. * \brief misdn_command(cc-b-free) subcommand handler
  10298. *
  10299. * \details
  10300. * misdn_command(cc-b-free,${MISDN_CC_RECORD_ID},<notify-context>,<user-a-extension>,<priority>)
  10301. * Set the dialplan location to notify when User-B is free and User-A is busy.
  10302. *
  10303. * \param chan Asterisk channel to operate upon.
  10304. * \param subcommand Arguments for the subcommand
  10305. *
  10306. * \retval 0 on success.
  10307. * \retval -1 on error.
  10308. */
  10309. static int misdn_command_cc_b_free(struct ast_channel *chan, struct misdn_command_args *subcommand)
  10310. {
  10311. unsigned index;
  10312. long record_id;
  10313. int priority;
  10314. char *context;
  10315. char *exten;
  10316. struct misdn_cc_record *cc_record;
  10317. static const char cmd_help[] = "%s(%s,${MISDN_CC_RECORD_ID},<notify-context>,<user-a-extension>,<priority>)\n";
  10318. /* Check that all arguments are present */
  10319. for (index = 0; index < 4; ++index) {
  10320. if (ast_strlen_zero(subcommand->arg[index])) {
  10321. ast_log(LOG_WARNING, cmd_help, misdn_command_name, subcommand->name);
  10322. return -1;
  10323. }
  10324. }
  10325. /* These must be numeric */
  10326. if (!isdigit(*subcommand->arg[0]) || !isdigit(*subcommand->arg[3])) {
  10327. ast_log(LOG_WARNING, cmd_help, misdn_command_name, subcommand->name);
  10328. return -1;
  10329. }
  10330. record_id = atol(subcommand->arg[0]);
  10331. context = subcommand->arg[1];
  10332. exten = subcommand->arg[2];
  10333. priority = atoi(subcommand->arg[3]);
  10334. AST_LIST_LOCK(&misdn_cc_records_db);
  10335. cc_record = misdn_cc_find_by_id(record_id);
  10336. if (cc_record) {
  10337. /* Save User-B free information */
  10338. ast_copy_string(cc_record->b_free.context, context, sizeof(cc_record->b_free.context));
  10339. ast_copy_string(cc_record->b_free.exten, exten, sizeof(cc_record->b_free.exten));
  10340. cc_record->b_free.priority = priority;
  10341. }
  10342. AST_LIST_UNLOCK(&misdn_cc_records_db);
  10343. return 0;
  10344. }
  10345. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  10346. #if defined(AST_MISDN_ENHANCEMENTS)
  10347. struct misdn_cc_request {
  10348. enum FacFunction ptmp;
  10349. enum FacFunction ptp;
  10350. };
  10351. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  10352. #if defined(AST_MISDN_ENHANCEMENTS)
  10353. /*!
  10354. * \internal
  10355. * \brief misdn_command(ccbs-request/ccnr-request) subcommand handler helper
  10356. *
  10357. * \details
  10358. * misdn_command(ccbs-request,${MISDN_CC_RECORD_ID},<notify-context>,<user-a-extension>,<priority>)
  10359. * misdn_command(ccnr-request,${MISDN_CC_RECORD_ID},<notify-context>,<user-a-extension>,<priority>)
  10360. * Set the dialplan location to notify when User-B is free and User-A is free.
  10361. *
  10362. * \param chan Asterisk channel to operate upon.
  10363. * \param subcommand Arguments for the subcommand
  10364. * \param request Which call-completion request message to generate.
  10365. *
  10366. * \retval 0 on success.
  10367. * \retval -1 on error.
  10368. */
  10369. static int misdn_command_cc_request(struct ast_channel *chan, struct misdn_command_args *subcommand, const struct misdn_cc_request *request)
  10370. {
  10371. unsigned index;
  10372. int request_retention;
  10373. long record_id;
  10374. int priority;
  10375. char *context;
  10376. char *exten;
  10377. const char *error_str;
  10378. struct misdn_cc_record *cc_record;
  10379. struct misdn_bchannel *bc;
  10380. struct misdn_bchannel dummy;
  10381. struct misdn_party_id id;
  10382. static const char cmd_help[] = "%s(%s,${MISDN_CC_RECORD_ID},<notify-context>,<user-a-extension>,<priority>)\n";
  10383. /* Check that all arguments are present */
  10384. for (index = 0; index < 4; ++index) {
  10385. if (ast_strlen_zero(subcommand->arg[index])) {
  10386. ast_log(LOG_WARNING, cmd_help, misdn_command_name, subcommand->name);
  10387. return -1;
  10388. }
  10389. }
  10390. /* These must be numeric */
  10391. if (!isdigit(*subcommand->arg[0]) || !isdigit(*subcommand->arg[3])) {
  10392. ast_log(LOG_WARNING, cmd_help, misdn_command_name, subcommand->name);
  10393. return -1;
  10394. }
  10395. record_id = atol(subcommand->arg[0]);
  10396. context = subcommand->arg[1];
  10397. exten = subcommand->arg[2];
  10398. priority = atoi(subcommand->arg[3]);
  10399. AST_LIST_LOCK(&misdn_cc_records_db);
  10400. cc_record = misdn_cc_find_by_id(record_id);
  10401. if (cc_record) {
  10402. /* Save User-B free information */
  10403. ast_copy_string(cc_record->remote_user_free.context, context,
  10404. sizeof(cc_record->remote_user_free.context));
  10405. ast_copy_string(cc_record->remote_user_free.exten, exten,
  10406. sizeof(cc_record->remote_user_free.exten));
  10407. cc_record->remote_user_free.priority = priority;
  10408. if (0 <= cc_record->port) {
  10409. if (cc_record->ptp) {
  10410. if (!cc_record->mode.ptp.bc) {
  10411. bc = misdn_lib_get_register_bc(cc_record->port);
  10412. if (bc) {
  10413. cc_record->mode.ptp.bc = bc;
  10414. cc_record->error_code = FacError_None;
  10415. cc_record->reject_code = FacReject_None;
  10416. cc_record->invoke_id = ++misdn_invoke_id;
  10417. cc_record->outstanding_message = 1;
  10418. cc_record->activation_requested = 1;
  10419. misdn_cfg_get(bc->port, MISDN_CFG_CC_REQUEST_RETENTION,
  10420. &request_retention, sizeof(request_retention));
  10421. cc_record->mode.ptp.requested_retention = request_retention ? 1 : 0;
  10422. /* Build message */
  10423. bc->fac_out.Function = request->ptp;
  10424. bc->fac_out.u.CCBS_T_Request.InvokeID = cc_record->invoke_id;
  10425. bc->fac_out.u.CCBS_T_Request.ComponentType = FacComponent_Invoke;
  10426. bc->fac_out.u.CCBS_T_Request.Component.Invoke.Q931ie =
  10427. cc_record->redial.setup_bc_hlc_llc;
  10428. memset(&id, 0, sizeof(id));
  10429. id.number_plan = cc_record->redial.dialed.number_plan;
  10430. id.number_type = cc_record->redial.dialed.number_type;
  10431. ast_copy_string(id.number, cc_record->redial.dialed.number,
  10432. sizeof(id.number));
  10433. misdn_Address_fill(
  10434. &bc->fac_out.u.CCBS_T_Request.Component.Invoke.Destination,
  10435. &id);
  10436. misdn_Address_fill(
  10437. &bc->fac_out.u.CCBS_T_Request.Component.Invoke.Originating,
  10438. &cc_record->redial.caller);
  10439. bc->fac_out.u.CCBS_T_Request.Component.Invoke.PresentationAllowedIndicatorPresent = 1;
  10440. bc->fac_out.u.CCBS_T_Request.Component.Invoke.PresentationAllowedIndicator =
  10441. (cc_record->redial.caller.presentation != 0) ? 0 : 1;
  10442. bc->fac_out.u.CCBS_T_Request.Component.Invoke.RetentionSupported =
  10443. request_retention ? 1 : 0;
  10444. /* Send message */
  10445. print_facility(&bc->fac_out, bc);
  10446. misdn_lib_send_event(bc, EVENT_REGISTER);
  10447. }
  10448. }
  10449. } else {
  10450. cc_record->error_code = FacError_None;
  10451. cc_record->reject_code = FacReject_None;
  10452. cc_record->invoke_id = ++misdn_invoke_id;
  10453. cc_record->outstanding_message = 1;
  10454. cc_record->activation_requested = 1;
  10455. /* Build message */
  10456. misdn_make_dummy(&dummy, cc_record->port, 0,
  10457. misdn_lib_port_is_nt(cc_record->port), 0);
  10458. dummy.fac_out.Function = request->ptmp;
  10459. dummy.fac_out.u.CCBSRequest.InvokeID = cc_record->invoke_id;
  10460. dummy.fac_out.u.CCBSRequest.ComponentType = FacComponent_Invoke;
  10461. dummy.fac_out.u.CCBSRequest.Component.Invoke.CallLinkageID =
  10462. cc_record->mode.ptmp.linkage_id;
  10463. /* Send message */
  10464. print_facility(&dummy.fac_out, &dummy);
  10465. misdn_lib_send_event(&dummy, EVENT_FACILITY);
  10466. }
  10467. }
  10468. }
  10469. AST_LIST_UNLOCK(&misdn_cc_records_db);
  10470. /* Wait for the response to the call completion request. */
  10471. misdn_cc_response_wait(chan, MISDN_CC_REQUEST_WAIT_MAX, record_id);
  10472. AST_LIST_LOCK(&misdn_cc_records_db);
  10473. cc_record = misdn_cc_find_by_id(record_id);
  10474. if (cc_record) {
  10475. if (!cc_record->activated) {
  10476. if (cc_record->port < 0) {
  10477. /* The network did not tell us that call completion was available. */
  10478. error_str = "No port number";
  10479. } else if (cc_record->outstanding_message) {
  10480. cc_record->outstanding_message = 0;
  10481. error_str = misdn_no_response_from_network;
  10482. } else if (cc_record->reject_code != FacReject_None) {
  10483. error_str = misdn_to_str_reject_code(cc_record->reject_code);
  10484. } else if (cc_record->error_code != FacError_None) {
  10485. error_str = misdn_to_str_error_code(cc_record->error_code);
  10486. } else if (cc_record->ptp) {
  10487. if (cc_record->mode.ptp.bc) {
  10488. error_str = "Call-completion already requested";
  10489. } else {
  10490. error_str = "Could not allocate call-completion signaling link";
  10491. }
  10492. } else {
  10493. /* Should never happen. */
  10494. error_str = "Unexpected error";
  10495. }
  10496. /* No need to keep the call completion record. */
  10497. if (cc_record->ptp && cc_record->mode.ptp.bc) {
  10498. /* Close the call-completion signaling link */
  10499. bc = cc_record->mode.ptp.bc;
  10500. bc->fac_out.Function = Fac_None;
  10501. bc->out_cause = AST_CAUSE_NORMAL_CLEARING;
  10502. misdn_lib_send_event(bc, EVENT_RELEASE_COMPLETE);
  10503. }
  10504. misdn_cc_delete(cc_record);
  10505. } else {
  10506. error_str = NULL;
  10507. }
  10508. } else {
  10509. error_str = misdn_cc_record_not_found;
  10510. }
  10511. AST_LIST_UNLOCK(&misdn_cc_records_db);
  10512. if (error_str) {
  10513. ast_verb(1, "%s(%s) diagnostic '%s' on channel %s\n",
  10514. misdn_command_name, subcommand->name, error_str, ast_channel_name(chan));
  10515. pbx_builtin_setvar_helper(chan, MISDN_ERROR_MSG, error_str);
  10516. pbx_builtin_setvar_helper(chan, MISDN_CC_STATUS, "ERROR");
  10517. } else {
  10518. pbx_builtin_setvar_helper(chan, MISDN_CC_STATUS, "ACTIVATED");
  10519. }
  10520. return 0;
  10521. }
  10522. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  10523. #if defined(AST_MISDN_ENHANCEMENTS)
  10524. /*!
  10525. * \internal
  10526. * \brief misdn_command(ccbs-request) subcommand handler
  10527. *
  10528. * \details
  10529. * misdn_command(ccbs-request,${MISDN_CC_RECORD_ID},<notify-context>,<user-a-extension>,<priority>)
  10530. * Set the dialplan location to notify when User-B is free and User-A is free.
  10531. *
  10532. * \param chan Asterisk channel to operate upon.
  10533. * \param subcommand Arguments for the subcommand
  10534. *
  10535. * \retval 0 on success.
  10536. * \retval -1 on error.
  10537. */
  10538. static int misdn_command_ccbs_request(struct ast_channel *chan, struct misdn_command_args *subcommand)
  10539. {
  10540. static const struct misdn_cc_request request = {
  10541. .ptmp = Fac_CCBSRequest,
  10542. .ptp = Fac_CCBS_T_Request
  10543. };
  10544. return misdn_command_cc_request(chan, subcommand, &request);
  10545. }
  10546. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  10547. #if defined(AST_MISDN_ENHANCEMENTS)
  10548. /*!
  10549. * \internal
  10550. * \brief misdn_command(ccnr-request) subcommand handler
  10551. *
  10552. * \details
  10553. * misdn_command(ccnr-request,${MISDN_CC_RECORD_ID},<notify-context>,<user-a-extension>,<priority>)
  10554. * Set the dialplan location to notify when User-B is free and User-A is free.
  10555. *
  10556. * \param chan Asterisk channel to operate upon.
  10557. * \param subcommand Arguments for the subcommand
  10558. *
  10559. * \retval 0 on success.
  10560. * \retval -1 on error.
  10561. */
  10562. static int misdn_command_ccnr_request(struct ast_channel *chan, struct misdn_command_args *subcommand)
  10563. {
  10564. static const struct misdn_cc_request request = {
  10565. .ptmp = Fac_CCNRRequest,
  10566. .ptp = Fac_CCNR_T_Request
  10567. };
  10568. return misdn_command_cc_request(chan, subcommand, &request);
  10569. }
  10570. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  10571. #if defined(AST_MISDN_ENHANCEMENTS)
  10572. struct misdn_command_table {
  10573. /*! \brief subcommand name */
  10574. const char *name;
  10575. /*! \brief subcommand handler */
  10576. int (*func)(struct ast_channel *chan, struct misdn_command_args *subcommand);
  10577. /*! \brief TRUE if the subcommand can only be executed on mISDN channels */
  10578. int misdn_only;
  10579. };
  10580. static const struct misdn_command_table misdn_commands[] = {
  10581. /* *INDENT-OFF* */
  10582. /* subcommand-name subcommand-handler mISDN only */
  10583. { "cc-initialize", misdn_command_cc_initialize, 0 },
  10584. { "cc-deactivate", misdn_command_cc_deactivate, 0 },
  10585. { "cc-a-busy", misdn_command_cc_a_busy, 0 },
  10586. { "cc-b-free", misdn_command_cc_b_free, 0 },
  10587. { "ccbs-request", misdn_command_ccbs_request, 0 },
  10588. { "ccnr-request", misdn_command_ccnr_request, 0 },
  10589. /* *INDENT-ON* */
  10590. };
  10591. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  10592. #if defined(AST_MISDN_ENHANCEMENTS)
  10593. /*!
  10594. * \internal
  10595. * \brief misdn_command() dialplan application.
  10596. *
  10597. * \param chan Asterisk channel to operate upon.
  10598. * \param data Application options string.
  10599. *
  10600. * \retval 0 on success.
  10601. * \retval -1 on error.
  10602. */
  10603. static int misdn_command_exec(struct ast_channel *chan, const char *data)
  10604. {
  10605. char *parse;
  10606. unsigned index;
  10607. struct misdn_command_args subcommand;
  10608. if (ast_strlen_zero((char *) data)) {
  10609. ast_log(LOG_ERROR, "%s requires arguments\n", misdn_command_name);
  10610. return -1;
  10611. }
  10612. ast_debug(1, "%s(%s)\n", misdn_command_name, (char *) data);
  10613. parse = ast_strdupa(data);
  10614. AST_STANDARD_APP_ARGS(subcommand, parse);
  10615. if (!subcommand.argc || ast_strlen_zero(subcommand.name)) {
  10616. ast_log(LOG_ERROR, "%s requires a subcommand\n", misdn_command_name);
  10617. return -1;
  10618. }
  10619. for (index = 0; index < ARRAY_LEN(misdn_commands); ++index) {
  10620. if (strcasecmp(misdn_commands[index].name, subcommand.name) == 0) {
  10621. strcpy(subcommand.name, misdn_commands[index].name);
  10622. if (misdn_commands[index].misdn_only
  10623. && strcasecmp(ast_channel_tech(chan)->type, misdn_type) != 0) {
  10624. ast_log(LOG_WARNING,
  10625. "%s(%s) only makes sense with %s channels!\n",
  10626. misdn_command_name, subcommand.name, misdn_type);
  10627. return -1;
  10628. }
  10629. return misdn_commands[index].func(chan, &subcommand);
  10630. }
  10631. }
  10632. ast_log(LOG_WARNING, "%s(%s) subcommand is unknown\n", misdn_command_name,
  10633. subcommand.name);
  10634. return -1;
  10635. }
  10636. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  10637. static int misdn_facility_exec(struct ast_channel *chan, const char *data)
  10638. {
  10639. struct chan_list *ch = MISDN_ASTERISK_TECH_PVT(chan);
  10640. char *parse;
  10641. unsigned max_len;
  10642. AST_DECLARE_APP_ARGS(args,
  10643. AST_APP_ARG(facility_type);
  10644. AST_APP_ARG(arg)[99];
  10645. );
  10646. chan_misdn_log(0, 0, "TYPE: %s\n", ast_channel_tech(chan)->type);
  10647. if (strcasecmp(ast_channel_tech(chan)->type, misdn_type)) {
  10648. ast_log(LOG_WARNING, "misdn_facility only makes sense with %s channels!\n", misdn_type);
  10649. return -1;
  10650. }
  10651. if (ast_strlen_zero((char *) data)) {
  10652. ast_log(LOG_WARNING, "misdn_facility requires arguments: facility_type[,<args>]\n");
  10653. return -1;
  10654. }
  10655. parse = ast_strdupa(data);
  10656. AST_STANDARD_APP_ARGS(args, parse);
  10657. if (ast_strlen_zero(args.facility_type)) {
  10658. ast_log(LOG_WARNING, "misdn_facility requires arguments: facility_type[,<args>]\n");
  10659. return -1;
  10660. }
  10661. if (!strcasecmp(args.facility_type, "calldeflect")) {
  10662. if (ast_strlen_zero(args.arg[0])) {
  10663. ast_log(LOG_WARNING, "Facility: Call Deflection requires an argument: Number\n");
  10664. }
  10665. #if defined(AST_MISDN_ENHANCEMENTS)
  10666. max_len = sizeof(ch->bc->fac_out.u.CallDeflection.Component.Invoke.Deflection.Party.Number) - 1;
  10667. if (max_len < strlen(args.arg[0])) {
  10668. ast_log(LOG_WARNING,
  10669. "Facility: Number argument too long (up to %u digits are allowed). Ignoring.\n",
  10670. max_len);
  10671. return 0;
  10672. }
  10673. ch->bc->fac_out.Function = Fac_CallDeflection;
  10674. ch->bc->fac_out.u.CallDeflection.InvokeID = ++misdn_invoke_id;
  10675. ch->bc->fac_out.u.CallDeflection.ComponentType = FacComponent_Invoke;
  10676. ch->bc->fac_out.u.CallDeflection.Component.Invoke.PresentationAllowedToDivertedToUserPresent = 1;
  10677. ch->bc->fac_out.u.CallDeflection.Component.Invoke.PresentationAllowedToDivertedToUser = 0;
  10678. ch->bc->fac_out.u.CallDeflection.Component.Invoke.Deflection.Party.Type = 0;/* unknown */
  10679. ch->bc->fac_out.u.CallDeflection.Component.Invoke.Deflection.Party.LengthOfNumber = strlen(args.arg[0]);
  10680. strcpy((char *) ch->bc->fac_out.u.CallDeflection.Component.Invoke.Deflection.Party.Number, args.arg[0]);
  10681. ch->bc->fac_out.u.CallDeflection.Component.Invoke.Deflection.Subaddress.Length = 0;
  10682. #else /* !defined(AST_MISDN_ENHANCEMENTS) */
  10683. max_len = sizeof(ch->bc->fac_out.u.CDeflection.DeflectedToNumber) - 1;
  10684. if (max_len < strlen(args.arg[0])) {
  10685. ast_log(LOG_WARNING,
  10686. "Facility: Number argument too long (up to %u digits are allowed). Ignoring.\n",
  10687. max_len);
  10688. return 0;
  10689. }
  10690. ch->bc->fac_out.Function = Fac_CD;
  10691. ch->bc->fac_out.u.CDeflection.PresentationAllowed = 0;
  10692. //ch->bc->fac_out.u.CDeflection.DeflectedToSubaddress[0] = 0;
  10693. strcpy((char *) ch->bc->fac_out.u.CDeflection.DeflectedToNumber, args.arg[0]);
  10694. #endif /* !defined(AST_MISDN_ENHANCEMENTS) */
  10695. /* Send message */
  10696. print_facility(&ch->bc->fac_out, ch->bc);
  10697. misdn_lib_send_event(ch->bc, EVENT_FACILITY);
  10698. #if defined(AST_MISDN_ENHANCEMENTS)
  10699. } else if (!strcasecmp(args.facility_type, "callrerouteing")
  10700. || !strcasecmp(args.facility_type, "callrerouting")) {
  10701. if (ast_strlen_zero(args.arg[0])) {
  10702. ast_log(LOG_WARNING, "Facility: Call rerouting requires an argument: Number\n");
  10703. }
  10704. max_len = sizeof(ch->bc->fac_out.u.CallRerouteing.Component.Invoke.CalledAddress.Party.Number) - 1;
  10705. if (max_len < strlen(args.arg[0])) {
  10706. ast_log(LOG_WARNING,
  10707. "Facility: Number argument too long (up to %u digits are allowed). Ignoring.\n",
  10708. max_len);
  10709. return 0;
  10710. }
  10711. ch->bc->fac_out.Function = Fac_CallRerouteing;
  10712. ch->bc->fac_out.u.CallRerouteing.InvokeID = ++misdn_invoke_id;
  10713. ch->bc->fac_out.u.CallRerouteing.ComponentType = FacComponent_Invoke;
  10714. ch->bc->fac_out.u.CallRerouteing.Component.Invoke.ReroutingReason = 0;/* unknown */
  10715. ch->bc->fac_out.u.CallRerouteing.Component.Invoke.ReroutingCounter = 1;
  10716. ch->bc->fac_out.u.CallRerouteing.Component.Invoke.CalledAddress.Party.Type = 0;/* unknown */
  10717. ch->bc->fac_out.u.CallRerouteing.Component.Invoke.CalledAddress.Party.LengthOfNumber = strlen(args.arg[0]);
  10718. strcpy((char *) ch->bc->fac_out.u.CallRerouteing.Component.Invoke.CalledAddress.Party.Number, args.arg[0]);
  10719. ch->bc->fac_out.u.CallRerouteing.Component.Invoke.CalledAddress.Subaddress.Length = 0;
  10720. ch->bc->fac_out.u.CallRerouteing.Component.Invoke.CallingPartySubaddress.Length = 0;
  10721. /* 0x90 0x90 0xa3 3.1 kHz audio, circuit mode, 64kbit/sec, level1/a-Law */
  10722. ch->bc->fac_out.u.CallRerouteing.Component.Invoke.Q931ie.Bc.Length = 3;
  10723. ch->bc->fac_out.u.CallRerouteing.Component.Invoke.Q931ie.Bc.Contents[0] = 0x90;
  10724. ch->bc->fac_out.u.CallRerouteing.Component.Invoke.Q931ie.Bc.Contents[1] = 0x90;
  10725. ch->bc->fac_out.u.CallRerouteing.Component.Invoke.Q931ie.Bc.Contents[2] = 0xa3;
  10726. ch->bc->fac_out.u.CallRerouteing.Component.Invoke.Q931ie.Hlc.Length = 0;
  10727. ch->bc->fac_out.u.CallRerouteing.Component.Invoke.Q931ie.Llc.Length = 0;
  10728. ch->bc->fac_out.u.CallRerouteing.Component.Invoke.Q931ie.UserInfo.Length = 0;
  10729. ch->bc->fac_out.u.CallRerouteing.Component.Invoke.LastRerouting.Type = 1;/* presentationRestricted */
  10730. ch->bc->fac_out.u.CallRerouteing.Component.Invoke.SubscriptionOption = 0;/* no notification to caller */
  10731. /* Send message */
  10732. print_facility(&ch->bc->fac_out, ch->bc);
  10733. misdn_lib_send_event(ch->bc, EVENT_FACILITY);
  10734. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  10735. } else {
  10736. chan_misdn_log(1, ch->bc->port, "Unknown Facility: %s\n", args.facility_type);
  10737. }
  10738. return 0;
  10739. }
  10740. static int misdn_check_l2l1(struct ast_channel *chan, const char *data)
  10741. {
  10742. char *parse;
  10743. char group[BUFFERSIZE + 1];
  10744. char *port_str;
  10745. int port = 0;
  10746. int timeout;
  10747. int dowait = 0;
  10748. int port_up;
  10749. AST_DECLARE_APP_ARGS(args,
  10750. AST_APP_ARG(grouppar);
  10751. AST_APP_ARG(timeout);
  10752. );
  10753. if (ast_strlen_zero((char *) data)) {
  10754. ast_log(LOG_WARNING, "misdn_check_l2l1 Requires arguments\n");
  10755. return -1;
  10756. }
  10757. parse = ast_strdupa(data);
  10758. AST_STANDARD_APP_ARGS(args, parse);
  10759. if (args.argc != 2) {
  10760. ast_log(LOG_WARNING, "Wrong argument count\n");
  10761. return 0;
  10762. }
  10763. /*ast_log(LOG_NOTICE, "Arguments: group/port '%s' timeout '%s'\n", args.grouppar, args.timeout);*/
  10764. timeout = atoi(args.timeout);
  10765. port_str = args.grouppar;
  10766. if (port_str[0] == 'g' && port_str[1] == ':') {
  10767. /* We make a group call lets checkout which ports are in my group */
  10768. port_str += 2;
  10769. ast_copy_string(group, port_str, sizeof(group));
  10770. chan_misdn_log(2, 0, "Checking Ports in group: %s\n", group);
  10771. for (port = misdn_cfg_get_next_port(port);
  10772. port > 0;
  10773. port = misdn_cfg_get_next_port(port)) {
  10774. char cfg_group[BUFFERSIZE + 1];
  10775. chan_misdn_log(2, 0, "trying port %d\n", port);
  10776. misdn_cfg_get(port, MISDN_CFG_GROUPNAME, cfg_group, sizeof(cfg_group));
  10777. if (!strcasecmp(cfg_group, group)) {
  10778. port_up = misdn_lib_port_up(port, 1);
  10779. if (!port_up) {
  10780. chan_misdn_log(2, 0, " --> port '%d'\n", port);
  10781. misdn_lib_get_port_up(port);
  10782. dowait = 1;
  10783. }
  10784. }
  10785. }
  10786. } else {
  10787. port = atoi(port_str);
  10788. chan_misdn_log(2, 0, "Checking Port: %d\n", port);
  10789. port_up = misdn_lib_port_up(port, 1);
  10790. if (!port_up) {
  10791. misdn_lib_get_port_up(port);
  10792. dowait = 1;
  10793. }
  10794. }
  10795. if (dowait) {
  10796. chan_misdn_log(2, 0, "Waiting for '%d' seconds\n", timeout);
  10797. ast_safe_sleep(chan, timeout * 1000);
  10798. }
  10799. return 0;
  10800. }
  10801. static int misdn_set_opt_exec(struct ast_channel *chan, const char *data)
  10802. {
  10803. struct chan_list *ch = MISDN_ASTERISK_TECH_PVT(chan);
  10804. char *tok;
  10805. char *tokb;
  10806. char *parse;
  10807. int keyidx = 0;
  10808. int rxgain = 0;
  10809. int txgain = 0;
  10810. int change_jitter = 0;
  10811. if (strcasecmp(ast_channel_tech(chan)->type, misdn_type)) {
  10812. ast_log(LOG_WARNING, "misdn_set_opt makes sense only with %s channels!\n", misdn_type);
  10813. return -1;
  10814. }
  10815. if (ast_strlen_zero((char *) data)) {
  10816. ast_log(LOG_WARNING, "misdn_set_opt Requires arguments\n");
  10817. return -1;
  10818. }
  10819. parse = ast_strdupa(data);
  10820. for (tok = strtok_r(parse, ":", &tokb);
  10821. tok;
  10822. tok = strtok_r(NULL, ":", &tokb)) {
  10823. int neglect = 0;
  10824. if (tok[0] == '!') {
  10825. neglect = 1;
  10826. tok++;
  10827. }
  10828. switch(tok[0]) {
  10829. case 'd' :
  10830. ast_copy_string(ch->bc->display, ++tok, sizeof(ch->bc->display));
  10831. chan_misdn_log(1, ch->bc->port, "SETOPT: Display:%s\n", ch->bc->display);
  10832. break;
  10833. case 'n':
  10834. chan_misdn_log(1, ch->bc->port, "SETOPT: No DSP\n");
  10835. ch->bc->nodsp = 1;
  10836. break;
  10837. case 'j':
  10838. chan_misdn_log(1, ch->bc->port, "SETOPT: jitter\n");
  10839. tok++;
  10840. change_jitter = 1;
  10841. switch (tok[0]) {
  10842. case 'b':
  10843. ch->jb_len = atoi(++tok);
  10844. chan_misdn_log(1, ch->bc->port, " --> buffer_len:%d\n", ch->jb_len);
  10845. break;
  10846. case 't' :
  10847. ch->jb_upper_threshold = atoi(++tok);
  10848. chan_misdn_log(1, ch->bc->port, " --> upper_threshold:%d\n", ch->jb_upper_threshold);
  10849. break;
  10850. case 'n':
  10851. ch->bc->nojitter = 1;
  10852. chan_misdn_log(1, ch->bc->port, " --> nojitter\n");
  10853. break;
  10854. default:
  10855. ch->jb_len = 4000;
  10856. ch->jb_upper_threshold = 0;
  10857. chan_misdn_log(1, ch->bc->port, " --> buffer_len:%d (default)\n", ch->jb_len);
  10858. chan_misdn_log(1, ch->bc->port, " --> upper_threshold:%d (default)\n", ch->jb_upper_threshold);
  10859. break;
  10860. }
  10861. break;
  10862. case 'v':
  10863. tok++;
  10864. switch (tok[0]) {
  10865. case 'r' :
  10866. rxgain = atoi(++tok);
  10867. if (rxgain < -8) {
  10868. rxgain = -8;
  10869. }
  10870. if (rxgain > 8) {
  10871. rxgain = 8;
  10872. }
  10873. ch->bc->rxgain = rxgain;
  10874. chan_misdn_log(1, ch->bc->port, "SETOPT: Volume:%d\n", rxgain);
  10875. break;
  10876. case 't':
  10877. txgain = atoi(++tok);
  10878. if (txgain < -8) {
  10879. txgain = -8;
  10880. }
  10881. if (txgain > 8) {
  10882. txgain = 8;
  10883. }
  10884. ch->bc->txgain = txgain;
  10885. chan_misdn_log(1, ch->bc->port, "SETOPT: Volume:%d\n", txgain);
  10886. break;
  10887. }
  10888. break;
  10889. case 'c':
  10890. keyidx = atoi(++tok);
  10891. {
  10892. char keys[4096];
  10893. char *key = NULL;
  10894. char *tmp = keys;
  10895. int i;
  10896. misdn_cfg_get(0, MISDN_GEN_CRYPT_KEYS, keys, sizeof(keys));
  10897. for (i = 0; i < keyidx; i++) {
  10898. key = strsep(&tmp, ",");
  10899. }
  10900. if (key) {
  10901. ast_copy_string(ch->bc->crypt_key, key, sizeof(ch->bc->crypt_key));
  10902. }
  10903. chan_misdn_log(0, ch->bc->port, "SETOPT: crypt with key:%s\n", ch->bc->crypt_key);
  10904. break;
  10905. }
  10906. case 'e':
  10907. chan_misdn_log(1, ch->bc->port, "SETOPT: EchoCancel\n");
  10908. if (neglect) {
  10909. chan_misdn_log(1, ch->bc->port, " --> disabled\n");
  10910. #ifdef MISDN_1_2
  10911. *ch->bc->pipeline = 0;
  10912. #else
  10913. ch->bc->ec_enable = 0;
  10914. #endif
  10915. } else {
  10916. #ifdef MISDN_1_2
  10917. update_pipeline_config(ch->bc);
  10918. #else
  10919. ch->bc->ec_enable = 1;
  10920. ch->bc->orig = ch->originator;
  10921. tok++;
  10922. if (*tok) {
  10923. ch->bc->ec_deftaps = atoi(tok);
  10924. }
  10925. #endif
  10926. }
  10927. break;
  10928. case 'h':
  10929. chan_misdn_log(1, ch->bc->port, "SETOPT: Digital\n");
  10930. if (strlen(tok) > 1 && tok[1] == '1') {
  10931. chan_misdn_log(1, ch->bc->port, "SETOPT: HDLC \n");
  10932. if (!ch->bc->hdlc) {
  10933. ch->bc->hdlc = 1;
  10934. }
  10935. }
  10936. ch->bc->capability = INFO_CAPABILITY_DIGITAL_UNRESTRICTED;
  10937. break;
  10938. case 's':
  10939. chan_misdn_log(1, ch->bc->port, "SETOPT: Send DTMF\n");
  10940. ch->bc->send_dtmf = 1;
  10941. break;
  10942. case 'f':
  10943. chan_misdn_log(1, ch->bc->port, "SETOPT: Faxdetect\n");
  10944. ch->faxdetect = 1;
  10945. misdn_cfg_get(ch->bc->port, MISDN_CFG_FAXDETECT_TIMEOUT, &ch->faxdetect_timeout, sizeof(ch->faxdetect_timeout));
  10946. break;
  10947. case 'a':
  10948. chan_misdn_log(1, ch->bc->port, "SETOPT: AST_DSP (for DTMF)\n");
  10949. ch->ast_dsp = 1;
  10950. break;
  10951. case 'p':
  10952. chan_misdn_log(1, ch->bc->port, "SETOPT: callerpres: %s\n", &tok[1]);
  10953. /* CRICH: callingpres!!! */
  10954. if (strstr(tok, "allowed")) {
  10955. ch->bc->presentation = 0;
  10956. ch->bc->set_presentation = 1;
  10957. } else if (strstr(tok, "restricted")) {
  10958. ch->bc->presentation = 1;
  10959. ch->bc->set_presentation = 1;
  10960. } else if (strstr(tok, "not_screened")) {
  10961. chan_misdn_log(0, ch->bc->port, "SETOPT: callerpres: not_screened is deprecated\n");
  10962. ch->bc->presentation = 1;
  10963. ch->bc->set_presentation = 1;
  10964. }
  10965. break;
  10966. case 'i' :
  10967. chan_misdn_log(1, ch->bc->port, "Ignoring dtmf tones, just use them inband\n");
  10968. ch->ignore_dtmf = 1;
  10969. break;
  10970. default:
  10971. break;
  10972. }
  10973. }
  10974. if (change_jitter) {
  10975. config_jitterbuffer(ch);
  10976. }
  10977. if (ch->faxdetect || ch->ast_dsp) {
  10978. if (!ch->dsp) {
  10979. ch->dsp = ast_dsp_new();
  10980. }
  10981. if (ch->dsp) {
  10982. ast_dsp_set_features(ch->dsp, DSP_FEATURE_DIGIT_DETECT | DSP_FEATURE_FAX_DETECT);
  10983. }
  10984. }
  10985. if (ch->ast_dsp) {
  10986. chan_misdn_log(1, ch->bc->port, "SETOPT: with AST_DSP we deactivate mISDN_dsp\n");
  10987. ch->bc->nodsp = 1;
  10988. }
  10989. return 0;
  10990. }
  10991. int chan_misdn_jb_empty(struct misdn_bchannel *bc, char *buf, int len)
  10992. {
  10993. struct chan_list *ch;
  10994. int res;
  10995. ch = find_chan_by_bc(bc);
  10996. if (!ch) {
  10997. return 0;
  10998. }
  10999. if (ch->jb) {
  11000. res = misdn_jb_empty(ch->jb, buf, len);
  11001. } else {
  11002. res = 0;
  11003. }
  11004. chan_list_unref(ch, "Done emptying jb");
  11005. return res;
  11006. }
  11007. /*******************************************************/
  11008. /***************** JITTERBUFFER ************************/
  11009. /*******************************************************/
  11010. /* allocates the jb-structure and initialize the elements*/
  11011. struct misdn_jb *misdn_jb_init(int size, int upper_threshold)
  11012. {
  11013. struct misdn_jb *jb;
  11014. jb = ast_calloc(1, sizeof(*jb));
  11015. if (!jb) {
  11016. chan_misdn_log(-1, 0, "No free Mem for jb\n");
  11017. return NULL;
  11018. }
  11019. jb->size = size;
  11020. jb->upper_threshold = upper_threshold;
  11021. //jb->wp = 0;
  11022. //jb->rp = 0;
  11023. //jb->state_full = 0;
  11024. //jb->state_empty = 0;
  11025. //jb->bytes_wrote = 0;
  11026. jb->samples = ast_calloc(size, sizeof(*jb->samples));
  11027. if (!jb->samples) {
  11028. ast_free(jb);
  11029. chan_misdn_log(-1, 0, "No free Mem for jb->samples\n");
  11030. return NULL;
  11031. }
  11032. jb->ok = ast_calloc(size, sizeof(*jb->ok));
  11033. if (!jb->ok) {
  11034. ast_free(jb->samples);
  11035. ast_free(jb);
  11036. chan_misdn_log(-1, 0, "No free Mem for jb->ok\n");
  11037. return NULL;
  11038. }
  11039. ast_mutex_init(&jb->mutexjb);
  11040. return jb;
  11041. }
  11042. /* frees the data and destroys the given jitterbuffer struct */
  11043. void misdn_jb_destroy(struct misdn_jb *jb)
  11044. {
  11045. ast_mutex_destroy(&jb->mutexjb);
  11046. ast_free(jb->ok);
  11047. ast_free(jb->samples);
  11048. ast_free(jb);
  11049. }
  11050. /* fills the jitterbuffer with len data returns < 0 if there was an
  11051. error (buffer overflow). */
  11052. int misdn_jb_fill(struct misdn_jb *jb, const char *data, int len)
  11053. {
  11054. int i;
  11055. int j;
  11056. int rp;
  11057. int wp;
  11058. if (!jb || ! data) {
  11059. return 0;
  11060. }
  11061. ast_mutex_lock(&jb->mutexjb);
  11062. wp = jb->wp;
  11063. rp = jb->rp;
  11064. for (i = 0; i < len; i++) {
  11065. jb->samples[wp] = data[i];
  11066. jb->ok[wp] = 1;
  11067. wp = (wp != jb->size - 1) ? wp + 1 : 0;
  11068. if (wp == jb->rp) {
  11069. jb->state_full = 1;
  11070. }
  11071. }
  11072. if (wp >= rp) {
  11073. jb->state_buffer = wp - rp;
  11074. } else {
  11075. jb->state_buffer = jb->size - rp + wp;
  11076. }
  11077. chan_misdn_log(9, 0, "misdn_jb_fill: written:%d | Buffer status:%d p:%p\n", len, jb->state_buffer, jb);
  11078. if (jb->state_full) {
  11079. jb->wp = wp;
  11080. rp = wp;
  11081. for (j = 0; j < jb->upper_threshold; j++) {
  11082. rp = (rp != 0) ? rp - 1 : jb->size - 1;
  11083. }
  11084. jb->rp = rp;
  11085. jb->state_full = 0;
  11086. jb->state_empty = 1;
  11087. ast_mutex_unlock(&jb->mutexjb);
  11088. return -1;
  11089. }
  11090. if (!jb->state_empty) {
  11091. jb->bytes_wrote += len;
  11092. if (jb->bytes_wrote >= jb->upper_threshold) {
  11093. jb->state_empty = 1;
  11094. jb->bytes_wrote = 0;
  11095. }
  11096. }
  11097. jb->wp = wp;
  11098. ast_mutex_unlock(&jb->mutexjb);
  11099. return 0;
  11100. }
  11101. /* gets len bytes out of the jitterbuffer if available, else only the
  11102. available data is returned and the return value indicates the number
  11103. of data. */
  11104. int misdn_jb_empty(struct misdn_jb *jb, char *data, int len)
  11105. {
  11106. int i;
  11107. int wp;
  11108. int rp;
  11109. int read = 0;
  11110. ast_mutex_lock(&jb->mutexjb);
  11111. rp = jb->rp;
  11112. wp = jb->wp;
  11113. if (jb->state_empty) {
  11114. for (i = 0; i < len; i++) {
  11115. if (wp == rp) {
  11116. jb->rp = rp;
  11117. jb->state_empty = 0;
  11118. ast_mutex_unlock(&jb->mutexjb);
  11119. return read;
  11120. } else {
  11121. if (jb->ok[rp] == 1) {
  11122. data[i] = jb->samples[rp];
  11123. jb->ok[rp] = 0;
  11124. rp = (rp != jb->size - 1) ? rp + 1 : 0;
  11125. read += 1;
  11126. }
  11127. }
  11128. }
  11129. if (wp >= rp) {
  11130. jb->state_buffer = wp - rp;
  11131. } else {
  11132. jb->state_buffer = jb->size - rp + wp;
  11133. }
  11134. chan_misdn_log(9, 0, "misdn_jb_empty: read:%d | Buffer status:%d p:%p\n", len, jb->state_buffer, jb);
  11135. jb->rp = rp;
  11136. } else {
  11137. chan_misdn_log(9, 0, "misdn_jb_empty: Wait...requested:%d p:%p\n", len, jb);
  11138. }
  11139. ast_mutex_unlock(&jb->mutexjb);
  11140. return read;
  11141. }
  11142. /*******************************************************/
  11143. /*************** JITTERBUFFER END *********************/
  11144. /*******************************************************/
  11145. static void chan_misdn_log(int level, int port, char *tmpl, ...)
  11146. {
  11147. va_list ap;
  11148. char buf[1024];
  11149. char port_buf[8];
  11150. if (!(0 <= port && port <= max_ports)) {
  11151. ast_log(LOG_WARNING, "chan_misdn_log called with out-of-range port number! (%d)\n", port);
  11152. port = 0;
  11153. level = -1;
  11154. } else if (!(level == -1
  11155. || (misdn_debug_only[port]
  11156. ? (level == 1 && misdn_debug[port]) || level == misdn_debug[port]
  11157. : level <= misdn_debug[port])
  11158. || (level <= misdn_debug[0] && !ast_strlen_zero(global_tracefile)))) {
  11159. /*
  11160. * We are not going to print anything so lets not
  11161. * go to all the work of generating a string.
  11162. */
  11163. return;
  11164. }
  11165. snprintf(port_buf, sizeof(port_buf), "P[%2d] ", port);
  11166. va_start(ap, tmpl);
  11167. vsnprintf(buf, sizeof(buf), tmpl, ap);
  11168. va_end(ap);
  11169. if (level == -1) {
  11170. ast_log(LOG_WARNING, "%s", buf);
  11171. } else if (misdn_debug_only[port]
  11172. ? (level == 1 && misdn_debug[port]) || level == misdn_debug[port]
  11173. : level <= misdn_debug[port]) {
  11174. ast_verbose("%s%s", port_buf, buf);
  11175. }
  11176. if (level <= misdn_debug[0] && !ast_strlen_zero(global_tracefile)) {
  11177. char ctimebuf[30];
  11178. time_t tm;
  11179. char *tmp;
  11180. char *p;
  11181. FILE *fp;
  11182. fp = fopen(global_tracefile, "a+");
  11183. if (!fp) {
  11184. ast_verbose("Error opening Tracefile: [ %s ] %s\n", global_tracefile, strerror(errno));
  11185. return;
  11186. }
  11187. tm = time(NULL);
  11188. tmp = ctime_r(&tm, ctimebuf);
  11189. p = strchr(tmp, '\n');
  11190. if (p) {
  11191. *p = ':';
  11192. }
  11193. fputs(tmp, fp);
  11194. fputs(" ", fp);
  11195. fputs(port_buf, fp);
  11196. fputs(" ", fp);
  11197. fputs(buf, fp);
  11198. fclose(fp);
  11199. }
  11200. }
  11201. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Channel driver for mISDN Support (BRI/PRI)",
  11202. .support_level = AST_MODULE_SUPPORT_EXTENDED,
  11203. .load = load_module,
  11204. .unload = unload_module,
  11205. .reload = reload,
  11206. .load_pri = AST_MODPRI_CHANNEL_DRIVER,
  11207. );