chan_misdn.c 379 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722
  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. * \extref MISDN http://www.misdn.org/
  27. *
  28. * \ingroup channel_drivers
  29. */
  30. /*!
  31. * \note
  32. * To use the CCBS/CCNR supplementary service feature and other
  33. * supplementary services using FACILITY messages requires a
  34. * modified version of mISDN.
  35. *
  36. * \note
  37. * The latest modified mISDN v1.1.x based version is available at:
  38. * http://svn.digium.com/svn/thirdparty/mISDN/trunk
  39. * http://svn.digium.com/svn/thirdparty/mISDNuser/trunk
  40. *
  41. * \note
  42. * Taged versions of the modified mISDN code are available under:
  43. * http://svn.digium.com/svn/thirdparty/mISDN/tags
  44. * http://svn.digium.com/svn/thirdparty/mISDNuser/tags
  45. */
  46. /* Define to enable cli commands to generate canned CCBS messages. */
  47. // #define CCBS_TEST_MESSAGES 1
  48. /*** MODULEINFO
  49. <depend>isdnnet</depend>
  50. <depend>misdn</depend>
  51. <depend>suppserv</depend>
  52. <support_level>extended</support_level>
  53. ***/
  54. #include "asterisk.h"
  55. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  56. #include <pthread.h>
  57. #include <sys/socket.h>
  58. #include <sys/time.h>
  59. #include <arpa/inet.h>
  60. #include <fcntl.h>
  61. #include <sys/ioctl.h>
  62. #include <signal.h>
  63. #include <sys/file.h>
  64. #include <semaphore.h>
  65. #include <ctype.h>
  66. #include <time.h>
  67. #include "asterisk/channel.h"
  68. #include "asterisk/config.h"
  69. #include "asterisk/module.h"
  70. #include "asterisk/pbx.h"
  71. #include "asterisk/io.h"
  72. #include "asterisk/frame.h"
  73. #include "asterisk/translate.h"
  74. #include "asterisk/cli.h"
  75. #include "asterisk/musiconhold.h"
  76. #include "asterisk/dsp.h"
  77. #include "asterisk/file.h"
  78. #include "asterisk/callerid.h"
  79. #include "asterisk/indications.h"
  80. #include "asterisk/app.h"
  81. #include "asterisk/features.h"
  82. #include "asterisk/term.h"
  83. #include "asterisk/sched.h"
  84. #include "asterisk/stringfields.h"
  85. #include "asterisk/abstract_jb.h"
  86. #include "asterisk/causes.h"
  87. #include "asterisk/format.h"
  88. #include "asterisk/format_cap.h"
  89. #include "chan_misdn_config.h"
  90. #include "isdn_lib.h"
  91. static char global_tracefile[BUFFERSIZE + 1];
  92. static int g_config_initialized = 0;
  93. struct misdn_jb{
  94. int size;
  95. int upper_threshold;
  96. char *samples, *ok;
  97. int wp,rp;
  98. int state_empty;
  99. int state_full;
  100. int state_buffer;
  101. int bytes_wrote;
  102. ast_mutex_t mutexjb;
  103. };
  104. /*! \brief allocates the jb-structure and initialize the elements */
  105. struct misdn_jb *misdn_jb_init(int size, int upper_threshold);
  106. /*! \brief frees the data and destroys the given jitterbuffer struct */
  107. void misdn_jb_destroy(struct misdn_jb *jb);
  108. /*! \brief fills the jitterbuffer with len data returns < 0 if there was an
  109. error (buffer overrun). */
  110. int misdn_jb_fill(struct misdn_jb *jb, const char *data, int len);
  111. /*! \brief gets len bytes out of the jitterbuffer if available, else only the
  112. available data is returned and the return value indicates the number
  113. of data. */
  114. int misdn_jb_empty(struct misdn_jb *jb, char *data, int len);
  115. static char *complete_ch(struct ast_cli_args *a);
  116. static char *complete_debug_port(struct ast_cli_args *a);
  117. static char *complete_show_config(struct ast_cli_args *a);
  118. /* BEGIN: chan_misdn.h */
  119. #if defined(AST_MISDN_ENHANCEMENTS)
  120. /*
  121. * This timeout duration is to clean up any call completion records that
  122. * are forgotten about by the switch.
  123. */
  124. #define MISDN_CC_RECORD_AGE_MAX (6UL * 60 * 60) /* seconds */
  125. #define MISDN_CC_REQUEST_WAIT_MAX 5 /* seconds */
  126. /*!
  127. * \brief Caller that initialized call completion services
  128. *
  129. * \details
  130. * This data is the payload for a datastore that is put on the channel that
  131. * initializes call completion services. This datastore is set to be inherited
  132. * by the outbound mISDN channel. When one of these channels hangs up, the
  133. * channel pointer will be set to NULL. That way, we can ensure that we do not
  134. * touch this channel after it gets destroyed.
  135. */
  136. struct misdn_cc_caller {
  137. /*! \brief The channel that initialized call completion services */
  138. struct ast_channel *chan;
  139. };
  140. struct misdn_cc_notify {
  141. /*! \brief Dialplan: Notify extension priority */
  142. int priority;
  143. /*! \brief Dialplan: Notify extension context */
  144. char context[AST_MAX_CONTEXT];
  145. /*! \brief Dialplan: Notify extension number (User-A) */
  146. char exten[AST_MAX_EXTENSION];
  147. };
  148. /*! \brief mISDN call completion record */
  149. struct misdn_cc_record {
  150. /*! \brief Call completion record linked list */
  151. AST_LIST_ENTRY(misdn_cc_record) list;
  152. /*! \brief Time the record was created. */
  153. time_t time_created;
  154. /*! \brief MISDN_CC_RECORD_ID value */
  155. long record_id;
  156. /*!
  157. * \brief Logical Layer 1 port associated with this
  158. * call completion record
  159. */
  160. int port;
  161. /*! \brief TRUE if point-to-point mode (CCBS-T/CCNR-T mode) */
  162. int ptp;
  163. /*! \brief Mode specific parameters */
  164. union {
  165. /*! \brief point-to-point specific parameters. */
  166. struct {
  167. /*!
  168. * \brief Call-completion signaling link.
  169. * NULL if signaling link not established.
  170. */
  171. struct misdn_bchannel *bc;
  172. /*!
  173. * \brief TRUE if we requested the request retention option
  174. * to be enabled.
  175. */
  176. int requested_retention;
  177. /*!
  178. * \brief TRUE if the request retention option is enabled.
  179. */
  180. int retention_enabled;
  181. } ptp;
  182. /*! \brief point-to-multi-point specific parameters. */
  183. struct {
  184. /*! \brief CallLinkageID (valid when port determined) */
  185. int linkage_id;
  186. /*! \breif CCBSReference (valid when activated is TRUE) */
  187. int reference_id;
  188. /*! \brief globalRecall(0), specificRecall(1) */
  189. int recall_mode;
  190. } ptmp;
  191. } mode;
  192. /*! \brief TRUE if call completion activated */
  193. int activated;
  194. /*! \brief Outstanding message ID (valid when outstanding_message) */
  195. int invoke_id;
  196. /*! \brief TRUE if waiting for a response from a message (invoke_id is valid) */
  197. int outstanding_message;
  198. /*! \brief TRUE if activation has been requested */
  199. int activation_requested;
  200. /*!
  201. * \brief TRUE if User-A is free
  202. * \note PTMP - Used to answer CCBSStatusRequest.
  203. * PTP - Determines how to respond to CCBS_T_RemoteUserFree.
  204. */
  205. int party_a_free;
  206. /*! \brief Error code received from last outstanding message. */
  207. enum FacErrorCode error_code;
  208. /*! \brief Reject code received from last outstanding message. */
  209. enum FacRejectCode reject_code;
  210. /*!
  211. * \brief Saved struct misdn_bchannel call information when
  212. * attempted to call User-B
  213. */
  214. struct {
  215. /*! \brief User-A caller id information */
  216. struct misdn_party_id caller;
  217. /*! \brief User-B number information */
  218. struct misdn_party_dialing dialed;
  219. /*! \brief The BC, HLC (optional) and LLC (optional) contents from the SETUP message. */
  220. struct Q931_Bc_Hlc_Llc setup_bc_hlc_llc;
  221. /*! \brief SETUP message bearer capability field code value */
  222. int capability;
  223. /*! \brief TRUE if call made in digital HDLC mode */
  224. int hdlc;
  225. } redial;
  226. /*! \brief Dialplan location to indicate User-B free and User-A is free */
  227. struct misdn_cc_notify remote_user_free;
  228. /*! \brief Dialplan location to indicate User-B free and User-A is busy */
  229. struct misdn_cc_notify b_free;
  230. };
  231. /*! \brief mISDN call completion record database */
  232. static AST_LIST_HEAD_STATIC(misdn_cc_records_db, misdn_cc_record);
  233. /*! \brief Next call completion record ID to use */
  234. static __u16 misdn_cc_record_id;
  235. /*! \brief Next invoke ID to use */
  236. static __s16 misdn_invoke_id;
  237. static const char misdn_no_response_from_network[] = "No response from network";
  238. static const char misdn_cc_record_not_found[] = "Call completion record not found";
  239. /* mISDN channel variable names */
  240. #define MISDN_CC_RECORD_ID "MISDN_CC_RECORD_ID"
  241. #define MISDN_CC_STATUS "MISDN_CC_STATUS"
  242. #define MISDN_ERROR_MSG "MISDN_ERROR_MSG"
  243. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  244. static ast_mutex_t release_lock;
  245. enum misdn_chan_state {
  246. MISDN_NOTHING = 0, /*!< at beginning */
  247. MISDN_WAITING4DIGS, /*!< when waiting for info */
  248. MISDN_EXTCANTMATCH, /*!< when asterisk couldn't match our ext */
  249. MISDN_INCOMING_SETUP, /*!< for incoming setup */
  250. MISDN_DIALING, /*!< when pbx_start */
  251. MISDN_PROGRESS, /*!< we have progress */
  252. MISDN_PROCEEDING, /*!< we have progress */
  253. MISDN_CALLING, /*!< when misdn_call is called */
  254. MISDN_CALLING_ACKNOWLEDGE, /*!< when we get SETUP_ACK */
  255. MISDN_ALERTING, /*!< when Alerting */
  256. MISDN_BUSY, /*!< when BUSY */
  257. MISDN_CONNECTED, /*!< when connected */
  258. MISDN_DISCONNECTED, /*!< when connected */
  259. MISDN_CLEANING, /*!< when hangup from * but we were connected before */
  260. };
  261. /*! Asterisk created the channel (outgoing call) */
  262. #define ORG_AST 1
  263. /*! mISDN created the channel (incoming call) */
  264. #define ORG_MISDN 2
  265. enum misdn_hold_state {
  266. MISDN_HOLD_IDLE, /*!< HOLD not active */
  267. MISDN_HOLD_ACTIVE, /*!< Call is held */
  268. MISDN_HOLD_TRANSFER, /*!< Held call is being transferred */
  269. MISDN_HOLD_DISCONNECT, /*!< Held call is being disconnected */
  270. };
  271. struct hold_info {
  272. /*!
  273. * \brief Call HOLD state.
  274. */
  275. enum misdn_hold_state state;
  276. /*!
  277. * \brief Logical port the channel call record is HELD on
  278. * because the B channel is no longer associated.
  279. */
  280. int port;
  281. /*!
  282. * \brief Original B channel number the HELD call was using.
  283. * \note Used only for debug display messages.
  284. */
  285. int channel;
  286. };
  287. #define chan_list_ref(obj, debug) (ao2_t_ref((obj), +1, (debug)), (obj))
  288. #define chan_list_unref(obj, debug) (ao2_t_ref((obj), -1, (debug)), NULL)
  289. /*!
  290. * \brief Channel call record structure
  291. */
  292. struct chan_list {
  293. /*!
  294. * \brief The "allowed_bearers" string read in from /etc/asterisk/misdn.conf
  295. */
  296. char allowed_bearers[BUFFERSIZE + 1];
  297. /*!
  298. * \brief State of the channel
  299. */
  300. enum misdn_chan_state state;
  301. /*!
  302. * \brief TRUE if a hangup needs to be queued
  303. * \note This is a debug flag only used to catch calls to hangup_chan() that are already hungup.
  304. */
  305. int need_queue_hangup;
  306. /*!
  307. * \brief TRUE if a channel can be hung up by calling asterisk directly when done.
  308. */
  309. int need_hangup;
  310. /*!
  311. * \brief TRUE if we could send an AST_CONTROL_BUSY if needed.
  312. */
  313. int need_busy;
  314. /*!
  315. * \brief Who originally created this channel. ORG_AST or ORG_MISDN
  316. */
  317. int originator;
  318. /*!
  319. * \brief TRUE of we are not to respond immediately to a SETUP message. Check the dialplan first.
  320. * \note The "noautorespond_on_setup" boolean read in from /etc/asterisk/misdn.conf
  321. */
  322. int noautorespond_on_setup;
  323. int norxtone; /*!< Boolean assigned values but the value is not used. */
  324. /*!
  325. * \brief TRUE if we are not to generate tones (Playtones)
  326. */
  327. int notxtone;
  328. /*!
  329. * \brief TRUE if echo canceller is enabled. Value is toggled.
  330. */
  331. int toggle_ec;
  332. /*!
  333. * \brief TRUE if you want to send Tone Indications to an incoming
  334. * ISDN channel on a TE Port.
  335. * \note The "incoming_early_audio" boolean read in from /etc/asterisk/misdn.conf
  336. */
  337. int incoming_early_audio;
  338. /*!
  339. * \brief TRUE if DTMF digits are to be passed inband only.
  340. * \note It is settable by the misdn_set_opt() application.
  341. */
  342. int ignore_dtmf;
  343. /*!
  344. * \brief Pipe file descriptor handles array.
  345. * Read from pipe[0], write to pipe[1]
  346. */
  347. int pipe[2];
  348. /*!
  349. * \brief Read buffer for inbound audio from pipe[0]
  350. */
  351. char ast_rd_buf[4096];
  352. /*!
  353. * \brief Inbound audio frame returned by misdn_read().
  354. */
  355. struct ast_frame frame;
  356. /*!
  357. * \brief Fax detection option. (0:no 1:yes 2:yes+nojump)
  358. * \note The "faxdetect" option string read in from /etc/asterisk/misdn.conf
  359. * \note It is settable by the misdn_set_opt() application.
  360. */
  361. int faxdetect;
  362. /*!
  363. * \brief Number of seconds to detect a Fax machine when detection enabled.
  364. * \note 0 disables the timeout.
  365. * \note The "faxdetect_timeout" value read in from /etc/asterisk/misdn.conf
  366. */
  367. int faxdetect_timeout;
  368. /*!
  369. * \brief Starting time of fax detection with timeout when nonzero.
  370. */
  371. struct timeval faxdetect_tv;
  372. /*!
  373. * \brief TRUE if a fax has been detected.
  374. */
  375. int faxhandled;
  376. /*!
  377. * \brief TRUE if we will use the Asterisk DSP to detect DTMF/Fax
  378. * \note The "astdtmf" boolean read in from /etc/asterisk/misdn.conf
  379. */
  380. int ast_dsp;
  381. /*!
  382. * \brief Jitterbuffer length
  383. * \note The "jitterbuffer" value read in from /etc/asterisk/misdn.conf
  384. */
  385. int jb_len;
  386. /*!
  387. * \brief Jitterbuffer upper threshold
  388. * \note The "jitterbuffer_upper_threshold" value read in from /etc/asterisk/misdn.conf
  389. */
  390. int jb_upper_threshold;
  391. /*!
  392. * \brief Allocated jitterbuffer controller
  393. * \note misdn_jb_init() creates the jitterbuffer.
  394. * \note Must use misdn_jb_destroy() to clean up.
  395. */
  396. struct misdn_jb *jb;
  397. /*!
  398. * \brief Allocated DSP controller
  399. * \note ast_dsp_new() creates the DSP controller.
  400. * \note Must use ast_dsp_free() to clean up.
  401. */
  402. struct ast_dsp *dsp;
  403. /*!
  404. * \brief Associated Asterisk channel structure.
  405. */
  406. struct ast_channel * ast;
  407. /*!
  408. * \brief Associated B channel structure.
  409. */
  410. struct misdn_bchannel *bc;
  411. #if defined(AST_MISDN_ENHANCEMENTS)
  412. /*!
  413. * \brief Peer channel for which call completion was initialized.
  414. */
  415. struct misdn_cc_caller *peer;
  416. /*! \brief Associated call completion record ID (-1 if not associated) */
  417. long record_id;
  418. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  419. /*!
  420. * \brief HELD channel call information
  421. */
  422. struct hold_info hold;
  423. /*!
  424. * \brief From associated B channel: Layer 3 process ID
  425. * \note Used to find the HELD channel call record when retrieving a call.
  426. */
  427. unsigned int l3id;
  428. /*!
  429. * \brief From associated B channel: B Channel mISDN driver layer ID from mISDN_get_layerid()
  430. * \note Used only for debug display messages.
  431. */
  432. int addr;
  433. /*!
  434. * \brief Incoming call dialplan context identifier.
  435. * \note The "context" string read in from /etc/asterisk/misdn.conf
  436. */
  437. char context[AST_MAX_CONTEXT];
  438. /*!
  439. * \brief The configured music-on-hold class to use for this call.
  440. * \note The "musicclass" string read in from /etc/asterisk/misdn.conf
  441. */
  442. char mohinterpret[MAX_MUSICCLASS];
  443. /*!
  444. * \brief Number of outgoing audio frames dropped since last debug gripe message.
  445. */
  446. int dropped_frame_cnt;
  447. /*!
  448. * \brief TRUE if we must do the ringback tones.
  449. * \note The "far_alerting" boolean read in from /etc/asterisk/misdn.conf
  450. */
  451. int far_alerting;
  452. /*!
  453. * \brief TRUE if NT should disconnect an overlap dialing call when a timeout occurs.
  454. * \note The "nttimeout" boolean read in from /etc/asterisk/misdn.conf
  455. */
  456. int nttimeout;
  457. /*!
  458. * \brief Tone zone sound used for dialtone generation.
  459. * \note Used as a boolean. Non-NULL to prod generation if enabled.
  460. */
  461. struct ast_tone_zone_sound *ts;
  462. /*!
  463. * \brief Enables overlap dialing for the set amount of seconds. (0 = Disabled)
  464. * \note The "overlapdial" value read in from /etc/asterisk/misdn.conf
  465. */
  466. int overlap_dial;
  467. /*!
  468. * \brief Overlap dialing timeout Task ID. -1 if not running.
  469. */
  470. int overlap_dial_task;
  471. /*!
  472. * \brief overlap_tv access lock.
  473. */
  474. ast_mutex_t overlap_tv_lock;
  475. /*!
  476. * \brief Overlap timer start time. Timer restarted for every digit received.
  477. */
  478. struct timeval overlap_tv;
  479. /*!
  480. * \brief Next channel call record in the list.
  481. */
  482. struct chan_list *next;
  483. };
  484. int MAXTICS = 8;
  485. void export_ch(struct ast_channel *chan, struct misdn_bchannel *bc, struct chan_list *ch);
  486. void import_ch(struct ast_channel *chan, struct misdn_bchannel *bc, struct chan_list *ch);
  487. static struct ast_frame *process_ast_dsp(struct chan_list *tmp, struct ast_frame *frame);
  488. struct robin_list {
  489. char *group;
  490. int port;
  491. int channel;
  492. struct robin_list *next;
  493. struct robin_list *prev;
  494. };
  495. static struct robin_list *robin = NULL;
  496. static void free_robin_list(void)
  497. {
  498. struct robin_list *r;
  499. struct robin_list *next;
  500. for (r = robin, robin = NULL; r; r = next) {
  501. next = r->next;
  502. ast_free(r->group);
  503. ast_free(r);
  504. }
  505. }
  506. static struct robin_list *get_robin_position(char *group)
  507. {
  508. struct robin_list *new;
  509. struct robin_list *iter = robin;
  510. for (; iter; iter = iter->next) {
  511. if (!strcasecmp(iter->group, group)) {
  512. return iter;
  513. }
  514. }
  515. new = ast_calloc(1, sizeof(*new));
  516. if (!new) {
  517. return NULL;
  518. }
  519. new->group = ast_strdup(group);
  520. if (!new->group) {
  521. ast_free(new);
  522. return NULL;
  523. }
  524. new->channel = 1;
  525. if (robin) {
  526. new->next = robin;
  527. robin->prev = new;
  528. }
  529. robin = new;
  530. return robin;
  531. }
  532. /*! \brief the main schedule context for stuff like l1 watcher, overlap dial, ... */
  533. static struct ast_sched_context *misdn_tasks = NULL;
  534. static pthread_t misdn_tasks_thread;
  535. static int *misdn_ports;
  536. static void chan_misdn_log(int level, int port, char *tmpl, ...)
  537. __attribute__((format(printf, 3, 4)));
  538. static struct ast_channel *misdn_new(struct chan_list *cl, int state, char *exten, char *callerid, struct ast_format_cap *cap, const char *linkedid, int port, int c);
  539. static void send_digit_to_chan(struct chan_list *cl, char digit);
  540. static int pbx_start_chan(struct chan_list *ch);
  541. #define MISDN_ASTERISK_TECH_PVT(ast) ast->tech_pvt
  542. #include "asterisk/strings.h"
  543. /* #define MISDN_DEBUG 1 */
  544. static const char misdn_type[] = "mISDN";
  545. static int tracing = 0;
  546. /*! \brief Only alaw and mulaw is allowed for now */
  547. static struct ast_format prefformat; /* AST_FORMAT_SLINEAR ; AST_FORMAT_ULAW | */
  548. static int *misdn_debug;
  549. static int *misdn_debug_only;
  550. static int max_ports;
  551. static int *misdn_in_calls;
  552. static int *misdn_out_calls;
  553. /*!
  554. * \brief Global channel call record list head.
  555. */
  556. static struct chan_list *cl_te=NULL;
  557. static ast_mutex_t cl_te_lock;
  558. static enum event_response_e
  559. cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data);
  560. static int send_cause2ast(struct ast_channel *ast, struct misdn_bchannel *bc, struct chan_list *ch);
  561. static void cl_queue_chan(struct chan_list *chan);
  562. static int dialtone_indicate(struct chan_list *cl);
  563. static void hanguptone_indicate(struct chan_list *cl);
  564. static int stop_indicate(struct chan_list *cl);
  565. static int start_bc_tones(struct chan_list *cl);
  566. static int stop_bc_tones(struct chan_list *cl);
  567. static void release_chan_early(struct chan_list *ch);
  568. static void release_chan(struct chan_list *ch, struct misdn_bchannel *bc);
  569. #if defined(AST_MISDN_ENHANCEMENTS)
  570. static const char misdn_command_name[] = "misdn_command";
  571. static int misdn_command_exec(struct ast_channel *chan, const char *data);
  572. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  573. static int misdn_check_l2l1(struct ast_channel *chan, const char *data);
  574. static int misdn_set_opt_exec(struct ast_channel *chan, const char *data);
  575. static int misdn_facility_exec(struct ast_channel *chan, const char *data);
  576. int chan_misdn_jb_empty(struct misdn_bchannel *bc, char *buf, int len);
  577. void debug_numtype(int port, int numtype, char *type);
  578. int add_out_calls(int port);
  579. int add_in_calls(int port);
  580. #ifdef MISDN_1_2
  581. static int update_pipeline_config(struct misdn_bchannel *bc);
  582. #else
  583. static int update_ec_config(struct misdn_bchannel *bc);
  584. #endif
  585. /*************** Helpers *****************/
  586. static int misdn_chan_is_valid(struct chan_list *ch)
  587. {
  588. struct chan_list *list;
  589. ast_mutex_lock(&cl_te_lock);
  590. for (list = cl_te; list; list = list->next) {
  591. if (list == ch) {
  592. ast_mutex_unlock(&cl_te_lock);
  593. return 1;
  594. }
  595. }
  596. ast_mutex_unlock(&cl_te_lock);
  597. return 0;
  598. }
  599. /*! Returns a reference to the found chan_list. */
  600. static struct chan_list *get_chan_by_ast(struct ast_channel *ast)
  601. {
  602. struct chan_list *tmp;
  603. ast_mutex_lock(&cl_te_lock);
  604. for (tmp = cl_te; tmp; tmp = tmp->next) {
  605. if (tmp->ast == ast) {
  606. chan_list_ref(tmp, "Found chan_list by ast");
  607. ast_mutex_unlock(&cl_te_lock);
  608. return tmp;
  609. }
  610. }
  611. ast_mutex_unlock(&cl_te_lock);
  612. return NULL;
  613. }
  614. /*! Returns a reference to the found chan_list. */
  615. static struct chan_list *get_chan_by_ast_name(const char *name)
  616. {
  617. struct chan_list *tmp;
  618. ast_mutex_lock(&cl_te_lock);
  619. for (tmp = cl_te; tmp; tmp = tmp->next) {
  620. if (tmp->ast && strcmp(tmp->ast->name, name) == 0) {
  621. chan_list_ref(tmp, "Found chan_list by ast name");
  622. ast_mutex_unlock(&cl_te_lock);
  623. return tmp;
  624. }
  625. }
  626. ast_mutex_unlock(&cl_te_lock);
  627. return NULL;
  628. }
  629. #if defined(AST_MISDN_ENHANCEMENTS)
  630. /*!
  631. * \internal
  632. * \brief Destroy the misdn_cc_ds_info datastore payload
  633. *
  634. * \param[in] data the datastore payload, a reference to an misdn_cc_caller
  635. *
  636. * \details
  637. * Since the payload is a reference to an astobj2 object, we just decrement its
  638. * reference count. Before doing so, we NULL out the channel pointer inside of
  639. * the misdn_cc_caller instance. This function will be called in one of two
  640. * cases. In both cases, we no longer need the channel pointer:
  641. *
  642. * - The original channel that initialized call completion services, the same
  643. * channel that is stored here, has been destroyed early. This could happen
  644. * if it transferred the mISDN channel, for example.
  645. *
  646. * - The mISDN channel that had this datastore inherited on to it is now being
  647. * destroyed. If this is the case, then the call completion events have
  648. * already occurred and the appropriate channel variables have already been
  649. * set on the original channel that requested call completion services.
  650. *
  651. * \return Nothing
  652. */
  653. static void misdn_cc_ds_destroy(void *data)
  654. {
  655. struct misdn_cc_caller *cc_caller = data;
  656. ao2_lock(cc_caller);
  657. cc_caller->chan = NULL;
  658. ao2_unlock(cc_caller);
  659. ao2_ref(cc_caller, -1);
  660. }
  661. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  662. #if defined(AST_MISDN_ENHANCEMENTS)
  663. /*!
  664. * \internal
  665. * \brief Duplicate the misdn_cc_ds_info datastore payload
  666. *
  667. * \param[in] data the datastore payload, a reference to an misdn_cc_caller
  668. *
  669. * \details
  670. * All we need to do is bump the reference count and return the same instance.
  671. *
  672. * \return A reference to an instance of a misdn_cc_caller
  673. */
  674. static void *misdn_cc_ds_duplicate(void *data)
  675. {
  676. struct misdn_cc_caller *cc_caller = data;
  677. ao2_ref(cc_caller, +1);
  678. return cc_caller;
  679. }
  680. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  681. #if defined(AST_MISDN_ENHANCEMENTS)
  682. static const struct ast_datastore_info misdn_cc_ds_info = {
  683. .type = "misdn_cc",
  684. .destroy = misdn_cc_ds_destroy,
  685. .duplicate = misdn_cc_ds_duplicate,
  686. };
  687. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  688. #if defined(AST_MISDN_ENHANCEMENTS)
  689. /*!
  690. * \internal
  691. * \brief Set a channel var on the peer channel for call completion services
  692. *
  693. * \param[in] peer The peer that initialized call completion services
  694. * \param[in] var The variable name to set
  695. * \param[in] value The variable value to set
  696. *
  697. * This function may be called from outside of the channel thread. It handles
  698. * the fact that the peer channel may be hung up and destroyed at any time.
  699. *
  700. * \return nothing
  701. */
  702. static void misdn_cc_set_peer_var(struct misdn_cc_caller *peer, const char *var,
  703. const char *value)
  704. {
  705. ao2_lock(peer);
  706. /*! \todo XXX This nastiness can go away once ast_channel is ref counted! */
  707. while (peer->chan && ast_channel_trylock(peer->chan)) {
  708. ao2_unlock(peer);
  709. sched_yield();
  710. ao2_lock(peer);
  711. }
  712. if (peer->chan) {
  713. pbx_builtin_setvar_helper(peer->chan, var, value);
  714. ast_channel_unlock(peer->chan);
  715. }
  716. ao2_unlock(peer);
  717. }
  718. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  719. #if defined(AST_MISDN_ENHANCEMENTS)
  720. /*!
  721. * \internal
  722. * \brief Get a reference to the CC caller if it exists
  723. */
  724. static struct misdn_cc_caller *misdn_cc_caller_get(struct ast_channel *chan)
  725. {
  726. struct ast_datastore *datastore;
  727. struct misdn_cc_caller *cc_caller;
  728. ast_channel_lock(chan);
  729. if (!(datastore = ast_channel_datastore_find(chan, &misdn_cc_ds_info, NULL))) {
  730. ast_channel_unlock(chan);
  731. return NULL;
  732. }
  733. ao2_ref(datastore->data, +1);
  734. cc_caller = datastore->data;
  735. ast_channel_unlock(chan);
  736. return cc_caller;
  737. }
  738. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  739. #if defined(AST_MISDN_ENHANCEMENTS)
  740. /*!
  741. * \internal
  742. * \brief Find the call completion record given the record id.
  743. *
  744. * \param record_id
  745. *
  746. * \retval pointer to found call completion record
  747. * \retval NULL if not found
  748. *
  749. * \note Assumes the misdn_cc_records_db lock is already obtained.
  750. */
  751. static struct misdn_cc_record *misdn_cc_find_by_id(long record_id)
  752. {
  753. struct misdn_cc_record *current;
  754. AST_LIST_TRAVERSE(&misdn_cc_records_db, current, list) {
  755. if (current->record_id == record_id) {
  756. /* Found the record */
  757. break;
  758. }
  759. }
  760. return current;
  761. }
  762. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  763. #if defined(AST_MISDN_ENHANCEMENTS)
  764. /*!
  765. * \internal
  766. * \brief Find the call completion record given the port and call linkage id.
  767. *
  768. * \param port Logical port number
  769. * \param linkage_id Call linkage ID number from switch.
  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_linkage(int port, int linkage_id)
  777. {
  778. struct misdn_cc_record *current;
  779. AST_LIST_TRAVERSE(&misdn_cc_records_db, current, list) {
  780. if (current->port == port
  781. && !current->ptp
  782. && current->mode.ptmp.linkage_id == linkage_id) {
  783. /* Found the record */
  784. break;
  785. }
  786. }
  787. return current;
  788. }
  789. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  790. #if defined(AST_MISDN_ENHANCEMENTS)
  791. /*!
  792. * \internal
  793. * \brief Find the call completion record given the port and outstanding invocation id.
  794. *
  795. * \param port Logical port number
  796. * \param invoke_id Outstanding message invocation ID number.
  797. *
  798. * \retval pointer to found call completion record
  799. * \retval NULL if not found
  800. *
  801. * \note Assumes the misdn_cc_records_db lock is already obtained.
  802. */
  803. static struct misdn_cc_record *misdn_cc_find_by_invoke(int port, int invoke_id)
  804. {
  805. struct misdn_cc_record *current;
  806. AST_LIST_TRAVERSE(&misdn_cc_records_db, current, list) {
  807. if (current->outstanding_message
  808. && current->invoke_id == invoke_id
  809. && current->port == port) {
  810. /* Found the record */
  811. break;
  812. }
  813. }
  814. return current;
  815. }
  816. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  817. #if defined(AST_MISDN_ENHANCEMENTS)
  818. /*!
  819. * \internal
  820. * \brief Find the call completion record given the port and CCBS reference id.
  821. *
  822. * \param port Logical port number
  823. * \param reference_id CCBS reference ID number from switch.
  824. *
  825. * \retval pointer to found call completion record
  826. * \retval NULL if not found
  827. *
  828. * \note Assumes the misdn_cc_records_db lock is already obtained.
  829. */
  830. static struct misdn_cc_record *misdn_cc_find_by_reference(int port, int reference_id)
  831. {
  832. struct misdn_cc_record *current;
  833. AST_LIST_TRAVERSE(&misdn_cc_records_db, current, list) {
  834. if (current->activated
  835. && current->port == port
  836. && !current->ptp
  837. && current->mode.ptmp.reference_id == reference_id) {
  838. /* Found the record */
  839. break;
  840. }
  841. }
  842. return current;
  843. }
  844. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  845. #if defined(AST_MISDN_ENHANCEMENTS)
  846. /*!
  847. * \internal
  848. * \brief Find the call completion record given the B channel pointer
  849. *
  850. * \param bc B channel control structure pointer.
  851. *
  852. * \retval pointer to found call completion record
  853. * \retval NULL if not found
  854. *
  855. * \note Assumes the misdn_cc_records_db lock is already obtained.
  856. */
  857. static struct misdn_cc_record *misdn_cc_find_by_bc(const struct misdn_bchannel *bc)
  858. {
  859. struct misdn_cc_record *current;
  860. if (bc) {
  861. AST_LIST_TRAVERSE(&misdn_cc_records_db, current, list) {
  862. if (current->ptp
  863. && current->mode.ptp.bc == bc) {
  864. /* Found the record */
  865. break;
  866. }
  867. }
  868. } else {
  869. current = NULL;
  870. }
  871. return current;
  872. }
  873. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  874. #if defined(AST_MISDN_ENHANCEMENTS)
  875. /*!
  876. * \internal
  877. * \brief Delete the given call completion record
  878. *
  879. * \param doomed Call completion record to destroy
  880. *
  881. * \return Nothing
  882. *
  883. * \note Assumes the misdn_cc_records_db lock is already obtained.
  884. */
  885. static void misdn_cc_delete(struct misdn_cc_record *doomed)
  886. {
  887. struct misdn_cc_record *current;
  888. AST_LIST_TRAVERSE_SAFE_BEGIN(&misdn_cc_records_db, current, list) {
  889. if (current == doomed) {
  890. AST_LIST_REMOVE_CURRENT(list);
  891. ast_free(current);
  892. return;
  893. }
  894. }
  895. AST_LIST_TRAVERSE_SAFE_END;
  896. /* The doomed node is not in the call completion database */
  897. }
  898. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  899. #if defined(AST_MISDN_ENHANCEMENTS)
  900. /*!
  901. * \internal
  902. * \brief Delete all old call completion records
  903. *
  904. * \return Nothing
  905. *
  906. * \note Assumes the misdn_cc_records_db lock is already obtained.
  907. */
  908. static void misdn_cc_remove_old(void)
  909. {
  910. struct misdn_cc_record *current;
  911. time_t now;
  912. now = time(NULL);
  913. AST_LIST_TRAVERSE_SAFE_BEGIN(&misdn_cc_records_db, current, list) {
  914. if (MISDN_CC_RECORD_AGE_MAX < now - current->time_created) {
  915. if (current->ptp && current->mode.ptp.bc) {
  916. /* Close the old call-completion signaling link */
  917. current->mode.ptp.bc->fac_out.Function = Fac_None;
  918. current->mode.ptp.bc->out_cause = AST_CAUSE_NORMAL_CLEARING;
  919. misdn_lib_send_event(current->mode.ptp.bc, EVENT_RELEASE_COMPLETE);
  920. }
  921. /* Remove the old call completion record */
  922. AST_LIST_REMOVE_CURRENT(list);
  923. ast_free(current);
  924. }
  925. }
  926. AST_LIST_TRAVERSE_SAFE_END;
  927. }
  928. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  929. #if defined(AST_MISDN_ENHANCEMENTS)
  930. /*!
  931. * \internal
  932. * \brief Allocate the next record id.
  933. *
  934. * \retval New record id on success.
  935. * \retval -1 on error.
  936. *
  937. * \note Assumes the misdn_cc_records_db lock is already obtained.
  938. */
  939. static long misdn_cc_record_id_new(void)
  940. {
  941. long record_id;
  942. long first_id;
  943. record_id = ++misdn_cc_record_id;
  944. first_id = record_id;
  945. while (misdn_cc_find_by_id(record_id)) {
  946. record_id = ++misdn_cc_record_id;
  947. if (record_id == first_id) {
  948. /*
  949. * We have a resource leak.
  950. * We should never need to allocate 64k records.
  951. */
  952. chan_misdn_log(0, 0, " --> ERROR Too many call completion records!\n");
  953. record_id = -1;
  954. break;
  955. }
  956. }
  957. return record_id;
  958. }
  959. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  960. #if defined(AST_MISDN_ENHANCEMENTS)
  961. /*!
  962. * \internal
  963. * \brief Create a new call completion record
  964. *
  965. * \retval pointer to new call completion record
  966. * \retval NULL if failed
  967. *
  968. * \note Assumes the misdn_cc_records_db lock is already obtained.
  969. */
  970. static struct misdn_cc_record *misdn_cc_new(void)
  971. {
  972. struct misdn_cc_record *cc_record;
  973. long record_id;
  974. misdn_cc_remove_old();
  975. cc_record = ast_calloc(1, sizeof(*cc_record));
  976. if (cc_record) {
  977. record_id = misdn_cc_record_id_new();
  978. if (record_id < 0) {
  979. ast_free(cc_record);
  980. return NULL;
  981. }
  982. /* Initialize the new record */
  983. cc_record->record_id = record_id;
  984. cc_record->port = -1;/* Invalid port so it will never be found this way */
  985. cc_record->invoke_id = ++misdn_invoke_id;
  986. cc_record->party_a_free = 1;/* Default User-A as free */
  987. cc_record->error_code = FacError_None;
  988. cc_record->reject_code = FacReject_None;
  989. cc_record->time_created = time(NULL);
  990. /* Insert the new record into the database */
  991. AST_LIST_INSERT_HEAD(&misdn_cc_records_db, cc_record, list);
  992. }
  993. return cc_record;
  994. }
  995. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  996. #if defined(AST_MISDN_ENHANCEMENTS)
  997. /*!
  998. * \internal
  999. * \brief Destroy the call completion record database
  1000. *
  1001. * \return Nothing
  1002. */
  1003. static void misdn_cc_destroy(void)
  1004. {
  1005. struct misdn_cc_record *current;
  1006. while ((current = AST_LIST_REMOVE_HEAD(&misdn_cc_records_db, list))) {
  1007. /* Do a misdn_cc_delete(current) inline */
  1008. ast_free(current);
  1009. }
  1010. }
  1011. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  1012. #if defined(AST_MISDN_ENHANCEMENTS)
  1013. /*!
  1014. * \internal
  1015. * \brief Initialize the call completion record database
  1016. *
  1017. * \return Nothing
  1018. */
  1019. static void misdn_cc_init(void)
  1020. {
  1021. misdn_cc_record_id = 0;
  1022. }
  1023. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  1024. #if defined(AST_MISDN_ENHANCEMENTS)
  1025. /*!
  1026. * \internal
  1027. * \brief Check the status of an outstanding invocation request.
  1028. *
  1029. * \param data Points to an integer containing the call completion record id.
  1030. *
  1031. * \retval 0 if got a response.
  1032. * \retval -1 if no response yet.
  1033. */
  1034. static int misdn_cc_response_check(void *data)
  1035. {
  1036. int not_responded;
  1037. struct misdn_cc_record *cc_record;
  1038. AST_LIST_LOCK(&misdn_cc_records_db);
  1039. cc_record = misdn_cc_find_by_id(*(long *) data);
  1040. if (cc_record) {
  1041. if (cc_record->outstanding_message) {
  1042. not_responded = -1;
  1043. } else {
  1044. not_responded = 0;
  1045. }
  1046. } else {
  1047. /* No record so there is no response to check. */
  1048. not_responded = 0;
  1049. }
  1050. AST_LIST_UNLOCK(&misdn_cc_records_db);
  1051. return not_responded;
  1052. }
  1053. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  1054. #if defined(AST_MISDN_ENHANCEMENTS)
  1055. /*!
  1056. * \internal
  1057. * \brief Wait for a response from the switch for an outstanding
  1058. * invocation request.
  1059. *
  1060. * \param chan Asterisk channel to operate upon.
  1061. * \param wait_seconds Number of seconds to wait
  1062. * \param record_id Call completion record ID.
  1063. *
  1064. * \return Nothing
  1065. */
  1066. static void misdn_cc_response_wait(struct ast_channel *chan, int wait_seconds, long record_id)
  1067. {
  1068. unsigned count;
  1069. for (count = 2 * MISDN_CC_REQUEST_WAIT_MAX; count--;) {
  1070. /* Sleep in 500 ms increments */
  1071. if (ast_safe_sleep_conditional(chan, 500, misdn_cc_response_check, &record_id) != 0) {
  1072. /* We got hung up or our response came in. */
  1073. break;
  1074. }
  1075. }
  1076. }
  1077. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  1078. #if defined(AST_MISDN_ENHANCEMENTS)
  1079. /*!
  1080. * \internal
  1081. * \brief Convert the mISDN reject code to a string
  1082. *
  1083. * \param code mISDN reject code.
  1084. *
  1085. * \return The mISDN reject code as a string
  1086. */
  1087. static const char *misdn_to_str_reject_code(enum FacRejectCode code)
  1088. {
  1089. static const struct {
  1090. enum FacRejectCode code;
  1091. char *name;
  1092. } arr[] = {
  1093. /* *INDENT-OFF* */
  1094. { FacReject_None, "No reject occurred" },
  1095. { FacReject_Unknown, "Unknown reject code" },
  1096. { FacReject_Gen_UnrecognizedComponent, "General: Unrecognized Component" },
  1097. { FacReject_Gen_MistypedComponent, "General: Mistyped Component" },
  1098. { FacReject_Gen_BadlyStructuredComponent, "General: Badly Structured Component" },
  1099. { FacReject_Inv_DuplicateInvocation, "Invoke: Duplicate Invocation" },
  1100. { FacReject_Inv_UnrecognizedOperation, "Invoke: Unrecognized Operation" },
  1101. { FacReject_Inv_MistypedArgument, "Invoke: Mistyped Argument" },
  1102. { FacReject_Inv_ResourceLimitation, "Invoke: Resource Limitation" },
  1103. { FacReject_Inv_InitiatorReleasing, "Invoke: Initiator Releasing" },
  1104. { FacReject_Inv_UnrecognizedLinkedID, "Invoke: Unrecognized Linked ID" },
  1105. { FacReject_Inv_LinkedResponseUnexpected, "Invoke: Linked Response Unexpected" },
  1106. { FacReject_Inv_UnexpectedChildOperation, "Invoke: Unexpected Child Operation" },
  1107. { FacReject_Res_UnrecognizedInvocation, "Result: Unrecognized Invocation" },
  1108. { FacReject_Res_ResultResponseUnexpected, "Result: Result Response Unexpected" },
  1109. { FacReject_Res_MistypedResult, "Result: Mistyped Result" },
  1110. { FacReject_Err_UnrecognizedInvocation, "Error: Unrecognized Invocation" },
  1111. { FacReject_Err_ErrorResponseUnexpected, "Error: Error Response Unexpected" },
  1112. { FacReject_Err_UnrecognizedError, "Error: Unrecognized Error" },
  1113. { FacReject_Err_UnexpectedError, "Error: Unexpected Error" },
  1114. { FacReject_Err_MistypedParameter, "Error: Mistyped Parameter" },
  1115. /* *INDENT-ON* */
  1116. };
  1117. unsigned index;
  1118. for (index = 0; index < ARRAY_LEN(arr); ++index) {
  1119. if (arr[index].code == code) {
  1120. return arr[index].name;
  1121. }
  1122. }
  1123. return "unknown";
  1124. }
  1125. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  1126. #if defined(AST_MISDN_ENHANCEMENTS)
  1127. /*!
  1128. * \internal
  1129. * \brief Convert the mISDN error code to a string
  1130. *
  1131. * \param code mISDN error code.
  1132. *
  1133. * \return The mISDN error code as a string
  1134. */
  1135. static const char *misdn_to_str_error_code(enum FacErrorCode code)
  1136. {
  1137. static const struct {
  1138. enum FacErrorCode code;
  1139. char *name;
  1140. } arr[] = {
  1141. /* *INDENT-OFF* */
  1142. { FacError_None, "No error occurred" },
  1143. { FacError_Unknown, "Unknown OID error code" },
  1144. { FacError_Gen_NotSubscribed, "General: Not Subscribed" },
  1145. { FacError_Gen_NotAvailable, "General: Not Available" },
  1146. { FacError_Gen_NotImplemented, "General: Not Implemented" },
  1147. { FacError_Gen_InvalidServedUserNr, "General: Invalid Served User Number" },
  1148. { FacError_Gen_InvalidCallState, "General: Invalid Call State" },
  1149. { FacError_Gen_BasicServiceNotProvided, "General: Basic Service Not Provided" },
  1150. { FacError_Gen_NotIncomingCall, "General: Not Incoming Call" },
  1151. { FacError_Gen_SupplementaryServiceInteractionNotAllowed,"General: Supplementary Service Interaction Not Allowed" },
  1152. { FacError_Gen_ResourceUnavailable, "General: Resource Unavailable" },
  1153. { FacError_Div_InvalidDivertedToNr, "Diversion: Invalid Diverted To Number" },
  1154. { FacError_Div_SpecialServiceNr, "Diversion: Special Service Number" },
  1155. { FacError_Div_DiversionToServedUserNr, "Diversion: Diversion To Served User Number" },
  1156. { FacError_Div_IncomingCallAccepted, "Diversion: Incoming Call Accepted" },
  1157. { FacError_Div_NumberOfDiversionsExceeded, "Diversion: Number Of Diversions Exceeded" },
  1158. { FacError_Div_NotActivated, "Diversion: Not Activated" },
  1159. { FacError_Div_RequestAlreadyAccepted, "Diversion: Request Already Accepted" },
  1160. { FacError_AOC_NoChargingInfoAvailable, "AOC: No Charging Info Available" },
  1161. { FacError_CCBS_InvalidCallLinkageID, "CCBS: Invalid Call Linkage ID" },
  1162. { FacError_CCBS_InvalidCCBSReference, "CCBS: Invalid CCBS Reference" },
  1163. { FacError_CCBS_LongTermDenial, "CCBS: Long Term Denial" },
  1164. { FacError_CCBS_ShortTermDenial, "CCBS: Short Term Denial" },
  1165. { FacError_CCBS_IsAlreadyActivated, "CCBS: Is Already Activated" },
  1166. { FacError_CCBS_AlreadyAccepted, "CCBS: Already Accepted" },
  1167. { FacError_CCBS_OutgoingCCBSQueueFull, "CCBS: Outgoing CCBS Queue Full" },
  1168. { FacError_CCBS_CallFailureReasonNotBusy, "CCBS: Call Failure Reason Not Busy" },
  1169. { FacError_CCBS_NotReadyForCall, "CCBS: Not Ready For Call" },
  1170. { FacError_CCBS_T_LongTermDenial, "CCBS-T: Long Term Denial" },
  1171. { FacError_CCBS_T_ShortTermDenial, "CCBS-T: Short Term Denial" },
  1172. { FacError_ECT_LinkIdNotAssignedByNetwork, "ECT: Link ID Not Assigned By Network" },
  1173. /* *INDENT-ON* */
  1174. };
  1175. unsigned index;
  1176. for (index = 0; index < ARRAY_LEN(arr); ++index) {
  1177. if (arr[index].code == code) {
  1178. return arr[index].name;
  1179. }
  1180. }
  1181. return "unknown";
  1182. }
  1183. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  1184. #if defined(AST_MISDN_ENHANCEMENTS)
  1185. /*!
  1186. * \internal
  1187. * \brief Convert mISDN redirecting reason to diversion reason.
  1188. *
  1189. * \param reason mISDN redirecting reason code.
  1190. *
  1191. * \return Supported diversion reason code.
  1192. */
  1193. static unsigned misdn_to_diversion_reason(enum mISDN_REDIRECTING_REASON reason)
  1194. {
  1195. unsigned diversion_reason;
  1196. switch (reason) {
  1197. case mISDN_REDIRECTING_REASON_CALL_FWD:
  1198. diversion_reason = 1;/* cfu */
  1199. break;
  1200. case mISDN_REDIRECTING_REASON_CALL_FWD_BUSY:
  1201. diversion_reason = 2;/* cfb */
  1202. break;
  1203. case mISDN_REDIRECTING_REASON_NO_REPLY:
  1204. diversion_reason = 3;/* cfnr */
  1205. break;
  1206. default:
  1207. diversion_reason = 0;/* unknown */
  1208. break;
  1209. }
  1210. return diversion_reason;
  1211. }
  1212. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  1213. #if defined(AST_MISDN_ENHANCEMENTS)
  1214. /*!
  1215. * \internal
  1216. * \brief Convert diversion reason to mISDN redirecting reason
  1217. *
  1218. * \param diversion_reason Diversion reason to convert
  1219. *
  1220. * \return Supported redirecting reason code.
  1221. */
  1222. static enum mISDN_REDIRECTING_REASON diversion_reason_to_misdn(unsigned diversion_reason)
  1223. {
  1224. enum mISDN_REDIRECTING_REASON reason;
  1225. switch (diversion_reason) {
  1226. case 1:/* cfu */
  1227. reason = mISDN_REDIRECTING_REASON_CALL_FWD;
  1228. break;
  1229. case 2:/* cfb */
  1230. reason = mISDN_REDIRECTING_REASON_CALL_FWD_BUSY;
  1231. break;
  1232. case 3:/* cfnr */
  1233. reason = mISDN_REDIRECTING_REASON_NO_REPLY;
  1234. break;
  1235. default:
  1236. reason = mISDN_REDIRECTING_REASON_UNKNOWN;
  1237. break;
  1238. }
  1239. return reason;
  1240. }
  1241. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  1242. #if defined(AST_MISDN_ENHANCEMENTS)
  1243. /*!
  1244. * \internal
  1245. * \brief Convert the mISDN presentation to PresentedNumberUnscreened type
  1246. *
  1247. * \param presentation mISDN presentation to convert
  1248. * \param number_present TRUE if the number is present
  1249. *
  1250. * \return PresentedNumberUnscreened type
  1251. */
  1252. static unsigned misdn_to_PresentedNumberUnscreened_type(int presentation, int number_present)
  1253. {
  1254. unsigned type;
  1255. switch (presentation) {
  1256. case 0:/* allowed */
  1257. if (number_present) {
  1258. type = 0;/* presentationAllowedNumber */
  1259. } else {
  1260. type = 2;/* numberNotAvailableDueToInterworking */
  1261. }
  1262. break;
  1263. case 1:/* restricted */
  1264. if (number_present) {
  1265. type = 3;/* presentationRestrictedNumber */
  1266. } else {
  1267. type = 1;/* presentationRestricted */
  1268. }
  1269. break;
  1270. default:
  1271. type = 2;/* numberNotAvailableDueToInterworking */
  1272. break;
  1273. }
  1274. return type;
  1275. }
  1276. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  1277. #if defined(AST_MISDN_ENHANCEMENTS)
  1278. /*!
  1279. * \internal
  1280. * \brief Convert the PresentedNumberUnscreened type to mISDN presentation
  1281. *
  1282. * \param type PresentedNumberUnscreened type
  1283. *
  1284. * \return mISDN presentation
  1285. */
  1286. static int PresentedNumberUnscreened_to_misdn_pres(unsigned type)
  1287. {
  1288. int presentation;
  1289. switch (type) {
  1290. default:
  1291. case 0:/* presentationAllowedNumber */
  1292. presentation = 0;/* allowed */
  1293. break;
  1294. case 1:/* presentationRestricted */
  1295. case 3:/* presentationRestrictedNumber */
  1296. presentation = 1;/* restricted */
  1297. break;
  1298. case 2:/* numberNotAvailableDueToInterworking */
  1299. presentation = 2;/* unavailable */
  1300. break;
  1301. }
  1302. return presentation;
  1303. }
  1304. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  1305. #if defined(AST_MISDN_ENHANCEMENTS)
  1306. /*!
  1307. * \internal
  1308. * \brief Convert the mISDN numbering plan to PartyNumber numbering plan
  1309. *
  1310. * \param number_plan mISDN numbering plan
  1311. *
  1312. * \return PartyNumber numbering plan
  1313. */
  1314. static unsigned misdn_to_PartyNumber_plan(enum mISDN_NUMBER_PLAN number_plan)
  1315. {
  1316. unsigned party_plan;
  1317. switch (number_plan) {
  1318. default:
  1319. case NUMPLAN_UNKNOWN:
  1320. party_plan = 0;/* unknown */
  1321. break;
  1322. case NUMPLAN_ISDN:
  1323. party_plan = 1;/* public */
  1324. break;
  1325. case NUMPLAN_DATA:
  1326. party_plan = 3;/* data */
  1327. break;
  1328. case NUMPLAN_TELEX:
  1329. party_plan = 4;/* telex */
  1330. break;
  1331. case NUMPLAN_NATIONAL:
  1332. party_plan = 8;/* nationalStandard */
  1333. break;
  1334. case NUMPLAN_PRIVATE:
  1335. party_plan = 5;/* private */
  1336. break;
  1337. }
  1338. return party_plan;
  1339. }
  1340. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  1341. #if defined(AST_MISDN_ENHANCEMENTS)
  1342. /*!
  1343. * \internal
  1344. * \brief Convert PartyNumber numbering plan to mISDN numbering plan
  1345. *
  1346. * \param party_plan PartyNumber numbering plan
  1347. *
  1348. * \return mISDN numbering plan
  1349. */
  1350. static enum mISDN_NUMBER_PLAN PartyNumber_to_misdn_plan(unsigned party_plan)
  1351. {
  1352. enum mISDN_NUMBER_PLAN number_plan;
  1353. switch (party_plan) {
  1354. default:
  1355. case 0:/* unknown */
  1356. number_plan = NUMPLAN_UNKNOWN;
  1357. break;
  1358. case 1:/* public */
  1359. number_plan = NUMPLAN_ISDN;
  1360. break;
  1361. case 3:/* data */
  1362. number_plan = NUMPLAN_DATA;
  1363. break;
  1364. case 4:/* telex */
  1365. number_plan = NUMPLAN_TELEX;
  1366. break;
  1367. case 8:/* nationalStandard */
  1368. number_plan = NUMPLAN_NATIONAL;
  1369. break;
  1370. case 5:/* private */
  1371. number_plan = NUMPLAN_PRIVATE;
  1372. break;
  1373. }
  1374. return number_plan;
  1375. }
  1376. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  1377. #if defined(AST_MISDN_ENHANCEMENTS)
  1378. /*!
  1379. * \internal
  1380. * \brief Convert mISDN type-of-number to PartyNumber public type-of-number
  1381. *
  1382. * \param ton mISDN type-of-number
  1383. *
  1384. * \return PartyNumber public type-of-number
  1385. */
  1386. static unsigned misdn_to_PartyNumber_ton_public(enum mISDN_NUMBER_TYPE ton)
  1387. {
  1388. unsigned party_ton;
  1389. switch (ton) {
  1390. default:
  1391. case NUMTYPE_UNKNOWN:
  1392. party_ton = 0;/* unknown */
  1393. break;
  1394. case NUMTYPE_INTERNATIONAL:
  1395. party_ton = 1;/* internationalNumber */
  1396. break;
  1397. case NUMTYPE_NATIONAL:
  1398. party_ton = 2;/* nationalNumber */
  1399. break;
  1400. case NUMTYPE_NETWORK_SPECIFIC:
  1401. party_ton = 3;/* networkSpecificNumber */
  1402. break;
  1403. case NUMTYPE_SUBSCRIBER:
  1404. party_ton = 4;/* subscriberNumber */
  1405. break;
  1406. case NUMTYPE_ABBREVIATED:
  1407. party_ton = 6;/* abbreviatedNumber */
  1408. break;
  1409. }
  1410. return party_ton;
  1411. }
  1412. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  1413. #if defined(AST_MISDN_ENHANCEMENTS)
  1414. /*!
  1415. * \internal
  1416. * \brief Convert the PartyNumber public type-of-number to mISDN type-of-number
  1417. *
  1418. * \param party_ton PartyNumber public type-of-number
  1419. *
  1420. * \return mISDN type-of-number
  1421. */
  1422. static enum mISDN_NUMBER_TYPE PartyNumber_to_misdn_ton_public(unsigned party_ton)
  1423. {
  1424. enum mISDN_NUMBER_TYPE ton;
  1425. switch (party_ton) {
  1426. default:
  1427. case 0:/* unknown */
  1428. ton = NUMTYPE_UNKNOWN;
  1429. break;
  1430. case 1:/* internationalNumber */
  1431. ton = NUMTYPE_INTERNATIONAL;
  1432. break;
  1433. case 2:/* nationalNumber */
  1434. ton = NUMTYPE_NATIONAL;
  1435. break;
  1436. case 3:/* networkSpecificNumber */
  1437. ton = NUMTYPE_NETWORK_SPECIFIC;
  1438. break;
  1439. case 4:/* subscriberNumber */
  1440. ton = NUMTYPE_SUBSCRIBER;
  1441. break;
  1442. case 6:/* abbreviatedNumber */
  1443. ton = NUMTYPE_ABBREVIATED;
  1444. break;
  1445. }
  1446. return ton;
  1447. }
  1448. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  1449. #if defined(AST_MISDN_ENHANCEMENTS)
  1450. /*!
  1451. * \internal
  1452. * \brief Convert mISDN type-of-number to PartyNumber private type-of-number
  1453. *
  1454. * \param ton mISDN type-of-number
  1455. *
  1456. * \return PartyNumber private type-of-number
  1457. */
  1458. static unsigned misdn_to_PartyNumber_ton_private(enum mISDN_NUMBER_TYPE ton)
  1459. {
  1460. unsigned party_ton;
  1461. switch (ton) {
  1462. default:
  1463. case NUMTYPE_UNKNOWN:
  1464. party_ton = 0;/* unknown */
  1465. break;
  1466. case NUMTYPE_INTERNATIONAL:
  1467. party_ton = 1;/* level2RegionalNumber */
  1468. break;
  1469. case NUMTYPE_NATIONAL:
  1470. party_ton = 2;/* level1RegionalNumber */
  1471. break;
  1472. case NUMTYPE_NETWORK_SPECIFIC:
  1473. party_ton = 3;/* pTNSpecificNumber */
  1474. break;
  1475. case NUMTYPE_SUBSCRIBER:
  1476. party_ton = 4;/* localNumber */
  1477. break;
  1478. case NUMTYPE_ABBREVIATED:
  1479. party_ton = 6;/* abbreviatedNumber */
  1480. break;
  1481. }
  1482. return party_ton;
  1483. }
  1484. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  1485. #if defined(AST_MISDN_ENHANCEMENTS)
  1486. /*!
  1487. * \internal
  1488. * \brief Convert the PartyNumber private type-of-number to mISDN type-of-number
  1489. *
  1490. * \param party_ton PartyNumber private type-of-number
  1491. *
  1492. * \return mISDN type-of-number
  1493. */
  1494. static enum mISDN_NUMBER_TYPE PartyNumber_to_misdn_ton_private(unsigned party_ton)
  1495. {
  1496. enum mISDN_NUMBER_TYPE ton;
  1497. switch (party_ton) {
  1498. default:
  1499. case 0:/* unknown */
  1500. ton = NUMTYPE_UNKNOWN;
  1501. break;
  1502. case 1:/* level2RegionalNumber */
  1503. ton = NUMTYPE_INTERNATIONAL;
  1504. break;
  1505. case 2:/* level1RegionalNumber */
  1506. ton = NUMTYPE_NATIONAL;
  1507. break;
  1508. case 3:/* pTNSpecificNumber */
  1509. ton = NUMTYPE_NETWORK_SPECIFIC;
  1510. break;
  1511. case 4:/* localNumber */
  1512. ton = NUMTYPE_SUBSCRIBER;
  1513. break;
  1514. case 6:/* abbreviatedNumber */
  1515. ton = NUMTYPE_ABBREVIATED;
  1516. break;
  1517. }
  1518. return ton;
  1519. }
  1520. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  1521. /*!
  1522. * \internal
  1523. * \brief Convert the mISDN type of number code to a string
  1524. *
  1525. * \param number_type mISDN type of number code.
  1526. *
  1527. * \return The mISDN type of number code as a string
  1528. */
  1529. static const char *misdn_to_str_ton(enum mISDN_NUMBER_TYPE number_type)
  1530. {
  1531. const char *str;
  1532. switch (number_type) {
  1533. default:
  1534. case NUMTYPE_UNKNOWN:
  1535. str = "Unknown";
  1536. break;
  1537. case NUMTYPE_INTERNATIONAL:
  1538. str = "International";
  1539. break;
  1540. case NUMTYPE_NATIONAL:
  1541. str = "National";
  1542. break;
  1543. case NUMTYPE_NETWORK_SPECIFIC:
  1544. str = "Network Specific";
  1545. break;
  1546. case NUMTYPE_SUBSCRIBER:
  1547. str = "Subscriber";
  1548. break;
  1549. case NUMTYPE_ABBREVIATED:
  1550. str = "Abbreviated";
  1551. break;
  1552. }
  1553. return str;
  1554. }
  1555. /*!
  1556. * \internal
  1557. * \brief Convert the mISDN type of number code to Asterisk type of number code
  1558. *
  1559. * \param number_type mISDN type of number code.
  1560. *
  1561. * \return Asterisk type of number code
  1562. */
  1563. static int misdn_to_ast_ton(enum mISDN_NUMBER_TYPE number_type)
  1564. {
  1565. int ast_number_type;
  1566. switch (number_type) {
  1567. default:
  1568. case NUMTYPE_UNKNOWN:
  1569. ast_number_type = NUMTYPE_UNKNOWN << 4;
  1570. break;
  1571. case NUMTYPE_INTERNATIONAL:
  1572. ast_number_type = NUMTYPE_INTERNATIONAL << 4;
  1573. break;
  1574. case NUMTYPE_NATIONAL:
  1575. ast_number_type = NUMTYPE_NATIONAL << 4;
  1576. break;
  1577. case NUMTYPE_NETWORK_SPECIFIC:
  1578. ast_number_type = NUMTYPE_NETWORK_SPECIFIC << 4;
  1579. break;
  1580. case NUMTYPE_SUBSCRIBER:
  1581. ast_number_type = NUMTYPE_SUBSCRIBER << 4;
  1582. break;
  1583. case NUMTYPE_ABBREVIATED:
  1584. ast_number_type = NUMTYPE_ABBREVIATED << 4;
  1585. break;
  1586. }
  1587. return ast_number_type;
  1588. }
  1589. /*!
  1590. * \internal
  1591. * \brief Convert the Asterisk type of number code to mISDN type of number code
  1592. *
  1593. * \param ast_number_type Asterisk type of number code.
  1594. *
  1595. * \return mISDN type of number code
  1596. */
  1597. static enum mISDN_NUMBER_TYPE ast_to_misdn_ton(unsigned ast_number_type)
  1598. {
  1599. enum mISDN_NUMBER_TYPE number_type;
  1600. switch ((ast_number_type >> 4) & 0x07) {
  1601. default:
  1602. case NUMTYPE_UNKNOWN:
  1603. number_type = NUMTYPE_UNKNOWN;
  1604. break;
  1605. case NUMTYPE_INTERNATIONAL:
  1606. number_type = NUMTYPE_INTERNATIONAL;
  1607. break;
  1608. case NUMTYPE_NATIONAL:
  1609. number_type = NUMTYPE_NATIONAL;
  1610. break;
  1611. case NUMTYPE_NETWORK_SPECIFIC:
  1612. number_type = NUMTYPE_NETWORK_SPECIFIC;
  1613. break;
  1614. case NUMTYPE_SUBSCRIBER:
  1615. number_type = NUMTYPE_SUBSCRIBER;
  1616. break;
  1617. case NUMTYPE_ABBREVIATED:
  1618. number_type = NUMTYPE_ABBREVIATED;
  1619. break;
  1620. }
  1621. return number_type;
  1622. }
  1623. /*!
  1624. * \internal
  1625. * \brief Convert the mISDN numbering plan code to a string
  1626. *
  1627. * \param number_plan mISDN numbering plan code.
  1628. *
  1629. * \return The mISDN numbering plan code as a string
  1630. */
  1631. static const char *misdn_to_str_plan(enum mISDN_NUMBER_PLAN number_plan)
  1632. {
  1633. const char *str;
  1634. switch (number_plan) {
  1635. default:
  1636. case NUMPLAN_UNKNOWN:
  1637. str = "Unknown";
  1638. break;
  1639. case NUMPLAN_ISDN:
  1640. str = "ISDN";
  1641. break;
  1642. case NUMPLAN_DATA:
  1643. str = "Data";
  1644. break;
  1645. case NUMPLAN_TELEX:
  1646. str = "Telex";
  1647. break;
  1648. case NUMPLAN_NATIONAL:
  1649. str = "National";
  1650. break;
  1651. case NUMPLAN_PRIVATE:
  1652. str = "Private";
  1653. break;
  1654. }
  1655. return str;
  1656. }
  1657. /*!
  1658. * \internal
  1659. * \brief Convert the mISDN numbering plan code to Asterisk numbering plan code
  1660. *
  1661. * \param number_plan mISDN numbering plan code.
  1662. *
  1663. * \return Asterisk numbering plan code
  1664. */
  1665. static int misdn_to_ast_plan(enum mISDN_NUMBER_PLAN number_plan)
  1666. {
  1667. int ast_number_plan;
  1668. switch (number_plan) {
  1669. default:
  1670. case NUMPLAN_UNKNOWN:
  1671. ast_number_plan = NUMPLAN_UNKNOWN;
  1672. break;
  1673. case NUMPLAN_ISDN:
  1674. ast_number_plan = NUMPLAN_ISDN;
  1675. break;
  1676. case NUMPLAN_DATA:
  1677. ast_number_plan = NUMPLAN_DATA;
  1678. break;
  1679. case NUMPLAN_TELEX:
  1680. ast_number_plan = NUMPLAN_TELEX;
  1681. break;
  1682. case NUMPLAN_NATIONAL:
  1683. ast_number_plan = NUMPLAN_NATIONAL;
  1684. break;
  1685. case NUMPLAN_PRIVATE:
  1686. ast_number_plan = NUMPLAN_PRIVATE;
  1687. break;
  1688. }
  1689. return ast_number_plan;
  1690. }
  1691. /*!
  1692. * \internal
  1693. * \brief Convert the Asterisk numbering plan code to mISDN numbering plan code
  1694. *
  1695. * \param ast_number_plan Asterisk numbering plan code.
  1696. *
  1697. * \return mISDN numbering plan code
  1698. */
  1699. static enum mISDN_NUMBER_PLAN ast_to_misdn_plan(unsigned ast_number_plan)
  1700. {
  1701. enum mISDN_NUMBER_PLAN number_plan;
  1702. switch (ast_number_plan & 0x0F) {
  1703. default:
  1704. case NUMPLAN_UNKNOWN:
  1705. number_plan = NUMPLAN_UNKNOWN;
  1706. break;
  1707. case NUMPLAN_ISDN:
  1708. number_plan = NUMPLAN_ISDN;
  1709. break;
  1710. case NUMPLAN_DATA:
  1711. number_plan = NUMPLAN_DATA;
  1712. break;
  1713. case NUMPLAN_TELEX:
  1714. number_plan = NUMPLAN_TELEX;
  1715. break;
  1716. case NUMPLAN_NATIONAL:
  1717. number_plan = NUMPLAN_NATIONAL;
  1718. break;
  1719. case NUMPLAN_PRIVATE:
  1720. number_plan = NUMPLAN_PRIVATE;
  1721. break;
  1722. }
  1723. return number_plan;
  1724. }
  1725. /*!
  1726. * \internal
  1727. * \brief Convert the mISDN presentation code to a string
  1728. *
  1729. * \param presentation mISDN number presentation restriction code.
  1730. *
  1731. * \return The mISDN presentation code as a string
  1732. */
  1733. static const char *misdn_to_str_pres(int presentation)
  1734. {
  1735. const char *str;
  1736. switch (presentation) {
  1737. case 0:
  1738. str = "Allowed";
  1739. break;
  1740. case 1:
  1741. str = "Restricted";
  1742. break;
  1743. case 2:
  1744. str = "Unavailable";
  1745. break;
  1746. default:
  1747. str = "Unknown";
  1748. break;
  1749. }
  1750. return str;
  1751. }
  1752. /*!
  1753. * \internal
  1754. * \brief Convert the mISDN presentation code to Asterisk presentation code
  1755. *
  1756. * \param presentation mISDN number presentation restriction code.
  1757. *
  1758. * \return Asterisk presentation code
  1759. */
  1760. static int misdn_to_ast_pres(int presentation)
  1761. {
  1762. switch (presentation) {
  1763. default:
  1764. case 0:
  1765. presentation = AST_PRES_ALLOWED;
  1766. break;
  1767. case 1:
  1768. presentation = AST_PRES_RESTRICTED;
  1769. break;
  1770. case 2:
  1771. presentation = AST_PRES_UNAVAILABLE;
  1772. break;
  1773. }
  1774. return presentation;
  1775. }
  1776. /*!
  1777. * \internal
  1778. * \brief Convert the Asterisk presentation code to mISDN presentation code
  1779. *
  1780. * \param presentation Asterisk number presentation restriction code.
  1781. *
  1782. * \return mISDN presentation code
  1783. */
  1784. static int ast_to_misdn_pres(int presentation)
  1785. {
  1786. switch (presentation & AST_PRES_RESTRICTION) {
  1787. default:
  1788. case AST_PRES_ALLOWED:
  1789. presentation = 0;
  1790. break;
  1791. case AST_PRES_RESTRICTED:
  1792. presentation = 1;
  1793. break;
  1794. case AST_PRES_UNAVAILABLE:
  1795. presentation = 2;
  1796. break;
  1797. }
  1798. return presentation;
  1799. }
  1800. /*!
  1801. * \internal
  1802. * \brief Convert the mISDN screening code to a string
  1803. *
  1804. * \param screening mISDN number screening code.
  1805. *
  1806. * \return The mISDN screening code as a string
  1807. */
  1808. static const char *misdn_to_str_screen(int screening)
  1809. {
  1810. const char *str;
  1811. switch (screening) {
  1812. case 0:
  1813. str = "Unscreened";
  1814. break;
  1815. case 1:
  1816. str = "Passed Screen";
  1817. break;
  1818. case 2:
  1819. str = "Failed Screen";
  1820. break;
  1821. case 3:
  1822. str = "Network Number";
  1823. break;
  1824. default:
  1825. str = "Unknown";
  1826. break;
  1827. }
  1828. return str;
  1829. }
  1830. /*!
  1831. * \internal
  1832. * \brief Convert the mISDN screening code to Asterisk screening code
  1833. *
  1834. * \param screening mISDN number screening code.
  1835. *
  1836. * \return Asterisk screening code
  1837. */
  1838. static int misdn_to_ast_screen(int screening)
  1839. {
  1840. switch (screening) {
  1841. default:
  1842. case 0:
  1843. screening = AST_PRES_USER_NUMBER_UNSCREENED;
  1844. break;
  1845. case 1:
  1846. screening = AST_PRES_USER_NUMBER_PASSED_SCREEN;
  1847. break;
  1848. case 2:
  1849. screening = AST_PRES_USER_NUMBER_FAILED_SCREEN;
  1850. break;
  1851. case 3:
  1852. screening = AST_PRES_NETWORK_NUMBER;
  1853. break;
  1854. }
  1855. return screening;
  1856. }
  1857. /*!
  1858. * \internal
  1859. * \brief Convert the Asterisk screening code to mISDN screening code
  1860. *
  1861. * \param screening Asterisk number screening code.
  1862. *
  1863. * \return mISDN screening code
  1864. */
  1865. static int ast_to_misdn_screen(int screening)
  1866. {
  1867. switch (screening & AST_PRES_NUMBER_TYPE) {
  1868. default:
  1869. case AST_PRES_USER_NUMBER_UNSCREENED:
  1870. screening = 0;
  1871. break;
  1872. case AST_PRES_USER_NUMBER_PASSED_SCREEN:
  1873. screening = 1;
  1874. break;
  1875. case AST_PRES_USER_NUMBER_FAILED_SCREEN:
  1876. screening = 2;
  1877. break;
  1878. case AST_PRES_NETWORK_NUMBER:
  1879. screening = 3;
  1880. break;
  1881. }
  1882. return screening;
  1883. }
  1884. /*!
  1885. * \internal
  1886. * \brief Convert Asterisk redirecting reason to mISDN redirecting reason code.
  1887. *
  1888. * \param ast Asterisk redirecting reason code.
  1889. *
  1890. * \return mISDN reason code
  1891. */
  1892. static enum mISDN_REDIRECTING_REASON ast_to_misdn_reason(const enum AST_REDIRECTING_REASON ast)
  1893. {
  1894. unsigned index;
  1895. static const struct misdn_reasons {
  1896. enum AST_REDIRECTING_REASON ast;
  1897. enum mISDN_REDIRECTING_REASON q931;
  1898. } misdn_reason_table[] = {
  1899. /* *INDENT-OFF* */
  1900. { AST_REDIRECTING_REASON_UNKNOWN, mISDN_REDIRECTING_REASON_UNKNOWN },
  1901. { AST_REDIRECTING_REASON_USER_BUSY, mISDN_REDIRECTING_REASON_CALL_FWD_BUSY },
  1902. { AST_REDIRECTING_REASON_NO_ANSWER, mISDN_REDIRECTING_REASON_NO_REPLY },
  1903. { AST_REDIRECTING_REASON_UNAVAILABLE, mISDN_REDIRECTING_REASON_NO_REPLY },
  1904. { AST_REDIRECTING_REASON_UNCONDITIONAL, mISDN_REDIRECTING_REASON_CALL_FWD },
  1905. { AST_REDIRECTING_REASON_TIME_OF_DAY, mISDN_REDIRECTING_REASON_UNKNOWN },
  1906. { AST_REDIRECTING_REASON_DO_NOT_DISTURB, mISDN_REDIRECTING_REASON_UNKNOWN },
  1907. { AST_REDIRECTING_REASON_DEFLECTION, mISDN_REDIRECTING_REASON_DEFLECTION },
  1908. { AST_REDIRECTING_REASON_FOLLOW_ME, mISDN_REDIRECTING_REASON_UNKNOWN },
  1909. { AST_REDIRECTING_REASON_OUT_OF_ORDER, mISDN_REDIRECTING_REASON_OUT_OF_ORDER },
  1910. { AST_REDIRECTING_REASON_AWAY, mISDN_REDIRECTING_REASON_UNKNOWN },
  1911. { AST_REDIRECTING_REASON_CALL_FWD_DTE, mISDN_REDIRECTING_REASON_CALL_FWD_DTE }
  1912. /* *INDENT-ON* */
  1913. };
  1914. for (index = 0; index < ARRAY_LEN(misdn_reason_table); ++index) {
  1915. if (misdn_reason_table[index].ast == ast) {
  1916. return misdn_reason_table[index].q931;
  1917. }
  1918. }
  1919. return mISDN_REDIRECTING_REASON_UNKNOWN;
  1920. }
  1921. /*!
  1922. * \internal
  1923. * \brief Convert the mISDN redirecting reason to Asterisk redirecting reason code
  1924. *
  1925. * \param q931 mISDN redirecting reason code.
  1926. *
  1927. * \return Asterisk redirecting reason code
  1928. */
  1929. static enum AST_REDIRECTING_REASON misdn_to_ast_reason(const enum mISDN_REDIRECTING_REASON q931)
  1930. {
  1931. enum AST_REDIRECTING_REASON ast;
  1932. switch (q931) {
  1933. default:
  1934. case mISDN_REDIRECTING_REASON_UNKNOWN:
  1935. ast = AST_REDIRECTING_REASON_UNKNOWN;
  1936. break;
  1937. case mISDN_REDIRECTING_REASON_CALL_FWD_BUSY:
  1938. ast = AST_REDIRECTING_REASON_USER_BUSY;
  1939. break;
  1940. case mISDN_REDIRECTING_REASON_NO_REPLY:
  1941. ast = AST_REDIRECTING_REASON_NO_ANSWER;
  1942. break;
  1943. case mISDN_REDIRECTING_REASON_DEFLECTION:
  1944. ast = AST_REDIRECTING_REASON_DEFLECTION;
  1945. break;
  1946. case mISDN_REDIRECTING_REASON_OUT_OF_ORDER:
  1947. ast = AST_REDIRECTING_REASON_OUT_OF_ORDER;
  1948. break;
  1949. case mISDN_REDIRECTING_REASON_CALL_FWD_DTE:
  1950. ast = AST_REDIRECTING_REASON_CALL_FWD_DTE;
  1951. break;
  1952. case mISDN_REDIRECTING_REASON_CALL_FWD:
  1953. ast = AST_REDIRECTING_REASON_UNCONDITIONAL;
  1954. break;
  1955. }
  1956. return ast;
  1957. }
  1958. struct allowed_bearers {
  1959. char *name; /*!< Bearer capability name string used in /etc/misdn.conf allowed_bearers */
  1960. char *display; /*!< Bearer capability displayable name */
  1961. int cap; /*!< SETUP message bearer capability field code value */
  1962. int deprecated; /*!< TRUE if this entry is deprecated. (Misspelled or bad name to use) */
  1963. };
  1964. /* *INDENT-OFF* */
  1965. static const struct allowed_bearers allowed_bearers_array[] = {
  1966. /* Name, Displayable Name Bearer Capability, Deprecated */
  1967. { "speech", "Speech", INFO_CAPABILITY_SPEECH, 0 },
  1968. { "3_1khz", "3.1KHz Audio", INFO_CAPABILITY_AUDIO_3_1K, 0 },
  1969. { "digital_unrestricted", "Unrestricted Digital", INFO_CAPABILITY_DIGITAL_UNRESTRICTED, 0 },
  1970. { "digital_restricted", "Restricted Digital", INFO_CAPABILITY_DIGITAL_RESTRICTED, 0 },
  1971. { "digital_restriced", "Restricted Digital", INFO_CAPABILITY_DIGITAL_RESTRICTED, 1 }, /* Allow misspelling for backwards compatibility */
  1972. { "video", "Video", INFO_CAPABILITY_VIDEO, 0 }
  1973. };
  1974. /* *INDENT-ON* */
  1975. static const char *bearer2str(int cap)
  1976. {
  1977. unsigned index;
  1978. for (index = 0; index < ARRAY_LEN(allowed_bearers_array); ++index) {
  1979. if (allowed_bearers_array[index].cap == cap) {
  1980. return allowed_bearers_array[index].display;
  1981. }
  1982. }
  1983. return "Unknown Bearer";
  1984. }
  1985. #if defined(AST_MISDN_ENHANCEMENTS)
  1986. /*!
  1987. * \internal
  1988. * \brief Fill in facility PartyNumber information
  1989. *
  1990. * \param party PartyNumber structure to fill in.
  1991. * \param id Information to put in PartyNumber structure.
  1992. *
  1993. * \return Nothing
  1994. */
  1995. static void misdn_PartyNumber_fill(struct FacPartyNumber *party, const struct misdn_party_id *id)
  1996. {
  1997. ast_copy_string((char *) party->Number, id->number, sizeof(party->Number));
  1998. party->LengthOfNumber = strlen((char *) party->Number);
  1999. party->Type = misdn_to_PartyNumber_plan(id->number_plan);
  2000. switch (party->Type) {
  2001. case 1:/* public */
  2002. party->TypeOfNumber = misdn_to_PartyNumber_ton_public(id->number_type);
  2003. break;
  2004. case 5:/* private */
  2005. party->TypeOfNumber = misdn_to_PartyNumber_ton_private(id->number_type);
  2006. break;
  2007. default:
  2008. party->TypeOfNumber = 0;/* Don't care */
  2009. break;
  2010. }
  2011. }
  2012. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2013. #if defined(AST_MISDN_ENHANCEMENTS)
  2014. /*!
  2015. * \internal
  2016. * \brief Extract the information from PartyNumber
  2017. *
  2018. * \param id Where to put extracted PartyNumber information
  2019. * \param party PartyNumber information to extract
  2020. *
  2021. * \return Nothing
  2022. */
  2023. static void misdn_PartyNumber_extract(struct misdn_party_id *id, const struct FacPartyNumber *party)
  2024. {
  2025. if (party->LengthOfNumber) {
  2026. ast_copy_string(id->number, (char *) party->Number, sizeof(id->number));
  2027. id->number_plan = PartyNumber_to_misdn_plan(party->Type);
  2028. switch (party->Type) {
  2029. case 1:/* public */
  2030. id->number_type = PartyNumber_to_misdn_ton_public(party->TypeOfNumber);
  2031. break;
  2032. case 5:/* private */
  2033. id->number_type = PartyNumber_to_misdn_ton_private(party->TypeOfNumber);
  2034. break;
  2035. default:
  2036. id->number_type = NUMTYPE_UNKNOWN;
  2037. break;
  2038. }
  2039. } else {
  2040. /* Number not present */
  2041. id->number_type = NUMTYPE_UNKNOWN;
  2042. id->number_plan = NUMPLAN_ISDN;
  2043. id->number[0] = 0;
  2044. }
  2045. }
  2046. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2047. #if defined(AST_MISDN_ENHANCEMENTS)
  2048. /*!
  2049. * \internal
  2050. * \brief Fill in facility Address information
  2051. *
  2052. * \param Address Address structure to fill in.
  2053. * \param id Information to put in Address structure.
  2054. *
  2055. * \return Nothing
  2056. */
  2057. static void misdn_Address_fill(struct FacAddress *Address, const struct misdn_party_id *id)
  2058. {
  2059. misdn_PartyNumber_fill(&Address->Party, id);
  2060. /* Subaddresses are not supported yet */
  2061. Address->Subaddress.Length = 0;
  2062. }
  2063. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2064. #if defined(AST_MISDN_ENHANCEMENTS)
  2065. /*!
  2066. * \internal
  2067. * \brief Fill in facility PresentedNumberUnscreened information
  2068. *
  2069. * \param presented PresentedNumberUnscreened structure to fill in.
  2070. * \param id Information to put in PresentedNumberUnscreened structure.
  2071. *
  2072. * \return Nothing
  2073. */
  2074. static void misdn_PresentedNumberUnscreened_fill(struct FacPresentedNumberUnscreened *presented, const struct misdn_party_id *id)
  2075. {
  2076. presented->Type = misdn_to_PresentedNumberUnscreened_type(id->presentation, id->number[0] ? 1 : 0);
  2077. misdn_PartyNumber_fill(&presented->Unscreened, id);
  2078. }
  2079. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2080. #if defined(AST_MISDN_ENHANCEMENTS)
  2081. /*!
  2082. * \internal
  2083. * \brief Extract the information from PartyNumber
  2084. *
  2085. * \param id Where to put extracted PresentedNumberUnscreened information
  2086. * \param presented PresentedNumberUnscreened information to extract
  2087. *
  2088. * \return Nothing
  2089. */
  2090. static void misdn_PresentedNumberUnscreened_extract(struct misdn_party_id *id, const struct FacPresentedNumberUnscreened *presented)
  2091. {
  2092. id->presentation = PresentedNumberUnscreened_to_misdn_pres(presented->Type);
  2093. id->screening = 0;/* unscreened */
  2094. switch (presented->Type) {
  2095. case 0:/* presentationAllowedNumber */
  2096. case 3:/* presentationRestrictedNumber */
  2097. misdn_PartyNumber_extract(id, &presented->Unscreened);
  2098. break;
  2099. case 1:/* presentationRestricted */
  2100. case 2:/* numberNotAvailableDueToInterworking */
  2101. default:
  2102. /* Number not present (And uninitialized so do not even look at it!) */
  2103. id->number_type = NUMTYPE_UNKNOWN;
  2104. id->number_plan = NUMPLAN_ISDN;
  2105. id->number[0] = 0;
  2106. break;
  2107. }
  2108. }
  2109. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2110. #if defined(AST_MISDN_ENHANCEMENTS)
  2111. static const char Level_Spacing[] = " ";/* Work for up to 10 levels */
  2112. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2113. #if defined(AST_MISDN_ENHANCEMENTS)
  2114. static void print_facility_PartyNumber(unsigned Level, const struct FacPartyNumber *Party, const struct misdn_bchannel *bc)
  2115. {
  2116. if (Party->LengthOfNumber) {
  2117. const char *Spacing;
  2118. Spacing = &Level_Spacing[sizeof(Level_Spacing) - 1 - Level];
  2119. chan_misdn_log(1, bc->port, " -->%s PartyNumber: Type:%d\n",
  2120. Spacing, Party->Type);
  2121. switch (Party->Type) {
  2122. case 0: /* Unknown PartyNumber */
  2123. chan_misdn_log(1, bc->port, " -->%s Unknown: %s\n",
  2124. Spacing, Party->Number);
  2125. break;
  2126. case 1: /* Public PartyNumber */
  2127. chan_misdn_log(1, bc->port, " -->%s Public TON:%d %s\n",
  2128. Spacing, Party->TypeOfNumber, Party->Number);
  2129. break;
  2130. case 2: /* NSAP encoded PartyNumber */
  2131. chan_misdn_log(1, bc->port, " -->%s NSAP: %s\n",
  2132. Spacing, Party->Number);
  2133. break;
  2134. case 3: /* Data PartyNumber (Not used) */
  2135. chan_misdn_log(1, bc->port, " -->%s Data: %s\n",
  2136. Spacing, Party->Number);
  2137. break;
  2138. case 4: /* Telex PartyNumber (Not used) */
  2139. chan_misdn_log(1, bc->port, " -->%s Telex: %s\n",
  2140. Spacing, Party->Number);
  2141. break;
  2142. case 5: /* Private PartyNumber */
  2143. chan_misdn_log(1, bc->port, " -->%s Private TON:%d %s\n",
  2144. Spacing, Party->TypeOfNumber, Party->Number);
  2145. break;
  2146. case 8: /* National Standard PartyNumber (Not used) */
  2147. chan_misdn_log(1, bc->port, " -->%s National: %s\n",
  2148. Spacing, Party->Number);
  2149. break;
  2150. default:
  2151. break;
  2152. }
  2153. }
  2154. }
  2155. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2156. #if defined(AST_MISDN_ENHANCEMENTS)
  2157. static void print_facility_Subaddress(unsigned Level, const struct FacPartySubaddress *Subaddress, const struct misdn_bchannel *bc)
  2158. {
  2159. if (Subaddress->Length) {
  2160. const char *Spacing;
  2161. Spacing = &Level_Spacing[sizeof(Level_Spacing) - 1 - Level];
  2162. chan_misdn_log(1, bc->port, " -->%s Subaddress: Type:%d\n",
  2163. Spacing, Subaddress->Type);
  2164. switch (Subaddress->Type) {
  2165. case 0: /* UserSpecified */
  2166. if (Subaddress->u.UserSpecified.OddCountPresent) {
  2167. chan_misdn_log(1, bc->port, " -->%s User BCD OddCount:%d NumOctets:%d\n",
  2168. Spacing, Subaddress->u.UserSpecified.OddCount, Subaddress->Length);
  2169. } else {
  2170. chan_misdn_log(1, bc->port, " -->%s User: %s\n",
  2171. Spacing, Subaddress->u.UserSpecified.Information);
  2172. }
  2173. break;
  2174. case 1: /* NSAP */
  2175. chan_misdn_log(1, bc->port, " -->%s NSAP: %s\n",
  2176. Spacing, Subaddress->u.Nsap);
  2177. break;
  2178. default:
  2179. break;
  2180. }
  2181. }
  2182. }
  2183. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2184. #if defined(AST_MISDN_ENHANCEMENTS)
  2185. static void print_facility_Address(unsigned Level, const struct FacAddress *Address, const struct misdn_bchannel *bc)
  2186. {
  2187. print_facility_PartyNumber(Level, &Address->Party, bc);
  2188. print_facility_Subaddress(Level, &Address->Subaddress, bc);
  2189. }
  2190. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2191. #if defined(AST_MISDN_ENHANCEMENTS)
  2192. static void print_facility_PresentedNumberUnscreened(unsigned Level, const struct FacPresentedNumberUnscreened *Presented, const struct misdn_bchannel *bc)
  2193. {
  2194. const char *Spacing;
  2195. Spacing = &Level_Spacing[sizeof(Level_Spacing) - 1 - Level];
  2196. chan_misdn_log(1, bc->port, " -->%s Unscreened Type:%d\n", Spacing, Presented->Type);
  2197. switch (Presented->Type) {
  2198. case 0: /* presentationAllowedNumber */
  2199. chan_misdn_log(1, bc->port, " -->%s Allowed:\n", Spacing);
  2200. print_facility_PartyNumber(Level + 2, &Presented->Unscreened, bc);
  2201. break;
  2202. case 1: /* presentationRestricted */
  2203. chan_misdn_log(1, bc->port, " -->%s Restricted\n", Spacing);
  2204. break;
  2205. case 2: /* numberNotAvailableDueToInterworking */
  2206. chan_misdn_log(1, bc->port, " -->%s Not Available\n", Spacing);
  2207. break;
  2208. case 3: /* presentationRestrictedNumber */
  2209. chan_misdn_log(1, bc->port, " -->%s Restricted:\n", Spacing);
  2210. print_facility_PartyNumber(Level + 2, &Presented->Unscreened, bc);
  2211. break;
  2212. default:
  2213. break;
  2214. }
  2215. }
  2216. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2217. #if defined(AST_MISDN_ENHANCEMENTS)
  2218. static void print_facility_AddressScreened(unsigned Level, const struct FacAddressScreened *Address, const struct misdn_bchannel *bc)
  2219. {
  2220. const char *Spacing;
  2221. Spacing = &Level_Spacing[sizeof(Level_Spacing) - 1 - Level];
  2222. chan_misdn_log(1, bc->port, " -->%s ScreeningIndicator:%d\n", Spacing, Address->ScreeningIndicator);
  2223. print_facility_PartyNumber(Level, &Address->Party, bc);
  2224. print_facility_Subaddress(Level, &Address->Subaddress, bc);
  2225. }
  2226. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2227. #if defined(AST_MISDN_ENHANCEMENTS)
  2228. static void print_facility_PresentedAddressScreened(unsigned Level, const struct FacPresentedAddressScreened *Presented, const struct misdn_bchannel *bc)
  2229. {
  2230. const char *Spacing;
  2231. Spacing = &Level_Spacing[sizeof(Level_Spacing) - 1 - Level];
  2232. chan_misdn_log(1, bc->port, " -->%s Screened Type:%d\n", Spacing, Presented->Type);
  2233. switch (Presented->Type) {
  2234. case 0: /* presentationAllowedAddress */
  2235. chan_misdn_log(1, bc->port, " -->%s Allowed:\n", Spacing);
  2236. print_facility_AddressScreened(Level + 2, &Presented->Address, bc);
  2237. break;
  2238. case 1: /* presentationRestricted */
  2239. chan_misdn_log(1, bc->port, " -->%s Restricted\n", Spacing);
  2240. break;
  2241. case 2: /* numberNotAvailableDueToInterworking */
  2242. chan_misdn_log(1, bc->port, " -->%s Not Available\n", Spacing);
  2243. break;
  2244. case 3: /* presentationRestrictedAddress */
  2245. chan_misdn_log(1, bc->port, " -->%s Restricted:\n", Spacing);
  2246. print_facility_AddressScreened(Level + 2, &Presented->Address, bc);
  2247. break;
  2248. default:
  2249. break;
  2250. }
  2251. }
  2252. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2253. #if defined(AST_MISDN_ENHANCEMENTS)
  2254. static void print_facility_Q931_Bc_Hlc_Llc(unsigned Level, const struct Q931_Bc_Hlc_Llc *Q931ie, const struct misdn_bchannel *bc)
  2255. {
  2256. const char *Spacing;
  2257. Spacing = &Level_Spacing[sizeof(Level_Spacing) - 1 - Level];
  2258. chan_misdn_log(1, bc->port, " -->%s Q931ie:\n", Spacing);
  2259. if (Q931ie->Bc.Length) {
  2260. chan_misdn_log(1, bc->port, " -->%s Bc Len:%d\n", Spacing, Q931ie->Bc.Length);
  2261. }
  2262. if (Q931ie->Hlc.Length) {
  2263. chan_misdn_log(1, bc->port, " -->%s Hlc Len:%d\n", Spacing, Q931ie->Hlc.Length);
  2264. }
  2265. if (Q931ie->Llc.Length) {
  2266. chan_misdn_log(1, bc->port, " -->%s Llc Len:%d\n", Spacing, Q931ie->Llc.Length);
  2267. }
  2268. }
  2269. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2270. #if defined(AST_MISDN_ENHANCEMENTS)
  2271. static void print_facility_Q931_Bc_Hlc_Llc_Uu(unsigned Level, const struct Q931_Bc_Hlc_Llc_Uu *Q931ie, const struct misdn_bchannel *bc)
  2272. {
  2273. const char *Spacing;
  2274. Spacing = &Level_Spacing[sizeof(Level_Spacing) - 1 - Level];
  2275. chan_misdn_log(1, bc->port, " -->%s Q931ie:\n", Spacing);
  2276. if (Q931ie->Bc.Length) {
  2277. chan_misdn_log(1, bc->port, " -->%s Bc Len:%d\n", Spacing, Q931ie->Bc.Length);
  2278. }
  2279. if (Q931ie->Hlc.Length) {
  2280. chan_misdn_log(1, bc->port, " -->%s Hlc Len:%d\n", Spacing, Q931ie->Hlc.Length);
  2281. }
  2282. if (Q931ie->Llc.Length) {
  2283. chan_misdn_log(1, bc->port, " -->%s Llc Len:%d\n", Spacing, Q931ie->Llc.Length);
  2284. }
  2285. if (Q931ie->UserInfo.Length) {
  2286. chan_misdn_log(1, bc->port, " -->%s UserInfo Len:%d\n", Spacing, Q931ie->UserInfo.Length);
  2287. }
  2288. }
  2289. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2290. #if defined(AST_MISDN_ENHANCEMENTS)
  2291. static void print_facility_CallInformation(unsigned Level, const struct FacCallInformation *CallInfo, const struct misdn_bchannel *bc)
  2292. {
  2293. const char *Spacing;
  2294. Spacing = &Level_Spacing[sizeof(Level_Spacing) - 1 - Level];
  2295. chan_misdn_log(1, bc->port, " -->%s CCBSReference:%d\n",
  2296. Spacing, CallInfo->CCBSReference);
  2297. chan_misdn_log(1, bc->port, " -->%s AddressOfB:\n", Spacing);
  2298. print_facility_Address(Level + 1, &CallInfo->AddressOfB, bc);
  2299. print_facility_Q931_Bc_Hlc_Llc(Level, &CallInfo->Q931ie, bc);
  2300. if (CallInfo->SubaddressOfA.Length) {
  2301. chan_misdn_log(1, bc->port, " -->%s SubaddressOfA:\n", Spacing);
  2302. print_facility_Subaddress(Level + 1, &CallInfo->SubaddressOfA, bc);
  2303. }
  2304. }
  2305. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2306. #if defined(AST_MISDN_ENHANCEMENTS)
  2307. static void print_facility_ServedUserNr(unsigned Level, const struct FacPartyNumber *Party, const struct misdn_bchannel *bc)
  2308. {
  2309. const char *Spacing;
  2310. Spacing = &Level_Spacing[sizeof(Level_Spacing) - 1 - Level];
  2311. if (Party->LengthOfNumber) {
  2312. print_facility_PartyNumber(Level, Party, bc);
  2313. } else {
  2314. chan_misdn_log(1, bc->port, " -->%s All Numbers\n", Spacing);
  2315. }
  2316. }
  2317. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2318. #if defined(AST_MISDN_ENHANCEMENTS)
  2319. static void print_facility_IntResult(unsigned Level, const struct FacForwardingRecord *ForwardingRecord, const struct misdn_bchannel *bc)
  2320. {
  2321. const char *Spacing;
  2322. Spacing = &Level_Spacing[sizeof(Level_Spacing) - 1 - Level];
  2323. chan_misdn_log(1, bc->port, " -->%s Procedure:%d BasicService:%d\n",
  2324. Spacing,
  2325. ForwardingRecord->Procedure,
  2326. ForwardingRecord->BasicService);
  2327. chan_misdn_log(1, bc->port, " -->%s ForwardedTo:\n", Spacing);
  2328. print_facility_Address(Level + 1, &ForwardingRecord->ForwardedTo, bc);
  2329. chan_misdn_log(1, bc->port, " -->%s ServedUserNr:\n", Spacing);
  2330. print_facility_ServedUserNr(Level + 1, &ForwardingRecord->ServedUser, bc);
  2331. }
  2332. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2333. static void print_facility(const struct FacParm *fac, const struct misdn_bchannel *bc)
  2334. {
  2335. #if defined(AST_MISDN_ENHANCEMENTS)
  2336. unsigned Index;
  2337. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2338. switch (fac->Function) {
  2339. #if defined(AST_MISDN_ENHANCEMENTS)
  2340. case Fac_ActivationDiversion:
  2341. chan_misdn_log(1, bc->port, " --> ActivationDiversion: InvokeID:%d\n",
  2342. fac->u.ActivationDiversion.InvokeID);
  2343. switch (fac->u.ActivationDiversion.ComponentType) {
  2344. case FacComponent_Invoke:
  2345. chan_misdn_log(1, bc->port, " --> Invoke: Procedure:%d BasicService:%d\n",
  2346. fac->u.ActivationDiversion.Component.Invoke.Procedure,
  2347. fac->u.ActivationDiversion.Component.Invoke.BasicService);
  2348. chan_misdn_log(1, bc->port, " --> ForwardedTo:\n");
  2349. print_facility_Address(3, &fac->u.ActivationDiversion.Component.Invoke.ForwardedTo, bc);
  2350. chan_misdn_log(1, bc->port, " --> ServedUserNr:\n");
  2351. print_facility_ServedUserNr(3, &fac->u.ActivationDiversion.Component.Invoke.ServedUser, bc);
  2352. break;
  2353. case FacComponent_Result:
  2354. chan_misdn_log(1, bc->port, " --> Result\n");
  2355. break;
  2356. default:
  2357. break;
  2358. }
  2359. break;
  2360. case Fac_DeactivationDiversion:
  2361. chan_misdn_log(1, bc->port, " --> DeactivationDiversion: InvokeID:%d\n",
  2362. fac->u.DeactivationDiversion.InvokeID);
  2363. switch (fac->u.DeactivationDiversion.ComponentType) {
  2364. case FacComponent_Invoke:
  2365. chan_misdn_log(1, bc->port, " --> Invoke: Procedure:%d BasicService:%d\n",
  2366. fac->u.DeactivationDiversion.Component.Invoke.Procedure,
  2367. fac->u.DeactivationDiversion.Component.Invoke.BasicService);
  2368. chan_misdn_log(1, bc->port, " --> ServedUserNr:\n");
  2369. print_facility_ServedUserNr(3, &fac->u.DeactivationDiversion.Component.Invoke.ServedUser, bc);
  2370. break;
  2371. case FacComponent_Result:
  2372. chan_misdn_log(1, bc->port, " --> Result\n");
  2373. break;
  2374. default:
  2375. break;
  2376. }
  2377. break;
  2378. case Fac_ActivationStatusNotificationDiv:
  2379. chan_misdn_log(1, bc->port, " --> ActivationStatusNotificationDiv: InvokeID:%d Procedure:%d BasicService:%d\n",
  2380. fac->u.ActivationStatusNotificationDiv.InvokeID,
  2381. fac->u.ActivationStatusNotificationDiv.Procedure,
  2382. fac->u.ActivationStatusNotificationDiv.BasicService);
  2383. chan_misdn_log(1, bc->port, " --> ForwardedTo:\n");
  2384. print_facility_Address(2, &fac->u.ActivationStatusNotificationDiv.ForwardedTo, bc);
  2385. chan_misdn_log(1, bc->port, " --> ServedUserNr:\n");
  2386. print_facility_ServedUserNr(2, &fac->u.ActivationStatusNotificationDiv.ServedUser, bc);
  2387. break;
  2388. case Fac_DeactivationStatusNotificationDiv:
  2389. chan_misdn_log(1, bc->port, " --> DeactivationStatusNotificationDiv: InvokeID:%d Procedure:%d BasicService:%d\n",
  2390. fac->u.DeactivationStatusNotificationDiv.InvokeID,
  2391. fac->u.DeactivationStatusNotificationDiv.Procedure,
  2392. fac->u.DeactivationStatusNotificationDiv.BasicService);
  2393. chan_misdn_log(1, bc->port, " --> ServedUserNr:\n");
  2394. print_facility_ServedUserNr(2, &fac->u.DeactivationStatusNotificationDiv.ServedUser, bc);
  2395. break;
  2396. case Fac_InterrogationDiversion:
  2397. chan_misdn_log(1, bc->port, " --> InterrogationDiversion: InvokeID:%d\n",
  2398. fac->u.InterrogationDiversion.InvokeID);
  2399. switch (fac->u.InterrogationDiversion.ComponentType) {
  2400. case FacComponent_Invoke:
  2401. chan_misdn_log(1, bc->port, " --> Invoke: Procedure:%d BasicService:%d\n",
  2402. fac->u.InterrogationDiversion.Component.Invoke.Procedure,
  2403. fac->u.InterrogationDiversion.Component.Invoke.BasicService);
  2404. chan_misdn_log(1, bc->port, " --> ServedUserNr:\n");
  2405. print_facility_ServedUserNr(3, &fac->u.InterrogationDiversion.Component.Invoke.ServedUser, bc);
  2406. break;
  2407. case FacComponent_Result:
  2408. chan_misdn_log(1, bc->port, " --> Result:\n");
  2409. if (fac->u.InterrogationDiversion.Component.Result.NumRecords) {
  2410. for (Index = 0; Index < fac->u.InterrogationDiversion.Component.Result.NumRecords; ++Index) {
  2411. chan_misdn_log(1, bc->port, " --> IntResult[%d]:\n", Index);
  2412. print_facility_IntResult(3, &fac->u.InterrogationDiversion.Component.Result.List[Index], bc);
  2413. }
  2414. }
  2415. break;
  2416. default:
  2417. break;
  2418. }
  2419. break;
  2420. case Fac_DiversionInformation:
  2421. chan_misdn_log(1, bc->port, " --> DiversionInformation: InvokeID:%d Reason:%d BasicService:%d\n",
  2422. fac->u.DiversionInformation.InvokeID,
  2423. fac->u.DiversionInformation.DiversionReason,
  2424. fac->u.DiversionInformation.BasicService);
  2425. if (fac->u.DiversionInformation.ServedUserSubaddress.Length) {
  2426. chan_misdn_log(1, bc->port, " --> ServedUserSubaddress:\n");
  2427. print_facility_Subaddress(2, &fac->u.DiversionInformation.ServedUserSubaddress, bc);
  2428. }
  2429. if (fac->u.DiversionInformation.CallingAddressPresent) {
  2430. chan_misdn_log(1, bc->port, " --> CallingAddress:\n");
  2431. print_facility_PresentedAddressScreened(2, &fac->u.DiversionInformation.CallingAddress, bc);
  2432. }
  2433. if (fac->u.DiversionInformation.OriginalCalledPresent) {
  2434. chan_misdn_log(1, bc->port, " --> OriginalCalledNr:\n");
  2435. print_facility_PresentedNumberUnscreened(2, &fac->u.DiversionInformation.OriginalCalled, bc);
  2436. }
  2437. if (fac->u.DiversionInformation.LastDivertingPresent) {
  2438. chan_misdn_log(1, bc->port, " --> LastDivertingNr:\n");
  2439. print_facility_PresentedNumberUnscreened(2, &fac->u.DiversionInformation.LastDiverting, bc);
  2440. }
  2441. if (fac->u.DiversionInformation.LastDivertingReasonPresent) {
  2442. chan_misdn_log(1, bc->port, " --> LastDivertingReason:%d\n", fac->u.DiversionInformation.LastDivertingReason);
  2443. }
  2444. if (fac->u.DiversionInformation.UserInfo.Length) {
  2445. chan_misdn_log(1, bc->port, " --> UserInfo Length:%d\n", fac->u.DiversionInformation.UserInfo.Length);
  2446. }
  2447. break;
  2448. case Fac_CallDeflection:
  2449. chan_misdn_log(1, bc->port, " --> CallDeflection: InvokeID:%d\n",
  2450. fac->u.CallDeflection.InvokeID);
  2451. switch (fac->u.CallDeflection.ComponentType) {
  2452. case FacComponent_Invoke:
  2453. chan_misdn_log(1, bc->port, " --> Invoke:\n");
  2454. if (fac->u.CallDeflection.Component.Invoke.PresentationAllowedToDivertedToUserPresent) {
  2455. chan_misdn_log(1, bc->port, " --> PresentationAllowed:%d\n",
  2456. fac->u.CallDeflection.Component.Invoke.PresentationAllowedToDivertedToUser);
  2457. }
  2458. chan_misdn_log(1, bc->port, " --> DeflectionAddress:\n");
  2459. print_facility_Address(3, &fac->u.CallDeflection.Component.Invoke.Deflection, bc);
  2460. break;
  2461. case FacComponent_Result:
  2462. chan_misdn_log(1, bc->port, " --> Result\n");
  2463. break;
  2464. default:
  2465. break;
  2466. }
  2467. break;
  2468. case Fac_CallRerouteing:
  2469. chan_misdn_log(1, bc->port, " --> CallRerouteing: InvokeID:%d\n",
  2470. fac->u.CallRerouteing.InvokeID);
  2471. switch (fac->u.CallRerouteing.ComponentType) {
  2472. case FacComponent_Invoke:
  2473. chan_misdn_log(1, bc->port, " --> Invoke: Reason:%d Counter:%d\n",
  2474. fac->u.CallRerouteing.Component.Invoke.ReroutingReason,
  2475. fac->u.CallRerouteing.Component.Invoke.ReroutingCounter);
  2476. chan_misdn_log(1, bc->port, " --> CalledAddress:\n");
  2477. print_facility_Address(3, &fac->u.CallRerouteing.Component.Invoke.CalledAddress, bc);
  2478. print_facility_Q931_Bc_Hlc_Llc_Uu(2, &fac->u.CallRerouteing.Component.Invoke.Q931ie, bc);
  2479. chan_misdn_log(1, bc->port, " --> LastReroutingNr:\n");
  2480. print_facility_PresentedNumberUnscreened(3, &fac->u.CallRerouteing.Component.Invoke.LastRerouting, bc);
  2481. chan_misdn_log(1, bc->port, " --> SubscriptionOption:%d\n",
  2482. fac->u.CallRerouteing.Component.Invoke.SubscriptionOption);
  2483. if (fac->u.CallRerouteing.Component.Invoke.CallingPartySubaddress.Length) {
  2484. chan_misdn_log(1, bc->port, " --> CallingParty:\n");
  2485. print_facility_Subaddress(3, &fac->u.CallRerouteing.Component.Invoke.CallingPartySubaddress, bc);
  2486. }
  2487. break;
  2488. case FacComponent_Result:
  2489. chan_misdn_log(1, bc->port, " --> Result\n");
  2490. break;
  2491. default:
  2492. break;
  2493. }
  2494. break;
  2495. case Fac_InterrogateServedUserNumbers:
  2496. chan_misdn_log(1, bc->port, " --> InterrogateServedUserNumbers: InvokeID:%d\n",
  2497. fac->u.InterrogateServedUserNumbers.InvokeID);
  2498. switch (fac->u.InterrogateServedUserNumbers.ComponentType) {
  2499. case FacComponent_Invoke:
  2500. chan_misdn_log(1, bc->port, " --> Invoke\n");
  2501. break;
  2502. case FacComponent_Result:
  2503. chan_misdn_log(1, bc->port, " --> Result:\n");
  2504. if (fac->u.InterrogateServedUserNumbers.Component.Result.NumRecords) {
  2505. for (Index = 0; Index < fac->u.InterrogateServedUserNumbers.Component.Result.NumRecords; ++Index) {
  2506. chan_misdn_log(1, bc->port, " --> ServedUserNr[%d]:\n", Index);
  2507. print_facility_PartyNumber(3, &fac->u.InterrogateServedUserNumbers.Component.Result.List[Index], bc);
  2508. }
  2509. }
  2510. break;
  2511. default:
  2512. break;
  2513. }
  2514. break;
  2515. case Fac_DivertingLegInformation1:
  2516. chan_misdn_log(1, bc->port, " --> DivertingLegInformation1: InvokeID:%d Reason:%d SubscriptionOption:%d\n",
  2517. fac->u.DivertingLegInformation1.InvokeID,
  2518. fac->u.DivertingLegInformation1.DiversionReason,
  2519. fac->u.DivertingLegInformation1.SubscriptionOption);
  2520. if (fac->u.DivertingLegInformation1.DivertedToPresent) {
  2521. chan_misdn_log(1, bc->port, " --> DivertedToNr:\n");
  2522. print_facility_PresentedNumberUnscreened(2, &fac->u.DivertingLegInformation1.DivertedTo, bc);
  2523. }
  2524. break;
  2525. case Fac_DivertingLegInformation2:
  2526. chan_misdn_log(1, bc->port, " --> DivertingLegInformation2: InvokeID:%d Reason:%d Count:%d\n",
  2527. fac->u.DivertingLegInformation2.InvokeID,
  2528. fac->u.DivertingLegInformation2.DiversionReason,
  2529. fac->u.DivertingLegInformation2.DiversionCounter);
  2530. if (fac->u.DivertingLegInformation2.DivertingPresent) {
  2531. chan_misdn_log(1, bc->port, " --> DivertingNr:\n");
  2532. print_facility_PresentedNumberUnscreened(2, &fac->u.DivertingLegInformation2.Diverting, bc);
  2533. }
  2534. if (fac->u.DivertingLegInformation2.OriginalCalledPresent) {
  2535. chan_misdn_log(1, bc->port, " --> OriginalCalledNr:\n");
  2536. print_facility_PresentedNumberUnscreened(2, &fac->u.DivertingLegInformation2.OriginalCalled, bc);
  2537. }
  2538. break;
  2539. case Fac_DivertingLegInformation3:
  2540. chan_misdn_log(1, bc->port, " --> DivertingLegInformation3: InvokeID:%d PresentationAllowed:%d\n",
  2541. fac->u.DivertingLegInformation3.InvokeID,
  2542. fac->u.DivertingLegInformation3.PresentationAllowedIndicator);
  2543. break;
  2544. #else /* !defined(AST_MISDN_ENHANCEMENTS) */
  2545. case Fac_CD:
  2546. chan_misdn_log(1, bc->port, " --> calldeflect to: %s, presentable: %s\n", fac->u.CDeflection.DeflectedToNumber,
  2547. fac->u.CDeflection.PresentationAllowed ? "yes" : "no");
  2548. break;
  2549. #endif /* !defined(AST_MISDN_ENHANCEMENTS) */
  2550. case Fac_AOCDCurrency:
  2551. if (fac->u.AOCDcur.chargeNotAvailable) {
  2552. chan_misdn_log(1, bc->port, " --> AOCD currency: charge not available\n");
  2553. } else if (fac->u.AOCDcur.freeOfCharge) {
  2554. chan_misdn_log(1, bc->port, " --> AOCD currency: free of charge\n");
  2555. } else if (fac->u.AOCDchu.billingId >= 0) {
  2556. chan_misdn_log(1, bc->port, " --> AOCD currency: currency:%s amount:%d multiplier:%d typeOfChargingInfo:%s billingId:%d\n",
  2557. fac->u.AOCDcur.currency, fac->u.AOCDcur.currencyAmount, fac->u.AOCDcur.multiplier,
  2558. (fac->u.AOCDcur.typeOfChargingInfo == 0) ? "subTotal" : "total", fac->u.AOCDcur.billingId);
  2559. } else {
  2560. chan_misdn_log(1, bc->port, " --> AOCD currency: currency:%s amount:%d multiplier:%d typeOfChargingInfo:%s\n",
  2561. fac->u.AOCDcur.currency, fac->u.AOCDcur.currencyAmount, fac->u.AOCDcur.multiplier,
  2562. (fac->u.AOCDcur.typeOfChargingInfo == 0) ? "subTotal" : "total");
  2563. }
  2564. break;
  2565. case Fac_AOCDChargingUnit:
  2566. if (fac->u.AOCDchu.chargeNotAvailable) {
  2567. chan_misdn_log(1, bc->port, " --> AOCD charging unit: charge not available\n");
  2568. } else if (fac->u.AOCDchu.freeOfCharge) {
  2569. chan_misdn_log(1, bc->port, " --> AOCD charging unit: free of charge\n");
  2570. } else if (fac->u.AOCDchu.billingId >= 0) {
  2571. chan_misdn_log(1, bc->port, " --> AOCD charging unit: recordedUnits:%d typeOfChargingInfo:%s billingId:%d\n",
  2572. fac->u.AOCDchu.recordedUnits, (fac->u.AOCDchu.typeOfChargingInfo == 0) ? "subTotal" : "total", fac->u.AOCDchu.billingId);
  2573. } else {
  2574. chan_misdn_log(1, bc->port, " --> AOCD charging unit: recordedUnits:%d typeOfChargingInfo:%s\n",
  2575. fac->u.AOCDchu.recordedUnits, (fac->u.AOCDchu.typeOfChargingInfo == 0) ? "subTotal" : "total");
  2576. }
  2577. break;
  2578. #if defined(AST_MISDN_ENHANCEMENTS)
  2579. case Fac_ERROR:
  2580. chan_misdn_log(1, bc->port, " --> ERROR: InvokeID:%d, Code:0x%02x\n",
  2581. fac->u.ERROR.invokeId, fac->u.ERROR.errorValue);
  2582. break;
  2583. case Fac_RESULT:
  2584. chan_misdn_log(1, bc->port, " --> RESULT: InvokeID:%d\n",
  2585. fac->u.RESULT.InvokeID);
  2586. break;
  2587. case Fac_REJECT:
  2588. if (fac->u.REJECT.InvokeIDPresent) {
  2589. chan_misdn_log(1, bc->port, " --> REJECT: InvokeID:%d, Code:0x%02x\n",
  2590. fac->u.REJECT.InvokeID, fac->u.REJECT.Code);
  2591. } else {
  2592. chan_misdn_log(1, bc->port, " --> REJECT: Code:0x%02x\n",
  2593. fac->u.REJECT.Code);
  2594. }
  2595. break;
  2596. case Fac_EctExecute:
  2597. chan_misdn_log(1, bc->port, " --> EctExecute: InvokeID:%d\n",
  2598. fac->u.EctExecute.InvokeID);
  2599. break;
  2600. case Fac_ExplicitEctExecute:
  2601. chan_misdn_log(1, bc->port, " --> ExplicitEctExecute: InvokeID:%d LinkID:%d\n",
  2602. fac->u.ExplicitEctExecute.InvokeID,
  2603. fac->u.ExplicitEctExecute.LinkID);
  2604. break;
  2605. case Fac_RequestSubaddress:
  2606. chan_misdn_log(1, bc->port, " --> RequestSubaddress: InvokeID:%d\n",
  2607. fac->u.RequestSubaddress.InvokeID);
  2608. break;
  2609. case Fac_SubaddressTransfer:
  2610. chan_misdn_log(1, bc->port, " --> SubaddressTransfer: InvokeID:%d\n",
  2611. fac->u.SubaddressTransfer.InvokeID);
  2612. print_facility_Subaddress(1, &fac->u.SubaddressTransfer.Subaddress, bc);
  2613. break;
  2614. case Fac_EctLinkIdRequest:
  2615. chan_misdn_log(1, bc->port, " --> EctLinkIdRequest: InvokeID:%d\n",
  2616. fac->u.EctLinkIdRequest.InvokeID);
  2617. switch (fac->u.EctLinkIdRequest.ComponentType) {
  2618. case FacComponent_Invoke:
  2619. chan_misdn_log(1, bc->port, " --> Invoke\n");
  2620. break;
  2621. case FacComponent_Result:
  2622. chan_misdn_log(1, bc->port, " --> Result: LinkID:%d\n",
  2623. fac->u.EctLinkIdRequest.Component.Result.LinkID);
  2624. break;
  2625. default:
  2626. break;
  2627. }
  2628. break;
  2629. case Fac_EctInform:
  2630. chan_misdn_log(1, bc->port, " --> EctInform: InvokeID:%d Status:%d\n",
  2631. fac->u.EctInform.InvokeID,
  2632. fac->u.EctInform.Status);
  2633. if (fac->u.EctInform.RedirectionPresent) {
  2634. chan_misdn_log(1, bc->port, " --> Redirection Number\n");
  2635. print_facility_PresentedNumberUnscreened(2, &fac->u.EctInform.Redirection, bc);
  2636. }
  2637. break;
  2638. case Fac_EctLoopTest:
  2639. chan_misdn_log(1, bc->port, " --> EctLoopTest: InvokeID:%d\n",
  2640. fac->u.EctLoopTest.InvokeID);
  2641. switch (fac->u.EctLoopTest.ComponentType) {
  2642. case FacComponent_Invoke:
  2643. chan_misdn_log(1, bc->port, " --> Invoke: CallTransferID:%d\n",
  2644. fac->u.EctLoopTest.Component.Invoke.CallTransferID);
  2645. break;
  2646. case FacComponent_Result:
  2647. chan_misdn_log(1, bc->port, " --> Result: LoopResult:%d\n",
  2648. fac->u.EctLoopTest.Component.Result.LoopResult);
  2649. break;
  2650. default:
  2651. break;
  2652. }
  2653. break;
  2654. case Fac_StatusRequest:
  2655. chan_misdn_log(1, bc->port, " --> StatusRequest: InvokeID:%d\n",
  2656. fac->u.StatusRequest.InvokeID);
  2657. switch (fac->u.StatusRequest.ComponentType) {
  2658. case FacComponent_Invoke:
  2659. chan_misdn_log(1, bc->port, " --> Invoke: Compatibility:%d\n",
  2660. fac->u.StatusRequest.Component.Invoke.CompatibilityMode);
  2661. break;
  2662. case FacComponent_Result:
  2663. chan_misdn_log(1, bc->port, " --> Result: Status:%d\n",
  2664. fac->u.StatusRequest.Component.Result.Status);
  2665. break;
  2666. default:
  2667. break;
  2668. }
  2669. break;
  2670. case Fac_CallInfoRetain:
  2671. chan_misdn_log(1, bc->port, " --> CallInfoRetain: InvokeID:%d, LinkageID:%d\n",
  2672. fac->u.CallInfoRetain.InvokeID, fac->u.CallInfoRetain.CallLinkageID);
  2673. break;
  2674. case Fac_CCBSDeactivate:
  2675. chan_misdn_log(1, bc->port, " --> CCBSDeactivate: InvokeID:%d\n",
  2676. fac->u.CCBSDeactivate.InvokeID);
  2677. switch (fac->u.CCBSDeactivate.ComponentType) {
  2678. case FacComponent_Invoke:
  2679. chan_misdn_log(1, bc->port, " --> Invoke: CCBSReference:%d\n",
  2680. fac->u.CCBSDeactivate.Component.Invoke.CCBSReference);
  2681. break;
  2682. case FacComponent_Result:
  2683. chan_misdn_log(1, bc->port, " --> Result\n");
  2684. break;
  2685. default:
  2686. break;
  2687. }
  2688. break;
  2689. case Fac_CCBSErase:
  2690. chan_misdn_log(1, bc->port, " --> CCBSErase: InvokeID:%d, CCBSReference:%d RecallMode:%d, Reason:%d\n",
  2691. fac->u.CCBSErase.InvokeID, fac->u.CCBSErase.CCBSReference,
  2692. fac->u.CCBSErase.RecallMode, fac->u.CCBSErase.Reason);
  2693. chan_misdn_log(1, bc->port, " --> AddressOfB\n");
  2694. print_facility_Address(2, &fac->u.CCBSErase.AddressOfB, bc);
  2695. print_facility_Q931_Bc_Hlc_Llc(1, &fac->u.CCBSErase.Q931ie, bc);
  2696. break;
  2697. case Fac_CCBSRemoteUserFree:
  2698. chan_misdn_log(1, bc->port, " --> CCBSRemoteUserFree: InvokeID:%d, CCBSReference:%d RecallMode:%d\n",
  2699. fac->u.CCBSRemoteUserFree.InvokeID, fac->u.CCBSRemoteUserFree.CCBSReference,
  2700. fac->u.CCBSRemoteUserFree.RecallMode);
  2701. chan_misdn_log(1, bc->port, " --> AddressOfB\n");
  2702. print_facility_Address(2, &fac->u.CCBSRemoteUserFree.AddressOfB, bc);
  2703. print_facility_Q931_Bc_Hlc_Llc(1, &fac->u.CCBSRemoteUserFree.Q931ie, bc);
  2704. break;
  2705. case Fac_CCBSCall:
  2706. chan_misdn_log(1, bc->port, " --> CCBSCall: InvokeID:%d, CCBSReference:%d\n",
  2707. fac->u.CCBSCall.InvokeID, fac->u.CCBSCall.CCBSReference);
  2708. break;
  2709. case Fac_CCBSStatusRequest:
  2710. chan_misdn_log(1, bc->port, " --> CCBSStatusRequest: InvokeID:%d\n",
  2711. fac->u.CCBSStatusRequest.InvokeID);
  2712. switch (fac->u.CCBSStatusRequest.ComponentType) {
  2713. case FacComponent_Invoke:
  2714. chan_misdn_log(1, bc->port, " --> Invoke: CCBSReference:%d RecallMode:%d\n",
  2715. fac->u.CCBSStatusRequest.Component.Invoke.CCBSReference,
  2716. fac->u.CCBSStatusRequest.Component.Invoke.RecallMode);
  2717. print_facility_Q931_Bc_Hlc_Llc(2, &fac->u.CCBSStatusRequest.Component.Invoke.Q931ie, bc);
  2718. break;
  2719. case FacComponent_Result:
  2720. chan_misdn_log(1, bc->port, " --> Result: Free:%d\n",
  2721. fac->u.CCBSStatusRequest.Component.Result.Free);
  2722. break;
  2723. default:
  2724. break;
  2725. }
  2726. break;
  2727. case Fac_CCBSBFree:
  2728. chan_misdn_log(1, bc->port, " --> CCBSBFree: InvokeID:%d, CCBSReference:%d RecallMode:%d\n",
  2729. fac->u.CCBSBFree.InvokeID, fac->u.CCBSBFree.CCBSReference,
  2730. fac->u.CCBSBFree.RecallMode);
  2731. chan_misdn_log(1, bc->port, " --> AddressOfB\n");
  2732. print_facility_Address(2, &fac->u.CCBSBFree.AddressOfB, bc);
  2733. print_facility_Q931_Bc_Hlc_Llc(1, &fac->u.CCBSBFree.Q931ie, bc);
  2734. break;
  2735. case Fac_EraseCallLinkageID:
  2736. chan_misdn_log(1, bc->port, " --> EraseCallLinkageID: InvokeID:%d, LinkageID:%d\n",
  2737. fac->u.EraseCallLinkageID.InvokeID, fac->u.EraseCallLinkageID.CallLinkageID);
  2738. break;
  2739. case Fac_CCBSStopAlerting:
  2740. chan_misdn_log(1, bc->port, " --> CCBSStopAlerting: InvokeID:%d, CCBSReference:%d\n",
  2741. fac->u.CCBSStopAlerting.InvokeID, fac->u.CCBSStopAlerting.CCBSReference);
  2742. break;
  2743. case Fac_CCBSRequest:
  2744. chan_misdn_log(1, bc->port, " --> CCBSRequest: InvokeID:%d\n",
  2745. fac->u.CCBSRequest.InvokeID);
  2746. switch (fac->u.CCBSRequest.ComponentType) {
  2747. case FacComponent_Invoke:
  2748. chan_misdn_log(1, bc->port, " --> Invoke: LinkageID:%d\n",
  2749. fac->u.CCBSRequest.Component.Invoke.CallLinkageID);
  2750. break;
  2751. case FacComponent_Result:
  2752. chan_misdn_log(1, bc->port, " --> Result: CCBSReference:%d RecallMode:%d\n",
  2753. fac->u.CCBSRequest.Component.Result.CCBSReference,
  2754. fac->u.CCBSRequest.Component.Result.RecallMode);
  2755. break;
  2756. default:
  2757. break;
  2758. }
  2759. break;
  2760. case Fac_CCBSInterrogate:
  2761. chan_misdn_log(1, bc->port, " --> CCBSInterrogate: InvokeID:%d\n",
  2762. fac->u.CCBSInterrogate.InvokeID);
  2763. switch (fac->u.CCBSInterrogate.ComponentType) {
  2764. case FacComponent_Invoke:
  2765. chan_misdn_log(1, bc->port, " --> Invoke\n");
  2766. if (fac->u.CCBSInterrogate.Component.Invoke.CCBSReferencePresent) {
  2767. chan_misdn_log(1, bc->port, " --> CCBSReference:%d\n",
  2768. fac->u.CCBSInterrogate.Component.Invoke.CCBSReference);
  2769. }
  2770. if (fac->u.CCBSInterrogate.Component.Invoke.AParty.LengthOfNumber) {
  2771. chan_misdn_log(1, bc->port, " --> AParty\n");
  2772. print_facility_PartyNumber(3, &fac->u.CCBSInterrogate.Component.Invoke.AParty, bc);
  2773. }
  2774. break;
  2775. case FacComponent_Result:
  2776. chan_misdn_log(1, bc->port, " --> Result: RecallMode:%d\n",
  2777. fac->u.CCBSInterrogate.Component.Result.RecallMode);
  2778. if (fac->u.CCBSInterrogate.Component.Result.NumRecords) {
  2779. for (Index = 0; Index < fac->u.CCBSInterrogate.Component.Result.NumRecords; ++Index) {
  2780. chan_misdn_log(1, bc->port, " --> CallDetails[%d]:\n", Index);
  2781. print_facility_CallInformation(3, &fac->u.CCBSInterrogate.Component.Result.CallDetails[Index], bc);
  2782. }
  2783. }
  2784. break;
  2785. default:
  2786. break;
  2787. }
  2788. break;
  2789. case Fac_CCNRRequest:
  2790. chan_misdn_log(1, bc->port, " --> CCNRRequest: InvokeID:%d\n",
  2791. fac->u.CCNRRequest.InvokeID);
  2792. switch (fac->u.CCNRRequest.ComponentType) {
  2793. case FacComponent_Invoke:
  2794. chan_misdn_log(1, bc->port, " --> Invoke: LinkageID:%d\n",
  2795. fac->u.CCNRRequest.Component.Invoke.CallLinkageID);
  2796. break;
  2797. case FacComponent_Result:
  2798. chan_misdn_log(1, bc->port, " --> Result: CCBSReference:%d RecallMode:%d\n",
  2799. fac->u.CCNRRequest.Component.Result.CCBSReference,
  2800. fac->u.CCNRRequest.Component.Result.RecallMode);
  2801. break;
  2802. default:
  2803. break;
  2804. }
  2805. break;
  2806. case Fac_CCNRInterrogate:
  2807. chan_misdn_log(1, bc->port, " --> CCNRInterrogate: InvokeID:%d\n",
  2808. fac->u.CCNRInterrogate.InvokeID);
  2809. switch (fac->u.CCNRInterrogate.ComponentType) {
  2810. case FacComponent_Invoke:
  2811. chan_misdn_log(1, bc->port, " --> Invoke\n");
  2812. if (fac->u.CCNRInterrogate.Component.Invoke.CCBSReferencePresent) {
  2813. chan_misdn_log(1, bc->port, " --> CCBSReference:%d\n",
  2814. fac->u.CCNRInterrogate.Component.Invoke.CCBSReference);
  2815. }
  2816. if (fac->u.CCNRInterrogate.Component.Invoke.AParty.LengthOfNumber) {
  2817. chan_misdn_log(1, bc->port, " --> AParty\n");
  2818. print_facility_PartyNumber(3, &fac->u.CCNRInterrogate.Component.Invoke.AParty, bc);
  2819. }
  2820. break;
  2821. case FacComponent_Result:
  2822. chan_misdn_log(1, bc->port, " --> Result: RecallMode:%d\n",
  2823. fac->u.CCNRInterrogate.Component.Result.RecallMode);
  2824. if (fac->u.CCNRInterrogate.Component.Result.NumRecords) {
  2825. for (Index = 0; Index < fac->u.CCNRInterrogate.Component.Result.NumRecords; ++Index) {
  2826. chan_misdn_log(1, bc->port, " --> CallDetails[%d]:\n", Index);
  2827. print_facility_CallInformation(3, &fac->u.CCNRInterrogate.Component.Result.CallDetails[Index], bc);
  2828. }
  2829. }
  2830. break;
  2831. default:
  2832. break;
  2833. }
  2834. break;
  2835. case Fac_CCBS_T_Call:
  2836. chan_misdn_log(1, bc->port, " --> CCBS_T_Call: InvokeID:%d\n",
  2837. fac->u.CCBS_T_Call.InvokeID);
  2838. break;
  2839. case Fac_CCBS_T_Suspend:
  2840. chan_misdn_log(1, bc->port, " --> CCBS_T_Suspend: InvokeID:%d\n",
  2841. fac->u.CCBS_T_Suspend.InvokeID);
  2842. break;
  2843. case Fac_CCBS_T_Resume:
  2844. chan_misdn_log(1, bc->port, " --> CCBS_T_Resume: InvokeID:%d\n",
  2845. fac->u.CCBS_T_Resume.InvokeID);
  2846. break;
  2847. case Fac_CCBS_T_RemoteUserFree:
  2848. chan_misdn_log(1, bc->port, " --> CCBS_T_RemoteUserFree: InvokeID:%d\n",
  2849. fac->u.CCBS_T_RemoteUserFree.InvokeID);
  2850. break;
  2851. case Fac_CCBS_T_Available:
  2852. chan_misdn_log(1, bc->port, " --> CCBS_T_Available: InvokeID:%d\n",
  2853. fac->u.CCBS_T_Available.InvokeID);
  2854. break;
  2855. case Fac_CCBS_T_Request:
  2856. chan_misdn_log(1, bc->port, " --> CCBS_T_Request: InvokeID:%d\n",
  2857. fac->u.CCBS_T_Request.InvokeID);
  2858. switch (fac->u.CCBS_T_Request.ComponentType) {
  2859. case FacComponent_Invoke:
  2860. chan_misdn_log(1, bc->port, " --> Invoke\n");
  2861. chan_misdn_log(1, bc->port, " --> DestinationAddress:\n");
  2862. print_facility_Address(3, &fac->u.CCBS_T_Request.Component.Invoke.Destination, bc);
  2863. print_facility_Q931_Bc_Hlc_Llc(2, &fac->u.CCBS_T_Request.Component.Invoke.Q931ie, bc);
  2864. if (fac->u.CCBS_T_Request.Component.Invoke.RetentionSupported) {
  2865. chan_misdn_log(1, bc->port, " --> RetentionSupported:1\n");
  2866. }
  2867. if (fac->u.CCBS_T_Request.Component.Invoke.PresentationAllowedIndicatorPresent) {
  2868. chan_misdn_log(1, bc->port, " --> PresentationAllowed:%d\n",
  2869. fac->u.CCBS_T_Request.Component.Invoke.PresentationAllowedIndicator);
  2870. }
  2871. if (fac->u.CCBS_T_Request.Component.Invoke.Originating.Party.LengthOfNumber) {
  2872. chan_misdn_log(1, bc->port, " --> OriginatingAddress:\n");
  2873. print_facility_Address(3, &fac->u.CCBS_T_Request.Component.Invoke.Originating, bc);
  2874. }
  2875. break;
  2876. case FacComponent_Result:
  2877. chan_misdn_log(1, bc->port, " --> Result: RetentionSupported:%d\n",
  2878. fac->u.CCBS_T_Request.Component.Result.RetentionSupported);
  2879. break;
  2880. default:
  2881. break;
  2882. }
  2883. break;
  2884. case Fac_CCNR_T_Request:
  2885. chan_misdn_log(1, bc->port, " --> CCNR_T_Request: InvokeID:%d\n",
  2886. fac->u.CCNR_T_Request.InvokeID);
  2887. switch (fac->u.CCNR_T_Request.ComponentType) {
  2888. case FacComponent_Invoke:
  2889. chan_misdn_log(1, bc->port, " --> Invoke\n");
  2890. chan_misdn_log(1, bc->port, " --> DestinationAddress:\n");
  2891. print_facility_Address(3, &fac->u.CCNR_T_Request.Component.Invoke.Destination, bc);
  2892. print_facility_Q931_Bc_Hlc_Llc(2, &fac->u.CCNR_T_Request.Component.Invoke.Q931ie, bc);
  2893. if (fac->u.CCNR_T_Request.Component.Invoke.RetentionSupported) {
  2894. chan_misdn_log(1, bc->port, " --> RetentionSupported:1\n");
  2895. }
  2896. if (fac->u.CCNR_T_Request.Component.Invoke.PresentationAllowedIndicatorPresent) {
  2897. chan_misdn_log(1, bc->port, " --> PresentationAllowed:%d\n",
  2898. fac->u.CCNR_T_Request.Component.Invoke.PresentationAllowedIndicator);
  2899. }
  2900. if (fac->u.CCNR_T_Request.Component.Invoke.Originating.Party.LengthOfNumber) {
  2901. chan_misdn_log(1, bc->port, " --> OriginatingAddress:\n");
  2902. print_facility_Address(3, &fac->u.CCNR_T_Request.Component.Invoke.Originating, bc);
  2903. }
  2904. break;
  2905. case FacComponent_Result:
  2906. chan_misdn_log(1, bc->port, " --> Result: RetentionSupported:%d\n",
  2907. fac->u.CCNR_T_Request.Component.Result.RetentionSupported);
  2908. break;
  2909. default:
  2910. break;
  2911. }
  2912. break;
  2913. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2914. case Fac_None:
  2915. /* No facility so print nothing */
  2916. break;
  2917. default:
  2918. chan_misdn_log(1, bc->port, " --> unknown facility\n");
  2919. break;
  2920. }
  2921. }
  2922. static void print_bearer(struct misdn_bchannel *bc)
  2923. {
  2924. chan_misdn_log(2, bc->port, " --> Bearer: %s\n", bearer2str(bc->capability));
  2925. switch(bc->law) {
  2926. case INFO_CODEC_ALAW:
  2927. chan_misdn_log(2, bc->port, " --> Codec: Alaw\n");
  2928. break;
  2929. case INFO_CODEC_ULAW:
  2930. chan_misdn_log(2, bc->port, " --> Codec: Ulaw\n");
  2931. break;
  2932. }
  2933. }
  2934. /*!
  2935. * \internal
  2936. * \brief Prefix a string to another string in place.
  2937. *
  2938. * \param str_prefix String to prefix to the main string.
  2939. * \param str_main String to get the prefix added to it.
  2940. * \param size Buffer size of the main string (Includes null terminator).
  2941. *
  2942. * \note The str_main buffer size must be greater than one.
  2943. *
  2944. * \return Nothing
  2945. */
  2946. static void misdn_prefix_string(const char *str_prefix, char *str_main, size_t size)
  2947. {
  2948. size_t len_over;
  2949. size_t len_total;
  2950. size_t len_main;
  2951. size_t len_prefix;
  2952. len_prefix = strlen(str_prefix);
  2953. if (!len_prefix) {
  2954. /* There is no prefix to prepend. */
  2955. return;
  2956. }
  2957. len_main = strlen(str_main);
  2958. len_total = len_prefix + len_main;
  2959. if (size <= len_total) {
  2960. /* We need to truncate since the buffer is too small. */
  2961. len_over = len_total + 1 - size;
  2962. if (len_over <= len_main) {
  2963. len_main -= len_over;
  2964. } else {
  2965. len_over -= len_main;
  2966. len_main = 0;
  2967. len_prefix -= len_over;
  2968. }
  2969. }
  2970. if (len_main) {
  2971. memmove(str_main + len_prefix, str_main, len_main);
  2972. }
  2973. memcpy(str_main, str_prefix, len_prefix);
  2974. str_main[len_prefix + len_main] = '\0';
  2975. }
  2976. /*!
  2977. * \internal
  2978. * \brief Add a configured prefix to the given number.
  2979. *
  2980. * \param port Logical port number
  2981. * \param number_type Type-of-number passed in.
  2982. * \param number Given number string to add prefix
  2983. * \param size Buffer size number string occupies.
  2984. *
  2985. * \return Nothing
  2986. */
  2987. static void misdn_add_number_prefix(int port, enum mISDN_NUMBER_TYPE number_type, char *number, size_t size)
  2988. {
  2989. enum misdn_cfg_elements type_prefix;
  2990. char num_prefix[MISDN_MAX_NUMBER_LEN];
  2991. /* Get prefix string. */
  2992. switch (number_type) {
  2993. case NUMTYPE_UNKNOWN:
  2994. type_prefix = MISDN_CFG_TON_PREFIX_UNKNOWN;
  2995. break;
  2996. case NUMTYPE_INTERNATIONAL:
  2997. type_prefix = MISDN_CFG_TON_PREFIX_INTERNATIONAL;
  2998. break;
  2999. case NUMTYPE_NATIONAL:
  3000. type_prefix = MISDN_CFG_TON_PREFIX_NATIONAL;
  3001. break;
  3002. case NUMTYPE_NETWORK_SPECIFIC:
  3003. type_prefix = MISDN_CFG_TON_PREFIX_NETWORK_SPECIFIC;
  3004. break;
  3005. case NUMTYPE_SUBSCRIBER:
  3006. type_prefix = MISDN_CFG_TON_PREFIX_SUBSCRIBER;
  3007. break;
  3008. case NUMTYPE_ABBREVIATED:
  3009. type_prefix = MISDN_CFG_TON_PREFIX_ABBREVIATED;
  3010. break;
  3011. default:
  3012. /* Type-of-number does not have a prefix that can be added. */
  3013. return;
  3014. }
  3015. misdn_cfg_get(port, type_prefix, num_prefix, sizeof(num_prefix));
  3016. misdn_prefix_string(num_prefix, number, size);
  3017. }
  3018. static void export_aoc_vars(int originator, struct ast_channel *ast, struct misdn_bchannel *bc)
  3019. {
  3020. char buf[128];
  3021. if (!bc->AOCD_need_export || !ast) {
  3022. return;
  3023. }
  3024. if (originator == ORG_AST) {
  3025. ast = ast_bridged_channel(ast);
  3026. if (!ast) {
  3027. return;
  3028. }
  3029. }
  3030. switch (bc->AOCDtype) {
  3031. case Fac_AOCDCurrency:
  3032. pbx_builtin_setvar_helper(ast, "AOCD_Type", "currency");
  3033. if (bc->AOCD.currency.chargeNotAvailable) {
  3034. pbx_builtin_setvar_helper(ast, "AOCD_ChargeAvailable", "no");
  3035. } else {
  3036. pbx_builtin_setvar_helper(ast, "AOCD_ChargeAvailable", "yes");
  3037. if (bc->AOCD.currency.freeOfCharge) {
  3038. pbx_builtin_setvar_helper(ast, "AOCD_FreeOfCharge", "yes");
  3039. } else {
  3040. pbx_builtin_setvar_helper(ast, "AOCD_FreeOfCharge", "no");
  3041. if (snprintf(buf, sizeof(buf), "%d %s", bc->AOCD.currency.currencyAmount * bc->AOCD.currency.multiplier, bc->AOCD.currency.currency) < sizeof(buf)) {
  3042. pbx_builtin_setvar_helper(ast, "AOCD_Amount", buf);
  3043. if (bc->AOCD.currency.billingId >= 0 && snprintf(buf, sizeof(buf), "%d", bc->AOCD.currency.billingId) < sizeof(buf)) {
  3044. pbx_builtin_setvar_helper(ast, "AOCD_BillingId", buf);
  3045. }
  3046. }
  3047. }
  3048. }
  3049. break;
  3050. case Fac_AOCDChargingUnit:
  3051. pbx_builtin_setvar_helper(ast, "AOCD_Type", "charging_unit");
  3052. if (bc->AOCD.chargingUnit.chargeNotAvailable) {
  3053. pbx_builtin_setvar_helper(ast, "AOCD_ChargeAvailable", "no");
  3054. } else {
  3055. pbx_builtin_setvar_helper(ast, "AOCD_ChargeAvailable", "yes");
  3056. if (bc->AOCD.chargingUnit.freeOfCharge) {
  3057. pbx_builtin_setvar_helper(ast, "AOCD_FreeOfCharge", "yes");
  3058. } else {
  3059. pbx_builtin_setvar_helper(ast, "AOCD_FreeOfCharge", "no");
  3060. if (snprintf(buf, sizeof(buf), "%d", bc->AOCD.chargingUnit.recordedUnits) < sizeof(buf)) {
  3061. pbx_builtin_setvar_helper(ast, "AOCD_RecordedUnits", buf);
  3062. if (bc->AOCD.chargingUnit.billingId >= 0 && snprintf(buf, sizeof(buf), "%d", bc->AOCD.chargingUnit.billingId) < sizeof(buf)) {
  3063. pbx_builtin_setvar_helper(ast, "AOCD_BillingId", buf);
  3064. }
  3065. }
  3066. }
  3067. }
  3068. break;
  3069. default:
  3070. break;
  3071. }
  3072. bc->AOCD_need_export = 0;
  3073. }
  3074. /*************** Helpers END *************/
  3075. static void sighandler(int sig)
  3076. {
  3077. }
  3078. static void *misdn_tasks_thread_func(void *data)
  3079. {
  3080. int wait;
  3081. struct sigaction sa;
  3082. sa.sa_handler = sighandler;
  3083. sa.sa_flags = SA_NODEFER;
  3084. sigemptyset(&sa.sa_mask);
  3085. sigaddset(&sa.sa_mask, SIGUSR1);
  3086. sigaction(SIGUSR1, &sa, NULL);
  3087. sem_post((sem_t *)data);
  3088. while (1) {
  3089. wait = ast_sched_wait(misdn_tasks);
  3090. if (wait < 0) {
  3091. wait = 8000;
  3092. }
  3093. if (poll(NULL, 0, wait) < 0) {
  3094. chan_misdn_log(4, 0, "Waking up misdn_tasks thread\n");
  3095. }
  3096. ast_sched_runq(misdn_tasks);
  3097. }
  3098. return NULL;
  3099. }
  3100. static void misdn_tasks_init(void)
  3101. {
  3102. sem_t blocker;
  3103. int i = 5;
  3104. if (sem_init(&blocker, 0, 0)) {
  3105. perror("chan_misdn: Failed to initialize semaphore!");
  3106. exit(1);
  3107. }
  3108. chan_misdn_log(4, 0, "Starting misdn_tasks thread\n");
  3109. misdn_tasks = ast_sched_context_create();
  3110. pthread_create(&misdn_tasks_thread, NULL, misdn_tasks_thread_func, &blocker);
  3111. while (sem_wait(&blocker) && --i) {
  3112. }
  3113. sem_destroy(&blocker);
  3114. }
  3115. static void misdn_tasks_destroy(void)
  3116. {
  3117. if (misdn_tasks) {
  3118. chan_misdn_log(4, 0, "Killing misdn_tasks thread\n");
  3119. if (pthread_cancel(misdn_tasks_thread) == 0) {
  3120. cb_log(4, 0, "Joining misdn_tasks thread\n");
  3121. pthread_join(misdn_tasks_thread, NULL);
  3122. }
  3123. ast_sched_context_destroy(misdn_tasks);
  3124. }
  3125. }
  3126. static inline void misdn_tasks_wakeup(void)
  3127. {
  3128. pthread_kill(misdn_tasks_thread, SIGUSR1);
  3129. }
  3130. static inline int _misdn_tasks_add_variable(int timeout, ast_sched_cb callback, const void *data, int variable)
  3131. {
  3132. int task_id;
  3133. if (!misdn_tasks) {
  3134. misdn_tasks_init();
  3135. }
  3136. task_id = ast_sched_add_variable(misdn_tasks, timeout, callback, data, variable);
  3137. misdn_tasks_wakeup();
  3138. return task_id;
  3139. }
  3140. static int misdn_tasks_add(int timeout, ast_sched_cb callback, const void *data)
  3141. {
  3142. return _misdn_tasks_add_variable(timeout, callback, data, 0);
  3143. }
  3144. static int misdn_tasks_add_variable(int timeout, ast_sched_cb callback, const void *data)
  3145. {
  3146. return _misdn_tasks_add_variable(timeout, callback, data, 1);
  3147. }
  3148. static void misdn_tasks_remove(int task_id)
  3149. {
  3150. AST_SCHED_DEL(misdn_tasks, task_id);
  3151. }
  3152. static int misdn_l1_task(const void *vdata)
  3153. {
  3154. const int *data = vdata;
  3155. misdn_lib_isdn_l1watcher(*data);
  3156. chan_misdn_log(5, *data, "L1watcher timeout\n");
  3157. return 1;
  3158. }
  3159. static int misdn_overlap_dial_task(const void *data)
  3160. {
  3161. struct timeval tv_end, tv_now;
  3162. int diff;
  3163. struct chan_list *ch = (struct chan_list *) data;
  3164. char *dad;
  3165. chan_misdn_log(4, ch->bc->port, "overlap dial task, chan_state: %d\n", ch->state);
  3166. if (ch->state != MISDN_WAITING4DIGS) {
  3167. ch->overlap_dial_task = -1;
  3168. return 0;
  3169. }
  3170. ast_mutex_lock(&ch->overlap_tv_lock);
  3171. tv_end = ch->overlap_tv;
  3172. ast_mutex_unlock(&ch->overlap_tv_lock);
  3173. tv_end.tv_sec += ch->overlap_dial;
  3174. tv_now = ast_tvnow();
  3175. diff = ast_tvdiff_ms(tv_end, tv_now);
  3176. if (100 < diff) {
  3177. return diff;
  3178. }
  3179. /* if we are 100ms near the timeout, we are satisfied.. */
  3180. stop_indicate(ch);
  3181. if (ast_strlen_zero(ch->bc->dialed.number)) {
  3182. dad = "s";
  3183. strcpy(ch->ast->exten, dad);
  3184. } else {
  3185. dad = ch->bc->dialed.number;
  3186. }
  3187. if (ast_exists_extension(ch->ast, ch->context, dad, 1, ch->bc->caller.number)) {
  3188. ch->state = MISDN_DIALING;
  3189. if (pbx_start_chan(ch) < 0) {
  3190. chan_misdn_log(-1, ch->bc->port, "ast_pbx_start returned < 0 in misdn_overlap_dial_task\n");
  3191. goto misdn_overlap_dial_task_disconnect;
  3192. }
  3193. } else {
  3194. misdn_overlap_dial_task_disconnect:
  3195. hanguptone_indicate(ch);
  3196. ch->bc->out_cause = AST_CAUSE_UNALLOCATED;
  3197. ch->state = MISDN_CLEANING;
  3198. misdn_lib_send_event(ch->bc, EVENT_DISCONNECT);
  3199. }
  3200. ch->overlap_dial_task = -1;
  3201. return 0;
  3202. }
  3203. static void send_digit_to_chan(struct chan_list *cl, char digit)
  3204. {
  3205. static const char * const dtmf_tones[] = {
  3206. /* *INDENT-OFF* */
  3207. "!941+1336/100,!0/100", /* 0 */
  3208. "!697+1209/100,!0/100", /* 1 */
  3209. "!697+1336/100,!0/100", /* 2 */
  3210. "!697+1477/100,!0/100", /* 3 */
  3211. "!770+1209/100,!0/100", /* 4 */
  3212. "!770+1336/100,!0/100", /* 5 */
  3213. "!770+1477/100,!0/100", /* 6 */
  3214. "!852+1209/100,!0/100", /* 7 */
  3215. "!852+1336/100,!0/100", /* 8 */
  3216. "!852+1477/100,!0/100", /* 9 */
  3217. "!697+1633/100,!0/100", /* A */
  3218. "!770+1633/100,!0/100", /* B */
  3219. "!852+1633/100,!0/100", /* C */
  3220. "!941+1633/100,!0/100", /* D */
  3221. "!941+1209/100,!0/100", /* * */
  3222. "!941+1477/100,!0/100", /* # */
  3223. /* *INDENT-ON* */
  3224. };
  3225. struct ast_channel *chan = cl->ast;
  3226. if (digit >= '0' && digit <='9') {
  3227. ast_playtones_start(chan, 0, dtmf_tones[digit - '0'], 0);
  3228. } else if (digit >= 'A' && digit <= 'D') {
  3229. ast_playtones_start(chan, 0, dtmf_tones[digit - 'A' + 10], 0);
  3230. } else if (digit == '*') {
  3231. ast_playtones_start(chan, 0, dtmf_tones[14], 0);
  3232. } else if (digit == '#') {
  3233. ast_playtones_start(chan, 0, dtmf_tones[15], 0);
  3234. } else {
  3235. /* not handled */
  3236. ast_debug(1, "Unable to handle DTMF tone '%c' for '%s'\n", digit, chan->name);
  3237. }
  3238. }
  3239. /*** CLI HANDLING ***/
  3240. static char *handle_cli_misdn_set_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3241. {
  3242. int level;
  3243. switch (cmd) {
  3244. case CLI_INIT:
  3245. e->command = "misdn set debug [on|off]";
  3246. e->usage =
  3247. "Usage: misdn set debug {on|off|<level>} [only] | [port <port> [only]]\n"
  3248. " Set the debug level of the mISDN channel.\n";
  3249. return NULL;
  3250. case CLI_GENERATE:
  3251. return complete_debug_port(a);
  3252. }
  3253. if (a->argc < 4 || a->argc > 7) {
  3254. return CLI_SHOWUSAGE;
  3255. }
  3256. if (!strcasecmp(a->argv[3], "on")) {
  3257. level = 1;
  3258. } else if (!strcasecmp(a->argv[3], "off")) {
  3259. level = 0;
  3260. } else if (isdigit(a->argv[3][0])) {
  3261. level = atoi(a->argv[3]);
  3262. } else {
  3263. return CLI_SHOWUSAGE;
  3264. }
  3265. switch (a->argc) {
  3266. case 4:
  3267. case 5:
  3268. {
  3269. int i;
  3270. int only = 0;
  3271. if (a->argc == 5) {
  3272. if (strncasecmp(a->argv[4], "only", strlen(a->argv[4]))) {
  3273. return CLI_SHOWUSAGE;
  3274. } else {
  3275. only = 1;
  3276. }
  3277. }
  3278. for (i = 0; i <= max_ports; i++) {
  3279. misdn_debug[i] = level;
  3280. misdn_debug_only[i] = only;
  3281. }
  3282. ast_cli(a->fd, "changing debug level for all ports to %d%s\n", misdn_debug[0], only ? " (only)" : "");
  3283. }
  3284. break;
  3285. case 6:
  3286. case 7:
  3287. {
  3288. int port;
  3289. if (strncasecmp(a->argv[4], "port", strlen(a->argv[4])))
  3290. return CLI_SHOWUSAGE;
  3291. port = atoi(a->argv[5]);
  3292. if (port <= 0 || port > max_ports) {
  3293. switch (max_ports) {
  3294. case 0:
  3295. ast_cli(a->fd, "port number not valid! no ports available so you won't get lucky with any number here...\n");
  3296. break;
  3297. case 1:
  3298. ast_cli(a->fd, "port number not valid! only port 1 is available.\n");
  3299. break;
  3300. default:
  3301. ast_cli(a->fd, "port number not valid! only ports 1 to %d are available.\n", max_ports);
  3302. }
  3303. return 0;
  3304. }
  3305. if (a->argc == 7) {
  3306. if (strncasecmp(a->argv[6], "only", strlen(a->argv[6]))) {
  3307. return CLI_SHOWUSAGE;
  3308. } else {
  3309. misdn_debug_only[port] = 1;
  3310. }
  3311. } else {
  3312. misdn_debug_only[port] = 0;
  3313. }
  3314. misdn_debug[port] = level;
  3315. ast_cli(a->fd, "changing debug level to %d%s for port %d\n", misdn_debug[port], misdn_debug_only[port] ? " (only)" : "", port);
  3316. }
  3317. }
  3318. return CLI_SUCCESS;
  3319. }
  3320. static char *handle_cli_misdn_set_crypt_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3321. {
  3322. switch (cmd) {
  3323. case CLI_INIT:
  3324. e->command = "misdn set crypt debug";
  3325. e->usage =
  3326. "Usage: misdn set crypt debug <level>\n"
  3327. " Set the crypt debug level of the mISDN channel. Level\n"
  3328. " must be 1 or 2.\n";
  3329. return NULL;
  3330. case CLI_GENERATE:
  3331. return NULL;
  3332. }
  3333. if (a->argc != 5) {
  3334. return CLI_SHOWUSAGE;
  3335. }
  3336. /* XXX Is this supposed to not do anything? XXX */
  3337. return CLI_SUCCESS;
  3338. }
  3339. static char *handle_cli_misdn_port_block(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3340. {
  3341. switch (cmd) {
  3342. case CLI_INIT:
  3343. e->command = "misdn port block";
  3344. e->usage =
  3345. "Usage: misdn port block <port>\n"
  3346. " Block the specified port by <port>.\n";
  3347. return NULL;
  3348. case CLI_GENERATE:
  3349. return NULL;
  3350. }
  3351. if (a->argc != 4) {
  3352. return CLI_SHOWUSAGE;
  3353. }
  3354. misdn_lib_port_block(atoi(a->argv[3]));
  3355. return CLI_SUCCESS;
  3356. }
  3357. static char *handle_cli_misdn_port_unblock(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3358. {
  3359. switch (cmd) {
  3360. case CLI_INIT:
  3361. e->command = "misdn port unblock";
  3362. e->usage =
  3363. "Usage: misdn port unblock <port>\n"
  3364. " Unblock the port specified by <port>.\n";
  3365. return NULL;
  3366. case CLI_GENERATE:
  3367. return NULL;
  3368. }
  3369. if (a->argc != 4) {
  3370. return CLI_SHOWUSAGE;
  3371. }
  3372. misdn_lib_port_unblock(atoi(a->argv[3]));
  3373. return CLI_SUCCESS;
  3374. }
  3375. static char *handle_cli_misdn_restart_port(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3376. {
  3377. switch (cmd) {
  3378. case CLI_INIT:
  3379. e->command = "misdn restart port";
  3380. e->usage =
  3381. "Usage: misdn restart port <port>\n"
  3382. " Restart the given port.\n";
  3383. return NULL;
  3384. case CLI_GENERATE:
  3385. return NULL;
  3386. }
  3387. if (a->argc != 4) {
  3388. return CLI_SHOWUSAGE;
  3389. }
  3390. misdn_lib_port_restart(atoi(a->argv[3]));
  3391. return CLI_SUCCESS;
  3392. }
  3393. static char *handle_cli_misdn_restart_pid(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3394. {
  3395. switch (cmd) {
  3396. case CLI_INIT:
  3397. e->command = "misdn restart pid";
  3398. e->usage =
  3399. "Usage: misdn restart pid <pid>\n"
  3400. " Restart the given pid\n";
  3401. return NULL;
  3402. case CLI_GENERATE:
  3403. return NULL;
  3404. }
  3405. if (a->argc != 4) {
  3406. return CLI_SHOWUSAGE;
  3407. }
  3408. misdn_lib_pid_restart(atoi(a->argv[3]));
  3409. return CLI_SUCCESS;
  3410. }
  3411. static char *handle_cli_misdn_port_up(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3412. {
  3413. switch (cmd) {
  3414. case CLI_INIT:
  3415. e->command = "misdn port up";
  3416. e->usage =
  3417. "Usage: misdn port up <port>\n"
  3418. " Try to establish L1 on the given port.\n";
  3419. return NULL;
  3420. case CLI_GENERATE:
  3421. return NULL;
  3422. }
  3423. if (a->argc != 4) {
  3424. return CLI_SHOWUSAGE;
  3425. }
  3426. misdn_lib_get_port_up(atoi(a->argv[3]));
  3427. return CLI_SUCCESS;
  3428. }
  3429. static char *handle_cli_misdn_port_down(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3430. {
  3431. switch (cmd) {
  3432. case CLI_INIT:
  3433. e->command = "misdn port down";
  3434. e->usage =
  3435. "Usage: misdn port down <port>\n"
  3436. " Try to deactivate the L1 on the given port.\n";
  3437. return NULL;
  3438. case CLI_GENERATE:
  3439. return NULL;
  3440. }
  3441. if (a->argc != 4) {
  3442. return CLI_SHOWUSAGE;
  3443. }
  3444. misdn_lib_get_port_down(atoi(a->argv[3]));
  3445. return CLI_SUCCESS;
  3446. }
  3447. static inline void show_config_description(int fd, enum misdn_cfg_elements elem)
  3448. {
  3449. char section[BUFFERSIZE];
  3450. char name[BUFFERSIZE];
  3451. char desc[BUFFERSIZE];
  3452. char def[BUFFERSIZE];
  3453. char tmp[BUFFERSIZE];
  3454. misdn_cfg_get_name(elem, tmp, sizeof(tmp));
  3455. term_color(name, tmp, COLOR_BRWHITE, 0, sizeof(tmp));
  3456. misdn_cfg_get_desc(elem, desc, sizeof(desc), def, sizeof(def));
  3457. if (elem < MISDN_CFG_LAST) {
  3458. term_color(section, "PORTS SECTION", COLOR_YELLOW, 0, sizeof(section));
  3459. } else {
  3460. term_color(section, "GENERAL SECTION", COLOR_YELLOW, 0, sizeof(section));
  3461. }
  3462. if (*def) {
  3463. ast_cli(fd, "[%s] %s (Default: %s)\n\t%s\n", section, name, def, desc);
  3464. } else {
  3465. ast_cli(fd, "[%s] %s\n\t%s\n", section, name, desc);
  3466. }
  3467. }
  3468. static char *handle_cli_misdn_show_config(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3469. {
  3470. char buffer[BUFFERSIZE];
  3471. enum misdn_cfg_elements elem;
  3472. int linebreak;
  3473. int onlyport = -1;
  3474. int ok = 0;
  3475. switch (cmd) {
  3476. case CLI_INIT:
  3477. e->command = "misdn show config";
  3478. e->usage =
  3479. "Usage: misdn show config [<port> | description <config element> | descriptions [general|ports]]\n"
  3480. " Use 0 for <port> to only print the general config.\n";
  3481. return NULL;
  3482. case CLI_GENERATE:
  3483. return complete_show_config(a);
  3484. }
  3485. if (a->argc >= 4) {
  3486. if (!strcmp(a->argv[3], "description")) {
  3487. if (a->argc == 5) {
  3488. enum misdn_cfg_elements elem = misdn_cfg_get_elem(a->argv[4]);
  3489. if (elem == MISDN_CFG_FIRST) {
  3490. ast_cli(a->fd, "Unknown element: %s\n", a->argv[4]);
  3491. } else {
  3492. show_config_description(a->fd, elem);
  3493. }
  3494. return CLI_SUCCESS;
  3495. }
  3496. return CLI_SHOWUSAGE;
  3497. } else if (!strcmp(a->argv[3], "descriptions")) {
  3498. if ((a->argc == 4) || ((a->argc == 5) && !strcmp(a->argv[4], "general"))) {
  3499. for (elem = MISDN_GEN_FIRST + 1; elem < MISDN_GEN_LAST; ++elem) {
  3500. show_config_description(a->fd, elem);
  3501. ast_cli(a->fd, "\n");
  3502. }
  3503. ok = 1;
  3504. }
  3505. if ((a->argc == 4) || ((a->argc == 5) && !strcmp(a->argv[4], "ports"))) {
  3506. for (elem = MISDN_CFG_FIRST + 1; elem < MISDN_CFG_LAST - 1 /* the ptp hack, remove the -1 when ptp is gone */; ++elem) {
  3507. show_config_description(a->fd, elem);
  3508. ast_cli(a->fd, "\n");
  3509. }
  3510. ok = 1;
  3511. }
  3512. return ok ? CLI_SUCCESS : CLI_SHOWUSAGE;
  3513. } else if (!sscanf(a->argv[3], "%5d", &onlyport) || onlyport < 0) {
  3514. ast_cli(a->fd, "Unknown option: %s\n", a->argv[3]);
  3515. return CLI_SHOWUSAGE;
  3516. }
  3517. }
  3518. if (a->argc == 3 || onlyport == 0) {
  3519. ast_cli(a->fd, "mISDN General-Config:\n");
  3520. for (elem = MISDN_GEN_FIRST + 1, linebreak = 1; elem < MISDN_GEN_LAST; elem++, linebreak++) {
  3521. misdn_cfg_get_config_string(0, elem, buffer, sizeof(buffer));
  3522. ast_cli(a->fd, "%-36s%s", buffer, !(linebreak % 2) ? "\n" : "");
  3523. }
  3524. ast_cli(a->fd, "\n");
  3525. }
  3526. if (onlyport < 0) {
  3527. int port = misdn_cfg_get_next_port(0);
  3528. for (; port > 0; port = misdn_cfg_get_next_port(port)) {
  3529. ast_cli(a->fd, "\n[PORT %d]\n", port);
  3530. for (elem = MISDN_CFG_FIRST + 1, linebreak = 1; elem < MISDN_CFG_LAST; elem++, linebreak++) {
  3531. misdn_cfg_get_config_string(port, elem, buffer, sizeof(buffer));
  3532. ast_cli(a->fd, "%-36s%s", buffer, !(linebreak % 2) ? "\n" : "");
  3533. }
  3534. ast_cli(a->fd, "\n");
  3535. }
  3536. }
  3537. if (onlyport > 0) {
  3538. if (misdn_cfg_is_port_valid(onlyport)) {
  3539. ast_cli(a->fd, "[PORT %d]\n", onlyport);
  3540. for (elem = MISDN_CFG_FIRST + 1, linebreak = 1; elem < MISDN_CFG_LAST; elem++, linebreak++) {
  3541. misdn_cfg_get_config_string(onlyport, elem, buffer, sizeof(buffer));
  3542. ast_cli(a->fd, "%-36s%s", buffer, !(linebreak % 2) ? "\n" : "");
  3543. }
  3544. ast_cli(a->fd, "\n");
  3545. } else {
  3546. ast_cli(a->fd, "Port %d is not active!\n", onlyport);
  3547. }
  3548. }
  3549. return CLI_SUCCESS;
  3550. }
  3551. struct state_struct {
  3552. enum misdn_chan_state state;
  3553. char txt[255];
  3554. };
  3555. static const struct state_struct state_array[] = {
  3556. /* *INDENT-OFF* */
  3557. { MISDN_NOTHING, "NOTHING" }, /* at beginning */
  3558. { MISDN_WAITING4DIGS, "WAITING4DIGS" }, /* when waiting for infos */
  3559. { MISDN_EXTCANTMATCH, "EXTCANTMATCH" }, /* when asterisk couldn't match our ext */
  3560. { MISDN_INCOMING_SETUP, "INCOMING SETUP" }, /* when pbx_start */
  3561. { MISDN_DIALING, "DIALING" }, /* when pbx_start */
  3562. { MISDN_PROGRESS, "PROGRESS" }, /* when pbx_start */
  3563. { MISDN_PROCEEDING, "PROCEEDING" }, /* when pbx_start */
  3564. { MISDN_CALLING, "CALLING" }, /* when misdn_call is called */
  3565. { MISDN_CALLING_ACKNOWLEDGE, "CALLING_ACKNOWLEDGE" }, /* when misdn_call is called */
  3566. { MISDN_ALERTING, "ALERTING" }, /* when Alerting */
  3567. { MISDN_BUSY, "BUSY" }, /* when BUSY */
  3568. { MISDN_CONNECTED, "CONNECTED" }, /* when connected */
  3569. { MISDN_DISCONNECTED, "DISCONNECTED" }, /* when connected */
  3570. { MISDN_CLEANING, "CLEANING" }, /* when hangup from * but we were connected before */
  3571. /* *INDENT-ON* */
  3572. };
  3573. static const char *misdn_get_ch_state(struct chan_list *p)
  3574. {
  3575. int i;
  3576. static char state[8];
  3577. if (!p) {
  3578. return NULL;
  3579. }
  3580. for (i = 0; i < ARRAY_LEN(state_array); i++) {
  3581. if (state_array[i].state == p->state) {
  3582. return state_array[i].txt;
  3583. }
  3584. }
  3585. snprintf(state, sizeof(state), "%d", p->state) ;
  3586. return state;
  3587. }
  3588. static void reload_config(void)
  3589. {
  3590. int i, cfg_debug;
  3591. if (!g_config_initialized) {
  3592. ast_log(LOG_WARNING, "chan_misdn is not initialized properly, still reloading ?\n");
  3593. return ;
  3594. }
  3595. free_robin_list();
  3596. misdn_cfg_reload();
  3597. misdn_cfg_update_ptp();
  3598. misdn_cfg_get(0, MISDN_GEN_TRACEFILE, global_tracefile, sizeof(global_tracefile));
  3599. misdn_cfg_get(0, MISDN_GEN_DEBUG, &cfg_debug, sizeof(cfg_debug));
  3600. for (i = 0; i <= max_ports; i++) {
  3601. misdn_debug[i] = cfg_debug;
  3602. misdn_debug_only[i] = 0;
  3603. }
  3604. }
  3605. static char *handle_cli_misdn_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3606. {
  3607. switch (cmd) {
  3608. case CLI_INIT:
  3609. e->command = "misdn reload";
  3610. e->usage =
  3611. "Usage: misdn reload\n"
  3612. " Reload internal mISDN config, read from the config\n"
  3613. " file.\n";
  3614. return NULL;
  3615. case CLI_GENERATE:
  3616. return NULL;
  3617. }
  3618. if (a->argc != 2) {
  3619. return CLI_SHOWUSAGE;
  3620. }
  3621. ast_cli(a->fd, "Reloading mISDN configuration\n");
  3622. reload_config();
  3623. return CLI_SUCCESS;
  3624. }
  3625. static void print_bc_info(int fd, struct chan_list *help, struct misdn_bchannel *bc)
  3626. {
  3627. struct ast_channel *ast = help->ast;
  3628. ast_cli(fd,
  3629. "* Pid:%d Port:%d Ch:%d Mode:%s Orig:%s dialed:%s\n"
  3630. " --> caller:\"%s\" <%s>\n"
  3631. " --> redirecting-from:\"%s\" <%s>\n"
  3632. " --> redirecting-to:\"%s\" <%s>\n"
  3633. " --> context:%s state:%s\n",
  3634. bc->pid,
  3635. bc->port,
  3636. bc->channel,
  3637. bc->nt ? "NT" : "TE",
  3638. help->originator == ORG_AST ? "*" : "I",
  3639. ast ? ast->exten : "",
  3640. (ast && ast->caller.id.name.valid && ast->caller.id.name.str)
  3641. ? ast->caller.id.name.str : "",
  3642. (ast && ast->caller.id.number.valid && ast->caller.id.number.str)
  3643. ? ast->caller.id.number.str : "",
  3644. bc->redirecting.from.name,
  3645. bc->redirecting.from.number,
  3646. bc->redirecting.to.name,
  3647. bc->redirecting.to.number,
  3648. ast ? ast->context : "",
  3649. misdn_get_ch_state(help));
  3650. if (misdn_debug[bc->port] > 0) {
  3651. ast_cli(fd,
  3652. " --> astname: %s\n"
  3653. " --> ch_l3id: %x\n"
  3654. " --> ch_addr: %x\n"
  3655. " --> bc_addr: %x\n"
  3656. " --> bc_l3id: %x\n"
  3657. " --> display: %s\n"
  3658. " --> activated: %d\n"
  3659. " --> state: %s\n"
  3660. " --> capability: %s\n"
  3661. #ifdef MISDN_1_2
  3662. " --> pipeline: %s\n"
  3663. #else
  3664. " --> echo_cancel: %d\n"
  3665. #endif
  3666. " --> notone : rx %d tx:%d\n"
  3667. " --> bc_hold: %d\n",
  3668. ast ? ast->name : "",
  3669. help->l3id,
  3670. help->addr,
  3671. bc->addr,
  3672. bc->l3_id,
  3673. bc->display,
  3674. bc->active,
  3675. bc_state2str(bc->bc_state),
  3676. bearer2str(bc->capability),
  3677. #ifdef MISDN_1_2
  3678. bc->pipeline,
  3679. #else
  3680. bc->ec_enable,
  3681. #endif
  3682. help->norxtone, help->notxtone,
  3683. bc->holded);
  3684. }
  3685. }
  3686. static char *handle_cli_misdn_show_channels(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3687. {
  3688. struct chan_list *help;
  3689. switch (cmd) {
  3690. case CLI_INIT:
  3691. e->command = "misdn show channels";
  3692. e->usage =
  3693. "Usage: misdn show channels\n"
  3694. " Show the internal mISDN channel list\n";
  3695. return NULL;
  3696. case CLI_GENERATE:
  3697. return NULL;
  3698. }
  3699. if (a->argc != 3) {
  3700. return CLI_SHOWUSAGE;
  3701. }
  3702. ast_cli(a->fd, "Channel List: %p\n", cl_te);
  3703. /*
  3704. * Walking the list and dumping the channel information could
  3705. * take awhile. With the list locked for the duration, the
  3706. * channel driver cannot process signaling messages. However,
  3707. * since this is a CLI command it should not be run very often.
  3708. */
  3709. ast_mutex_lock(&cl_te_lock);
  3710. for (help = cl_te; help; help = help->next) {
  3711. struct misdn_bchannel *bc = help->bc;
  3712. struct ast_channel *ast = help->ast;
  3713. if (!ast) {
  3714. if (!bc) {
  3715. ast_cli(a->fd, "chan_list obj. with l3id:%x has no bc and no ast Leg\n", help->l3id);
  3716. continue;
  3717. }
  3718. ast_cli(a->fd, "bc with pid:%d has no Ast Leg\n", bc->pid);
  3719. }
  3720. if (misdn_debug[0] > 2) {
  3721. ast_cli(a->fd, "Bc:%p Ast:%p\n", bc, ast);
  3722. }
  3723. if (bc) {
  3724. print_bc_info(a->fd, help, bc);
  3725. } else {
  3726. if (help->hold.state != MISDN_HOLD_IDLE) {
  3727. ast_cli(a->fd, "ITS A HELD CALL BC:\n");
  3728. ast_cli(a->fd, " --> l3_id: %x\n"
  3729. " --> dialed:%s\n"
  3730. " --> caller:\"%s\" <%s>\n"
  3731. " --> hold_port: %d\n"
  3732. " --> hold_channel: %d\n",
  3733. help->l3id,
  3734. ast->exten,
  3735. S_COR(ast->caller.id.name.valid, ast->caller.id.name.str, ""),
  3736. S_COR(ast->caller.id.number.valid, ast->caller.id.number.str, ""),
  3737. help->hold.port,
  3738. help->hold.channel
  3739. );
  3740. } else {
  3741. ast_cli(a->fd, "* Channel in unknown STATE !!! Exten:%s, Callerid:%s\n",
  3742. ast->exten,
  3743. S_COR(ast->caller.id.number.valid, ast->caller.id.number.str, ""));
  3744. }
  3745. }
  3746. }
  3747. ast_mutex_unlock(&cl_te_lock);
  3748. misdn_dump_chanlist();
  3749. return CLI_SUCCESS;
  3750. }
  3751. static char *handle_cli_misdn_show_channel(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3752. {
  3753. struct chan_list *help;
  3754. switch (cmd) {
  3755. case CLI_INIT:
  3756. e->command = "misdn show channel";
  3757. e->usage =
  3758. "Usage: misdn show channel <channel>\n"
  3759. " Show an internal mISDN channel\n.";
  3760. return NULL;
  3761. case CLI_GENERATE:
  3762. return complete_ch(a);
  3763. }
  3764. if (a->argc != 4) {
  3765. return CLI_SHOWUSAGE;
  3766. }
  3767. ast_mutex_lock(&cl_te_lock);
  3768. for (help = cl_te; help; help = help->next) {
  3769. struct misdn_bchannel *bc = help->bc;
  3770. struct ast_channel *ast = help->ast;
  3771. if (bc && ast) {
  3772. if (!strcasecmp(ast->name, a->argv[3])) {
  3773. print_bc_info(a->fd, help, bc);
  3774. break;
  3775. }
  3776. }
  3777. }
  3778. ast_mutex_unlock(&cl_te_lock);
  3779. return CLI_SUCCESS;
  3780. }
  3781. static char *handle_cli_misdn_set_tics(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3782. {
  3783. switch (cmd) {
  3784. case CLI_INIT:
  3785. e->command = "misdn set tics";
  3786. e->usage =
  3787. "Usage: misdn set tics <value>\n";
  3788. return NULL;
  3789. case CLI_GENERATE:
  3790. return NULL;
  3791. }
  3792. if (a->argc != 4) {
  3793. return CLI_SHOWUSAGE;
  3794. }
  3795. /* XXX Wow, this does... a whole lot of nothing... XXX */
  3796. MAXTICS = atoi(a->argv[3]);
  3797. return CLI_SUCCESS;
  3798. }
  3799. static char *handle_cli_misdn_show_stacks(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3800. {
  3801. int port;
  3802. switch (cmd) {
  3803. case CLI_INIT:
  3804. e->command = "misdn show stacks";
  3805. e->usage =
  3806. "Usage: misdn show stacks\n"
  3807. " Show internal mISDN stack_list.\n";
  3808. return NULL;
  3809. case CLI_GENERATE:
  3810. return NULL;
  3811. }
  3812. if (a->argc != 3) {
  3813. return CLI_SHOWUSAGE;
  3814. }
  3815. ast_cli(a->fd, "BEGIN STACK_LIST:\n");
  3816. for (port = misdn_cfg_get_next_port(0); port > 0;
  3817. port = misdn_cfg_get_next_port(port)) {
  3818. char buf[128];
  3819. get_show_stack_details(port, buf);
  3820. ast_cli(a->fd, " %s Debug:%d%s\n", buf, misdn_debug[port], misdn_debug_only[port] ? "(only)" : "");
  3821. }
  3822. return CLI_SUCCESS;
  3823. }
  3824. static char *handle_cli_misdn_show_ports_stats(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3825. {
  3826. int port;
  3827. switch (cmd) {
  3828. case CLI_INIT:
  3829. e->command = "misdn show ports stats";
  3830. e->usage =
  3831. "Usage: misdn show ports stats\n"
  3832. " Show mISDNs channel's call statistics per port.\n";
  3833. return NULL;
  3834. case CLI_GENERATE:
  3835. return NULL;
  3836. }
  3837. if (a->argc != 4) {
  3838. return CLI_SHOWUSAGE;
  3839. }
  3840. ast_cli(a->fd, "Port\tin_calls\tout_calls\n");
  3841. for (port = misdn_cfg_get_next_port(0); port > 0;
  3842. port = misdn_cfg_get_next_port(port)) {
  3843. ast_cli(a->fd, "%d\t%d\t\t%d\n", port, misdn_in_calls[port], misdn_out_calls[port]);
  3844. }
  3845. ast_cli(a->fd, "\n");
  3846. return CLI_SUCCESS;
  3847. }
  3848. static char *handle_cli_misdn_show_port(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3849. {
  3850. int port;
  3851. char buf[128];
  3852. switch (cmd) {
  3853. case CLI_INIT:
  3854. e->command = "misdn show port";
  3855. e->usage =
  3856. "Usage: misdn show port <port>\n"
  3857. " Show detailed information for given port.\n";
  3858. return NULL;
  3859. case CLI_GENERATE:
  3860. return NULL;
  3861. }
  3862. if (a->argc != 4) {
  3863. return CLI_SHOWUSAGE;
  3864. }
  3865. port = atoi(a->argv[3]);
  3866. ast_cli(a->fd, "BEGIN STACK_LIST:\n");
  3867. get_show_stack_details(port, buf);
  3868. ast_cli(a->fd, " %s Debug:%d%s\n", buf, misdn_debug[port], misdn_debug_only[port] ? "(only)" : "");
  3869. return CLI_SUCCESS;
  3870. }
  3871. #if defined(AST_MISDN_ENHANCEMENTS) && defined(CCBS_TEST_MESSAGES)
  3872. static const struct FacParm Fac_Msgs[] = {
  3873. /* *INDENT-OFF* */
  3874. [0].Function = Fac_ERROR,
  3875. [0].u.ERROR.invokeId = 8,
  3876. [0].u.ERROR.errorValue = FacError_CCBS_AlreadyAccepted,
  3877. [1].Function = Fac_RESULT,
  3878. [1].u.RESULT.InvokeID = 9,
  3879. [2].Function = Fac_REJECT,
  3880. [2].u.REJECT.Code = FacReject_Gen_BadlyStructuredComponent,
  3881. [3].Function = Fac_REJECT,
  3882. [3].u.REJECT.InvokeIDPresent = 1,
  3883. [3].u.REJECT.InvokeID = 10,
  3884. [3].u.REJECT.Code = FacReject_Inv_InitiatorReleasing,
  3885. [4].Function = Fac_REJECT,
  3886. [4].u.REJECT.InvokeIDPresent = 1,
  3887. [4].u.REJECT.InvokeID = 11,
  3888. [4].u.REJECT.Code = FacReject_Res_MistypedResult,
  3889. [5].Function = Fac_REJECT,
  3890. [5].u.REJECT.InvokeIDPresent = 1,
  3891. [5].u.REJECT.InvokeID = 12,
  3892. [5].u.REJECT.Code = FacReject_Err_ErrorResponseUnexpected,
  3893. [6].Function = Fac_StatusRequest,
  3894. [6].u.StatusRequest.InvokeID = 13,
  3895. [6].u.StatusRequest.ComponentType = FacComponent_Invoke,
  3896. [6].u.StatusRequest.Component.Invoke.Q931ie.Bc.Length = 2,
  3897. [6].u.StatusRequest.Component.Invoke.Q931ie.Bc.Contents = "AB",
  3898. [6].u.StatusRequest.Component.Invoke.Q931ie.Llc.Length = 3,
  3899. [6].u.StatusRequest.Component.Invoke.Q931ie.Llc.Contents = "CDE",
  3900. [6].u.StatusRequest.Component.Invoke.Q931ie.Hlc.Length = 4,
  3901. [6].u.StatusRequest.Component.Invoke.Q931ie.Hlc.Contents = "FGHI",
  3902. [6].u.StatusRequest.Component.Invoke.CompatibilityMode = 1,
  3903. [7].Function = Fac_StatusRequest,
  3904. [7].u.StatusRequest.InvokeID = 14,
  3905. [7].u.StatusRequest.ComponentType = FacComponent_Result,
  3906. [7].u.StatusRequest.Component.Result.Status = 2,
  3907. [8].Function = Fac_CallInfoRetain,
  3908. [8].u.CallInfoRetain.InvokeID = 15,
  3909. [8].u.CallInfoRetain.CallLinkageID = 115,
  3910. [9].Function = Fac_EraseCallLinkageID,
  3911. [9].u.EraseCallLinkageID.InvokeID = 16,
  3912. [9].u.EraseCallLinkageID.CallLinkageID = 105,
  3913. [10].Function = Fac_CCBSDeactivate,
  3914. [10].u.CCBSDeactivate.InvokeID = 17,
  3915. [10].u.CCBSDeactivate.ComponentType = FacComponent_Invoke,
  3916. [10].u.CCBSDeactivate.Component.Invoke.CCBSReference = 2,
  3917. [11].Function = Fac_CCBSDeactivate,
  3918. [11].u.CCBSDeactivate.InvokeID = 18,
  3919. [11].u.CCBSDeactivate.ComponentType = FacComponent_Result,
  3920. [12].Function = Fac_CCBSErase,
  3921. [12].u.CCBSErase.InvokeID = 19,
  3922. [12].u.CCBSErase.Q931ie.Bc.Length = 2,
  3923. [12].u.CCBSErase.Q931ie.Bc.Contents = "JK",
  3924. [12].u.CCBSErase.AddressOfB.Party.Type = 0,
  3925. [12].u.CCBSErase.AddressOfB.Party.LengthOfNumber = 5,
  3926. [12].u.CCBSErase.AddressOfB.Party.Number = "33403",
  3927. [12].u.CCBSErase.AddressOfB.Subaddress.Type = 0,
  3928. [12].u.CCBSErase.AddressOfB.Subaddress.Length = 4,
  3929. [12].u.CCBSErase.AddressOfB.Subaddress.u.UserSpecified.Information = "3748",
  3930. [12].u.CCBSErase.RecallMode = 1,
  3931. [12].u.CCBSErase.CCBSReference = 102,
  3932. [12].u.CCBSErase.Reason = 3,
  3933. [13].Function = Fac_CCBSErase,
  3934. [13].u.CCBSErase.InvokeID = 20,
  3935. [13].u.CCBSErase.Q931ie.Bc.Length = 2,
  3936. [13].u.CCBSErase.Q931ie.Bc.Contents = "JK",
  3937. [13].u.CCBSErase.AddressOfB.Party.Type = 1,
  3938. [13].u.CCBSErase.AddressOfB.Party.LengthOfNumber = 11,
  3939. [13].u.CCBSErase.AddressOfB.Party.TypeOfNumber = 1,
  3940. [13].u.CCBSErase.AddressOfB.Party.Number = "18003020102",
  3941. [13].u.CCBSErase.AddressOfB.Subaddress.Type = 0,
  3942. [13].u.CCBSErase.AddressOfB.Subaddress.Length = 4,
  3943. [13].u.CCBSErase.AddressOfB.Subaddress.u.UserSpecified.OddCountPresent = 1,
  3944. [13].u.CCBSErase.AddressOfB.Subaddress.u.UserSpecified.OddCount = 1,
  3945. [13].u.CCBSErase.AddressOfB.Subaddress.u.UserSpecified.Information = "3748",
  3946. [13].u.CCBSErase.RecallMode = 1,
  3947. [13].u.CCBSErase.CCBSReference = 102,
  3948. [13].u.CCBSErase.Reason = 3,
  3949. [14].Function = Fac_CCBSErase,
  3950. [14].u.CCBSErase.InvokeID = 21,
  3951. [14].u.CCBSErase.Q931ie.Bc.Length = 2,
  3952. [14].u.CCBSErase.Q931ie.Bc.Contents = "JK",
  3953. [14].u.CCBSErase.AddressOfB.Party.Type = 2,
  3954. [14].u.CCBSErase.AddressOfB.Party.LengthOfNumber = 4,
  3955. [14].u.CCBSErase.AddressOfB.Party.Number = "1803",
  3956. [14].u.CCBSErase.AddressOfB.Subaddress.Type = 1,
  3957. [14].u.CCBSErase.AddressOfB.Subaddress.Length = 4,
  3958. [14].u.CCBSErase.AddressOfB.Subaddress.u.Nsap = "6492",
  3959. [14].u.CCBSErase.RecallMode = 1,
  3960. [14].u.CCBSErase.CCBSReference = 102,
  3961. [14].u.CCBSErase.Reason = 3,
  3962. [15].Function = Fac_CCBSErase,
  3963. [15].u.CCBSErase.InvokeID = 22,
  3964. [15].u.CCBSErase.Q931ie.Bc.Length = 2,
  3965. [15].u.CCBSErase.Q931ie.Bc.Contents = "JK",
  3966. [15].u.CCBSErase.AddressOfB.Party.Type = 3,
  3967. [15].u.CCBSErase.AddressOfB.Party.LengthOfNumber = 4,
  3968. [15].u.CCBSErase.AddressOfB.Party.Number = "1803",
  3969. [15].u.CCBSErase.RecallMode = 1,
  3970. [15].u.CCBSErase.CCBSReference = 102,
  3971. [15].u.CCBSErase.Reason = 3,
  3972. [16].Function = Fac_CCBSErase,
  3973. [16].u.CCBSErase.InvokeID = 23,
  3974. [16].u.CCBSErase.Q931ie.Bc.Length = 2,
  3975. [16].u.CCBSErase.Q931ie.Bc.Contents = "JK",
  3976. [16].u.CCBSErase.AddressOfB.Party.Type = 4,
  3977. [16].u.CCBSErase.AddressOfB.Party.LengthOfNumber = 4,
  3978. [16].u.CCBSErase.AddressOfB.Party.Number = "1803",
  3979. [16].u.CCBSErase.RecallMode = 1,
  3980. [16].u.CCBSErase.CCBSReference = 102,
  3981. [16].u.CCBSErase.Reason = 3,
  3982. [17].Function = Fac_CCBSErase,
  3983. [17].u.CCBSErase.InvokeID = 24,
  3984. [17].u.CCBSErase.Q931ie.Bc.Length = 2,
  3985. [17].u.CCBSErase.Q931ie.Bc.Contents = "JK",
  3986. [17].u.CCBSErase.AddressOfB.Party.Type = 5,
  3987. [17].u.CCBSErase.AddressOfB.Party.LengthOfNumber = 11,
  3988. [17].u.CCBSErase.AddressOfB.Party.TypeOfNumber = 4,
  3989. [17].u.CCBSErase.AddressOfB.Party.Number = "18003020102",
  3990. [17].u.CCBSErase.RecallMode = 1,
  3991. [17].u.CCBSErase.CCBSReference = 102,
  3992. [17].u.CCBSErase.Reason = 3,
  3993. [18].Function = Fac_CCBSErase,
  3994. [18].u.CCBSErase.InvokeID = 25,
  3995. [18].u.CCBSErase.Q931ie.Bc.Length = 2,
  3996. [18].u.CCBSErase.Q931ie.Bc.Contents = "JK",
  3997. [18].u.CCBSErase.AddressOfB.Party.Type = 8,
  3998. [18].u.CCBSErase.AddressOfB.Party.LengthOfNumber = 4,
  3999. [18].u.CCBSErase.AddressOfB.Party.Number = "1803",
  4000. [18].u.CCBSErase.RecallMode = 1,
  4001. [18].u.CCBSErase.CCBSReference = 102,
  4002. [18].u.CCBSErase.Reason = 3,
  4003. [19].Function = Fac_CCBSRemoteUserFree,
  4004. [19].u.CCBSRemoteUserFree.InvokeID = 26,
  4005. [19].u.CCBSRemoteUserFree.Q931ie.Bc.Length = 2,
  4006. [19].u.CCBSRemoteUserFree.Q931ie.Bc.Contents = "JK",
  4007. [19].u.CCBSRemoteUserFree.AddressOfB.Party.Type = 8,
  4008. [19].u.CCBSRemoteUserFree.AddressOfB.Party.LengthOfNumber = 4,
  4009. [19].u.CCBSRemoteUserFree.AddressOfB.Party.Number = "1803",
  4010. [19].u.CCBSRemoteUserFree.RecallMode = 1,
  4011. [19].u.CCBSRemoteUserFree.CCBSReference = 102,
  4012. [20].Function = Fac_CCBSCall,
  4013. [20].u.CCBSCall.InvokeID = 27,
  4014. [20].u.CCBSCall.CCBSReference = 115,
  4015. [21].Function = Fac_CCBSStatusRequest,
  4016. [21].u.CCBSStatusRequest.InvokeID = 28,
  4017. [21].u.CCBSStatusRequest.ComponentType = FacComponent_Invoke,
  4018. [21].u.CCBSStatusRequest.Component.Invoke.Q931ie.Bc.Length = 2,
  4019. [21].u.CCBSStatusRequest.Component.Invoke.Q931ie.Bc.Contents = "JK",
  4020. [21].u.CCBSStatusRequest.Component.Invoke.RecallMode = 1,
  4021. [21].u.CCBSStatusRequest.Component.Invoke.CCBSReference = 102,
  4022. [22].Function = Fac_CCBSStatusRequest,
  4023. [22].u.CCBSStatusRequest.InvokeID = 29,
  4024. [22].u.CCBSStatusRequest.ComponentType = FacComponent_Result,
  4025. [22].u.CCBSStatusRequest.Component.Result.Free = 1,
  4026. [23].Function = Fac_CCBSBFree,
  4027. [23].u.CCBSBFree.InvokeID = 30,
  4028. [23].u.CCBSBFree.Q931ie.Bc.Length = 2,
  4029. [23].u.CCBSBFree.Q931ie.Bc.Contents = "JK",
  4030. [23].u.CCBSBFree.AddressOfB.Party.Type = 8,
  4031. [23].u.CCBSBFree.AddressOfB.Party.LengthOfNumber = 4,
  4032. [23].u.CCBSBFree.AddressOfB.Party.Number = "1803",
  4033. [23].u.CCBSBFree.RecallMode = 1,
  4034. [23].u.CCBSBFree.CCBSReference = 14,
  4035. [24].Function = Fac_CCBSStopAlerting,
  4036. [24].u.CCBSStopAlerting.InvokeID = 31,
  4037. [24].u.CCBSStopAlerting.CCBSReference = 37,
  4038. [25].Function = Fac_CCBSRequest,
  4039. [25].u.CCBSRequest.InvokeID = 32,
  4040. [25].u.CCBSRequest.ComponentType = FacComponent_Invoke,
  4041. [25].u.CCBSRequest.Component.Invoke.CallLinkageID = 57,
  4042. [26].Function = Fac_CCBSRequest,
  4043. [26].u.CCBSRequest.InvokeID = 33,
  4044. [26].u.CCBSRequest.ComponentType = FacComponent_Result,
  4045. [26].u.CCBSRequest.Component.Result.RecallMode = 1,
  4046. [26].u.CCBSRequest.Component.Result.CCBSReference = 102,
  4047. [27].Function = Fac_CCBSInterrogate,
  4048. [27].u.CCBSInterrogate.InvokeID = 34,
  4049. [27].u.CCBSInterrogate.ComponentType = FacComponent_Invoke,
  4050. [27].u.CCBSInterrogate.Component.Invoke.AParty.Type = 8,
  4051. [27].u.CCBSInterrogate.Component.Invoke.AParty.LengthOfNumber = 4,
  4052. [27].u.CCBSInterrogate.Component.Invoke.AParty.Number = "1803",
  4053. [27].u.CCBSInterrogate.Component.Invoke.CCBSReferencePresent = 1,
  4054. [27].u.CCBSInterrogate.Component.Invoke.CCBSReference = 76,
  4055. [28].Function = Fac_CCBSInterrogate,
  4056. [28].u.CCBSInterrogate.InvokeID = 35,
  4057. [28].u.CCBSInterrogate.ComponentType = FacComponent_Invoke,
  4058. [28].u.CCBSInterrogate.Component.Invoke.AParty.Type = 8,
  4059. [28].u.CCBSInterrogate.Component.Invoke.AParty.LengthOfNumber = 4,
  4060. [28].u.CCBSInterrogate.Component.Invoke.AParty.Number = "1803",
  4061. [29].Function = Fac_CCBSInterrogate,
  4062. [29].u.CCBSInterrogate.InvokeID = 36,
  4063. [29].u.CCBSInterrogate.ComponentType = FacComponent_Invoke,
  4064. [29].u.CCBSInterrogate.Component.Invoke.CCBSReferencePresent = 1,
  4065. [29].u.CCBSInterrogate.Component.Invoke.CCBSReference = 76,
  4066. [30].Function = Fac_CCBSInterrogate,
  4067. [30].u.CCBSInterrogate.InvokeID = 37,
  4068. [30].u.CCBSInterrogate.ComponentType = FacComponent_Invoke,
  4069. [31].Function = Fac_CCBSInterrogate,
  4070. [31].u.CCBSInterrogate.InvokeID = 38,
  4071. [31].u.CCBSInterrogate.ComponentType = FacComponent_Result,
  4072. [31].u.CCBSInterrogate.Component.Result.RecallMode = 1,
  4073. [32].Function = Fac_CCBSInterrogate,
  4074. [32].u.CCBSInterrogate.InvokeID = 39,
  4075. [32].u.CCBSInterrogate.ComponentType = FacComponent_Result,
  4076. [32].u.CCBSInterrogate.Component.Result.RecallMode = 1,
  4077. [32].u.CCBSInterrogate.Component.Result.NumRecords = 1,
  4078. [32].u.CCBSInterrogate.Component.Result.CallDetails[0].CCBSReference = 12,
  4079. [32].u.CCBSInterrogate.Component.Result.CallDetails[0].Q931ie.Bc.Length = 2,
  4080. [32].u.CCBSInterrogate.Component.Result.CallDetails[0].Q931ie.Bc.Contents = "JK",
  4081. [32].u.CCBSInterrogate.Component.Result.CallDetails[0].AddressOfB.Party.Type = 8,
  4082. [32].u.CCBSInterrogate.Component.Result.CallDetails[0].AddressOfB.Party.LengthOfNumber = 4,
  4083. [32].u.CCBSInterrogate.Component.Result.CallDetails[0].AddressOfB.Party.Number = "1803",
  4084. [32].u.CCBSInterrogate.Component.Result.CallDetails[0].SubaddressOfA.Type = 1,
  4085. [32].u.CCBSInterrogate.Component.Result.CallDetails[0].SubaddressOfA.Length = 4,
  4086. [32].u.CCBSInterrogate.Component.Result.CallDetails[0].SubaddressOfA.u.Nsap = "6492",
  4087. [33].Function = Fac_CCBSInterrogate,
  4088. [33].u.CCBSInterrogate.InvokeID = 40,
  4089. [33].u.CCBSInterrogate.ComponentType = FacComponent_Result,
  4090. [33].u.CCBSInterrogate.Component.Result.RecallMode = 1,
  4091. [33].u.CCBSInterrogate.Component.Result.NumRecords = 2,
  4092. [33].u.CCBSInterrogate.Component.Result.CallDetails[0].CCBSReference = 12,
  4093. [33].u.CCBSInterrogate.Component.Result.CallDetails[0].Q931ie.Bc.Length = 2,
  4094. [33].u.CCBSInterrogate.Component.Result.CallDetails[0].Q931ie.Bc.Contents = "JK",
  4095. [33].u.CCBSInterrogate.Component.Result.CallDetails[0].AddressOfB.Party.Type = 8,
  4096. [33].u.CCBSInterrogate.Component.Result.CallDetails[0].AddressOfB.Party.LengthOfNumber = 4,
  4097. [33].u.CCBSInterrogate.Component.Result.CallDetails[0].AddressOfB.Party.Number = "1803",
  4098. [33].u.CCBSInterrogate.Component.Result.CallDetails[1].CCBSReference = 102,
  4099. [33].u.CCBSInterrogate.Component.Result.CallDetails[1].Q931ie.Bc.Length = 2,
  4100. [33].u.CCBSInterrogate.Component.Result.CallDetails[1].Q931ie.Bc.Contents = "LM",
  4101. [33].u.CCBSInterrogate.Component.Result.CallDetails[1].AddressOfB.Party.Type = 8,
  4102. [33].u.CCBSInterrogate.Component.Result.CallDetails[1].AddressOfB.Party.LengthOfNumber = 4,
  4103. [33].u.CCBSInterrogate.Component.Result.CallDetails[1].AddressOfB.Party.Number = "6229",
  4104. [33].u.CCBSInterrogate.Component.Result.CallDetails[1].AddressOfB.Subaddress.Type = 1,
  4105. [33].u.CCBSInterrogate.Component.Result.CallDetails[1].AddressOfB.Subaddress.Length = 4,
  4106. [33].u.CCBSInterrogate.Component.Result.CallDetails[1].AddressOfB.Subaddress.u.Nsap = "8592",
  4107. [33].u.CCBSInterrogate.Component.Result.CallDetails[1].SubaddressOfA.Type = 1,
  4108. [33].u.CCBSInterrogate.Component.Result.CallDetails[1].SubaddressOfA.Length = 4,
  4109. [33].u.CCBSInterrogate.Component.Result.CallDetails[1].SubaddressOfA.u.Nsap = "6492",
  4110. [34].Function = Fac_CCNRRequest,
  4111. [34].u.CCNRRequest.InvokeID = 512,
  4112. [34].u.CCNRRequest.ComponentType = FacComponent_Invoke,
  4113. [34].u.CCNRRequest.Component.Invoke.CallLinkageID = 57,
  4114. [35].Function = Fac_CCNRRequest,
  4115. [35].u.CCNRRequest.InvokeID = 150,
  4116. [35].u.CCNRRequest.ComponentType = FacComponent_Result,
  4117. [35].u.CCNRRequest.Component.Result.RecallMode = 1,
  4118. [35].u.CCNRRequest.Component.Result.CCBSReference = 102,
  4119. [36].Function = Fac_CCNRInterrogate,
  4120. [36].u.CCNRInterrogate.InvokeID = -129,
  4121. [36].u.CCNRInterrogate.ComponentType = FacComponent_Invoke,
  4122. [37].Function = Fac_CCNRInterrogate,
  4123. [37].u.CCNRInterrogate.InvokeID = -3,
  4124. [37].u.CCNRInterrogate.ComponentType = FacComponent_Result,
  4125. [37].u.CCNRInterrogate.Component.Result.RecallMode = 1,
  4126. [38].Function = Fac_CCBS_T_Call,
  4127. [38].u.EctExecute.InvokeID = 41,
  4128. [39].Function = Fac_CCBS_T_Suspend,
  4129. [39].u.EctExecute.InvokeID = 42,
  4130. [40].Function = Fac_CCBS_T_Resume,
  4131. [40].u.EctExecute.InvokeID = 43,
  4132. [41].Function = Fac_CCBS_T_RemoteUserFree,
  4133. [41].u.EctExecute.InvokeID = 44,
  4134. [42].Function = Fac_CCBS_T_Available,
  4135. [42].u.EctExecute.InvokeID = 45,
  4136. [43].Function = Fac_CCBS_T_Request,
  4137. [43].u.CCBS_T_Request.InvokeID = 46,
  4138. [43].u.CCBS_T_Request.ComponentType = FacComponent_Invoke,
  4139. [43].u.CCBS_T_Request.Component.Invoke.Destination.Party.Type = 8,
  4140. [43].u.CCBS_T_Request.Component.Invoke.Destination.Party.LengthOfNumber = 4,
  4141. [43].u.CCBS_T_Request.Component.Invoke.Destination.Party.Number = "6229",
  4142. [43].u.CCBS_T_Request.Component.Invoke.Q931ie.Bc.Length = 2,
  4143. [43].u.CCBS_T_Request.Component.Invoke.Q931ie.Bc.Contents = "LM",
  4144. [43].u.CCBS_T_Request.Component.Invoke.RetentionSupported = 1,
  4145. [43].u.CCBS_T_Request.Component.Invoke.PresentationAllowedIndicatorPresent = 1,
  4146. [43].u.CCBS_T_Request.Component.Invoke.PresentationAllowedIndicator = 1,
  4147. [43].u.CCBS_T_Request.Component.Invoke.Originating.Party.Type = 8,
  4148. [43].u.CCBS_T_Request.Component.Invoke.Originating.Party.LengthOfNumber = 4,
  4149. [43].u.CCBS_T_Request.Component.Invoke.Originating.Party.Number = "9864",
  4150. [44].Function = Fac_CCBS_T_Request,
  4151. [44].u.CCBS_T_Request.InvokeID = 47,
  4152. [44].u.CCBS_T_Request.ComponentType = FacComponent_Invoke,
  4153. [44].u.CCBS_T_Request.Component.Invoke.Destination.Party.Type = 8,
  4154. [44].u.CCBS_T_Request.Component.Invoke.Destination.Party.LengthOfNumber = 4,
  4155. [44].u.CCBS_T_Request.Component.Invoke.Destination.Party.Number = "6229",
  4156. [44].u.CCBS_T_Request.Component.Invoke.Q931ie.Bc.Length = 2,
  4157. [44].u.CCBS_T_Request.Component.Invoke.Q931ie.Bc.Contents = "LM",
  4158. [44].u.CCBS_T_Request.Component.Invoke.PresentationAllowedIndicatorPresent = 1,
  4159. [44].u.CCBS_T_Request.Component.Invoke.PresentationAllowedIndicator = 1,
  4160. [44].u.CCBS_T_Request.Component.Invoke.Originating.Party.Type = 8,
  4161. [44].u.CCBS_T_Request.Component.Invoke.Originating.Party.LengthOfNumber = 4,
  4162. [44].u.CCBS_T_Request.Component.Invoke.Originating.Party.Number = "9864",
  4163. [45].Function = Fac_CCBS_T_Request,
  4164. [45].u.CCBS_T_Request.InvokeID = 48,
  4165. [45].u.CCBS_T_Request.ComponentType = FacComponent_Invoke,
  4166. [45].u.CCBS_T_Request.Component.Invoke.Destination.Party.Type = 8,
  4167. [45].u.CCBS_T_Request.Component.Invoke.Destination.Party.LengthOfNumber = 4,
  4168. [45].u.CCBS_T_Request.Component.Invoke.Destination.Party.Number = "6229",
  4169. [45].u.CCBS_T_Request.Component.Invoke.Q931ie.Bc.Length = 2,
  4170. [45].u.CCBS_T_Request.Component.Invoke.Q931ie.Bc.Contents = "LM",
  4171. [45].u.CCBS_T_Request.Component.Invoke.Originating.Party.Type = 8,
  4172. [45].u.CCBS_T_Request.Component.Invoke.Originating.Party.LengthOfNumber = 4,
  4173. [45].u.CCBS_T_Request.Component.Invoke.Originating.Party.Number = "9864",
  4174. [46].Function = Fac_CCBS_T_Request,
  4175. [46].u.CCBS_T_Request.InvokeID = 49,
  4176. [46].u.CCBS_T_Request.ComponentType = FacComponent_Invoke,
  4177. [46].u.CCBS_T_Request.Component.Invoke.Destination.Party.Type = 8,
  4178. [46].u.CCBS_T_Request.Component.Invoke.Destination.Party.LengthOfNumber = 4,
  4179. [46].u.CCBS_T_Request.Component.Invoke.Destination.Party.Number = "6229",
  4180. [46].u.CCBS_T_Request.Component.Invoke.Q931ie.Bc.Length = 2,
  4181. [46].u.CCBS_T_Request.Component.Invoke.Q931ie.Bc.Contents = "LM",
  4182. [46].u.CCBS_T_Request.Component.Invoke.PresentationAllowedIndicatorPresent = 1,
  4183. [46].u.CCBS_T_Request.Component.Invoke.PresentationAllowedIndicator = 1,
  4184. [47].Function = Fac_CCBS_T_Request,
  4185. [47].u.CCBS_T_Request.InvokeID = 50,
  4186. [47].u.CCBS_T_Request.ComponentType = FacComponent_Invoke,
  4187. [47].u.CCBS_T_Request.Component.Invoke.Destination.Party.Type = 8,
  4188. [47].u.CCBS_T_Request.Component.Invoke.Destination.Party.LengthOfNumber = 4,
  4189. [47].u.CCBS_T_Request.Component.Invoke.Destination.Party.Number = "6229",
  4190. [47].u.CCBS_T_Request.Component.Invoke.Q931ie.Bc.Length = 2,
  4191. [47].u.CCBS_T_Request.Component.Invoke.Q931ie.Bc.Contents = "LM",
  4192. [48].Function = Fac_CCBS_T_Request,
  4193. [48].u.CCBS_T_Request.InvokeID = 51,
  4194. [48].u.CCBS_T_Request.ComponentType = FacComponent_Result,
  4195. [48].u.CCBS_T_Request.Component.Result.RetentionSupported = 1,
  4196. [49].Function = Fac_CCNR_T_Request,
  4197. [49].u.CCNR_T_Request.InvokeID = 52,
  4198. [49].u.CCNR_T_Request.ComponentType = FacComponent_Invoke,
  4199. [49].u.CCNR_T_Request.Component.Invoke.Destination.Party.Type = 8,
  4200. [49].u.CCNR_T_Request.Component.Invoke.Destination.Party.LengthOfNumber = 4,
  4201. [49].u.CCNR_T_Request.Component.Invoke.Destination.Party.Number = "6229",
  4202. [49].u.CCNR_T_Request.Component.Invoke.Q931ie.Bc.Length = 2,
  4203. [49].u.CCNR_T_Request.Component.Invoke.Q931ie.Bc.Contents = "LM",
  4204. [50].Function = Fac_CCNR_T_Request,
  4205. [50].u.CCNR_T_Request.InvokeID = 53,
  4206. [50].u.CCNR_T_Request.ComponentType = FacComponent_Result,
  4207. [50].u.CCNR_T_Request.Component.Result.RetentionSupported = 1,
  4208. [51].Function = Fac_EctExecute,
  4209. [51].u.EctExecute.InvokeID = 54,
  4210. [52].Function = Fac_ExplicitEctExecute,
  4211. [52].u.ExplicitEctExecute.InvokeID = 55,
  4212. [52].u.ExplicitEctExecute.LinkID = 23,
  4213. [53].Function = Fac_RequestSubaddress,
  4214. [53].u.RequestSubaddress.InvokeID = 56,
  4215. [54].Function = Fac_SubaddressTransfer,
  4216. [54].u.SubaddressTransfer.InvokeID = 57,
  4217. [54].u.SubaddressTransfer.Subaddress.Type = 1,
  4218. [54].u.SubaddressTransfer.Subaddress.Length = 4,
  4219. [54].u.SubaddressTransfer.Subaddress.u.Nsap = "6492",
  4220. [55].Function = Fac_EctLinkIdRequest,
  4221. [55].u.EctLinkIdRequest.InvokeID = 58,
  4222. [55].u.EctLinkIdRequest.ComponentType = FacComponent_Invoke,
  4223. [56].Function = Fac_EctLinkIdRequest,
  4224. [56].u.EctLinkIdRequest.InvokeID = 59,
  4225. [56].u.EctLinkIdRequest.ComponentType = FacComponent_Result,
  4226. [56].u.EctLinkIdRequest.Component.Result.LinkID = 76,
  4227. [57].Function = Fac_EctInform,
  4228. [57].u.EctInform.InvokeID = 60,
  4229. [57].u.EctInform.Status = 1,
  4230. [57].u.EctInform.RedirectionPresent = 1,
  4231. [57].u.EctInform.Redirection.Type = 0,
  4232. [57].u.EctInform.Redirection.Unscreened.Type = 8,
  4233. [57].u.EctInform.Redirection.Unscreened.LengthOfNumber = 4,
  4234. [57].u.EctInform.Redirection.Unscreened.Number = "6229",
  4235. [58].Function = Fac_EctInform,
  4236. [58].u.EctInform.InvokeID = 61,
  4237. [58].u.EctInform.Status = 1,
  4238. [58].u.EctInform.RedirectionPresent = 1,
  4239. [58].u.EctInform.Redirection.Type = 1,
  4240. [59].Function = Fac_EctInform,
  4241. [59].u.EctInform.InvokeID = 62,
  4242. [59].u.EctInform.Status = 1,
  4243. [59].u.EctInform.RedirectionPresent = 1,
  4244. [59].u.EctInform.Redirection.Type = 2,
  4245. [60].Function = Fac_EctInform,
  4246. [60].u.EctInform.InvokeID = 63,
  4247. [60].u.EctInform.Status = 1,
  4248. [60].u.EctInform.RedirectionPresent = 1,
  4249. [60].u.EctInform.Redirection.Type = 3,
  4250. [60].u.EctInform.Redirection.Unscreened.Type = 8,
  4251. [60].u.EctInform.Redirection.Unscreened.LengthOfNumber = 4,
  4252. [60].u.EctInform.Redirection.Unscreened.Number = "3340",
  4253. [61].Function = Fac_EctInform,
  4254. [61].u.EctInform.InvokeID = 64,
  4255. [61].u.EctInform.Status = 1,
  4256. [61].u.EctInform.RedirectionPresent = 0,
  4257. [62].Function = Fac_EctLoopTest,
  4258. [62].u.EctLoopTest.InvokeID = 65,
  4259. [62].u.EctLoopTest.ComponentType = FacComponent_Invoke,
  4260. [62].u.EctLoopTest.Component.Invoke.CallTransferID = 7,
  4261. [63].Function = Fac_EctLoopTest,
  4262. [63].u.EctLoopTest.InvokeID = 66,
  4263. [63].u.EctLoopTest.ComponentType = FacComponent_Result,
  4264. [63].u.EctLoopTest.Component.Result.LoopResult = 2,
  4265. [64].Function = Fac_ActivationDiversion,
  4266. [64].u.ActivationDiversion.InvokeID = 67,
  4267. [64].u.ActivationDiversion.ComponentType = FacComponent_Invoke,
  4268. [64].u.ActivationDiversion.Component.Invoke.Procedure = 2,
  4269. [64].u.ActivationDiversion.Component.Invoke.BasicService = 3,
  4270. [64].u.ActivationDiversion.Component.Invoke.ForwardedTo.Party.Type = 4,
  4271. [64].u.ActivationDiversion.Component.Invoke.ForwardedTo.Party.LengthOfNumber = 4,
  4272. [64].u.ActivationDiversion.Component.Invoke.ForwardedTo.Party.Number = "1803",
  4273. [64].u.ActivationDiversion.Component.Invoke.ServedUser.Type = 4,
  4274. [64].u.ActivationDiversion.Component.Invoke.ServedUser.LengthOfNumber = 4,
  4275. [64].u.ActivationDiversion.Component.Invoke.ServedUser.Number = "5398",
  4276. [65].Function = Fac_ActivationDiversion,
  4277. [65].u.ActivationDiversion.InvokeID = 68,
  4278. [65].u.ActivationDiversion.ComponentType = FacComponent_Invoke,
  4279. [65].u.ActivationDiversion.Component.Invoke.Procedure = 1,
  4280. [65].u.ActivationDiversion.Component.Invoke.BasicService = 5,
  4281. [65].u.ActivationDiversion.Component.Invoke.ForwardedTo.Party.Type = 4,
  4282. [65].u.ActivationDiversion.Component.Invoke.ForwardedTo.Party.LengthOfNumber = 4,
  4283. [65].u.ActivationDiversion.Component.Invoke.ForwardedTo.Party.Number = "1803",
  4284. [66].Function = Fac_ActivationDiversion,
  4285. [66].u.ActivationDiversion.InvokeID = 69,
  4286. [66].u.ActivationDiversion.ComponentType = FacComponent_Result,
  4287. [67].Function = Fac_DeactivationDiversion,
  4288. [67].u.DeactivationDiversion.InvokeID = 70,
  4289. [67].u.DeactivationDiversion.ComponentType = FacComponent_Invoke,
  4290. [67].u.DeactivationDiversion.Component.Invoke.Procedure = 1,
  4291. [67].u.DeactivationDiversion.Component.Invoke.BasicService = 5,
  4292. [68].Function = Fac_DeactivationDiversion,
  4293. [68].u.DeactivationDiversion.InvokeID = 71,
  4294. [68].u.DeactivationDiversion.ComponentType = FacComponent_Result,
  4295. [69].Function = Fac_ActivationStatusNotificationDiv,
  4296. [69].u.ActivationStatusNotificationDiv.InvokeID = 72,
  4297. [69].u.ActivationStatusNotificationDiv.Procedure = 1,
  4298. [69].u.ActivationStatusNotificationDiv.BasicService = 5,
  4299. [69].u.ActivationStatusNotificationDiv.ForwardedTo.Party.Type = 4,
  4300. [69].u.ActivationStatusNotificationDiv.ForwardedTo.Party.LengthOfNumber = 4,
  4301. [69].u.ActivationStatusNotificationDiv.ForwardedTo.Party.Number = "1803",
  4302. [70].Function = Fac_DeactivationStatusNotificationDiv,
  4303. [70].u.DeactivationStatusNotificationDiv.InvokeID = 73,
  4304. [70].u.DeactivationStatusNotificationDiv.Procedure = 1,
  4305. [70].u.DeactivationStatusNotificationDiv.BasicService = 5,
  4306. [71].Function = Fac_InterrogationDiversion,
  4307. [71].u.InterrogationDiversion.InvokeID = 74,
  4308. [71].u.InterrogationDiversion.ComponentType = FacComponent_Invoke,
  4309. [71].u.InterrogationDiversion.Component.Invoke.Procedure = 1,
  4310. [71].u.InterrogationDiversion.Component.Invoke.BasicService = 5,
  4311. [72].Function = Fac_InterrogationDiversion,
  4312. [72].u.InterrogationDiversion.InvokeID = 75,
  4313. [72].u.InterrogationDiversion.ComponentType = FacComponent_Invoke,
  4314. [72].u.InterrogationDiversion.Component.Invoke.Procedure = 1,
  4315. [73].Function = Fac_InterrogationDiversion,
  4316. [73].u.InterrogationDiversion.InvokeID = 76,
  4317. [73].u.InterrogationDiversion.ComponentType = FacComponent_Result,
  4318. [73].u.InterrogationDiversion.Component.Result.NumRecords = 2,
  4319. [73].u.InterrogationDiversion.Component.Result.List[0].Procedure = 2,
  4320. [73].u.InterrogationDiversion.Component.Result.List[0].BasicService = 5,
  4321. [73].u.InterrogationDiversion.Component.Result.List[0].ForwardedTo.Party.Type = 4,
  4322. [73].u.InterrogationDiversion.Component.Result.List[0].ForwardedTo.Party.LengthOfNumber = 4,
  4323. [73].u.InterrogationDiversion.Component.Result.List[0].ForwardedTo.Party.Number = "1803",
  4324. [73].u.InterrogationDiversion.Component.Result.List[1].Procedure = 1,
  4325. [73].u.InterrogationDiversion.Component.Result.List[1].BasicService = 3,
  4326. [73].u.InterrogationDiversion.Component.Result.List[1].ForwardedTo.Party.Type = 4,
  4327. [73].u.InterrogationDiversion.Component.Result.List[1].ForwardedTo.Party.LengthOfNumber = 4,
  4328. [73].u.InterrogationDiversion.Component.Result.List[1].ForwardedTo.Party.Number = "1903",
  4329. [73].u.InterrogationDiversion.Component.Result.List[1].ServedUser.Type = 4,
  4330. [73].u.InterrogationDiversion.Component.Result.List[1].ServedUser.LengthOfNumber = 4,
  4331. [73].u.InterrogationDiversion.Component.Result.List[1].ServedUser.Number = "5398",
  4332. [74].Function = Fac_DiversionInformation,
  4333. [74].u.DiversionInformation.InvokeID = 77,
  4334. [74].u.DiversionInformation.DiversionReason = 3,
  4335. [74].u.DiversionInformation.BasicService = 5,
  4336. [74].u.DiversionInformation.ServedUserSubaddress.Type = 1,
  4337. [74].u.DiversionInformation.ServedUserSubaddress.Length = 4,
  4338. [74].u.DiversionInformation.ServedUserSubaddress.u.Nsap = "6492",
  4339. [74].u.DiversionInformation.CallingAddressPresent = 1,
  4340. [74].u.DiversionInformation.CallingAddress.Type = 0,
  4341. [74].u.DiversionInformation.CallingAddress.Address.ScreeningIndicator = 3,
  4342. [74].u.DiversionInformation.CallingAddress.Address.Party.Type = 4,
  4343. [74].u.DiversionInformation.CallingAddress.Address.Party.LengthOfNumber = 4,
  4344. [74].u.DiversionInformation.CallingAddress.Address.Party.Number = "1803",
  4345. [74].u.DiversionInformation.OriginalCalledPresent = 1,
  4346. [74].u.DiversionInformation.OriginalCalled.Type = 1,
  4347. [74].u.DiversionInformation.LastDivertingPresent = 1,
  4348. [74].u.DiversionInformation.LastDiverting.Type = 2,
  4349. [74].u.DiversionInformation.LastDivertingReasonPresent = 1,
  4350. [74].u.DiversionInformation.LastDivertingReason = 3,
  4351. [74].u.DiversionInformation.UserInfo.Length = 5,
  4352. [74].u.DiversionInformation.UserInfo.Contents = "79828",
  4353. [75].Function = Fac_DiversionInformation,
  4354. [75].u.DiversionInformation.InvokeID = 78,
  4355. [75].u.DiversionInformation.DiversionReason = 3,
  4356. [75].u.DiversionInformation.BasicService = 5,
  4357. [75].u.DiversionInformation.CallingAddressPresent = 1,
  4358. [75].u.DiversionInformation.CallingAddress.Type = 1,
  4359. [75].u.DiversionInformation.OriginalCalledPresent = 1,
  4360. [75].u.DiversionInformation.OriginalCalled.Type = 2,
  4361. [75].u.DiversionInformation.LastDivertingPresent = 1,
  4362. [75].u.DiversionInformation.LastDiverting.Type = 1,
  4363. [76].Function = Fac_DiversionInformation,
  4364. [76].u.DiversionInformation.InvokeID = 79,
  4365. [76].u.DiversionInformation.DiversionReason = 2,
  4366. [76].u.DiversionInformation.BasicService = 3,
  4367. [76].u.DiversionInformation.CallingAddressPresent = 1,
  4368. [76].u.DiversionInformation.CallingAddress.Type = 2,
  4369. [77].Function = Fac_DiversionInformation,
  4370. [77].u.DiversionInformation.InvokeID = 80,
  4371. [77].u.DiversionInformation.DiversionReason = 3,
  4372. [77].u.DiversionInformation.BasicService = 5,
  4373. [77].u.DiversionInformation.CallingAddressPresent = 1,
  4374. [77].u.DiversionInformation.CallingAddress.Type = 3,
  4375. [77].u.DiversionInformation.CallingAddress.Address.ScreeningIndicator = 2,
  4376. [77].u.DiversionInformation.CallingAddress.Address.Party.Type = 4,
  4377. [77].u.DiversionInformation.CallingAddress.Address.Party.LengthOfNumber = 4,
  4378. [77].u.DiversionInformation.CallingAddress.Address.Party.Number = "1803",
  4379. [78].Function = Fac_DiversionInformation,
  4380. [78].u.DiversionInformation.InvokeID = 81,
  4381. [78].u.DiversionInformation.DiversionReason = 2,
  4382. [78].u.DiversionInformation.BasicService = 4,
  4383. [78].u.DiversionInformation.UserInfo.Length = 5,
  4384. [78].u.DiversionInformation.UserInfo.Contents = "79828",
  4385. [79].Function = Fac_DiversionInformation,
  4386. [79].u.DiversionInformation.InvokeID = 82,
  4387. [79].u.DiversionInformation.DiversionReason = 2,
  4388. [79].u.DiversionInformation.BasicService = 4,
  4389. [80].Function = Fac_CallDeflection,
  4390. [80].u.CallDeflection.InvokeID = 83,
  4391. [80].u.CallDeflection.ComponentType = FacComponent_Invoke,
  4392. [80].u.CallDeflection.Component.Invoke.Deflection.Party.Type = 4,
  4393. [80].u.CallDeflection.Component.Invoke.Deflection.Party.LengthOfNumber = 4,
  4394. [80].u.CallDeflection.Component.Invoke.Deflection.Party.Number = "1803",
  4395. [80].u.CallDeflection.Component.Invoke.PresentationAllowedToDivertedToUserPresent = 1,
  4396. [80].u.CallDeflection.Component.Invoke.PresentationAllowedToDivertedToUser = 1,
  4397. [81].Function = Fac_CallDeflection,
  4398. [81].u.CallDeflection.InvokeID = 84,
  4399. [81].u.CallDeflection.ComponentType = FacComponent_Invoke,
  4400. [81].u.CallDeflection.Component.Invoke.Deflection.Party.Type = 4,
  4401. [81].u.CallDeflection.Component.Invoke.Deflection.Party.LengthOfNumber = 4,
  4402. [81].u.CallDeflection.Component.Invoke.Deflection.Party.Number = "1803",
  4403. [81].u.CallDeflection.Component.Invoke.PresentationAllowedToDivertedToUserPresent = 1,
  4404. [81].u.CallDeflection.Component.Invoke.PresentationAllowedToDivertedToUser = 0,
  4405. [82].Function = Fac_CallDeflection,
  4406. [82].u.CallDeflection.InvokeID = 85,
  4407. [82].u.CallDeflection.ComponentType = FacComponent_Invoke,
  4408. [82].u.CallDeflection.Component.Invoke.Deflection.Party.Type = 4,
  4409. [82].u.CallDeflection.Component.Invoke.Deflection.Party.LengthOfNumber = 4,
  4410. [82].u.CallDeflection.Component.Invoke.Deflection.Party.Number = "1803",
  4411. [83].Function = Fac_CallDeflection,
  4412. [83].u.CallDeflection.InvokeID = 86,
  4413. [83].u.CallDeflection.ComponentType = FacComponent_Result,
  4414. [84].Function = Fac_CallRerouteing,
  4415. [84].u.CallRerouteing.InvokeID = 87,
  4416. [84].u.CallRerouteing.ComponentType = FacComponent_Invoke,
  4417. [84].u.CallRerouteing.Component.Invoke.ReroutingReason = 3,
  4418. [84].u.CallRerouteing.Component.Invoke.ReroutingCounter = 2,
  4419. [84].u.CallRerouteing.Component.Invoke.CalledAddress.Party.Type = 4,
  4420. [84].u.CallRerouteing.Component.Invoke.CalledAddress.Party.LengthOfNumber = 4,
  4421. [84].u.CallRerouteing.Component.Invoke.CalledAddress.Party.Number = "1803",
  4422. [84].u.CallRerouteing.Component.Invoke.Q931ie.Bc.Length = 2,
  4423. [84].u.CallRerouteing.Component.Invoke.Q931ie.Bc.Contents = "RT",
  4424. [84].u.CallRerouteing.Component.Invoke.Q931ie.Hlc.Length = 3,
  4425. [84].u.CallRerouteing.Component.Invoke.Q931ie.Hlc.Contents = "RTG",
  4426. [84].u.CallRerouteing.Component.Invoke.Q931ie.Llc.Length = 2,
  4427. [84].u.CallRerouteing.Component.Invoke.Q931ie.Llc.Contents = "MY",
  4428. [84].u.CallRerouteing.Component.Invoke.Q931ie.UserInfo.Length = 5,
  4429. [84].u.CallRerouteing.Component.Invoke.Q931ie.UserInfo.Contents = "YEHAW",
  4430. [84].u.CallRerouteing.Component.Invoke.LastRerouting.Type = 1,
  4431. [84].u.CallRerouteing.Component.Invoke.SubscriptionOption = 2,
  4432. [84].u.CallRerouteing.Component.Invoke.CallingPartySubaddress.Type = 1,
  4433. [84].u.CallRerouteing.Component.Invoke.CallingPartySubaddress.Length = 4,
  4434. [84].u.CallRerouteing.Component.Invoke.CallingPartySubaddress.u.Nsap = "6492",
  4435. [85].Function = Fac_CallRerouteing,
  4436. [85].u.CallRerouteing.InvokeID = 88,
  4437. [85].u.CallRerouteing.ComponentType = FacComponent_Invoke,
  4438. [85].u.CallRerouteing.Component.Invoke.ReroutingReason = 3,
  4439. [85].u.CallRerouteing.Component.Invoke.ReroutingCounter = 2,
  4440. [85].u.CallRerouteing.Component.Invoke.CalledAddress.Party.Type = 4,
  4441. [85].u.CallRerouteing.Component.Invoke.CalledAddress.Party.LengthOfNumber = 4,
  4442. [85].u.CallRerouteing.Component.Invoke.CalledAddress.Party.Number = "1803",
  4443. [85].u.CallRerouteing.Component.Invoke.Q931ie.Bc.Length = 2,
  4444. [85].u.CallRerouteing.Component.Invoke.Q931ie.Bc.Contents = "RT",
  4445. [85].u.CallRerouteing.Component.Invoke.LastRerouting.Type = 1,
  4446. [85].u.CallRerouteing.Component.Invoke.SubscriptionOption = 2,
  4447. [86].Function = Fac_CallRerouteing,
  4448. [86].u.CallRerouteing.InvokeID = 89,
  4449. [86].u.CallRerouteing.ComponentType = FacComponent_Invoke,
  4450. [86].u.CallRerouteing.Component.Invoke.ReroutingReason = 3,
  4451. [86].u.CallRerouteing.Component.Invoke.ReroutingCounter = 2,
  4452. [86].u.CallRerouteing.Component.Invoke.CalledAddress.Party.Type = 4,
  4453. [86].u.CallRerouteing.Component.Invoke.CalledAddress.Party.LengthOfNumber = 4,
  4454. [86].u.CallRerouteing.Component.Invoke.CalledAddress.Party.Number = "1803",
  4455. [86].u.CallRerouteing.Component.Invoke.Q931ie.Bc.Length = 2,
  4456. [86].u.CallRerouteing.Component.Invoke.Q931ie.Bc.Contents = "RT",
  4457. [86].u.CallRerouteing.Component.Invoke.LastRerouting.Type = 2,
  4458. [87].Function = Fac_CallRerouteing,
  4459. [87].u.CallRerouteing.InvokeID = 90,
  4460. [87].u.CallRerouteing.ComponentType = FacComponent_Result,
  4461. [88].Function = Fac_InterrogateServedUserNumbers,
  4462. [88].u.InterrogateServedUserNumbers.InvokeID = 91,
  4463. [88].u.InterrogateServedUserNumbers.ComponentType = FacComponent_Invoke,
  4464. [89].Function = Fac_InterrogateServedUserNumbers,
  4465. [89].u.InterrogateServedUserNumbers.InvokeID = 92,
  4466. [89].u.InterrogateServedUserNumbers.ComponentType = FacComponent_Result,
  4467. [89].u.InterrogateServedUserNumbers.Component.Result.NumRecords = 2,
  4468. [89].u.InterrogateServedUserNumbers.Component.Result.List[0].Type = 4,
  4469. [89].u.InterrogateServedUserNumbers.Component.Result.List[0].LengthOfNumber = 4,
  4470. [89].u.InterrogateServedUserNumbers.Component.Result.List[0].Number = "1803",
  4471. [89].u.InterrogateServedUserNumbers.Component.Result.List[1].Type = 4,
  4472. [89].u.InterrogateServedUserNumbers.Component.Result.List[1].LengthOfNumber = 4,
  4473. [89].u.InterrogateServedUserNumbers.Component.Result.List[1].Number = "5786",
  4474. [90].Function = Fac_DivertingLegInformation1,
  4475. [90].u.DivertingLegInformation1.InvokeID = 93,
  4476. [90].u.DivertingLegInformation1.DiversionReason = 4,
  4477. [90].u.DivertingLegInformation1.SubscriptionOption = 1,
  4478. [90].u.DivertingLegInformation1.DivertedToPresent = 1,
  4479. [90].u.DivertingLegInformation1.DivertedTo.Type = 2,
  4480. [91].Function = Fac_DivertingLegInformation1,
  4481. [91].u.DivertingLegInformation1.InvokeID = 94,
  4482. [91].u.DivertingLegInformation1.DiversionReason = 4,
  4483. [91].u.DivertingLegInformation1.SubscriptionOption = 1,
  4484. [92].Function = Fac_DivertingLegInformation2,
  4485. [92].u.DivertingLegInformation2.InvokeID = 95,
  4486. [92].u.DivertingLegInformation2.DiversionCounter = 3,
  4487. [92].u.DivertingLegInformation2.DiversionReason = 2,
  4488. [92].u.DivertingLegInformation2.DivertingPresent = 1,
  4489. [92].u.DivertingLegInformation2.Diverting.Type = 2,
  4490. [92].u.DivertingLegInformation2.OriginalCalledPresent = 1,
  4491. [92].u.DivertingLegInformation2.OriginalCalled.Type = 1,
  4492. [93].Function = Fac_DivertingLegInformation2,
  4493. [93].u.DivertingLegInformation2.InvokeID = 96,
  4494. [93].u.DivertingLegInformation2.DiversionCounter = 3,
  4495. [93].u.DivertingLegInformation2.DiversionReason = 2,
  4496. [93].u.DivertingLegInformation2.OriginalCalledPresent = 1,
  4497. [93].u.DivertingLegInformation2.OriginalCalled.Type = 1,
  4498. [94].Function = Fac_DivertingLegInformation2,
  4499. [94].u.DivertingLegInformation2.InvokeID = 97,
  4500. [94].u.DivertingLegInformation2.DiversionCounter = 1,
  4501. [94].u.DivertingLegInformation2.DiversionReason = 2,
  4502. [95].Function = Fac_DivertingLegInformation3,
  4503. [95].u.DivertingLegInformation3.InvokeID = 98,
  4504. [95].u.DivertingLegInformation3.PresentationAllowedIndicator = 1,
  4505. /* *INDENT-ON* */
  4506. };
  4507. #endif /* defined(AST_MISDN_ENHANCEMENTS) && defined(CCBS_TEST_MESSAGES) */
  4508. static char *handle_cli_misdn_send_facility(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  4509. {
  4510. const char *channame;
  4511. const char *nr;
  4512. struct chan_list *tmp;
  4513. int port;
  4514. const char *served_nr;
  4515. struct misdn_bchannel dummy, *bc=&dummy;
  4516. unsigned max_len;
  4517. switch (cmd) {
  4518. case CLI_INIT:
  4519. e->command = "misdn send facility";
  4520. e->usage = "Usage: misdn send facility <type> <channel|port> \"<args>\" \n"
  4521. "\t type is one of:\n"
  4522. "\t - calldeflect\n"
  4523. #if defined(AST_MISDN_ENHANCEMENTS)
  4524. "\t - callrerouting\n"
  4525. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  4526. "\t - CFActivate\n"
  4527. "\t - CFDeactivate\n";
  4528. return NULL;
  4529. case CLI_GENERATE:
  4530. return complete_ch(a);
  4531. }
  4532. if (a->argc < 5) {
  4533. return CLI_SHOWUSAGE;
  4534. }
  4535. if (strstr(a->argv[3], "calldeflect")) {
  4536. if (a->argc < 6) {
  4537. ast_verbose("calldeflect requires 1 arg: ToNumber\n\n");
  4538. return 0;
  4539. }
  4540. channame = a->argv[4];
  4541. nr = a->argv[5];
  4542. ast_verbose("Sending Calldeflection (%s) to %s\n", nr, channame);
  4543. tmp = get_chan_by_ast_name(channame);
  4544. if (!tmp) {
  4545. ast_verbose("Sending CD with nr %s to %s failed: Channel does not exist.\n", nr, channame);
  4546. return 0;
  4547. }
  4548. ao2_lock(tmp);
  4549. #if defined(AST_MISDN_ENHANCEMENTS)
  4550. max_len = sizeof(tmp->bc->fac_out.u.CallDeflection.Component.Invoke.Deflection.Party.Number) - 1;
  4551. if (max_len < strlen(nr)) {
  4552. ast_verbose("Sending CD with nr %s to %s failed: Number too long (up to %u digits are allowed).\n",
  4553. nr, channame, max_len);
  4554. ao2_unlock(tmp);
  4555. chan_list_unref(tmp, "Number too long");
  4556. return 0;
  4557. }
  4558. tmp->bc->fac_out.Function = Fac_CallDeflection;
  4559. tmp->bc->fac_out.u.CallDeflection.InvokeID = ++misdn_invoke_id;
  4560. tmp->bc->fac_out.u.CallDeflection.ComponentType = FacComponent_Invoke;
  4561. tmp->bc->fac_out.u.CallDeflection.Component.Invoke.PresentationAllowedToDivertedToUserPresent = 1;
  4562. tmp->bc->fac_out.u.CallDeflection.Component.Invoke.PresentationAllowedToDivertedToUser = 0;
  4563. tmp->bc->fac_out.u.CallDeflection.Component.Invoke.Deflection.Party.Type = 0;/* unknown */
  4564. tmp->bc->fac_out.u.CallDeflection.Component.Invoke.Deflection.Party.LengthOfNumber = strlen(nr);
  4565. strcpy((char *) tmp->bc->fac_out.u.CallDeflection.Component.Invoke.Deflection.Party.Number, nr);
  4566. tmp->bc->fac_out.u.CallDeflection.Component.Invoke.Deflection.Subaddress.Length = 0;
  4567. #else /* !defined(AST_MISDN_ENHANCEMENTS) */
  4568. max_len = sizeof(tmp->bc->fac_out.u.CDeflection.DeflectedToNumber) - 1;
  4569. if (max_len < strlen(nr)) {
  4570. ast_verbose("Sending CD with nr %s to %s failed: Number too long (up to %u digits are allowed).\n",
  4571. nr, channame, max_len);
  4572. ao2_unlock(tmp);
  4573. chan_list_unref(tmp, "Number too long");
  4574. return 0;
  4575. }
  4576. tmp->bc->fac_out.Function = Fac_CD;
  4577. tmp->bc->fac_out.u.CDeflection.PresentationAllowed = 0;
  4578. //tmp->bc->fac_out.u.CDeflection.DeflectedToSubaddress[0] = 0;
  4579. strcpy((char *) tmp->bc->fac_out.u.CDeflection.DeflectedToNumber, nr);
  4580. #endif /* !defined(AST_MISDN_ENHANCEMENTS) */
  4581. /* Send message */
  4582. print_facility(&tmp->bc->fac_out, tmp->bc);
  4583. ao2_unlock(tmp);
  4584. misdn_lib_send_event(tmp->bc, EVENT_FACILITY);
  4585. chan_list_unref(tmp, "Send facility complete");
  4586. #if defined(AST_MISDN_ENHANCEMENTS)
  4587. } else if (strstr(a->argv[3], "callrerouteing") || strstr(a->argv[3], "callrerouting")) {
  4588. if (a->argc < 6) {
  4589. ast_verbose("callrerouting requires 1 arg: ToNumber\n\n");
  4590. return 0;
  4591. }
  4592. channame = a->argv[4];
  4593. nr = a->argv[5];
  4594. ast_verbose("Sending Callrerouting (%s) to %s\n", nr, channame);
  4595. tmp = get_chan_by_ast_name(channame);
  4596. if (!tmp) {
  4597. ast_verbose("Sending Call Rerouting with nr %s to %s failed: Channel does not exist.\n", nr, channame);
  4598. return 0;
  4599. }
  4600. ao2_lock(tmp);
  4601. max_len = sizeof(tmp->bc->fac_out.u.CallRerouteing.Component.Invoke.CalledAddress.Party.Number) - 1;
  4602. if (max_len < strlen(nr)) {
  4603. ast_verbose("Sending Call Rerouting with nr %s to %s failed: Number too long (up to %u digits are allowed).\n",
  4604. nr, channame, max_len);
  4605. ao2_unlock(tmp);
  4606. chan_list_unref(tmp, "Number too long");
  4607. return 0;
  4608. }
  4609. tmp->bc->fac_out.Function = Fac_CallRerouteing;
  4610. tmp->bc->fac_out.u.CallRerouteing.InvokeID = ++misdn_invoke_id;
  4611. tmp->bc->fac_out.u.CallRerouteing.ComponentType = FacComponent_Invoke;
  4612. tmp->bc->fac_out.u.CallRerouteing.Component.Invoke.ReroutingReason = 0;/* unknown */
  4613. tmp->bc->fac_out.u.CallRerouteing.Component.Invoke.ReroutingCounter = 1;
  4614. tmp->bc->fac_out.u.CallRerouteing.Component.Invoke.CalledAddress.Party.Type = 0;/* unknown */
  4615. tmp->bc->fac_out.u.CallRerouteing.Component.Invoke.CalledAddress.Party.LengthOfNumber = strlen(nr);
  4616. strcpy((char *) tmp->bc->fac_out.u.CallRerouteing.Component.Invoke.CalledAddress.Party.Number, nr);
  4617. tmp->bc->fac_out.u.CallRerouteing.Component.Invoke.CalledAddress.Subaddress.Length = 0;
  4618. tmp->bc->fac_out.u.CallRerouteing.Component.Invoke.CallingPartySubaddress.Length = 0;
  4619. /* 0x90 0x90 0xa3 3.1 kHz audio, circuit mode, 64kbit/sec, level1/a-Law */
  4620. tmp->bc->fac_out.u.CallRerouteing.Component.Invoke.Q931ie.Bc.Length = 3;
  4621. tmp->bc->fac_out.u.CallRerouteing.Component.Invoke.Q931ie.Bc.Contents[0] = 0x90;
  4622. tmp->bc->fac_out.u.CallRerouteing.Component.Invoke.Q931ie.Bc.Contents[1] = 0x90;
  4623. tmp->bc->fac_out.u.CallRerouteing.Component.Invoke.Q931ie.Bc.Contents[2] = 0xa3;
  4624. tmp->bc->fac_out.u.CallRerouteing.Component.Invoke.Q931ie.Hlc.Length = 0;
  4625. tmp->bc->fac_out.u.CallRerouteing.Component.Invoke.Q931ie.Llc.Length = 0;
  4626. tmp->bc->fac_out.u.CallRerouteing.Component.Invoke.Q931ie.UserInfo.Length = 0;
  4627. tmp->bc->fac_out.u.CallRerouteing.Component.Invoke.LastRerouting.Type = 1;/* presentationRestricted */
  4628. tmp->bc->fac_out.u.CallRerouteing.Component.Invoke.SubscriptionOption = 0;/* no notification to caller */
  4629. /* Send message */
  4630. print_facility(&tmp->bc->fac_out, tmp->bc);
  4631. ao2_unlock(tmp);
  4632. misdn_lib_send_event(tmp->bc, EVENT_FACILITY);
  4633. chan_list_unref(tmp, "Send facility complete");
  4634. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  4635. } else if (strstr(a->argv[3], "CFActivate")) {
  4636. if (a->argc < 7) {
  4637. ast_verbose("CFActivate requires 2 args: 1.FromNumber, 2.ToNumber\n\n");
  4638. return 0;
  4639. }
  4640. port = atoi(a->argv[4]);
  4641. served_nr = a->argv[5];
  4642. nr = a->argv[6];
  4643. misdn_make_dummy(bc, port, 0, misdn_lib_port_is_nt(port), 0);
  4644. ast_verbose("Sending CFActivate Port:(%d) FromNr. (%s) to Nr. (%s)\n", port, served_nr, nr);
  4645. #if defined(AST_MISDN_ENHANCEMENTS)
  4646. bc->fac_out.Function = Fac_ActivationDiversion;
  4647. bc->fac_out.u.ActivationDiversion.InvokeID = ++misdn_invoke_id;
  4648. bc->fac_out.u.ActivationDiversion.ComponentType = FacComponent_Invoke;
  4649. bc->fac_out.u.ActivationDiversion.Component.Invoke.BasicService = 0;/* allServices */
  4650. bc->fac_out.u.ActivationDiversion.Component.Invoke.Procedure = 0;/* cfu (Call Forward Unconditional) */
  4651. ast_copy_string((char *) bc->fac_out.u.ActivationDiversion.Component.Invoke.ServedUser.Number,
  4652. served_nr, sizeof(bc->fac_out.u.ActivationDiversion.Component.Invoke.ServedUser.Number));
  4653. bc->fac_out.u.ActivationDiversion.Component.Invoke.ServedUser.LengthOfNumber =
  4654. strlen((char *) bc->fac_out.u.ActivationDiversion.Component.Invoke.ServedUser.Number);
  4655. bc->fac_out.u.ActivationDiversion.Component.Invoke.ServedUser.Type = 0;/* unknown */
  4656. ast_copy_string((char *) bc->fac_out.u.ActivationDiversion.Component.Invoke.ForwardedTo.Party.Number,
  4657. nr, sizeof(bc->fac_out.u.ActivationDiversion.Component.Invoke.ForwardedTo.Party.Number));
  4658. bc->fac_out.u.ActivationDiversion.Component.Invoke.ForwardedTo.Party.LengthOfNumber =
  4659. strlen((char *) bc->fac_out.u.ActivationDiversion.Component.Invoke.ForwardedTo.Party.Number);
  4660. bc->fac_out.u.ActivationDiversion.Component.Invoke.ForwardedTo.Party.Type = 0;/* unknown */
  4661. bc->fac_out.u.ActivationDiversion.Component.Invoke.ForwardedTo.Subaddress.Length = 0;
  4662. #else /* !defined(AST_MISDN_ENHANCEMENTS) */
  4663. bc->fac_out.Function = Fac_CFActivate;
  4664. bc->fac_out.u.CFActivate.BasicService = 0; /* All Services */
  4665. bc->fac_out.u.CFActivate.Procedure = 0; /* Unconditional */
  4666. ast_copy_string((char *) bc->fac_out.u.CFActivate.ServedUserNumber, served_nr, sizeof(bc->fac_out.u.CFActivate.ServedUserNumber));
  4667. ast_copy_string((char *) bc->fac_out.u.CFActivate.ForwardedToNumber, nr, sizeof(bc->fac_out.u.CFActivate.ForwardedToNumber));
  4668. #endif /* !defined(AST_MISDN_ENHANCEMENTS) */
  4669. /* Send message */
  4670. print_facility(&bc->fac_out, bc);
  4671. misdn_lib_send_event(bc, EVENT_FACILITY);
  4672. } else if (strstr(a->argv[3], "CFDeactivate")) {
  4673. if (a->argc < 6) {
  4674. ast_verbose("CFDeactivate requires 1 arg: FromNumber\n\n");
  4675. return 0;
  4676. }
  4677. port = atoi(a->argv[4]);
  4678. served_nr = a->argv[5];
  4679. misdn_make_dummy(bc, port, 0, misdn_lib_port_is_nt(port), 0);
  4680. ast_verbose("Sending CFDeactivate Port:(%d) FromNr. (%s)\n", port, served_nr);
  4681. #if defined(AST_MISDN_ENHANCEMENTS)
  4682. bc->fac_out.Function = Fac_DeactivationDiversion;
  4683. bc->fac_out.u.DeactivationDiversion.InvokeID = ++misdn_invoke_id;
  4684. bc->fac_out.u.DeactivationDiversion.ComponentType = FacComponent_Invoke;
  4685. bc->fac_out.u.DeactivationDiversion.Component.Invoke.BasicService = 0;/* allServices */
  4686. bc->fac_out.u.DeactivationDiversion.Component.Invoke.Procedure = 0;/* cfu (Call Forward Unconditional) */
  4687. ast_copy_string((char *) bc->fac_out.u.DeactivationDiversion.Component.Invoke.ServedUser.Number,
  4688. served_nr, sizeof(bc->fac_out.u.DeactivationDiversion.Component.Invoke.ServedUser.Number));
  4689. bc->fac_out.u.DeactivationDiversion.Component.Invoke.ServedUser.LengthOfNumber =
  4690. strlen((char *) bc->fac_out.u.DeactivationDiversion.Component.Invoke.ServedUser.Number);
  4691. bc->fac_out.u.DeactivationDiversion.Component.Invoke.ServedUser.Type = 0;/* unknown */
  4692. #else /* !defined(AST_MISDN_ENHANCEMENTS) */
  4693. bc->fac_out.Function = Fac_CFDeactivate;
  4694. bc->fac_out.u.CFDeactivate.BasicService = 0; /* All Services */
  4695. bc->fac_out.u.CFDeactivate.Procedure = 0; /* Unconditional */
  4696. ast_copy_string((char *) bc->fac_out.u.CFActivate.ServedUserNumber, served_nr, sizeof(bc->fac_out.u.CFActivate.ServedUserNumber));
  4697. #endif /* !defined(AST_MISDN_ENHANCEMENTS) */
  4698. /* Send message */
  4699. print_facility(&bc->fac_out, bc);
  4700. misdn_lib_send_event(bc, EVENT_FACILITY);
  4701. #if defined(AST_MISDN_ENHANCEMENTS) && defined(CCBS_TEST_MESSAGES)
  4702. } else if (strstr(a->argv[3], "test")) {
  4703. int msg_number;
  4704. if (a->argc < 5) {
  4705. ast_verbose("test (<port> [<msg#>]) | (<channel-name> <msg#>)\n\n");
  4706. return 0;
  4707. }
  4708. port = atoi(a->argv[4]);
  4709. channame = a->argv[4];
  4710. tmp = get_chan_by_ast_name(channame);
  4711. if (tmp) {
  4712. /* We are going to send this FACILITY message out on an existing connection */
  4713. msg_number = atoi(a->argv[5]);
  4714. if (msg_number < ARRAY_LEN(Fac_Msgs)) {
  4715. ao2_lock(tmp);
  4716. tmp->bc->fac_out = Fac_Msgs[msg_number];
  4717. /* Send message */
  4718. print_facility(&tmp->bc->fac_out, tmp->bc);
  4719. ao2_unlock(tmp);
  4720. misdn_lib_send_event(tmp->bc, EVENT_FACILITY);
  4721. } else {
  4722. ast_verbose("test <channel-name> <msg#>\n\n");
  4723. }
  4724. chan_list_unref(tmp, "Facility test done");
  4725. } else if (a->argc < 6) {
  4726. for (msg_number = 0; msg_number < ARRAY_LEN(Fac_Msgs); ++msg_number) {
  4727. misdn_make_dummy(bc, port, 0, misdn_lib_port_is_nt(port), 0);
  4728. bc->fac_out = Fac_Msgs[msg_number];
  4729. /* Send message */
  4730. print_facility(&bc->fac_out, bc);
  4731. misdn_lib_send_event(bc, EVENT_FACILITY);
  4732. sleep(1);
  4733. }
  4734. } else {
  4735. msg_number = atoi(a->argv[5]);
  4736. if (msg_number < ARRAY_LEN(Fac_Msgs)) {
  4737. misdn_make_dummy(bc, port, 0, misdn_lib_port_is_nt(port), 0);
  4738. bc->fac_out = Fac_Msgs[msg_number];
  4739. /* Send message */
  4740. print_facility(&bc->fac_out, bc);
  4741. misdn_lib_send_event(bc, EVENT_FACILITY);
  4742. } else {
  4743. ast_verbose("test <port> [<msg#>]\n\n");
  4744. }
  4745. }
  4746. } else if (strstr(a->argv[3], "register")) {
  4747. if (a->argc < 5) {
  4748. ast_verbose("register <port>\n\n");
  4749. return 0;
  4750. }
  4751. port = atoi(a->argv[4]);
  4752. bc = misdn_lib_get_register_bc(port);
  4753. if (!bc) {
  4754. ast_verbose("Could not allocate REGISTER bc struct\n\n");
  4755. return 0;
  4756. }
  4757. bc->fac_out = Fac_Msgs[45];
  4758. /* Send message */
  4759. print_facility(&bc->fac_out, bc);
  4760. misdn_lib_send_event(bc, EVENT_REGISTER);
  4761. #endif /* defined(AST_MISDN_ENHANCEMENTS) && defined(CCBS_TEST_MESSAGES) */
  4762. }
  4763. return CLI_SUCCESS;
  4764. }
  4765. static char *handle_cli_misdn_send_restart(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  4766. {
  4767. int port;
  4768. int channel;
  4769. switch (cmd) {
  4770. case CLI_INIT:
  4771. e->command = "misdn send restart";
  4772. e->usage =
  4773. "Usage: misdn send restart [port [channel]]\n"
  4774. " Send a restart for every bchannel on the given port.\n";
  4775. return NULL;
  4776. case CLI_GENERATE:
  4777. return NULL;
  4778. }
  4779. if (a->argc < 4 || a->argc > 5) {
  4780. return CLI_SHOWUSAGE;
  4781. }
  4782. port = atoi(a->argv[3]);
  4783. if (a->argc == 5) {
  4784. channel = atoi(a->argv[4]);
  4785. misdn_lib_send_restart(port, channel);
  4786. } else {
  4787. misdn_lib_send_restart(port, -1);
  4788. }
  4789. return CLI_SUCCESS;
  4790. }
  4791. static char *handle_cli_misdn_send_digit(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  4792. {
  4793. const char *channame;
  4794. const char *msg;
  4795. struct chan_list *tmp;
  4796. int i, msglen;
  4797. switch (cmd) {
  4798. case CLI_INIT:
  4799. e->command = "misdn send digit";
  4800. e->usage =
  4801. "Usage: misdn send digit <channel> \"<msg>\" \n"
  4802. " Send <digit> to <channel> as DTMF Tone\n"
  4803. " when channel is a mISDN channel\n";
  4804. return NULL;
  4805. case CLI_GENERATE:
  4806. return complete_ch(a);
  4807. }
  4808. if (a->argc != 5) {
  4809. return CLI_SHOWUSAGE;
  4810. }
  4811. channame = a->argv[3];
  4812. msg = a->argv[4];
  4813. msglen = strlen(msg);
  4814. ast_cli(a->fd, "Sending %s to %s\n", msg, channame);
  4815. tmp = get_chan_by_ast_name(channame);
  4816. if (!tmp) {
  4817. ast_cli(a->fd, "Sending %s to %s failed Channel does not exist\n", msg, channame);
  4818. return CLI_SUCCESS;
  4819. }
  4820. #if 1
  4821. for (i = 0; i < msglen; i++) {
  4822. if (!tmp->ast) {
  4823. break;
  4824. }
  4825. ast_cli(a->fd, "Sending: %c\n", msg[i]);
  4826. send_digit_to_chan(tmp, msg[i]);
  4827. /* res = ast_safe_sleep(tmp->ast, 250); */
  4828. usleep(250000);
  4829. /* res = ast_waitfor(tmp->ast,100); */
  4830. }
  4831. #else
  4832. if (tmp->ast) {
  4833. ast_dtmf_stream(tmp->ast, NULL, msg, 250);
  4834. }
  4835. #endif
  4836. chan_list_unref(tmp, "Digit(s) sent");
  4837. return CLI_SUCCESS;
  4838. }
  4839. static char *handle_cli_misdn_toggle_echocancel(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  4840. {
  4841. const char *channame;
  4842. struct chan_list *tmp;
  4843. switch (cmd) {
  4844. case CLI_INIT:
  4845. e->command = "misdn toggle echocancel";
  4846. e->usage =
  4847. "Usage: misdn toggle echocancel <channel>\n"
  4848. " Toggle EchoCancel on mISDN Channel.\n";
  4849. return NULL;
  4850. case CLI_GENERATE:
  4851. return complete_ch(a);
  4852. }
  4853. if (a->argc != 4) {
  4854. return CLI_SHOWUSAGE;
  4855. }
  4856. channame = a->argv[3];
  4857. ast_cli(a->fd, "Toggling EchoCancel on %s\n", channame);
  4858. tmp = get_chan_by_ast_name(channame);
  4859. if (!tmp) {
  4860. ast_cli(a->fd, "Toggling EchoCancel %s failed Channel does not exist\n", channame);
  4861. return CLI_SUCCESS;
  4862. }
  4863. tmp->toggle_ec = tmp->toggle_ec ? 0 : 1;
  4864. if (tmp->toggle_ec) {
  4865. #ifdef MISDN_1_2
  4866. update_pipeline_config(tmp->bc);
  4867. #else
  4868. update_ec_config(tmp->bc);
  4869. #endif
  4870. manager_ec_enable(tmp->bc);
  4871. } else {
  4872. manager_ec_disable(tmp->bc);
  4873. }
  4874. chan_list_unref(tmp, "Done toggling echo cancel");
  4875. return CLI_SUCCESS;
  4876. }
  4877. static char *handle_cli_misdn_send_display(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  4878. {
  4879. const char *channame;
  4880. const char *msg;
  4881. struct chan_list *tmp;
  4882. switch (cmd) {
  4883. case CLI_INIT:
  4884. e->command = "misdn send display";
  4885. e->usage =
  4886. "Usage: misdn send display <channel> \"<msg>\" \n"
  4887. " Send <msg> to <channel> as Display Message\n"
  4888. " when channel is a mISDN channel\n";
  4889. return NULL;
  4890. case CLI_GENERATE:
  4891. return complete_ch(a);
  4892. }
  4893. if (a->argc != 5) {
  4894. return CLI_SHOWUSAGE;
  4895. }
  4896. channame = a->argv[3];
  4897. msg = a->argv[4];
  4898. ast_cli(a->fd, "Sending %s to %s\n", msg, channame);
  4899. tmp = get_chan_by_ast_name(channame);
  4900. if (tmp && tmp->bc) {
  4901. ast_copy_string(tmp->bc->display, msg, sizeof(tmp->bc->display));
  4902. misdn_lib_send_event(tmp->bc, EVENT_INFORMATION);
  4903. chan_list_unref(tmp, "Done sending display");
  4904. } else {
  4905. if (tmp) {
  4906. chan_list_unref(tmp, "Display failed");
  4907. }
  4908. ast_cli(a->fd, "No such channel %s\n", channame);
  4909. return CLI_SUCCESS;
  4910. }
  4911. return CLI_SUCCESS;
  4912. }
  4913. static char *complete_ch(struct ast_cli_args *a)
  4914. {
  4915. return ast_complete_channels(a->line, a->word, a->pos, a->n, 3);
  4916. }
  4917. static char *complete_debug_port(struct ast_cli_args *a)
  4918. {
  4919. if (a->n) {
  4920. return NULL;
  4921. }
  4922. switch (a->pos) {
  4923. case 4:
  4924. if (a->word[0] == 'p') {
  4925. return ast_strdup("port");
  4926. } else if (a->word[0] == 'o') {
  4927. return ast_strdup("only");
  4928. }
  4929. break;
  4930. case 6:
  4931. if (a->word[0] == 'o') {
  4932. return ast_strdup("only");
  4933. }
  4934. break;
  4935. }
  4936. return NULL;
  4937. }
  4938. static char *complete_show_config(struct ast_cli_args *a)
  4939. {
  4940. char buffer[BUFFERSIZE];
  4941. enum misdn_cfg_elements elem;
  4942. int wordlen = strlen(a->word);
  4943. int which = 0;
  4944. int port = 0;
  4945. switch (a->pos) {
  4946. case 3:
  4947. if ((!strncmp(a->word, "description", wordlen)) && (++which > a->n)) {
  4948. return ast_strdup("description");
  4949. }
  4950. if ((!strncmp(a->word, "descriptions", wordlen)) && (++which > a->n)) {
  4951. return ast_strdup("descriptions");
  4952. }
  4953. if ((!strncmp(a->word, "0", wordlen)) && (++which > a->n)) {
  4954. return ast_strdup("0");
  4955. }
  4956. while ((port = misdn_cfg_get_next_port(port)) != -1) {
  4957. snprintf(buffer, sizeof(buffer), "%d", port);
  4958. if ((!strncmp(a->word, buffer, wordlen)) && (++which > a->n)) {
  4959. return ast_strdup(buffer);
  4960. }
  4961. }
  4962. break;
  4963. case 4:
  4964. if (strstr(a->line, "description ")) {
  4965. for (elem = MISDN_CFG_FIRST + 1; elem < MISDN_GEN_LAST; ++elem) {
  4966. if ((elem == MISDN_CFG_LAST) || (elem == MISDN_GEN_FIRST)) {
  4967. continue;
  4968. }
  4969. misdn_cfg_get_name(elem, buffer, sizeof(buffer));
  4970. if (!wordlen || !strncmp(a->word, buffer, wordlen)) {
  4971. if (++which > a->n) {
  4972. return ast_strdup(buffer);
  4973. }
  4974. }
  4975. }
  4976. } else if (strstr(a->line, "descriptions ")) {
  4977. if ((!wordlen || !strncmp(a->word, "general", wordlen)) && (++which > a->n)) {
  4978. return ast_strdup("general");
  4979. }
  4980. if ((!wordlen || !strncmp(a->word, "ports", wordlen)) && (++which > a->n)) {
  4981. return ast_strdup("ports");
  4982. }
  4983. }
  4984. break;
  4985. }
  4986. return NULL;
  4987. }
  4988. static struct ast_cli_entry chan_misdn_clis[] = {
  4989. /* *INDENT-OFF* */
  4990. AST_CLI_DEFINE(handle_cli_misdn_port_block, "Block the given port"),
  4991. AST_CLI_DEFINE(handle_cli_misdn_port_down, "Try to deactivate the L1 on the given port"),
  4992. AST_CLI_DEFINE(handle_cli_misdn_port_unblock, "Unblock the given port"),
  4993. AST_CLI_DEFINE(handle_cli_misdn_port_up, "Try to establish L1 on the given port"),
  4994. AST_CLI_DEFINE(handle_cli_misdn_reload, "Reload internal mISDN config, read from the config file"),
  4995. AST_CLI_DEFINE(handle_cli_misdn_restart_pid, "Restart the given pid"),
  4996. AST_CLI_DEFINE(handle_cli_misdn_restart_port, "Restart the given port"),
  4997. AST_CLI_DEFINE(handle_cli_misdn_show_channel, "Show an internal mISDN channel"),
  4998. AST_CLI_DEFINE(handle_cli_misdn_show_channels, "Show the internal mISDN channel list"),
  4999. AST_CLI_DEFINE(handle_cli_misdn_show_config, "Show internal mISDN config, read from the config file"),
  5000. AST_CLI_DEFINE(handle_cli_misdn_show_port, "Show detailed information for given port"),
  5001. AST_CLI_DEFINE(handle_cli_misdn_show_ports_stats, "Show mISDNs channel's call statistics per port"),
  5002. AST_CLI_DEFINE(handle_cli_misdn_show_stacks, "Show internal mISDN stack_list"),
  5003. AST_CLI_DEFINE(handle_cli_misdn_send_facility, "Sends a Facility Message to the mISDN Channel"),
  5004. AST_CLI_DEFINE(handle_cli_misdn_send_digit, "Send DTMF digit to mISDN Channel"),
  5005. AST_CLI_DEFINE(handle_cli_misdn_send_display, "Send Text to mISDN Channel"),
  5006. AST_CLI_DEFINE(handle_cli_misdn_send_restart, "Send a restart for every bchannel on the given port"),
  5007. AST_CLI_DEFINE(handle_cli_misdn_set_crypt_debug, "Set CryptDebuglevel of chan_misdn, at the moment, level={1,2}"),
  5008. AST_CLI_DEFINE(handle_cli_misdn_set_debug, "Set Debuglevel of chan_misdn"),
  5009. AST_CLI_DEFINE(handle_cli_misdn_set_tics, "???"),
  5010. AST_CLI_DEFINE(handle_cli_misdn_toggle_echocancel, "Toggle EchoCancel on mISDN Channel"),
  5011. /* *INDENT-ON* */
  5012. };
  5013. /*! \brief Updates caller ID information from config */
  5014. static void update_config(struct chan_list *ch)
  5015. {
  5016. struct ast_channel *ast;
  5017. struct misdn_bchannel *bc;
  5018. int port;
  5019. int hdlc = 0;
  5020. int pres;
  5021. int screen;
  5022. if (!ch) {
  5023. ast_log(LOG_WARNING, "Cannot configure without chanlist\n");
  5024. return;
  5025. }
  5026. ast = ch->ast;
  5027. bc = ch->bc;
  5028. if (! ast || ! bc) {
  5029. ast_log(LOG_WARNING, "Cannot configure without ast || bc\n");
  5030. return;
  5031. }
  5032. port = bc->port;
  5033. chan_misdn_log(7, port, "update_config: Getting Config\n");
  5034. misdn_cfg_get(port, MISDN_CFG_HDLC, &hdlc, sizeof(int));
  5035. if (hdlc) {
  5036. switch (bc->capability) {
  5037. case INFO_CAPABILITY_DIGITAL_UNRESTRICTED:
  5038. case INFO_CAPABILITY_DIGITAL_RESTRICTED:
  5039. chan_misdn_log(1, bc->port, " --> CONF HDLC\n");
  5040. bc->hdlc = 1;
  5041. break;
  5042. }
  5043. }
  5044. misdn_cfg_get(port, MISDN_CFG_PRES, &pres, sizeof(pres));
  5045. misdn_cfg_get(port, MISDN_CFG_SCREEN, &screen, sizeof(screen));
  5046. chan_misdn_log(2, port, " --> pres: %d screen: %d\n", pres, screen);
  5047. if (pres < 0 || screen < 0) {
  5048. chan_misdn_log(2, port, " --> pres: %x\n", ast->connected.id.number.presentation);
  5049. bc->caller.presentation = ast_to_misdn_pres(ast->connected.id.number.presentation);
  5050. chan_misdn_log(2, port, " --> PRES: %s(%d)\n", misdn_to_str_pres(bc->caller.presentation), bc->caller.presentation);
  5051. bc->caller.screening = ast_to_misdn_screen(ast->connected.id.number.presentation);
  5052. chan_misdn_log(2, port, " --> SCREEN: %s(%d)\n", misdn_to_str_screen(bc->caller.screening), bc->caller.screening);
  5053. } else {
  5054. bc->caller.screening = screen;
  5055. bc->caller.presentation = pres;
  5056. }
  5057. }
  5058. static void config_jitterbuffer(struct chan_list *ch)
  5059. {
  5060. struct misdn_bchannel *bc = ch->bc;
  5061. int len = ch->jb_len;
  5062. int threshold = ch->jb_upper_threshold;
  5063. chan_misdn_log(5, bc->port, "config_jb: Called\n");
  5064. if (!len) {
  5065. chan_misdn_log(1, bc->port, "config_jb: Deactivating Jitterbuffer\n");
  5066. bc->nojitter = 1;
  5067. } else {
  5068. if (len <= 100 || len > 8000) {
  5069. chan_misdn_log(0, bc->port, "config_jb: Jitterbuffer out of Bounds, setting to 1000\n");
  5070. len = 1000;
  5071. }
  5072. if (threshold > len) {
  5073. chan_misdn_log(0, bc->port, "config_jb: Jitterbuffer Threshold > Jitterbuffer setting to Jitterbuffer -1\n");
  5074. }
  5075. if (ch->jb) {
  5076. cb_log(0, bc->port, "config_jb: We've got a Jitterbuffer Already on this port.\n");
  5077. misdn_jb_destroy(ch->jb);
  5078. ch->jb = NULL;
  5079. }
  5080. ch->jb = misdn_jb_init(len, threshold);
  5081. if (!ch->jb) {
  5082. bc->nojitter = 1;
  5083. }
  5084. }
  5085. }
  5086. void debug_numtype(int port, int numtype, char *type)
  5087. {
  5088. switch (numtype) {
  5089. case NUMTYPE_UNKNOWN:
  5090. chan_misdn_log(2, port, " --> %s: Unknown\n", type);
  5091. break;
  5092. case NUMTYPE_INTERNATIONAL:
  5093. chan_misdn_log(2, port, " --> %s: International\n", type);
  5094. break;
  5095. case NUMTYPE_NATIONAL:
  5096. chan_misdn_log(2, port, " --> %s: National\n", type);
  5097. break;
  5098. case NUMTYPE_NETWORK_SPECIFIC:
  5099. chan_misdn_log(2, port, " --> %s: Network Specific\n", type);
  5100. break;
  5101. case NUMTYPE_SUBSCRIBER:
  5102. chan_misdn_log(2, port, " --> %s: Subscriber\n", type);
  5103. break;
  5104. case NUMTYPE_ABBREVIATED:
  5105. chan_misdn_log(2, port, " --> %s: Abbreviated\n", type);
  5106. break;
  5107. /* Maybe we should cut off the prefix if present ? */
  5108. default:
  5109. chan_misdn_log(0, port, " --> !!!! Wrong dialplan setting, please see the misdn.conf sample file\n ");
  5110. break;
  5111. }
  5112. }
  5113. #ifdef MISDN_1_2
  5114. static int update_pipeline_config(struct misdn_bchannel *bc)
  5115. {
  5116. int ec;
  5117. misdn_cfg_get(bc->port, MISDN_CFG_PIPELINE, bc->pipeline, sizeof(bc->pipeline));
  5118. if (*bc->pipeline) {
  5119. return 0;
  5120. }
  5121. misdn_cfg_get(bc->port, MISDN_CFG_ECHOCANCEL, &ec, sizeof(ec));
  5122. if (ec == 1) {
  5123. ast_copy_string(bc->pipeline, "mg2ec", sizeof(bc->pipeline));
  5124. } else if (ec > 1) {
  5125. snprintf(bc->pipeline, sizeof(bc->pipeline), "mg2ec(deftaps=%d)", ec);
  5126. }
  5127. return 0;
  5128. }
  5129. #else
  5130. static int update_ec_config(struct misdn_bchannel *bc)
  5131. {
  5132. int ec;
  5133. int port = bc->port;
  5134. misdn_cfg_get(port, MISDN_CFG_ECHOCANCEL, &ec, sizeof(ec));
  5135. if (ec == 1) {
  5136. bc->ec_enable = 1;
  5137. } else if (ec > 1) {
  5138. bc->ec_enable = 1;
  5139. bc->ec_deftaps = ec;
  5140. }
  5141. return 0;
  5142. }
  5143. #endif
  5144. static int read_config(struct chan_list *ch)
  5145. {
  5146. struct ast_channel *ast;
  5147. struct misdn_bchannel *bc;
  5148. int port;
  5149. int hdlc = 0;
  5150. char lang[BUFFERSIZE + 1];
  5151. char faxdetect[BUFFERSIZE + 1];
  5152. char buf[256];
  5153. char buf2[256];
  5154. ast_group_t pg;
  5155. ast_group_t cg;
  5156. if (!ch) {
  5157. ast_log(LOG_WARNING, "Cannot configure without chanlist\n");
  5158. return -1;
  5159. }
  5160. ast = ch->ast;
  5161. bc = ch->bc;
  5162. if (! ast || ! bc) {
  5163. ast_log(LOG_WARNING, "Cannot configure without ast || bc\n");
  5164. return -1;
  5165. }
  5166. port = bc->port;
  5167. chan_misdn_log(1, port, "read_config: Getting Config\n");
  5168. misdn_cfg_get(port, MISDN_CFG_LANGUAGE, lang, sizeof(lang));
  5169. ast_string_field_set(ast, language, lang);
  5170. misdn_cfg_get(port, MISDN_CFG_MUSICCLASS, ch->mohinterpret, sizeof(ch->mohinterpret));
  5171. misdn_cfg_get(port, MISDN_CFG_TXGAIN, &bc->txgain, sizeof(bc->txgain));
  5172. misdn_cfg_get(port, MISDN_CFG_RXGAIN, &bc->rxgain, sizeof(bc->rxgain));
  5173. misdn_cfg_get(port, MISDN_CFG_INCOMING_EARLY_AUDIO, &ch->incoming_early_audio, sizeof(ch->incoming_early_audio));
  5174. misdn_cfg_get(port, MISDN_CFG_SENDDTMF, &bc->send_dtmf, sizeof(bc->send_dtmf));
  5175. misdn_cfg_get(port, MISDN_CFG_ASTDTMF, &ch->ast_dsp, sizeof(int));
  5176. if (ch->ast_dsp) {
  5177. ch->ignore_dtmf = 1;
  5178. }
  5179. misdn_cfg_get(port, MISDN_CFG_NEED_MORE_INFOS, &bc->need_more_infos, sizeof(bc->need_more_infos));
  5180. misdn_cfg_get(port, MISDN_CFG_NTTIMEOUT, &ch->nttimeout, sizeof(ch->nttimeout));
  5181. misdn_cfg_get(port, MISDN_CFG_NOAUTORESPOND_ON_SETUP, &ch->noautorespond_on_setup, sizeof(ch->noautorespond_on_setup));
  5182. misdn_cfg_get(port, MISDN_CFG_FAR_ALERTING, &ch->far_alerting, sizeof(ch->far_alerting));
  5183. misdn_cfg_get(port, MISDN_CFG_ALLOWED_BEARERS, &ch->allowed_bearers, sizeof(ch->allowed_bearers));
  5184. misdn_cfg_get(port, MISDN_CFG_FAXDETECT, faxdetect, sizeof(faxdetect));
  5185. misdn_cfg_get(port, MISDN_CFG_HDLC, &hdlc, sizeof(hdlc));
  5186. if (hdlc) {
  5187. switch (bc->capability) {
  5188. case INFO_CAPABILITY_DIGITAL_UNRESTRICTED:
  5189. case INFO_CAPABILITY_DIGITAL_RESTRICTED:
  5190. chan_misdn_log(1, bc->port, " --> CONF HDLC\n");
  5191. bc->hdlc = 1;
  5192. break;
  5193. }
  5194. }
  5195. /*Initialize new Jitterbuffer*/
  5196. misdn_cfg_get(port, MISDN_CFG_JITTERBUFFER, &ch->jb_len, sizeof(ch->jb_len));
  5197. misdn_cfg_get(port, MISDN_CFG_JITTERBUFFER_UPPER_THRESHOLD, &ch->jb_upper_threshold, sizeof(ch->jb_upper_threshold));
  5198. config_jitterbuffer(ch);
  5199. misdn_cfg_get(bc->port, MISDN_CFG_CONTEXT, ch->context, sizeof(ch->context));
  5200. ast_copy_string(ast->context, ch->context, sizeof(ast->context));
  5201. #ifdef MISDN_1_2
  5202. update_pipeline_config(bc);
  5203. #else
  5204. update_ec_config(bc);
  5205. #endif
  5206. misdn_cfg_get(bc->port, MISDN_CFG_EARLY_BCONNECT, &bc->early_bconnect, sizeof(bc->early_bconnect));
  5207. misdn_cfg_get(port, MISDN_CFG_DISPLAY_CONNECTED, &bc->display_connected, sizeof(bc->display_connected));
  5208. misdn_cfg_get(port, MISDN_CFG_DISPLAY_SETUP, &bc->display_setup, sizeof(bc->display_setup));
  5209. misdn_cfg_get(port, MISDN_CFG_OUTGOING_COLP, &bc->outgoing_colp, sizeof(bc->outgoing_colp));
  5210. misdn_cfg_get(port, MISDN_CFG_PICKUPGROUP, &pg, sizeof(pg));
  5211. misdn_cfg_get(port, MISDN_CFG_CALLGROUP, &cg, sizeof(cg));
  5212. chan_misdn_log(5, port, " --> * CallGrp:%s PickupGrp:%s\n", ast_print_group(buf, sizeof(buf), cg), ast_print_group(buf2, sizeof(buf2), pg));
  5213. ast->pickupgroup = pg;
  5214. ast->callgroup = cg;
  5215. if (ch->originator == ORG_AST) {
  5216. char callerid[BUFFERSIZE + 1];
  5217. /* ORIGINATOR Asterisk (outgoing call) */
  5218. misdn_cfg_get(port, MISDN_CFG_TE_CHOOSE_CHANNEL, &(bc->te_choose_channel), sizeof(bc->te_choose_channel));
  5219. if (strstr(faxdetect, "outgoing") || strstr(faxdetect, "both")) {
  5220. ch->faxdetect = strstr(faxdetect, "nojump") ? 2 : 1;
  5221. }
  5222. misdn_cfg_get(port, MISDN_CFG_CALLERID, callerid, sizeof(callerid));
  5223. if (!ast_strlen_zero(callerid)) {
  5224. char *cid_name = NULL;
  5225. char *cid_num = NULL;
  5226. ast_callerid_parse(callerid, &cid_name, &cid_num);
  5227. if (cid_name) {
  5228. ast_copy_string(bc->caller.name, cid_name, sizeof(bc->caller.name));
  5229. } else {
  5230. bc->caller.name[0] = '\0';
  5231. }
  5232. if (cid_num) {
  5233. ast_copy_string(bc->caller.number, cid_num, sizeof(bc->caller.number));
  5234. } else {
  5235. bc->caller.number[0] = '\0';
  5236. }
  5237. chan_misdn_log(1, port, " --> * Setting caller to \"%s\" <%s>\n", bc->caller.name, bc->caller.number);
  5238. }
  5239. misdn_cfg_get(port, MISDN_CFG_DIALPLAN, &bc->dialed.number_type, sizeof(bc->dialed.number_type));
  5240. bc->dialed.number_plan = NUMPLAN_ISDN;
  5241. debug_numtype(port, bc->dialed.number_type, "TON");
  5242. ch->overlap_dial = 0;
  5243. } else {
  5244. /* ORIGINATOR MISDN (incoming call) */
  5245. if (strstr(faxdetect, "incoming") || strstr(faxdetect, "both")) {
  5246. ch->faxdetect = (strstr(faxdetect, "nojump")) ? 2 : 1;
  5247. }
  5248. /* Add configured prefix to caller.number */
  5249. misdn_add_number_prefix(bc->port, bc->caller.number_type, bc->caller.number, sizeof(bc->caller.number));
  5250. if (ast_strlen_zero(bc->dialed.number) && !ast_strlen_zero(bc->keypad)) {
  5251. ast_copy_string(bc->dialed.number, bc->keypad, sizeof(bc->dialed.number));
  5252. }
  5253. /* Add configured prefix to dialed.number */
  5254. misdn_add_number_prefix(bc->port, bc->dialed.number_type, bc->dialed.number, sizeof(bc->dialed.number));
  5255. ast_copy_string(ast->exten, bc->dialed.number, sizeof(ast->exten));
  5256. misdn_cfg_get(bc->port, MISDN_CFG_OVERLAP_DIAL, &ch->overlap_dial, sizeof(ch->overlap_dial));
  5257. ast_mutex_init(&ch->overlap_tv_lock);
  5258. } /* ORIG MISDN END */
  5259. misdn_cfg_get(port, MISDN_CFG_INCOMING_CALLERID_TAG, bc->incoming_cid_tag, sizeof(bc->incoming_cid_tag));
  5260. if (!ast_strlen_zero(bc->incoming_cid_tag)) {
  5261. chan_misdn_log(1, port, " --> * Setting incoming caller id tag to \"%s\"\n", bc->incoming_cid_tag);
  5262. }
  5263. ch->overlap_dial_task = -1;
  5264. if (ch->faxdetect || ch->ast_dsp) {
  5265. misdn_cfg_get(port, MISDN_CFG_FAXDETECT_TIMEOUT, &ch->faxdetect_timeout, sizeof(ch->faxdetect_timeout));
  5266. if (!ch->dsp) {
  5267. ch->dsp = ast_dsp_new();
  5268. }
  5269. if (ch->dsp) {
  5270. ast_dsp_set_features(ch->dsp, DSP_FEATURE_DIGIT_DETECT | (ch->faxdetect ? DSP_FEATURE_FAX_DETECT : 0));
  5271. }
  5272. }
  5273. /* AOCD initialization */
  5274. bc->AOCDtype = Fac_None;
  5275. return 0;
  5276. }
  5277. /*!
  5278. * \internal
  5279. * \brief Send a connected line update to the other channel
  5280. *
  5281. * \param ast Current Asterisk channel
  5282. * \param id Party id information to send to the other side
  5283. * \param source Why are we sending this update
  5284. * \param cid_tag User tag to apply to the party id.
  5285. *
  5286. * \return Nothing
  5287. */
  5288. 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)
  5289. {
  5290. struct ast_party_connected_line connected;
  5291. struct ast_set_party_connected_line update_connected;
  5292. ast_party_connected_line_init(&connected);
  5293. memset(&update_connected, 0, sizeof(update_connected));
  5294. update_connected.id.number = 1;
  5295. connected.id.number.valid = 1;
  5296. connected.id.number.str = (char *) id->number;
  5297. connected.id.number.plan = misdn_to_ast_ton(id->number_type)
  5298. | misdn_to_ast_plan(id->number_plan);
  5299. connected.id.number.presentation = misdn_to_ast_pres(id->presentation)
  5300. | misdn_to_ast_screen(id->screening);
  5301. connected.id.tag = cid_tag;
  5302. connected.source = source;
  5303. ast_channel_queue_connected_line_update(ast, &connected, &update_connected);
  5304. }
  5305. /*!
  5306. * \internal
  5307. * \brief Update the caller id party on this channel.
  5308. *
  5309. * \param ast Current Asterisk channel
  5310. * \param id Remote party id information to update.
  5311. * \param cid_tag User tag to apply to the party id.
  5312. *
  5313. * \return Nothing
  5314. */
  5315. static void misdn_update_caller_id(struct ast_channel *ast, const struct misdn_party_id *id, char *cid_tag)
  5316. {
  5317. struct ast_party_caller caller;
  5318. struct ast_set_party_caller update_caller;
  5319. memset(&update_caller, 0, sizeof(update_caller));
  5320. update_caller.id.number = 1;
  5321. update_caller.ani.number = 1;
  5322. ast_channel_lock(ast);
  5323. ast_party_caller_set_init(&caller, &ast->caller);
  5324. caller.id.number.valid = 1;
  5325. caller.id.number.str = (char *) id->number;
  5326. caller.id.number.plan = misdn_to_ast_ton(id->number_type)
  5327. | misdn_to_ast_plan(id->number_plan);
  5328. caller.id.number.presentation = misdn_to_ast_pres(id->presentation)
  5329. | misdn_to_ast_screen(id->screening);
  5330. caller.ani.number = caller.id.number;
  5331. caller.id.tag = cid_tag;
  5332. caller.ani.tag = cid_tag;
  5333. ast_channel_set_caller_event(ast, &caller, &update_caller);
  5334. ast_channel_unlock(ast);
  5335. }
  5336. /*!
  5337. * \internal
  5338. * \brief Update the remote party id information.
  5339. *
  5340. * \param ast Current Asterisk channel
  5341. * \param id Remote party id information to update.
  5342. * \param source Why are we sending this update
  5343. * \param cid_tag User tag to apply to the party id.
  5344. *
  5345. * \return Nothing
  5346. */
  5347. 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)
  5348. {
  5349. misdn_update_caller_id(ast, id, cid_tag);
  5350. misdn_queue_connected_line_update(ast, id, source, cid_tag);
  5351. }
  5352. /*!
  5353. * \internal
  5354. * \brief Get the connected line information out of the Asterisk channel.
  5355. *
  5356. * \param ast Current Asterisk channel
  5357. * \param bc Associated B channel
  5358. * \param originator Who originally created this channel. ORG_AST or ORG_MISDN
  5359. *
  5360. * \return Nothing
  5361. */
  5362. static void misdn_get_connected_line(struct ast_channel *ast, struct misdn_bchannel *bc, int originator)
  5363. {
  5364. int number_type;
  5365. if (originator == ORG_MISDN) {
  5366. /* ORIGINATOR MISDN (incoming call) */
  5367. ast_copy_string(bc->connected.name,
  5368. S_COR(ast->connected.id.name.valid, ast->connected.id.name.str, ""),
  5369. sizeof(bc->connected.name));
  5370. if (ast->connected.id.number.valid) {
  5371. ast_copy_string(bc->connected.number, S_OR(ast->connected.id.number.str, ""),
  5372. sizeof(bc->connected.number));
  5373. bc->connected.presentation = ast_to_misdn_pres(ast->connected.id.number.presentation);
  5374. bc->connected.screening = ast_to_misdn_screen(ast->connected.id.number.presentation);
  5375. bc->connected.number_type = ast_to_misdn_ton(ast->connected.id.number.plan);
  5376. bc->connected.number_plan = ast_to_misdn_plan(ast->connected.id.number.plan);
  5377. } else {
  5378. bc->connected.number[0] = '\0';
  5379. bc->connected.presentation = 0;/* Allowed */
  5380. bc->connected.screening = 0;/* Unscreened */
  5381. bc->connected.number_type = NUMTYPE_UNKNOWN;
  5382. bc->connected.number_plan = NUMPLAN_UNKNOWN;
  5383. }
  5384. misdn_cfg_get(bc->port, MISDN_CFG_CPNDIALPLAN, &number_type, sizeof(number_type));
  5385. if (0 <= number_type) {
  5386. /* Force us to send in CONNECT message */
  5387. bc->connected.number_type = number_type;
  5388. bc->connected.number_plan = NUMPLAN_ISDN;
  5389. }
  5390. debug_numtype(bc->port, bc->connected.number_type, "CTON");
  5391. } else {
  5392. /* ORIGINATOR Asterisk (outgoing call) */
  5393. ast_copy_string(bc->caller.name,
  5394. S_COR(ast->connected.id.name.valid, ast->connected.id.name.str, ""),
  5395. sizeof(bc->caller.name));
  5396. if (ast->connected.id.number.valid) {
  5397. ast_copy_string(bc->caller.number, S_OR(ast->connected.id.number.str, ""),
  5398. sizeof(bc->caller.number));
  5399. bc->caller.presentation = ast_to_misdn_pres(ast->connected.id.number.presentation);
  5400. bc->caller.screening = ast_to_misdn_screen(ast->connected.id.number.presentation);
  5401. bc->caller.number_type = ast_to_misdn_ton(ast->connected.id.number.plan);
  5402. bc->caller.number_plan = ast_to_misdn_plan(ast->connected.id.number.plan);
  5403. } else {
  5404. bc->caller.number[0] = '\0';
  5405. bc->caller.presentation = 0;/* Allowed */
  5406. bc->caller.screening = 0;/* Unscreened */
  5407. bc->caller.number_type = NUMTYPE_UNKNOWN;
  5408. bc->caller.number_plan = NUMPLAN_UNKNOWN;
  5409. }
  5410. misdn_cfg_get(bc->port, MISDN_CFG_LOCALDIALPLAN, &number_type, sizeof(number_type));
  5411. if (0 <= number_type) {
  5412. /* Force us to send in SETUP message */
  5413. bc->caller.number_type = number_type;
  5414. bc->caller.number_plan = NUMPLAN_ISDN;
  5415. }
  5416. debug_numtype(bc->port, bc->caller.number_type, "LTON");
  5417. }
  5418. }
  5419. /*!
  5420. * \internal
  5421. * \brief Notify peer that the connected line has changed.
  5422. *
  5423. * \param ast Current Asterisk channel
  5424. * \param bc Associated B channel
  5425. * \param originator Who originally created this channel. ORG_AST or ORG_MISDN
  5426. *
  5427. * \return Nothing
  5428. */
  5429. static void misdn_update_connected_line(struct ast_channel *ast, struct misdn_bchannel *bc, int originator)
  5430. {
  5431. struct chan_list *ch;
  5432. misdn_get_connected_line(ast, bc, originator);
  5433. if (originator == ORG_MISDN) {
  5434. bc->redirecting.to = bc->connected;
  5435. } else {
  5436. bc->redirecting.to = bc->caller;
  5437. }
  5438. switch (bc->outgoing_colp) {
  5439. case 1:/* restricted */
  5440. bc->redirecting.to.presentation = 1;/* restricted */
  5441. break;
  5442. case 2:/* blocked */
  5443. /* Don't tell the remote party that the call was transferred. */
  5444. return;
  5445. default:
  5446. break;
  5447. }
  5448. ch = MISDN_ASTERISK_TECH_PVT(ast);
  5449. if (ch->state == MISDN_CONNECTED
  5450. || originator != ORG_MISDN) {
  5451. int is_ptmp;
  5452. is_ptmp = !misdn_lib_is_ptp(bc->port);
  5453. if (is_ptmp) {
  5454. /*
  5455. * We should not send these messages to the network if we are
  5456. * the CPE side since phones do not transfer calls within
  5457. * themselves. Well... If you consider handing the handset to
  5458. * someone else a transfer then how is the network to know?
  5459. */
  5460. if (!misdn_lib_port_is_nt(bc->port)) {
  5461. return;
  5462. }
  5463. if (ch->state != MISDN_CONNECTED) {
  5464. /* Send NOTIFY(Nie(transfer-active), RDNie(redirecting.to data)) */
  5465. bc->redirecting.to_changed = 1;
  5466. bc->notify_description_code = mISDN_NOTIFY_CODE_CALL_TRANSFER_ACTIVE;
  5467. misdn_lib_send_event(bc, EVENT_NOTIFY);
  5468. #if defined(AST_MISDN_ENHANCEMENTS)
  5469. } else {
  5470. /* Send FACILITY(Fie(RequestSubaddress), Nie(transfer-active), RDNie(redirecting.to data)) */
  5471. bc->redirecting.to_changed = 1;
  5472. bc->notify_description_code = mISDN_NOTIFY_CODE_CALL_TRANSFER_ACTIVE;
  5473. bc->fac_out.Function = Fac_RequestSubaddress;
  5474. bc->fac_out.u.RequestSubaddress.InvokeID = ++misdn_invoke_id;
  5475. /* Send message */
  5476. print_facility(&bc->fac_out, bc);
  5477. misdn_lib_send_event(bc, EVENT_FACILITY);
  5478. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  5479. }
  5480. #if defined(AST_MISDN_ENHANCEMENTS)
  5481. } else {
  5482. /* Send FACILITY(Fie(EctInform(transfer-active, redirecting.to data))) */
  5483. bc->fac_out.Function = Fac_EctInform;
  5484. bc->fac_out.u.EctInform.InvokeID = ++misdn_invoke_id;
  5485. bc->fac_out.u.EctInform.Status = 1;/* active */
  5486. bc->fac_out.u.EctInform.RedirectionPresent = 1;/* Must be present when status is active */
  5487. misdn_PresentedNumberUnscreened_fill(&bc->fac_out.u.EctInform.Redirection,
  5488. &bc->redirecting.to);
  5489. /* Send message */
  5490. print_facility(&bc->fac_out, bc);
  5491. misdn_lib_send_event(bc, EVENT_FACILITY);
  5492. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  5493. }
  5494. }
  5495. }
  5496. /*!
  5497. * \internal
  5498. * \brief Copy the redirecting information out of the Asterisk channel
  5499. *
  5500. * \param bc Associated B channel
  5501. * \param ast Current Asterisk channel
  5502. *
  5503. * \return Nothing
  5504. */
  5505. static void misdn_copy_redirecting_from_ast(struct misdn_bchannel *bc, struct ast_channel *ast)
  5506. {
  5507. ast_copy_string(bc->redirecting.from.name,
  5508. S_COR(ast->redirecting.from.name.valid, ast->redirecting.from.name.str, ""),
  5509. sizeof(bc->redirecting.from.name));
  5510. if (ast->redirecting.from.number.valid) {
  5511. ast_copy_string(bc->redirecting.from.number, S_OR(ast->redirecting.from.number.str, ""),
  5512. sizeof(bc->redirecting.from.number));
  5513. bc->redirecting.from.presentation = ast_to_misdn_pres(ast->redirecting.from.number.presentation);
  5514. bc->redirecting.from.screening = ast_to_misdn_screen(ast->redirecting.from.number.presentation);
  5515. bc->redirecting.from.number_type = ast_to_misdn_ton(ast->redirecting.from.number.plan);
  5516. bc->redirecting.from.number_plan = ast_to_misdn_plan(ast->redirecting.from.number.plan);
  5517. } else {
  5518. bc->redirecting.from.number[0] = '\0';
  5519. bc->redirecting.from.presentation = 0;/* Allowed */
  5520. bc->redirecting.from.screening = 0;/* Unscreened */
  5521. bc->redirecting.from.number_type = NUMTYPE_UNKNOWN;
  5522. bc->redirecting.from.number_plan = NUMPLAN_UNKNOWN;
  5523. }
  5524. ast_copy_string(bc->redirecting.to.name,
  5525. S_COR(ast->redirecting.to.name.valid, ast->redirecting.to.name.str, ""),
  5526. sizeof(bc->redirecting.to.name));
  5527. if (ast->redirecting.to.number.valid) {
  5528. ast_copy_string(bc->redirecting.to.number, S_OR(ast->redirecting.to.number.str, ""),
  5529. sizeof(bc->redirecting.to.number));
  5530. bc->redirecting.to.presentation = ast_to_misdn_pres(ast->redirecting.to.number.presentation);
  5531. bc->redirecting.to.screening = ast_to_misdn_screen(ast->redirecting.to.number.presentation);
  5532. bc->redirecting.to.number_type = ast_to_misdn_ton(ast->redirecting.to.number.plan);
  5533. bc->redirecting.to.number_plan = ast_to_misdn_plan(ast->redirecting.to.number.plan);
  5534. } else {
  5535. bc->redirecting.to.number[0] = '\0';
  5536. bc->redirecting.to.presentation = 0;/* Allowed */
  5537. bc->redirecting.to.screening = 0;/* Unscreened */
  5538. bc->redirecting.to.number_type = NUMTYPE_UNKNOWN;
  5539. bc->redirecting.to.number_plan = NUMPLAN_UNKNOWN;
  5540. }
  5541. bc->redirecting.reason = ast_to_misdn_reason(ast->redirecting.reason);
  5542. bc->redirecting.count = ast->redirecting.count;
  5543. }
  5544. /*!
  5545. * \internal
  5546. * \brief Copy the redirecting info into the Asterisk channel
  5547. *
  5548. * \param ast Current Asterisk channel
  5549. * \param redirect Associated B channel redirecting info
  5550. * \param tag Caller ID tag to set in the redirecting party fields
  5551. *
  5552. * \return Nothing
  5553. */
  5554. static void misdn_copy_redirecting_to_ast(struct ast_channel *ast, const struct misdn_party_redirecting *redirect, char *tag)
  5555. {
  5556. struct ast_party_redirecting redirecting;
  5557. struct ast_set_party_redirecting update_redirecting;
  5558. ast_party_redirecting_set_init(&redirecting, &ast->redirecting);
  5559. memset(&update_redirecting, 0, sizeof(update_redirecting));
  5560. update_redirecting.from.number = 1;
  5561. redirecting.from.number.valid = 1;
  5562. redirecting.from.number.str = (char *) redirect->from.number;
  5563. redirecting.from.number.plan =
  5564. misdn_to_ast_ton(redirect->from.number_type)
  5565. | misdn_to_ast_plan(redirect->from.number_plan);
  5566. redirecting.from.number.presentation =
  5567. misdn_to_ast_pres(redirect->from.presentation)
  5568. | misdn_to_ast_screen(redirect->from.screening);
  5569. redirecting.from.tag = tag;
  5570. update_redirecting.to.number = 1;
  5571. redirecting.to.number.valid = 1;
  5572. redirecting.to.number.str = (char *) redirect->to.number;
  5573. redirecting.to.number.plan =
  5574. misdn_to_ast_ton(redirect->to.number_type)
  5575. | misdn_to_ast_plan(redirect->to.number_plan);
  5576. redirecting.to.number.presentation =
  5577. misdn_to_ast_pres(redirect->to.presentation)
  5578. | misdn_to_ast_screen(redirect->to.screening);
  5579. redirecting.to.tag = tag;
  5580. redirecting.reason = misdn_to_ast_reason(redirect->reason);
  5581. redirecting.count = redirect->count;
  5582. ast_channel_set_redirecting(ast, &redirecting, &update_redirecting);
  5583. }
  5584. /*!
  5585. * \internal
  5586. * \brief Notify peer that the redirecting information has changed.
  5587. *
  5588. * \param ast Current Asterisk channel
  5589. * \param bc Associated B channel
  5590. * \param originator Who originally created this channel. ORG_AST or ORG_MISDN
  5591. *
  5592. * \return Nothing
  5593. */
  5594. static void misdn_update_redirecting(struct ast_channel *ast, struct misdn_bchannel *bc, int originator)
  5595. {
  5596. int is_ptmp;
  5597. misdn_copy_redirecting_from_ast(bc, ast);
  5598. switch (bc->outgoing_colp) {
  5599. case 1:/* restricted */
  5600. bc->redirecting.to.presentation = 1;/* restricted */
  5601. break;
  5602. case 2:/* blocked */
  5603. /* Don't tell the remote party that the call was redirected. */
  5604. return;
  5605. default:
  5606. break;
  5607. }
  5608. if (originator != ORG_MISDN) {
  5609. return;
  5610. }
  5611. is_ptmp = !misdn_lib_is_ptp(bc->port);
  5612. if (is_ptmp) {
  5613. /*
  5614. * We should not send these messages to the network if we are
  5615. * the CPE side since phones do not redirect calls within
  5616. * themselves. Well... If you consider someone else picking up
  5617. * the handset a redirection then how is the network to know?
  5618. */
  5619. if (!misdn_lib_port_is_nt(bc->port)) {
  5620. return;
  5621. }
  5622. /* Send NOTIFY(call-is-diverting, redirecting.to data) */
  5623. bc->redirecting.to_changed = 1;
  5624. bc->notify_description_code = mISDN_NOTIFY_CODE_CALL_IS_DIVERTING;
  5625. misdn_lib_send_event(bc, EVENT_NOTIFY);
  5626. #if defined(AST_MISDN_ENHANCEMENTS)
  5627. } else {
  5628. int match; /* TRUE if the dialed number matches the redirecting to number */
  5629. match = (strcmp(ast->exten, bc->redirecting.to.number) == 0) ? 1 : 0;
  5630. if (!bc->div_leg_3_tx_pending
  5631. || !match) {
  5632. /* Send DivertingLegInformation1 */
  5633. bc->fac_out.Function = Fac_DivertingLegInformation1;
  5634. bc->fac_out.u.DivertingLegInformation1.InvokeID = ++misdn_invoke_id;
  5635. bc->fac_out.u.DivertingLegInformation1.DiversionReason =
  5636. misdn_to_diversion_reason(bc->redirecting.reason);
  5637. bc->fac_out.u.DivertingLegInformation1.SubscriptionOption = 2;/* notificationWithDivertedToNr */
  5638. bc->fac_out.u.DivertingLegInformation1.DivertedToPresent = 1;
  5639. misdn_PresentedNumberUnscreened_fill(&bc->fac_out.u.DivertingLegInformation1.DivertedTo, &bc->redirecting.to);
  5640. print_facility(&bc->fac_out, bc);
  5641. misdn_lib_send_event(bc, EVENT_FACILITY);
  5642. }
  5643. bc->div_leg_3_tx_pending = 0;
  5644. /* Send DivertingLegInformation3 */
  5645. bc->fac_out.Function = Fac_DivertingLegInformation3;
  5646. bc->fac_out.u.DivertingLegInformation3.InvokeID = ++misdn_invoke_id;
  5647. bc->fac_out.u.DivertingLegInformation3.PresentationAllowedIndicator =
  5648. bc->redirecting.to.presentation == 0 ? 1 : 0;
  5649. print_facility(&bc->fac_out, bc);
  5650. misdn_lib_send_event(bc, EVENT_FACILITY);
  5651. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  5652. }
  5653. }
  5654. /*****************************/
  5655. /*** AST Indications Start ***/
  5656. /*****************************/
  5657. static int misdn_call(struct ast_channel *ast, char *dest, int timeout)
  5658. {
  5659. int port = 0;
  5660. int r;
  5661. int exceed;
  5662. int number_type;
  5663. struct chan_list *ch;
  5664. struct misdn_bchannel *newbc;
  5665. char *dest_cp;
  5666. int append_msn = 0;
  5667. AST_DECLARE_APP_ARGS(args,
  5668. AST_APP_ARG(intf); /* The interface token is discarded. */
  5669. AST_APP_ARG(ext); /* extension token */
  5670. AST_APP_ARG(opts); /* options token */
  5671. );
  5672. if (!ast) {
  5673. ast_log(LOG_WARNING, " --> ! misdn_call called on ast_channel *ast where ast == NULL\n");
  5674. return -1;
  5675. }
  5676. if (((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) || !dest) {
  5677. ast_log(LOG_WARNING, " --> ! misdn_call called on %s, neither down nor reserved (or dest==NULL)\n", ast->name);
  5678. ast->hangupcause = AST_CAUSE_NORMAL_TEMPORARY_FAILURE;
  5679. ast_setstate(ast, AST_STATE_DOWN);
  5680. return -1;
  5681. }
  5682. ch = MISDN_ASTERISK_TECH_PVT(ast);
  5683. if (!ch) {
  5684. ast_log(LOG_WARNING, " --> ! misdn_call called on %s, chan_list *ch==NULL\n", ast->name);
  5685. ast->hangupcause = AST_CAUSE_NORMAL_TEMPORARY_FAILURE;
  5686. ast_setstate(ast, AST_STATE_DOWN);
  5687. return -1;
  5688. }
  5689. newbc = ch->bc;
  5690. if (!newbc) {
  5691. ast_log(LOG_WARNING, " --> ! misdn_call called on %s, newbc==NULL\n", ast->name);
  5692. ast->hangupcause = AST_CAUSE_NORMAL_TEMPORARY_FAILURE;
  5693. ast_setstate(ast, AST_STATE_DOWN);
  5694. return -1;
  5695. }
  5696. port = newbc->port;
  5697. #if defined(AST_MISDN_ENHANCEMENTS)
  5698. if ((ch->peer = misdn_cc_caller_get(ast))) {
  5699. chan_misdn_log(3, port, " --> Found CC caller data, peer:%s\n",
  5700. ch->peer->chan ? "available" : "NULL");
  5701. }
  5702. if (ch->record_id != -1) {
  5703. struct misdn_cc_record *cc_record;
  5704. /* This is a call completion retry call */
  5705. AST_LIST_LOCK(&misdn_cc_records_db);
  5706. cc_record = misdn_cc_find_by_id(ch->record_id);
  5707. if (!cc_record) {
  5708. AST_LIST_UNLOCK(&misdn_cc_records_db);
  5709. ast_log(LOG_WARNING, " --> ! misdn_call called on %s, cc_record==NULL\n", ast->name);
  5710. ast->hangupcause = AST_CAUSE_NORMAL_TEMPORARY_FAILURE;
  5711. ast_setstate(ast, AST_STATE_DOWN);
  5712. return -1;
  5713. }
  5714. /* Setup calling parameters to retry the call. */
  5715. newbc->dialed = cc_record->redial.dialed;
  5716. newbc->caller = cc_record->redial.caller;
  5717. memset(&newbc->redirecting, 0, sizeof(newbc->redirecting));
  5718. newbc->capability = cc_record->redial.capability;
  5719. newbc->hdlc = cc_record->redial.hdlc;
  5720. newbc->sending_complete = 1;
  5721. if (cc_record->ptp) {
  5722. newbc->fac_out.Function = Fac_CCBS_T_Call;
  5723. newbc->fac_out.u.CCBS_T_Call.InvokeID = ++misdn_invoke_id;
  5724. } else {
  5725. newbc->fac_out.Function = Fac_CCBSCall;
  5726. newbc->fac_out.u.CCBSCall.InvokeID = ++misdn_invoke_id;
  5727. newbc->fac_out.u.CCBSCall.CCBSReference = cc_record->mode.ptmp.reference_id;
  5728. }
  5729. AST_LIST_UNLOCK(&misdn_cc_records_db);
  5730. ast_copy_string(ast->exten, newbc->dialed.number, sizeof(ast->exten));
  5731. chan_misdn_log(1, port, "* Call completion to: %s\n", newbc->dialed.number);
  5732. chan_misdn_log(2, port, " --> * tech:%s context:%s\n", ast->name, ast->context);
  5733. } else
  5734. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  5735. {
  5736. /*
  5737. * dest is ---v
  5738. * Dial(mISDN/g:group_name[/extension[/options]])
  5739. * Dial(mISDN/port[:preselected_channel][/extension[/options]])
  5740. *
  5741. * The dial extension could be empty if you are using MISDN_KEYPAD
  5742. * to control ISDN provider features.
  5743. */
  5744. dest_cp = ast_strdupa(dest);
  5745. AST_NONSTANDARD_APP_ARGS(args, dest_cp, '/');
  5746. if (!args.ext) {
  5747. args.ext = "";
  5748. }
  5749. chan_misdn_log(1, port, "* CALL: %s\n", dest);
  5750. chan_misdn_log(2, port, " --> * dialed:%s tech:%s context:%s\n", args.ext, ast->name, ast->context);
  5751. ast_copy_string(ast->exten, args.ext, sizeof(ast->exten));
  5752. ast_copy_string(newbc->dialed.number, args.ext, sizeof(newbc->dialed.number));
  5753. if (ast_strlen_zero(newbc->caller.name)
  5754. && ast->connected.id.name.valid
  5755. && !ast_strlen_zero(ast->connected.id.name.str)) {
  5756. ast_copy_string(newbc->caller.name, ast->connected.id.name.str, sizeof(newbc->caller.name));
  5757. chan_misdn_log(3, port, " --> * set caller:\"%s\" <%s>\n", newbc->caller.name, newbc->caller.number);
  5758. }
  5759. if (ast_strlen_zero(newbc->caller.number)
  5760. && ast->connected.id.number.valid
  5761. && !ast_strlen_zero(ast->connected.id.number.str)) {
  5762. ast_copy_string(newbc->caller.number, ast->connected.id.number.str, sizeof(newbc->caller.number));
  5763. chan_misdn_log(3, port, " --> * set caller:\"%s\" <%s>\n", newbc->caller.name, newbc->caller.number);
  5764. }
  5765. misdn_cfg_get(port, MISDN_CFG_APPEND_MSN_TO_CALLERID_TAG, &append_msn, sizeof(append_msn));
  5766. if (append_msn) {
  5767. strncat(newbc->incoming_cid_tag, "_", sizeof(newbc->incoming_cid_tag) - strlen(newbc->incoming_cid_tag) - 1);
  5768. strncat(newbc->incoming_cid_tag, newbc->caller.number, sizeof(newbc->incoming_cid_tag) - strlen(newbc->incoming_cid_tag) - 1);
  5769. }
  5770. ast->caller.id.tag = ast_strdup(newbc->incoming_cid_tag);
  5771. misdn_cfg_get(port, MISDN_CFG_LOCALDIALPLAN, &number_type, sizeof(number_type));
  5772. if (number_type < 0) {
  5773. if (ast->connected.id.number.valid) {
  5774. newbc->caller.number_type = ast_to_misdn_ton(ast->connected.id.number.plan);
  5775. newbc->caller.number_plan = ast_to_misdn_plan(ast->connected.id.number.plan);
  5776. } else {
  5777. newbc->caller.number_type = NUMTYPE_UNKNOWN;
  5778. newbc->caller.number_plan = NUMPLAN_ISDN;
  5779. }
  5780. } else {
  5781. /* Force us to send in SETUP message */
  5782. newbc->caller.number_type = number_type;
  5783. newbc->caller.number_plan = NUMPLAN_ISDN;
  5784. }
  5785. debug_numtype(port, newbc->caller.number_type, "LTON");
  5786. newbc->capability = ast->transfercapability;
  5787. pbx_builtin_setvar_helper(ast, "TRANSFERCAPABILITY", ast_transfercapability2str(newbc->capability));
  5788. if (ast->transfercapability == INFO_CAPABILITY_DIGITAL_UNRESTRICTED) {
  5789. chan_misdn_log(2, port, " --> * Call with flag Digital\n");
  5790. }
  5791. /* update caller screening and presentation */
  5792. update_config(ch);
  5793. /* fill in some ies from channel dialplan variables */
  5794. import_ch(ast, newbc, ch);
  5795. /* Finally The Options Override Everything */
  5796. if (!ast_strlen_zero(args.opts)) {
  5797. misdn_set_opt_exec(ast, args.opts);
  5798. } else {
  5799. chan_misdn_log(2, port, "NO OPTS GIVEN\n");
  5800. }
  5801. if (newbc->set_presentation) {
  5802. newbc->caller.presentation = newbc->presentation;
  5803. }
  5804. misdn_copy_redirecting_from_ast(newbc, ast);
  5805. switch (newbc->outgoing_colp) {
  5806. case 1:/* restricted */
  5807. case 2:/* blocked */
  5808. newbc->redirecting.from.presentation = 1;/* restricted */
  5809. break;
  5810. default:
  5811. break;
  5812. }
  5813. #if defined(AST_MISDN_ENHANCEMENTS)
  5814. if (newbc->redirecting.from.number[0] && misdn_lib_is_ptp(port)) {
  5815. if (newbc->redirecting.count < 1) {
  5816. newbc->redirecting.count = 1;
  5817. }
  5818. /* Create DivertingLegInformation2 facility */
  5819. newbc->fac_out.Function = Fac_DivertingLegInformation2;
  5820. newbc->fac_out.u.DivertingLegInformation2.InvokeID = ++misdn_invoke_id;
  5821. newbc->fac_out.u.DivertingLegInformation2.DivertingPresent = 1;
  5822. misdn_PresentedNumberUnscreened_fill(
  5823. &newbc->fac_out.u.DivertingLegInformation2.Diverting,
  5824. &newbc->redirecting.from);
  5825. switch (newbc->outgoing_colp) {
  5826. case 2:/* blocked */
  5827. /* Block the number going out */
  5828. newbc->fac_out.u.DivertingLegInformation2.Diverting.Type = 1;/* presentationRestricted */
  5829. /* Don't tell about any previous diversions or why for that matter. */
  5830. newbc->fac_out.u.DivertingLegInformation2.DiversionCounter = 1;
  5831. newbc->fac_out.u.DivertingLegInformation2.DiversionReason = 0;/* unknown */
  5832. break;
  5833. default:
  5834. newbc->fac_out.u.DivertingLegInformation2.DiversionCounter =
  5835. newbc->redirecting.count;
  5836. newbc->fac_out.u.DivertingLegInformation2.DiversionReason =
  5837. misdn_to_diversion_reason(newbc->redirecting.reason);
  5838. break;
  5839. }
  5840. newbc->fac_out.u.DivertingLegInformation2.OriginalCalledPresent = 0;
  5841. if (1 < newbc->fac_out.u.DivertingLegInformation2.DiversionCounter) {
  5842. newbc->fac_out.u.DivertingLegInformation2.OriginalCalledPresent = 1;
  5843. newbc->fac_out.u.DivertingLegInformation2.OriginalCalled.Type = 2;/* numberNotAvailableDueToInterworking */
  5844. }
  5845. /*
  5846. * Expect a DivertingLegInformation3 to update the COLR of the
  5847. * redirecting-to party we are attempting to call now.
  5848. */
  5849. newbc->div_leg_3_rx_wanted = 1;
  5850. }
  5851. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  5852. }
  5853. exceed = add_out_calls(port);
  5854. if (exceed != 0) {
  5855. char tmp[16];
  5856. snprintf(tmp, sizeof(tmp), "%d", exceed);
  5857. pbx_builtin_setvar_helper(ast, "MAX_OVERFLOW", tmp);
  5858. ast->hangupcause = AST_CAUSE_NORMAL_TEMPORARY_FAILURE;
  5859. ast_setstate(ast, AST_STATE_DOWN);
  5860. return -1;
  5861. }
  5862. #if defined(AST_MISDN_ENHANCEMENTS)
  5863. if (newbc->fac_out.Function != Fac_None) {
  5864. print_facility(&newbc->fac_out, newbc);
  5865. }
  5866. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  5867. r = misdn_lib_send_event(newbc, EVENT_SETUP);
  5868. /** we should have l3id after sending setup **/
  5869. ch->l3id = newbc->l3_id;
  5870. if (r == -ENOCHAN) {
  5871. chan_misdn_log(0, port, " --> * Theres no Channel at the moment .. !\n");
  5872. chan_misdn_log(1, port, " --> * SEND: State Down pid:%d\n", newbc ? newbc->pid : -1);
  5873. ast->hangupcause = AST_CAUSE_NORMAL_CIRCUIT_CONGESTION;
  5874. ast_setstate(ast, AST_STATE_DOWN);
  5875. return -1;
  5876. }
  5877. chan_misdn_log(2, port, " --> * SEND: State Dialing pid:%d\n", newbc ? newbc->pid : 1);
  5878. ast_setstate(ast, AST_STATE_DIALING);
  5879. ast->hangupcause = AST_CAUSE_NORMAL_CLEARING;
  5880. if (newbc->nt) {
  5881. stop_bc_tones(ch);
  5882. }
  5883. ch->state = MISDN_CALLING;
  5884. return 0;
  5885. }
  5886. static int misdn_answer(struct ast_channel *ast)
  5887. {
  5888. struct chan_list *p;
  5889. const char *tmp;
  5890. if (!ast || !(p = MISDN_ASTERISK_TECH_PVT(ast))) {
  5891. return -1;
  5892. }
  5893. chan_misdn_log(1, p ? (p->bc ? p->bc->port : 0) : 0, "* ANSWER:\n");
  5894. if (!p) {
  5895. ast_log(LOG_WARNING, " --> Channel not connected ??\n");
  5896. ast_queue_hangup_with_cause(ast, AST_CAUSE_NETWORK_OUT_OF_ORDER);
  5897. }
  5898. if (!p->bc) {
  5899. chan_misdn_log(1, 0, " --> Got Answer, but there is no bc obj ??\n");
  5900. ast_queue_hangup_with_cause(ast, AST_CAUSE_PROTOCOL_ERROR);
  5901. }
  5902. ast_channel_lock(ast);
  5903. tmp = pbx_builtin_getvar_helper(ast, "CRYPT_KEY");
  5904. if (!ast_strlen_zero(tmp)) {
  5905. chan_misdn_log(1, p->bc->port, " --> Connection will be BF crypted\n");
  5906. ast_copy_string(p->bc->crypt_key, tmp, sizeof(p->bc->crypt_key));
  5907. } else {
  5908. chan_misdn_log(3, p->bc->port, " --> Connection is without BF encryption\n");
  5909. }
  5910. tmp = pbx_builtin_getvar_helper(ast, "MISDN_DIGITAL_TRANS");
  5911. if (!ast_strlen_zero(tmp) && ast_true(tmp)) {
  5912. chan_misdn_log(1, p->bc->port, " --> Connection is transparent digital\n");
  5913. p->bc->nodsp = 1;
  5914. p->bc->hdlc = 0;
  5915. p->bc->nojitter = 1;
  5916. }
  5917. ast_channel_unlock(ast);
  5918. p->state = MISDN_CONNECTED;
  5919. stop_indicate(p);
  5920. if (ast_strlen_zero(p->bc->connected.number)) {
  5921. chan_misdn_log(2,p->bc->port," --> empty connected number using dialed number\n");
  5922. ast_copy_string(p->bc->connected.number, p->bc->dialed.number, sizeof(p->bc->connected.number));
  5923. /*
  5924. * Use the misdn_set_opt() application to set the presentation
  5925. * before we answer or you can use the CONECTEDLINE() function
  5926. * to set everything before using the Answer() application.
  5927. */
  5928. p->bc->connected.presentation = p->bc->presentation;
  5929. p->bc->connected.screening = 0; /* unscreened */
  5930. p->bc->connected.number_type = p->bc->dialed.number_type;
  5931. p->bc->connected.number_plan = p->bc->dialed.number_plan;
  5932. }
  5933. switch (p->bc->outgoing_colp) {
  5934. case 1:/* restricted */
  5935. case 2:/* blocked */
  5936. p->bc->connected.presentation = 1;/* restricted */
  5937. break;
  5938. default:
  5939. break;
  5940. }
  5941. #if defined(AST_MISDN_ENHANCEMENTS)
  5942. if (p->bc->div_leg_3_tx_pending) {
  5943. p->bc->div_leg_3_tx_pending = 0;
  5944. /* Send DivertingLegInformation3 */
  5945. p->bc->fac_out.Function = Fac_DivertingLegInformation3;
  5946. p->bc->fac_out.u.DivertingLegInformation3.InvokeID = ++misdn_invoke_id;
  5947. p->bc->fac_out.u.DivertingLegInformation3.PresentationAllowedIndicator =
  5948. (p->bc->connected.presentation == 0) ? 1 : 0;
  5949. print_facility(&p->bc->fac_out, p->bc);
  5950. }
  5951. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  5952. misdn_lib_send_event(p->bc, EVENT_CONNECT);
  5953. start_bc_tones(p);
  5954. return 0;
  5955. }
  5956. static int misdn_digit_begin(struct ast_channel *chan, char digit)
  5957. {
  5958. /* XXX Modify this callback to support Asterisk controlling the length of DTMF */
  5959. return 0;
  5960. }
  5961. static int misdn_digit_end(struct ast_channel *ast, char digit, unsigned int duration)
  5962. {
  5963. struct chan_list *p;
  5964. struct misdn_bchannel *bc;
  5965. char buf[2] = { digit, 0 };
  5966. if (!ast || !(p = MISDN_ASTERISK_TECH_PVT(ast))) {
  5967. return -1;
  5968. }
  5969. bc = p->bc;
  5970. chan_misdn_log(1, bc ? bc->port : 0, "* IND : Digit %c\n", digit);
  5971. if (!bc) {
  5972. ast_log(LOG_WARNING, " --> !! Got Digit Event without having bchannel Object\n");
  5973. return -1;
  5974. }
  5975. switch (p->state) {
  5976. case MISDN_CALLING:
  5977. if (strlen(bc->infos_pending) < sizeof(bc->infos_pending) - 1) {
  5978. strncat(bc->infos_pending, buf, sizeof(bc->infos_pending) - strlen(bc->infos_pending) - 1);
  5979. }
  5980. break;
  5981. case MISDN_CALLING_ACKNOWLEDGE:
  5982. ast_copy_string(bc->info_dad, buf, sizeof(bc->info_dad));
  5983. if (strlen(bc->dialed.number) < sizeof(bc->dialed.number) - 1) {
  5984. strncat(bc->dialed.number, buf, sizeof(bc->dialed.number) - strlen(bc->dialed.number) - 1);
  5985. }
  5986. ast_copy_string(p->ast->exten, bc->dialed.number, sizeof(p->ast->exten));
  5987. misdn_lib_send_event(bc, EVENT_INFORMATION);
  5988. break;
  5989. default:
  5990. if (bc->send_dtmf) {
  5991. send_digit_to_chan(p, digit);
  5992. }
  5993. break;
  5994. }
  5995. return 0;
  5996. }
  5997. static int misdn_fixup(struct ast_channel *oldast, struct ast_channel *ast)
  5998. {
  5999. struct chan_list *p;
  6000. if (!ast || !(p = MISDN_ASTERISK_TECH_PVT(ast))) {
  6001. return -1;
  6002. }
  6003. 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);
  6004. p->ast = ast;
  6005. return 0;
  6006. }
  6007. static int misdn_indication(struct ast_channel *ast, int cond, const void *data, size_t datalen)
  6008. {
  6009. struct chan_list *p;
  6010. if (!ast || !(p = MISDN_ASTERISK_TECH_PVT(ast))) {
  6011. ast_log(LOG_WARNING, "Returned -1 in misdn_indication\n");
  6012. return -1;
  6013. }
  6014. if (!p->bc) {
  6015. if (p->hold.state == MISDN_HOLD_IDLE) {
  6016. chan_misdn_log(1, 0, "* IND : Indication [%d] ignored on %s\n", cond,
  6017. ast->name);
  6018. ast_log(LOG_WARNING, "Private Pointer but no bc ?\n");
  6019. } else {
  6020. chan_misdn_log(1, 0, "* IND : Indication [%d] ignored on hold %s\n",
  6021. cond, ast->name);
  6022. }
  6023. return -1;
  6024. }
  6025. chan_misdn_log(5, p->bc->port, "* IND : Indication [%d] on %s\n", cond, ast->name);
  6026. switch (cond) {
  6027. case AST_CONTROL_BUSY:
  6028. chan_misdn_log(1, p->bc->port, "* IND :\tbusy pid:%d\n", p->bc->pid);
  6029. ast_setstate(ast, AST_STATE_BUSY);
  6030. p->bc->out_cause = AST_CAUSE_USER_BUSY;
  6031. if (p->state != MISDN_CONNECTED) {
  6032. start_bc_tones(p);
  6033. misdn_lib_send_event(p->bc, EVENT_DISCONNECT);
  6034. }
  6035. return -1;
  6036. case AST_CONTROL_RING:
  6037. chan_misdn_log(1, p->bc->port, "* IND :\tring pid:%d\n", p->bc->pid);
  6038. return -1;
  6039. case AST_CONTROL_RINGING:
  6040. chan_misdn_log(1, p->bc->port, "* IND :\tringing pid:%d\n", p->bc->pid);
  6041. switch (p->state) {
  6042. case MISDN_ALERTING:
  6043. chan_misdn_log(2, p->bc->port, " --> * IND :\tringing pid:%d but I was Ringing before, so ignoring it\n", p->bc->pid);
  6044. break;
  6045. case MISDN_CONNECTED:
  6046. 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);
  6047. return -1;
  6048. default:
  6049. p->state = MISDN_ALERTING;
  6050. chan_misdn_log(2, p->bc->port, " --> * IND :\tringing pid:%d\n", p->bc->pid);
  6051. misdn_lib_send_event(p->bc, EVENT_ALERTING);
  6052. chan_misdn_log(3, p->bc->port, " --> * SEND: State Ring pid:%d\n", p->bc->pid);
  6053. ast_setstate(ast, AST_STATE_RING);
  6054. if (!p->bc->nt && (p->originator == ORG_MISDN) && !p->incoming_early_audio) {
  6055. chan_misdn_log(2, p->bc->port, " --> incoming_early_audio off\n");
  6056. } else {
  6057. return -1;
  6058. }
  6059. }
  6060. break;
  6061. case AST_CONTROL_ANSWER:
  6062. chan_misdn_log(1, p->bc->port, " --> * IND :\tanswer pid:%d\n", p->bc->pid);
  6063. start_bc_tones(p);
  6064. break;
  6065. case AST_CONTROL_TAKEOFFHOOK:
  6066. chan_misdn_log(1, p->bc->port, " --> *\ttakeoffhook pid:%d\n", p->bc->pid);
  6067. return -1;
  6068. case AST_CONTROL_OFFHOOK:
  6069. chan_misdn_log(1, p->bc->port, " --> *\toffhook pid:%d\n", p->bc->pid);
  6070. return -1;
  6071. case AST_CONTROL_FLASH:
  6072. chan_misdn_log(1, p->bc->port, " --> *\tflash pid:%d\n", p->bc->pid);
  6073. break;
  6074. case AST_CONTROL_PROGRESS:
  6075. chan_misdn_log(1, p->bc->port, " --> * IND :\tprogress pid:%d\n", p->bc->pid);
  6076. misdn_lib_send_event(p->bc, EVENT_PROGRESS);
  6077. break;
  6078. case AST_CONTROL_PROCEEDING:
  6079. chan_misdn_log(1, p->bc->port, " --> * IND :\tproceeding pid:%d\n", p->bc->pid);
  6080. misdn_lib_send_event(p->bc, EVENT_PROCEEDING);
  6081. break;
  6082. case AST_CONTROL_INCOMPLETE:
  6083. chan_misdn_log(1, p->bc->port, " --> *\tincomplete pid:%d\n", p->bc->pid);
  6084. if (!p->overlap_dial) {
  6085. /* Overlapped dialing not enabled - send hangup */
  6086. p->bc->out_cause = AST_CAUSE_INVALID_NUMBER_FORMAT;
  6087. start_bc_tones(p);
  6088. misdn_lib_send_event(p->bc, EVENT_DISCONNECT);
  6089. if (p->bc->nt) {
  6090. hanguptone_indicate(p);
  6091. }
  6092. }
  6093. break;
  6094. case AST_CONTROL_CONGESTION:
  6095. chan_misdn_log(1, p->bc->port, " --> * IND :\tcongestion pid:%d\n", p->bc->pid);
  6096. p->bc->out_cause = AST_CAUSE_SWITCH_CONGESTION;
  6097. start_bc_tones(p);
  6098. misdn_lib_send_event(p->bc, EVENT_DISCONNECT);
  6099. if (p->bc->nt) {
  6100. hanguptone_indicate(p);
  6101. }
  6102. break;
  6103. case -1 :
  6104. chan_misdn_log(1, p->bc->port, " --> * IND :\t-1! (stop indication) pid:%d\n", p->bc->pid);
  6105. stop_indicate(p);
  6106. if (p->state == MISDN_CONNECTED) {
  6107. start_bc_tones(p);
  6108. }
  6109. break;
  6110. case AST_CONTROL_HOLD:
  6111. ast_moh_start(ast, data, p->mohinterpret);
  6112. chan_misdn_log(1, p->bc->port, " --> *\tHOLD pid:%d\n", p->bc->pid);
  6113. break;
  6114. case AST_CONTROL_UNHOLD:
  6115. ast_moh_stop(ast);
  6116. chan_misdn_log(1, p->bc->port, " --> *\tUNHOLD pid:%d\n", p->bc->pid);
  6117. break;
  6118. case AST_CONTROL_CONNECTED_LINE:
  6119. chan_misdn_log(1, p->bc->port, "* IND :\tconnected line update pid:%d\n", p->bc->pid);
  6120. misdn_update_connected_line(ast, p->bc, p->originator);
  6121. break;
  6122. case AST_CONTROL_REDIRECTING:
  6123. chan_misdn_log(1, p->bc->port, "* IND :\tredirecting info update pid:%d\n", p->bc->pid);
  6124. misdn_update_redirecting(ast, p->bc, p->originator);
  6125. break;
  6126. default:
  6127. chan_misdn_log(1, p->bc->port, " --> * Unknown Indication:%d pid:%d\n", cond, p->bc->pid);
  6128. return -1;
  6129. }
  6130. return 0;
  6131. }
  6132. static int misdn_hangup(struct ast_channel *ast)
  6133. {
  6134. struct chan_list *p;
  6135. struct misdn_bchannel *bc;
  6136. const char *var;
  6137. if (!ast) {
  6138. return -1;
  6139. }
  6140. ast_debug(1, "misdn_hangup(%s)\n", ast->name);
  6141. /* Take the ast_channel's tech_pvt reference. */
  6142. ast_mutex_lock(&release_lock);
  6143. p = MISDN_ASTERISK_TECH_PVT(ast);
  6144. if (!p) {
  6145. ast_mutex_unlock(&release_lock);
  6146. return -1;
  6147. }
  6148. MISDN_ASTERISK_TECH_PVT(ast) = NULL;
  6149. if (!misdn_chan_is_valid(p)) {
  6150. ast_mutex_unlock(&release_lock);
  6151. chan_list_unref(p, "Release ast_channel reference. Was not active?");
  6152. return 0;
  6153. }
  6154. if (p->hold.state == MISDN_HOLD_IDLE) {
  6155. bc = p->bc;
  6156. } else {
  6157. p->hold.state = MISDN_HOLD_DISCONNECT;
  6158. bc = misdn_lib_find_held_bc(p->hold.port, p->l3id);
  6159. if (!bc) {
  6160. chan_misdn_log(4, p->hold.port,
  6161. "misdn_hangup: Could not find held bc for (%s)\n", ast->name);
  6162. release_chan_early(p);
  6163. ast_mutex_unlock(&release_lock);
  6164. chan_list_unref(p, "Release ast_channel reference");
  6165. return 0;
  6166. }
  6167. }
  6168. if (ast->_state == AST_STATE_RESERVED || p->state == MISDN_NOTHING) {
  6169. /* between request and call */
  6170. ast_debug(1, "State Reserved (or nothing) => chanIsAvail\n");
  6171. release_chan_early(p);
  6172. if (bc) {
  6173. misdn_lib_release(bc);
  6174. }
  6175. ast_mutex_unlock(&release_lock);
  6176. chan_list_unref(p, "Release ast_channel reference");
  6177. return 0;
  6178. }
  6179. if (!bc) {
  6180. ast_log(LOG_WARNING, "Hangup with private but no bc ? state:%s l3id:%x\n",
  6181. misdn_get_ch_state(p), p->l3id);
  6182. release_chan_early(p);
  6183. ast_mutex_unlock(&release_lock);
  6184. chan_list_unref(p, "Release ast_channel reference");
  6185. return 0;
  6186. }
  6187. p->ast = NULL;
  6188. p->need_hangup = 0;
  6189. p->need_queue_hangup = 0;
  6190. p->need_busy = 0;
  6191. if (!bc->nt) {
  6192. stop_bc_tones(p);
  6193. }
  6194. bc->out_cause = ast->hangupcause ? ast->hangupcause : AST_CAUSE_NORMAL_CLEARING;
  6195. /* Channel lock is already held when we are called. */
  6196. //ast_channel_lock(ast);
  6197. var = pbx_builtin_getvar_helper(ast, "HANGUPCAUSE");
  6198. if (!var) {
  6199. var = pbx_builtin_getvar_helper(ast, "PRI_CAUSE");
  6200. }
  6201. if (var) {
  6202. int tmpcause;
  6203. tmpcause = atoi(var);
  6204. bc->out_cause = tmpcause ? tmpcause : AST_CAUSE_NORMAL_CLEARING;
  6205. }
  6206. var = pbx_builtin_getvar_helper(ast, "MISDN_USERUSER");
  6207. if (var) {
  6208. ast_log(LOG_NOTICE, "MISDN_USERUSER: %s\n", var);
  6209. ast_copy_string(bc->uu, var, sizeof(bc->uu));
  6210. bc->uulen = strlen(bc->uu);
  6211. }
  6212. //ast_channel_unlock(ast);
  6213. chan_misdn_log(1, bc->port,
  6214. "* IND : HANGUP\tpid:%d context:%s dialed:%s caller:\"%s\" <%s> State:%s\n",
  6215. bc->pid,
  6216. ast->context,
  6217. ast->exten,
  6218. (ast->caller.id.name.valid && ast->caller.id.name.str)
  6219. ? ast->caller.id.name.str : "",
  6220. (ast->caller.id.number.valid && ast->caller.id.number.str)
  6221. ? ast->caller.id.number.str : "",
  6222. misdn_get_ch_state(p));
  6223. chan_misdn_log(3, bc->port, " --> l3id:%x\n", p->l3id);
  6224. chan_misdn_log(3, bc->port, " --> cause:%d\n", bc->cause);
  6225. chan_misdn_log(2, bc->port, " --> out_cause:%d\n", bc->out_cause);
  6226. switch (p->state) {
  6227. case MISDN_INCOMING_SETUP:
  6228. /*
  6229. * This is the only place in misdn_hangup, where we
  6230. * can call release_chan, else it might create a lot of trouble.
  6231. */
  6232. ast_log(LOG_NOTICE, "release channel, in INCOMING_SETUP state.. no other events happened\n");
  6233. release_chan(p, bc);
  6234. misdn_lib_send_event(bc, EVENT_RELEASE_COMPLETE);
  6235. ast_mutex_unlock(&release_lock);
  6236. chan_list_unref(p, "Release ast_channel reference");
  6237. return 0;
  6238. case MISDN_DIALING:
  6239. if (p->hold.state == MISDN_HOLD_IDLE) {
  6240. start_bc_tones(p);
  6241. hanguptone_indicate(p);
  6242. }
  6243. if (bc->need_disconnect) {
  6244. misdn_lib_send_event(bc, EVENT_DISCONNECT);
  6245. }
  6246. break;
  6247. case MISDN_CALLING_ACKNOWLEDGE:
  6248. if (p->hold.state == MISDN_HOLD_IDLE) {
  6249. start_bc_tones(p);
  6250. hanguptone_indicate(p);
  6251. }
  6252. if (bc->need_disconnect) {
  6253. misdn_lib_send_event(bc, EVENT_DISCONNECT);
  6254. }
  6255. break;
  6256. case MISDN_CALLING:
  6257. case MISDN_ALERTING:
  6258. case MISDN_PROGRESS:
  6259. case MISDN_PROCEEDING:
  6260. if (p->originator != ORG_AST && p->hold.state == MISDN_HOLD_IDLE) {
  6261. hanguptone_indicate(p);
  6262. }
  6263. if (bc->need_disconnect) {
  6264. misdn_lib_send_event(bc, EVENT_DISCONNECT);
  6265. }
  6266. break;
  6267. case MISDN_CONNECTED:
  6268. /* Alerting or Disconnect */
  6269. if (bc->nt && p->hold.state == MISDN_HOLD_IDLE) {
  6270. start_bc_tones(p);
  6271. hanguptone_indicate(p);
  6272. bc->progress_indicator = INFO_PI_INBAND_AVAILABLE;
  6273. }
  6274. if (bc->need_disconnect) {
  6275. misdn_lib_send_event(bc, EVENT_DISCONNECT);
  6276. }
  6277. break;
  6278. case MISDN_DISCONNECTED:
  6279. if (bc->need_release) {
  6280. misdn_lib_send_event(bc, EVENT_RELEASE);
  6281. }
  6282. break;
  6283. case MISDN_CLEANING:
  6284. ast_mutex_unlock(&release_lock);
  6285. chan_list_unref(p, "Release ast_channel reference");
  6286. return 0;
  6287. case MISDN_BUSY:
  6288. break;
  6289. default:
  6290. if (bc->nt) {
  6291. bc->out_cause = -1;
  6292. if (bc->need_release) {
  6293. misdn_lib_send_event(bc, EVENT_RELEASE);
  6294. }
  6295. } else {
  6296. if (bc->need_disconnect) {
  6297. misdn_lib_send_event(bc, EVENT_DISCONNECT);
  6298. }
  6299. }
  6300. break;
  6301. }
  6302. p->state = MISDN_CLEANING;
  6303. chan_misdn_log(3, bc->port, " --> Channel: %s hungup new state:%s\n", ast->name,
  6304. misdn_get_ch_state(p));
  6305. ast_mutex_unlock(&release_lock);
  6306. chan_list_unref(p, "Release ast_channel reference");
  6307. return 0;
  6308. }
  6309. static struct ast_frame *process_ast_dsp(struct chan_list *tmp, struct ast_frame *frame)
  6310. {
  6311. struct ast_frame *f;
  6312. if (tmp->dsp) {
  6313. f = ast_dsp_process(tmp->ast, tmp->dsp, frame);
  6314. } else {
  6315. chan_misdn_log(0, tmp->bc->port, "No DSP-Path found\n");
  6316. return NULL;
  6317. }
  6318. if (!f || (f->frametype != AST_FRAME_DTMF)) {
  6319. return f;
  6320. }
  6321. ast_debug(1, "Detected inband DTMF digit: %c\n", f->subclass.integer);
  6322. if (tmp->faxdetect && (f->subclass.integer == 'f')) {
  6323. /* Fax tone -- Handle and return NULL */
  6324. if (!tmp->faxhandled) {
  6325. struct ast_channel *ast = tmp->ast;
  6326. tmp->faxhandled++;
  6327. chan_misdn_log(0, tmp->bc->port, "Fax detected, preparing %s for fax transfer.\n", ast->name);
  6328. tmp->bc->rxgain = 0;
  6329. isdn_lib_update_rxgain(tmp->bc);
  6330. tmp->bc->txgain = 0;
  6331. isdn_lib_update_txgain(tmp->bc);
  6332. #ifdef MISDN_1_2
  6333. *tmp->bc->pipeline = 0;
  6334. #else
  6335. tmp->bc->ec_enable = 0;
  6336. #endif
  6337. isdn_lib_update_ec(tmp->bc);
  6338. isdn_lib_stop_dtmf(tmp->bc);
  6339. switch (tmp->faxdetect) {
  6340. case 1:
  6341. if (strcmp(ast->exten, "fax")) {
  6342. char *context;
  6343. char context_tmp[BUFFERSIZE];
  6344. misdn_cfg_get(tmp->bc->port, MISDN_CFG_FAXDETECT_CONTEXT, &context_tmp, sizeof(context_tmp));
  6345. context = ast_strlen_zero(context_tmp) ? (ast_strlen_zero(ast->macrocontext) ? ast->context : ast->macrocontext) : context_tmp;
  6346. if (ast_exists_extension(ast, context, "fax", 1,
  6347. S_COR(ast->caller.id.number.valid, ast->caller.id.number.str, NULL))) {
  6348. ast_verb(3, "Redirecting %s to fax extension (context:%s)\n", ast->name, context);
  6349. /* Save the DID/DNIS when we transfer the fax call to a "fax" extension */
  6350. pbx_builtin_setvar_helper(ast,"FAXEXTEN",ast->exten);
  6351. if (ast_async_goto(ast, context, "fax", 1)) {
  6352. ast_log(LOG_WARNING, "Failed to async goto '%s' into fax of '%s'\n", ast->name, context);
  6353. }
  6354. } else {
  6355. ast_log(LOG_NOTICE, "Fax detected but no fax extension, context:%s exten:%s\n", context, ast->exten);
  6356. }
  6357. } else {
  6358. ast_debug(1, "Already in a fax extension, not redirecting\n");
  6359. }
  6360. break;
  6361. case 2:
  6362. ast_verb(3, "Not redirecting %s to fax extension, nojump is set.\n", ast->name);
  6363. break;
  6364. default:
  6365. break;
  6366. }
  6367. } else {
  6368. ast_debug(1, "Fax already handled\n");
  6369. }
  6370. }
  6371. if (tmp->ast_dsp && (f->subclass.integer != 'f')) {
  6372. chan_misdn_log(2, tmp->bc->port, " --> * SEND: DTMF (AST_DSP) :%c\n", f->subclass.integer);
  6373. }
  6374. return f;
  6375. }
  6376. static struct ast_frame *misdn_read(struct ast_channel *ast)
  6377. {
  6378. struct chan_list *tmp;
  6379. int len, t;
  6380. struct pollfd pfd = { .fd = -1, .events = POLLIN };
  6381. if (!ast) {
  6382. chan_misdn_log(1, 0, "misdn_read called without ast\n");
  6383. return NULL;
  6384. }
  6385. if (!(tmp = MISDN_ASTERISK_TECH_PVT(ast))) {
  6386. chan_misdn_log(1, 0, "misdn_read called without ast->pvt\n");
  6387. return NULL;
  6388. }
  6389. if (!tmp->bc && tmp->hold.state == MISDN_HOLD_IDLE) {
  6390. chan_misdn_log(1, 0, "misdn_read called without bc\n");
  6391. return NULL;
  6392. }
  6393. pfd.fd = tmp->pipe[0];
  6394. t = ast_poll(&pfd, 1, 20);
  6395. if (t < 0) {
  6396. chan_misdn_log(-1, tmp->bc->port, "poll() error (err=%s)\n", strerror(errno));
  6397. return NULL;
  6398. }
  6399. if (!t) {
  6400. chan_misdn_log(3, tmp->bc->port, "poll() timed out\n");
  6401. len = 160;
  6402. } else if (pfd.revents & POLLIN) {
  6403. len = read(tmp->pipe[0], tmp->ast_rd_buf, sizeof(tmp->ast_rd_buf));
  6404. if (len <= 0) {
  6405. /* we hangup here, since our pipe is closed */
  6406. chan_misdn_log(2, tmp->bc->port, "misdn_read: Pipe closed, hanging up\n");
  6407. return NULL;
  6408. }
  6409. } else {
  6410. return NULL;
  6411. }
  6412. tmp->frame.frametype = AST_FRAME_VOICE;
  6413. ast_format_set(&tmp->frame.subclass.format, AST_FORMAT_ALAW, 0);
  6414. tmp->frame.datalen = len;
  6415. tmp->frame.samples = len;
  6416. tmp->frame.mallocd = 0;
  6417. tmp->frame.offset = 0;
  6418. tmp->frame.delivery = ast_tv(0, 0);
  6419. tmp->frame.src = NULL;
  6420. tmp->frame.data.ptr = tmp->ast_rd_buf;
  6421. if (tmp->faxdetect && !tmp->faxhandled) {
  6422. if (tmp->faxdetect_timeout) {
  6423. if (ast_tvzero(tmp->faxdetect_tv)) {
  6424. tmp->faxdetect_tv = ast_tvnow();
  6425. chan_misdn_log(2, tmp->bc->port, "faxdetect: starting detection with timeout: %ds ...\n", tmp->faxdetect_timeout);
  6426. return process_ast_dsp(tmp, &tmp->frame);
  6427. } else {
  6428. struct timeval tv_now = ast_tvnow();
  6429. int diff = ast_tvdiff_ms(tv_now, tmp->faxdetect_tv);
  6430. if (diff <= (tmp->faxdetect_timeout * 1000)) {
  6431. chan_misdn_log(5, tmp->bc->port, "faxdetect: detecting ...\n");
  6432. return process_ast_dsp(tmp, &tmp->frame);
  6433. } else {
  6434. chan_misdn_log(2, tmp->bc->port, "faxdetect: stopping detection (time ran out) ...\n");
  6435. tmp->faxdetect = 0;
  6436. return &tmp->frame;
  6437. }
  6438. }
  6439. } else {
  6440. chan_misdn_log(5, tmp->bc->port, "faxdetect: detecting ... (no timeout)\n");
  6441. return process_ast_dsp(tmp, &tmp->frame);
  6442. }
  6443. } else {
  6444. if (tmp->ast_dsp) {
  6445. return process_ast_dsp(tmp, &tmp->frame);
  6446. } else {
  6447. return &tmp->frame;
  6448. }
  6449. }
  6450. }
  6451. static int misdn_write(struct ast_channel *ast, struct ast_frame *frame)
  6452. {
  6453. struct chan_list *ch;
  6454. if (!ast || !(ch = MISDN_ASTERISK_TECH_PVT(ast))) {
  6455. return -1;
  6456. }
  6457. if (ch->hold.state != MISDN_HOLD_IDLE) {
  6458. chan_misdn_log(7, 0, "misdn_write: Returning because hold active\n");
  6459. return 0;
  6460. }
  6461. if (!ch->bc) {
  6462. ast_log(LOG_WARNING, "private but no bc\n");
  6463. return -1;
  6464. }
  6465. if (ch->notxtone) {
  6466. chan_misdn_log(7, ch->bc->port, "misdn_write: Returning because notxtone\n");
  6467. return 0;
  6468. }
  6469. if (!frame->subclass.format.id) {
  6470. chan_misdn_log(4, ch->bc->port, "misdn_write: * prods us\n");
  6471. return 0;
  6472. }
  6473. if (ast_format_cmp(&frame->subclass.format, &prefformat) == AST_FORMAT_CMP_NOT_EQUAL) {
  6474. chan_misdn_log(-1, ch->bc->port, "Got Unsupported Frame with Format:%s\n", ast_getformatname(&frame->subclass.format));
  6475. return 0;
  6476. }
  6477. if (!frame->samples) {
  6478. chan_misdn_log(4, ch->bc->port, "misdn_write: zero write\n");
  6479. if (!strcmp(frame->src,"ast_prod")) {
  6480. chan_misdn_log(1, ch->bc->port, "misdn_write: state (%s) prodded.\n", misdn_get_ch_state(ch));
  6481. if (ch->ts) {
  6482. chan_misdn_log(4, ch->bc->port, "Starting Playtones\n");
  6483. misdn_lib_tone_generator_start(ch->bc);
  6484. }
  6485. return 0;
  6486. }
  6487. return -1;
  6488. }
  6489. if (!ch->bc->addr) {
  6490. chan_misdn_log(8, ch->bc->port, "misdn_write: no addr for bc dropping:%d\n", frame->samples);
  6491. return 0;
  6492. }
  6493. #ifdef MISDN_DEBUG
  6494. {
  6495. int i;
  6496. int max = 5 > frame->samples ? frame->samples : 5;
  6497. ast_debug(1, "write2mISDN %p %d bytes: ", p, frame->samples);
  6498. for (i = 0; i < max; i++) {
  6499. ast_debug(1, "%2.2x ", ((char *) frame->data.ptr)[i]);
  6500. }
  6501. }
  6502. #endif
  6503. switch (ch->bc->bc_state) {
  6504. case BCHAN_ACTIVATED:
  6505. case BCHAN_BRIDGED:
  6506. break;
  6507. default:
  6508. if (!ch->dropped_frame_cnt) {
  6509. chan_misdn_log(5, ch->bc->port,
  6510. "BC not active (nor bridged) dropping: %d frames addr:%x exten:%s cid:%s ch->state:%s bc_state:%d l3id:%x\n",
  6511. frame->samples, ch->bc->addr, ast->exten,
  6512. S_COR(ast->caller.id.number.valid, ast->caller.id.number.str, ""),
  6513. misdn_get_ch_state(ch), ch->bc->bc_state, ch->bc->l3_id);
  6514. }
  6515. if (++ch->dropped_frame_cnt > 100) {
  6516. ch->dropped_frame_cnt = 0;
  6517. 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);
  6518. }
  6519. return 0;
  6520. }
  6521. chan_misdn_log(9, ch->bc->port, "Sending :%d bytes to MISDN\n", frame->samples);
  6522. if (!ch->bc->nojitter && misdn_cap_is_speech(ch->bc->capability)) {
  6523. /* Buffered Transmit (triggered by read from isdn side)*/
  6524. if (misdn_jb_fill(ch->jb, frame->data.ptr, frame->samples) < 0) {
  6525. if (ch->bc->active) {
  6526. cb_log(0, ch->bc->port, "Misdn Jitterbuffer Overflow.\n");
  6527. }
  6528. }
  6529. } else {
  6530. /* transmit without jitterbuffer */
  6531. misdn_lib_tx2misdn_frm(ch->bc, frame->data.ptr, frame->samples);
  6532. }
  6533. return 0;
  6534. }
  6535. static enum ast_bridge_result misdn_bridge(struct ast_channel *c0,
  6536. struct ast_channel *c1, int flags,
  6537. struct ast_frame **fo,
  6538. struct ast_channel **rc,
  6539. int timeoutms)
  6540. {
  6541. struct chan_list *ch1, *ch2;
  6542. struct ast_channel *carr[2], *who;
  6543. int to = -1;
  6544. struct ast_frame *f;
  6545. int p1_b, p2_b;
  6546. int bridging;
  6547. ch1 = get_chan_by_ast(c0);
  6548. if (!ch1) {
  6549. return -1;
  6550. }
  6551. ch2 = get_chan_by_ast(c1);
  6552. if (!ch2) {
  6553. chan_list_unref(ch1, "Failed to find ch2");
  6554. return -1;
  6555. }
  6556. carr[0] = c0;
  6557. carr[1] = c1;
  6558. misdn_cfg_get(ch1->bc->port, MISDN_CFG_BRIDGING, &p1_b, sizeof(p1_b));
  6559. misdn_cfg_get(ch2->bc->port, MISDN_CFG_BRIDGING, &p2_b, sizeof(p2_b));
  6560. if (! p1_b || ! p2_b) {
  6561. ast_log(LOG_NOTICE, "Falling back to Asterisk bridging\n");
  6562. chan_list_unref(ch1, "Bridge fallback ch1");
  6563. chan_list_unref(ch2, "Bridge fallback ch2");
  6564. return AST_BRIDGE_FAILED;
  6565. }
  6566. misdn_cfg_get(0, MISDN_GEN_BRIDGING, &bridging, sizeof(bridging));
  6567. if (bridging) {
  6568. /* trying to make a mISDN_dsp conference */
  6569. chan_misdn_log(1, ch1->bc->port, "I SEND: Making conference with Number:%d\n", ch1->bc->pid + 1);
  6570. misdn_lib_bridge(ch1->bc, ch2->bc);
  6571. }
  6572. ast_verb(3, "Native bridging %s and %s\n", c0->name, c1->name);
  6573. chan_misdn_log(1, ch1->bc->port, "* Making Native Bridge between \"%s\" <%s> and \"%s\" <%s>\n",
  6574. ch1->bc->caller.name,
  6575. ch1->bc->caller.number,
  6576. ch2->bc->caller.name,
  6577. ch2->bc->caller.number);
  6578. if (!(flags & AST_BRIDGE_DTMF_CHANNEL_0)) {
  6579. ch1->ignore_dtmf = 1;
  6580. }
  6581. if (!(flags & AST_BRIDGE_DTMF_CHANNEL_1)) {
  6582. ch2->ignore_dtmf = 1;
  6583. }
  6584. for (;/*ever*/;) {
  6585. to = -1;
  6586. who = ast_waitfor_n(carr, 2, &to);
  6587. if (!who) {
  6588. ast_log(LOG_NOTICE, "misdn_bridge: empty read, breaking out\n");
  6589. break;
  6590. }
  6591. f = ast_read(who);
  6592. if (!f || f->frametype == AST_FRAME_CONTROL) {
  6593. /* got hangup .. */
  6594. if (!f) {
  6595. chan_misdn_log(4, ch1->bc->port, "Read Null Frame\n");
  6596. } else {
  6597. chan_misdn_log(4, ch1->bc->port, "Read Frame Control class:%d\n", f->subclass.integer);
  6598. }
  6599. *fo = f;
  6600. *rc = who;
  6601. break;
  6602. }
  6603. if (f->frametype == AST_FRAME_DTMF) {
  6604. chan_misdn_log(1, 0, "Read DTMF %d from %s\n", f->subclass.integer, who->exten);
  6605. *fo = f;
  6606. *rc = who;
  6607. break;
  6608. }
  6609. #if 0
  6610. if (f->frametype == AST_FRAME_VOICE) {
  6611. chan_misdn_log(1, ch1->bc->port, "I SEND: Splitting conference with Number:%d\n", ch1->bc->pid +1);
  6612. continue;
  6613. }
  6614. #endif
  6615. ast_write((who == c0) ? c1 : c0, f);
  6616. }
  6617. chan_misdn_log(1, ch1->bc->port, "I SEND: Splitting conference with Number:%d\n", ch1->bc->pid + 1);
  6618. misdn_lib_split_bridge(ch1->bc, ch2->bc);
  6619. chan_list_unref(ch1, "Bridge complete ch1");
  6620. chan_list_unref(ch2, "Bridge complete ch2");
  6621. return AST_BRIDGE_COMPLETE;
  6622. }
  6623. /** AST INDICATIONS END **/
  6624. static int dialtone_indicate(struct chan_list *cl)
  6625. {
  6626. struct ast_channel *ast = cl->ast;
  6627. int nd = 0;
  6628. if (!ast) {
  6629. chan_misdn_log(0, cl->bc->port, "No Ast in dialtone_indicate\n");
  6630. return -1;
  6631. }
  6632. misdn_cfg_get(cl->bc->port, MISDN_CFG_NODIALTONE, &nd, sizeof(nd));
  6633. if (nd) {
  6634. chan_misdn_log(1, cl->bc->port, "Not sending Dialtone, because config wants it\n");
  6635. return 0;
  6636. }
  6637. chan_misdn_log(3, cl->bc->port, " --> Dial\n");
  6638. cl->ts = ast_get_indication_tone(ast->zone, "dial");
  6639. if (cl->ts) {
  6640. cl->notxtone = 0;
  6641. cl->norxtone = 0;
  6642. /* This prods us in misdn_write */
  6643. ast_playtones_start(ast, 0, cl->ts->data, 0);
  6644. }
  6645. return 0;
  6646. }
  6647. static void hanguptone_indicate(struct chan_list *cl)
  6648. {
  6649. misdn_lib_send_tone(cl->bc, TONE_HANGUP);
  6650. }
  6651. static int stop_indicate(struct chan_list *cl)
  6652. {
  6653. struct ast_channel *ast = cl->ast;
  6654. if (!ast) {
  6655. chan_misdn_log(0, cl->bc->port, "No Ast in stop_indicate\n");
  6656. return -1;
  6657. }
  6658. chan_misdn_log(3, cl->bc->port, " --> None\n");
  6659. misdn_lib_tone_generator_stop(cl->bc);
  6660. ast_playtones_stop(ast);
  6661. if (cl->ts) {
  6662. cl->ts = ast_tone_zone_sound_unref(cl->ts);
  6663. }
  6664. return 0;
  6665. }
  6666. static int start_bc_tones(struct chan_list* cl)
  6667. {
  6668. misdn_lib_tone_generator_stop(cl->bc);
  6669. cl->notxtone = 0;
  6670. cl->norxtone = 0;
  6671. return 0;
  6672. }
  6673. static int stop_bc_tones(struct chan_list *cl)
  6674. {
  6675. if (!cl) {
  6676. return -1;
  6677. }
  6678. cl->notxtone = 1;
  6679. cl->norxtone = 1;
  6680. return 0;
  6681. }
  6682. /*!
  6683. * \internal
  6684. * \brief Destroy the chan_list object.
  6685. *
  6686. * \param obj chan_list object to destroy.
  6687. *
  6688. * \return Nothing
  6689. */
  6690. static void chan_list_destructor(void *obj)
  6691. {
  6692. struct chan_list *ch = obj;
  6693. #if defined(AST_MISDN_ENHANCEMENTS)
  6694. if (ch->peer) {
  6695. ao2_ref(ch->peer, -1);
  6696. ch->peer = NULL;
  6697. }
  6698. #endif /* AST_MISDN_ENHANCEMENTS */
  6699. if (ch->dsp) {
  6700. ast_dsp_free(ch->dsp);
  6701. ch->dsp = NULL;
  6702. }
  6703. /* releasing jitterbuffer */
  6704. if (ch->jb) {
  6705. misdn_jb_destroy(ch->jb);
  6706. ch->jb = NULL;
  6707. }
  6708. if (ch->overlap_dial) {
  6709. if (ch->overlap_dial_task != -1) {
  6710. misdn_tasks_remove(ch->overlap_dial_task);
  6711. ch->overlap_dial_task = -1;
  6712. }
  6713. ast_mutex_destroy(&ch->overlap_tv_lock);
  6714. }
  6715. if (-1 < ch->pipe[0]) {
  6716. close(ch->pipe[0]);
  6717. }
  6718. if (-1 < ch->pipe[1]) {
  6719. close(ch->pipe[1]);
  6720. }
  6721. }
  6722. /*! Returns a reference to the new chan_list. */
  6723. static struct chan_list *chan_list_init(int orig)
  6724. {
  6725. struct chan_list *cl;
  6726. cl = ao2_alloc(sizeof(*cl), chan_list_destructor);
  6727. if (!cl) {
  6728. chan_misdn_log(-1, 0, "misdn_request: malloc failed!");
  6729. return NULL;
  6730. }
  6731. cl->originator = orig;
  6732. cl->need_queue_hangup = 1;
  6733. cl->need_hangup = 1;
  6734. cl->need_busy = 1;
  6735. cl->overlap_dial_task = -1;
  6736. #if defined(AST_MISDN_ENHANCEMENTS)
  6737. cl->record_id = -1;
  6738. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  6739. cl->pipe[0] = -1;
  6740. cl->pipe[1] = -1;
  6741. return cl;
  6742. }
  6743. static struct ast_channel *misdn_request(const char *type, struct ast_format_cap *cap, const struct ast_channel *requestor, void *data, int *cause)
  6744. {
  6745. struct ast_channel *ast;
  6746. char group[BUFFERSIZE + 1] = "";
  6747. char dial_str[128];
  6748. char *dest_cp;
  6749. char *p = NULL;
  6750. int channel = 0;
  6751. int port = 0;
  6752. struct misdn_bchannel *newbc = NULL;
  6753. int dec = 0;
  6754. #if defined(AST_MISDN_ENHANCEMENTS)
  6755. int cc_retry_call = 0; /* TRUE if this is a call completion retry call */
  6756. long record_id = -1;
  6757. struct misdn_cc_record *cc_record;
  6758. const char *err_msg;
  6759. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  6760. struct chan_list *cl;
  6761. AST_DECLARE_APP_ARGS(args,
  6762. AST_APP_ARG(intf); /* interface token */
  6763. AST_APP_ARG(ext); /* extension token */
  6764. AST_APP_ARG(opts); /* options token */
  6765. );
  6766. snprintf(dial_str, sizeof(dial_str), "%s/%s", misdn_type, (char *) data);
  6767. /*
  6768. * data is ---v
  6769. * Dial(mISDN/g:group_name[/extension[/options]])
  6770. * Dial(mISDN/port[:preselected_channel][/extension[/options]])
  6771. * Dial(mISDN/cc/cc-record-id)
  6772. *
  6773. * The dial extension could be empty if you are using MISDN_KEYPAD
  6774. * to control ISDN provider features.
  6775. */
  6776. dest_cp = ast_strdupa(data);
  6777. AST_NONSTANDARD_APP_ARGS(args, dest_cp, '/');
  6778. if (!args.ext) {
  6779. args.ext = "";
  6780. }
  6781. if (!ast_strlen_zero(args.intf)) {
  6782. if (args.intf[0] == 'g' && args.intf[1] == ':') {
  6783. /* We make a group call lets checkout which ports are in my group */
  6784. args.intf += 2;
  6785. ast_copy_string(group, args.intf, sizeof(group));
  6786. chan_misdn_log(2, 0, " --> Group Call group: %s\n", group);
  6787. #if defined(AST_MISDN_ENHANCEMENTS)
  6788. } else if (strcmp(args.intf, "cc") == 0) {
  6789. cc_retry_call = 1;
  6790. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  6791. } else if ((p = strchr(args.intf, ':'))) {
  6792. /* we have a preselected channel */
  6793. *p++ = 0;
  6794. channel = atoi(p);
  6795. port = atoi(args.intf);
  6796. chan_misdn_log(2, port, " --> Call on preselected Channel (%d).\n", channel);
  6797. } else {
  6798. port = atoi(args.intf);
  6799. }
  6800. } else {
  6801. ast_log(LOG_WARNING, " --> ! IND : Dial(%s) WITHOUT Port or Group, check extensions.conf\n", dial_str);
  6802. return NULL;
  6803. }
  6804. #if defined(AST_MISDN_ENHANCEMENTS)
  6805. if (cc_retry_call) {
  6806. if (ast_strlen_zero(args.ext)) {
  6807. ast_log(LOG_WARNING, " --> ! IND : Dial(%s) WITHOUT cc-record-id, check extensions.conf\n", dial_str);
  6808. return NULL;
  6809. }
  6810. if (!isdigit(*args.ext)) {
  6811. ast_log(LOG_WARNING, " --> ! IND : Dial(%s) cc-record-id must be a number.\n", dial_str);
  6812. return NULL;
  6813. }
  6814. record_id = atol(args.ext);
  6815. AST_LIST_LOCK(&misdn_cc_records_db);
  6816. cc_record = misdn_cc_find_by_id(record_id);
  6817. if (!cc_record) {
  6818. AST_LIST_UNLOCK(&misdn_cc_records_db);
  6819. err_msg = misdn_cc_record_not_found;
  6820. ast_log(LOG_WARNING, " --> ! IND : Dial(%s) %s.\n", dial_str, err_msg);
  6821. return NULL;
  6822. }
  6823. if (!cc_record->activated) {
  6824. AST_LIST_UNLOCK(&misdn_cc_records_db);
  6825. err_msg = "Call completion has not been activated";
  6826. ast_log(LOG_WARNING, " --> ! IND : Dial(%s) %s.\n", dial_str, err_msg);
  6827. return NULL;
  6828. }
  6829. port = cc_record->port;
  6830. AST_LIST_UNLOCK(&misdn_cc_records_db);
  6831. }
  6832. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  6833. if (misdn_cfg_is_group_method(group, METHOD_STANDARD_DEC)) {
  6834. chan_misdn_log(4, port, " --> STARTING STANDARD DEC...\n");
  6835. dec = 1;
  6836. }
  6837. if (!ast_strlen_zero(group)) {
  6838. char cfg_group[BUFFERSIZE + 1];
  6839. struct robin_list *rr = NULL;
  6840. /* Group dial */
  6841. if (misdn_cfg_is_group_method(group, METHOD_ROUND_ROBIN)) {
  6842. chan_misdn_log(4, port, " --> STARTING ROUND ROBIN...\n");
  6843. rr = get_robin_position(group);
  6844. }
  6845. if (rr) {
  6846. int port_start;
  6847. int bchan_start;
  6848. int port_up;
  6849. int check;
  6850. int maxbchans;
  6851. int wraped = 0;
  6852. if (!rr->port) {
  6853. rr->port = misdn_cfg_get_next_port_spin(0);
  6854. }
  6855. if (!rr->channel) {
  6856. rr->channel = 1;
  6857. }
  6858. bchan_start = rr->channel;
  6859. port_start = rr->port;
  6860. do {
  6861. misdn_cfg_get(rr->port, MISDN_CFG_GROUPNAME, cfg_group, sizeof(cfg_group));
  6862. if (strcasecmp(cfg_group, group)) {
  6863. wraped = 1;
  6864. rr->port = misdn_cfg_get_next_port_spin(rr->port);
  6865. rr->channel = 1;
  6866. continue;
  6867. }
  6868. misdn_cfg_get(rr->port, MISDN_CFG_PMP_L1_CHECK, &check, sizeof(check));
  6869. port_up = misdn_lib_port_up(rr->port, check);
  6870. if (!port_up) {
  6871. chan_misdn_log(1, rr->port, "L1 is not Up on this Port\n");
  6872. rr->port = misdn_cfg_get_next_port_spin(rr->port);
  6873. rr->channel = 1;
  6874. } else if (port_up < 0) {
  6875. ast_log(LOG_WARNING, "This port (%d) is blocked\n", rr->port);
  6876. rr->port = misdn_cfg_get_next_port_spin(rr->port);
  6877. rr->channel = 1;
  6878. } else {
  6879. chan_misdn_log(4, rr->port, "portup\n");
  6880. maxbchans = misdn_lib_get_maxchans(rr->port);
  6881. for (;rr->channel <= maxbchans;rr->channel++) {
  6882. /* ive come full circle and can stop now */
  6883. if (wraped && (rr->port == port_start) && (rr->channel == bchan_start)) {
  6884. break;
  6885. }
  6886. chan_misdn_log(4, rr->port, "Checking channel %d\n", rr->channel);
  6887. if ((newbc = misdn_lib_get_free_bc(rr->port, rr->channel, 0, 0))) {
  6888. chan_misdn_log(4, rr->port, " Success! Found port:%d channel:%d\n", newbc->port, newbc->channel);
  6889. rr->channel++;
  6890. break;
  6891. }
  6892. }
  6893. if (wraped && (rr->port == port_start) && (rr->channel <= bchan_start)) {
  6894. break;
  6895. } else if (!newbc || (rr->channel == maxbchans)) {
  6896. rr->port = misdn_cfg_get_next_port_spin(rr->port);
  6897. rr->channel = 1;
  6898. }
  6899. }
  6900. wraped = 1;
  6901. } while (!newbc && (rr->port > 0));
  6902. } else {
  6903. for (port = misdn_cfg_get_next_port(0); port > 0;
  6904. port = misdn_cfg_get_next_port(port)) {
  6905. misdn_cfg_get(port, MISDN_CFG_GROUPNAME, cfg_group, sizeof(cfg_group));
  6906. chan_misdn_log(3, port, "Group [%s] Port [%d]\n", group, port);
  6907. if (!strcasecmp(cfg_group, group)) {
  6908. int port_up;
  6909. int check;
  6910. misdn_cfg_get(port, MISDN_CFG_PMP_L1_CHECK, &check, sizeof(check));
  6911. port_up = misdn_lib_port_up(port, check);
  6912. chan_misdn_log(4, port, "portup:%d\n", port_up);
  6913. if (port_up > 0) {
  6914. newbc = misdn_lib_get_free_bc(port, 0, 0, dec);
  6915. if (newbc) {
  6916. break;
  6917. }
  6918. }
  6919. }
  6920. }
  6921. }
  6922. /* Group dial failed ?*/
  6923. if (!newbc) {
  6924. ast_log(LOG_WARNING,
  6925. "Could not Dial out on group '%s'.\n"
  6926. "\tEither the L2 and L1 on all of these ports where DOWN (see 'show application misdn_check_l2l1')\n"
  6927. "\tOr there was no free channel on none of the ports\n\n",
  6928. group);
  6929. return NULL;
  6930. }
  6931. } else {
  6932. /* 'Normal' Port dial * Port dial */
  6933. if (channel) {
  6934. chan_misdn_log(1, port, " --> preselected_channel: %d\n", channel);
  6935. }
  6936. newbc = misdn_lib_get_free_bc(port, channel, 0, dec);
  6937. if (!newbc) {
  6938. ast_log(LOG_WARNING, "Could not create channel on port:%d for Dial(%s)\n", port, dial_str);
  6939. return NULL;
  6940. }
  6941. }
  6942. /* create ast_channel and link all the objects together */
  6943. cl = chan_list_init(ORG_AST);
  6944. if (!cl) {
  6945. misdn_lib_release(newbc);
  6946. ast_log(LOG_ERROR, "Could not create call record for Dial(%s)\n", dial_str);
  6947. return NULL;
  6948. }
  6949. cl->bc = newbc;
  6950. ast = misdn_new(cl, AST_STATE_RESERVED, args.ext, NULL, cap, requestor ? requestor->linkedid : NULL, port, channel);
  6951. if (!ast) {
  6952. chan_list_unref(cl, "Failed to create a new channel");
  6953. misdn_lib_release(newbc);
  6954. ast_log(LOG_ERROR, "Could not create Asterisk channel for Dial(%s)\n", dial_str);
  6955. return NULL;
  6956. }
  6957. #if defined(AST_MISDN_ENHANCEMENTS)
  6958. cl->record_id = record_id;
  6959. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  6960. /* register chan in local list */
  6961. cl_queue_chan(cl);
  6962. /* fill in the config into the objects */
  6963. read_config(cl);
  6964. /* important */
  6965. cl->need_hangup = 0;
  6966. chan_list_unref(cl, "Successful misdn_request()");
  6967. return ast;
  6968. }
  6969. static int misdn_send_text(struct ast_channel *chan, const char *text)
  6970. {
  6971. struct chan_list *tmp = MISDN_ASTERISK_TECH_PVT(chan);
  6972. if (tmp && tmp->bc) {
  6973. ast_copy_string(tmp->bc->display, text, sizeof(tmp->bc->display));
  6974. misdn_lib_send_event(tmp->bc, EVENT_INFORMATION);
  6975. } else {
  6976. ast_log(LOG_WARNING, "No chan_list but send_text request?\n");
  6977. return -1;
  6978. }
  6979. return 0;
  6980. }
  6981. static struct ast_channel_tech misdn_tech = {
  6982. .type = misdn_type,
  6983. .description = "Channel driver for mISDN Support (Bri/Pri)",
  6984. .requester = misdn_request,
  6985. .send_digit_begin = misdn_digit_begin,
  6986. .send_digit_end = misdn_digit_end,
  6987. .call = misdn_call,
  6988. .bridge = misdn_bridge,
  6989. .hangup = misdn_hangup,
  6990. .answer = misdn_answer,
  6991. .read = misdn_read,
  6992. .write = misdn_write,
  6993. .indicate = misdn_indication,
  6994. .fixup = misdn_fixup,
  6995. .send_text = misdn_send_text,
  6996. .properties = 0,
  6997. };
  6998. static struct ast_channel_tech misdn_tech_wo_bridge = {
  6999. .type = misdn_type,
  7000. .description = "Channel driver for mISDN Support (Bri/Pri)",
  7001. .requester = misdn_request,
  7002. .send_digit_begin = misdn_digit_begin,
  7003. .send_digit_end = misdn_digit_end,
  7004. .call = misdn_call,
  7005. .hangup = misdn_hangup,
  7006. .answer = misdn_answer,
  7007. .read = misdn_read,
  7008. .write = misdn_write,
  7009. .indicate = misdn_indication,
  7010. .fixup = misdn_fixup,
  7011. .send_text = misdn_send_text,
  7012. .properties = 0,
  7013. };
  7014. static int glob_channel = 0;
  7015. static void update_name(struct ast_channel *tmp, int port, int c)
  7016. {
  7017. int chan_offset = 0;
  7018. int tmp_port = misdn_cfg_get_next_port(0);
  7019. char newname[255];
  7020. for (; tmp_port > 0; tmp_port = misdn_cfg_get_next_port(tmp_port)) {
  7021. if (tmp_port == port) {
  7022. break;
  7023. }
  7024. chan_offset += misdn_lib_port_is_pri(tmp_port) ? 30 : 2;
  7025. }
  7026. if (c < 0) {
  7027. c = 0;
  7028. }
  7029. snprintf(newname, sizeof(newname), "%s/%d-", misdn_type, chan_offset + c);
  7030. if (strncmp(tmp->name, newname, strlen(newname))) {
  7031. snprintf(newname, sizeof(newname), "%s/%d-u%d", misdn_type, chan_offset + c, glob_channel++);
  7032. ast_change_name(tmp, newname);
  7033. chan_misdn_log(3, port, " --> updating channel name to [%s]\n", tmp->name);
  7034. }
  7035. }
  7036. static struct ast_channel *misdn_new(struct chan_list *chlist, int state, char *exten, char *callerid, struct ast_format_cap *cap, const char *linkedid, int port, int c)
  7037. {
  7038. struct ast_channel *tmp;
  7039. char *cid_name = NULL;
  7040. char *cid_num = NULL;
  7041. int chan_offset = 0;
  7042. int tmp_port = misdn_cfg_get_next_port(0);
  7043. int bridging;
  7044. struct ast_format tmpfmt;
  7045. for (; tmp_port > 0; tmp_port = misdn_cfg_get_next_port(tmp_port)) {
  7046. if (tmp_port == port) {
  7047. break;
  7048. }
  7049. chan_offset += misdn_lib_port_is_pri(tmp_port) ? 30 : 2;
  7050. }
  7051. if (c < 0) {
  7052. c = 0;
  7053. }
  7054. if (callerid) {
  7055. ast_callerid_parse(callerid, &cid_name, &cid_num);
  7056. }
  7057. tmp = ast_channel_alloc(1, state, cid_num, cid_name, "", exten, "", linkedid, 0, "%s/%s%d-u%d", misdn_type, c ? "" : "tmp", chan_offset + c, glob_channel++);
  7058. if (tmp) {
  7059. chan_misdn_log(2, port, " --> * NEW CHANNEL dialed:%s caller:%s\n", exten, callerid);
  7060. ast_best_codec(cap, &tmpfmt);
  7061. ast_format_cap_add(tmp->nativeformats, &prefformat);
  7062. ast_format_copy(&tmp->writeformat, &tmpfmt);
  7063. ast_format_copy(&tmp->rawwriteformat, &tmpfmt);
  7064. ast_format_copy(&tmp->readformat, &tmpfmt);
  7065. ast_format_copy(&tmp->rawreadformat, &tmpfmt);
  7066. /* Link the channel and private together */
  7067. chan_list_ref(chlist, "Give a reference to ast_channel");
  7068. MISDN_ASTERISK_TECH_PVT(tmp) = chlist;
  7069. chlist->ast = tmp;
  7070. misdn_cfg_get(0, MISDN_GEN_BRIDGING, &bridging, sizeof(bridging));
  7071. tmp->tech = bridging ? &misdn_tech : &misdn_tech_wo_bridge;
  7072. tmp->priority = 1;
  7073. if (exten) {
  7074. ast_copy_string(tmp->exten, exten, sizeof(tmp->exten));
  7075. } else {
  7076. chan_misdn_log(1, 0, "misdn_new: no exten given.\n");
  7077. }
  7078. if (!ast_strlen_zero(cid_num)) {
  7079. /* Don't use ast_set_callerid() here because it will
  7080. * generate a needless NewCallerID event */
  7081. tmp->caller.ani.number.valid = 1;
  7082. tmp->caller.ani.number.str = ast_strdup(cid_num);
  7083. }
  7084. if (pipe(chlist->pipe) < 0) {
  7085. ast_log(LOG_ERROR, "Pipe failed\n");
  7086. }
  7087. ast_channel_set_fd(tmp, 0, chlist->pipe[0]);
  7088. tmp->rings = (state == AST_STATE_RING) ? 1 : 0;
  7089. ast_jb_configure(tmp, misdn_get_global_jbconf());
  7090. } else {
  7091. chan_misdn_log(-1, 0, "Unable to allocate channel structure\n");
  7092. }
  7093. return tmp;
  7094. }
  7095. /*! Returns a reference to the found chan_list. */
  7096. static struct chan_list *find_chan_by_bc(struct misdn_bchannel *bc)
  7097. {
  7098. struct chan_list *help;
  7099. ast_mutex_lock(&cl_te_lock);
  7100. for (help = cl_te; help; help = help->next) {
  7101. if (help->bc == bc) {
  7102. chan_list_ref(help, "Found chan_list by bc");
  7103. ast_mutex_unlock(&cl_te_lock);
  7104. return help;
  7105. }
  7106. }
  7107. ast_mutex_unlock(&cl_te_lock);
  7108. chan_misdn_log(6, bc->port,
  7109. "$$$ find_chan_by_bc: No channel found for dialed:%s caller:\"%s\" <%s>\n",
  7110. bc->dialed.number,
  7111. bc->caller.name,
  7112. bc->caller.number);
  7113. return NULL;
  7114. }
  7115. /*! Returns a reference to the found chan_list. */
  7116. static struct chan_list *find_hold_call(struct misdn_bchannel *bc)
  7117. {
  7118. struct chan_list *help;
  7119. if (bc->pri) {
  7120. return NULL;
  7121. }
  7122. chan_misdn_log(6, bc->port, "$$$ find_hold_call: channel:%d dialed:%s caller:\"%s\" <%s>\n",
  7123. bc->channel,
  7124. bc->dialed.number,
  7125. bc->caller.name,
  7126. bc->caller.number);
  7127. ast_mutex_lock(&cl_te_lock);
  7128. for (help = cl_te; help; help = help->next) {
  7129. chan_misdn_log(4, bc->port, "$$$ find_hold_call: --> hold:%d channel:%d\n", help->hold.state, help->hold.channel);
  7130. if (help->hold.state == MISDN_HOLD_ACTIVE && help->hold.port == bc->port) {
  7131. chan_list_ref(help, "Found chan_list hold call");
  7132. ast_mutex_unlock(&cl_te_lock);
  7133. return help;
  7134. }
  7135. }
  7136. ast_mutex_unlock(&cl_te_lock);
  7137. chan_misdn_log(6, bc->port,
  7138. "$$$ find_hold_call: No channel found for dialed:%s caller:\"%s\" <%s>\n",
  7139. bc->dialed.number,
  7140. bc->caller.name,
  7141. bc->caller.number);
  7142. return NULL;
  7143. }
  7144. /*! Returns a reference to the found chan_list. */
  7145. static struct chan_list *find_hold_call_l3(unsigned long l3_id)
  7146. {
  7147. struct chan_list *help;
  7148. ast_mutex_lock(&cl_te_lock);
  7149. for (help = cl_te; help; help = help->next) {
  7150. if (help->hold.state != MISDN_HOLD_IDLE && help->l3id == l3_id) {
  7151. chan_list_ref(help, "Found chan_list hold call l3");
  7152. ast_mutex_unlock(&cl_te_lock);
  7153. return help;
  7154. }
  7155. }
  7156. ast_mutex_unlock(&cl_te_lock);
  7157. return NULL;
  7158. }
  7159. #define TRANSFER_ON_HELD_CALL_HANGUP 1
  7160. #if defined(TRANSFER_ON_HELD_CALL_HANGUP)
  7161. /*!
  7162. * \internal
  7163. * \brief Find a suitable active call to go with a held call so we could try a transfer.
  7164. *
  7165. * \param bc B channel record.
  7166. *
  7167. * \return Found call record or NULL.
  7168. *
  7169. * \note Returns a reference to the found chan_list.
  7170. *
  7171. * \note There could be a possibility where we find the wrong active call to transfer.
  7172. * This concern is mitigated by the fact that there could be at most one other call
  7173. * on a PTMP BRI link to another device. Maybe the l3_id could help in locating an
  7174. * active call on the same TEI?
  7175. */
  7176. static struct chan_list *find_hold_active_call(struct misdn_bchannel *bc)
  7177. {
  7178. struct chan_list *list;
  7179. ast_mutex_lock(&cl_te_lock);
  7180. for (list = cl_te; list; list = list->next) {
  7181. if (list->hold.state == MISDN_HOLD_IDLE && list->bc && list->bc->port == bc->port
  7182. && list->ast) {
  7183. switch (list->state) {
  7184. case MISDN_PROCEEDING:
  7185. case MISDN_PROGRESS:
  7186. case MISDN_ALERTING:
  7187. case MISDN_CONNECTED:
  7188. chan_list_ref(list, "Found chan_list hold active call");
  7189. ast_mutex_unlock(&cl_te_lock);
  7190. return list;
  7191. default:
  7192. break;
  7193. }
  7194. }
  7195. }
  7196. ast_mutex_unlock(&cl_te_lock);
  7197. return NULL;
  7198. }
  7199. #endif /* defined(TRANSFER_ON_HELD_CALL_HANGUP) */
  7200. static void cl_queue_chan(struct chan_list *chan)
  7201. {
  7202. chan_misdn_log(4, chan->bc ? chan->bc->port : 0, "* Queuing chan %p\n", chan);
  7203. chan_list_ref(chan, "Adding chan_list to list");
  7204. ast_mutex_lock(&cl_te_lock);
  7205. chan->next = NULL;
  7206. if (!cl_te) {
  7207. /* List is empty, make head of list. */
  7208. cl_te = chan;
  7209. } else {
  7210. struct chan_list *help;
  7211. /* Put at end of list. */
  7212. for (help = cl_te; help->next; help = help->next) {
  7213. }
  7214. help->next = chan;
  7215. }
  7216. ast_mutex_unlock(&cl_te_lock);
  7217. }
  7218. static int cl_dequeue_chan(struct chan_list *chan)
  7219. {
  7220. int found_it;
  7221. struct chan_list *help;
  7222. ast_mutex_lock(&cl_te_lock);
  7223. if (!cl_te) {
  7224. /* List is empty. */
  7225. ast_mutex_unlock(&cl_te_lock);
  7226. return 0;
  7227. }
  7228. if (cl_te == chan) {
  7229. /* What we want is the head of the list. */
  7230. cl_te = cl_te->next;
  7231. ast_mutex_unlock(&cl_te_lock);
  7232. chan_list_unref(chan, "Removed chan_list from list head");
  7233. return 1;
  7234. }
  7235. found_it = 0;
  7236. for (help = cl_te; help->next; help = help->next) {
  7237. if (help->next == chan) {
  7238. /* Found it in the list. */
  7239. help->next = help->next->next;
  7240. found_it = 1;
  7241. break;
  7242. }
  7243. }
  7244. ast_mutex_unlock(&cl_te_lock);
  7245. if (found_it) {
  7246. chan_list_unref(chan, "Removed chan_list from list");
  7247. }
  7248. return found_it;
  7249. }
  7250. /** Channel Queue End **/
  7251. static int pbx_start_chan(struct chan_list *ch)
  7252. {
  7253. int ret = ast_pbx_start(ch->ast);
  7254. ch->need_hangup = (ret >= 0) ? 0 : 1;
  7255. return ret;
  7256. }
  7257. static void hangup_chan(struct chan_list *ch, struct misdn_bchannel *bc)
  7258. {
  7259. int port = bc->port;
  7260. if (!ch) {
  7261. cb_log(1, port, "Cannot hangup chan, no ch\n");
  7262. return;
  7263. }
  7264. cb_log(5, port, "hangup_chan called\n");
  7265. if (ch->need_hangup) {
  7266. cb_log(2, port, " --> hangup\n");
  7267. ch->need_hangup = 0;
  7268. ch->need_queue_hangup = 0;
  7269. if (ch->ast && send_cause2ast(ch->ast, bc, ch)) {
  7270. ast_hangup(ch->ast);
  7271. }
  7272. return;
  7273. }
  7274. if (!ch->need_queue_hangup) {
  7275. cb_log(2, port, " --> No need to queue hangup\n");
  7276. return;
  7277. }
  7278. ch->need_queue_hangup = 0;
  7279. if (ch->ast) {
  7280. if (send_cause2ast(ch->ast, bc, ch)) {
  7281. ast_queue_hangup_with_cause(ch->ast, bc->cause);
  7282. cb_log(2, port, " --> queue_hangup\n");
  7283. }
  7284. } else {
  7285. cb_log(1, port, "Cannot hangup chan, no ast\n");
  7286. }
  7287. }
  7288. /*!
  7289. * \internal
  7290. * \brief ISDN asked us to release channel, pendant to misdn_hangup.
  7291. *
  7292. * \param ch Call channel record to release.
  7293. * \param bc Current B channel record associated with ch.
  7294. *
  7295. * \return Nothing
  7296. *
  7297. * \note The only valid thing to do with ch after calling is to chan_list_unref(ch, "").
  7298. */
  7299. static void release_chan(struct chan_list *ch, struct misdn_bchannel *bc)
  7300. {
  7301. struct ast_channel *ast;
  7302. chan_misdn_log(5, bc->port, "release_chan: bc with pid:%d l3id: %x\n", bc->pid, bc->l3_id);
  7303. ast_mutex_lock(&release_lock);
  7304. for (;;) {
  7305. ast = ch->ast;
  7306. if (!ast || !ast_channel_trylock(ast)) {
  7307. break;
  7308. }
  7309. DEADLOCK_AVOIDANCE(&release_lock);
  7310. }
  7311. if (!cl_dequeue_chan(ch)) {
  7312. /* Someone already released it. */
  7313. if (ast) {
  7314. ast_channel_unlock(ast);
  7315. }
  7316. ast_mutex_unlock(&release_lock);
  7317. return;
  7318. }
  7319. ch->state = MISDN_CLEANING;
  7320. ch->ast = NULL;
  7321. if (ast) {
  7322. struct chan_list *ast_ch;
  7323. ast_ch = MISDN_ASTERISK_TECH_PVT(ast);
  7324. MISDN_ASTERISK_TECH_PVT(ast) = NULL;
  7325. chan_misdn_log(1, bc->port,
  7326. "* RELEASING CHANNEL pid:%d context:%s dialed:%s caller:\"%s\" <%s>\n",
  7327. bc->pid,
  7328. ast->context,
  7329. ast->exten,
  7330. S_COR(ast->caller.id.name.valid, ast->caller.id.name.str, ""),
  7331. S_COR(ast->caller.id.number.valid, ast->caller.id.number.str, ""));
  7332. if (ast->_state != AST_STATE_RESERVED) {
  7333. chan_misdn_log(3, bc->port, " --> Setting AST State to down\n");
  7334. ast_setstate(ast, AST_STATE_DOWN);
  7335. }
  7336. ast_channel_unlock(ast);
  7337. if (ast_ch) {
  7338. chan_list_unref(ast_ch, "Release ast_channel reference.");
  7339. }
  7340. }
  7341. if (ch->originator == ORG_AST) {
  7342. --misdn_out_calls[bc->port];
  7343. } else {
  7344. --misdn_in_calls[bc->port];
  7345. }
  7346. ast_mutex_unlock(&release_lock);
  7347. }
  7348. /*!
  7349. * \internal
  7350. * \brief Do everything in release_chan() that makes sense without a bc.
  7351. *
  7352. * \param ch Call channel record to release.
  7353. *
  7354. * \return Nothing
  7355. *
  7356. * \note The only valid thing to do with ch after calling is to chan_list_unref(ch, "").
  7357. */
  7358. static void release_chan_early(struct chan_list *ch)
  7359. {
  7360. struct ast_channel *ast;
  7361. ast_mutex_lock(&release_lock);
  7362. for (;;) {
  7363. ast = ch->ast;
  7364. if (!ast || !ast_channel_trylock(ast)) {
  7365. break;
  7366. }
  7367. DEADLOCK_AVOIDANCE(&release_lock);
  7368. }
  7369. if (!cl_dequeue_chan(ch)) {
  7370. /* Someone already released it. */
  7371. if (ast) {
  7372. ast_channel_unlock(ast);
  7373. }
  7374. ast_mutex_unlock(&release_lock);
  7375. return;
  7376. }
  7377. ch->state = MISDN_CLEANING;
  7378. ch->ast = NULL;
  7379. if (ast) {
  7380. struct chan_list *ast_ch;
  7381. ast_ch = MISDN_ASTERISK_TECH_PVT(ast);
  7382. MISDN_ASTERISK_TECH_PVT(ast) = NULL;
  7383. if (ast->_state != AST_STATE_RESERVED) {
  7384. ast_setstate(ast, AST_STATE_DOWN);
  7385. }
  7386. ast_channel_unlock(ast);
  7387. if (ast_ch) {
  7388. chan_list_unref(ast_ch, "Release ast_channel reference.");
  7389. }
  7390. }
  7391. if (ch->hold.state != MISDN_HOLD_IDLE) {
  7392. if (ch->originator == ORG_AST) {
  7393. --misdn_out_calls[ch->hold.port];
  7394. } else {
  7395. --misdn_in_calls[ch->hold.port];
  7396. }
  7397. }
  7398. ast_mutex_unlock(&release_lock);
  7399. }
  7400. /*!
  7401. * \internal
  7402. * \brief Attempt to transfer the active channel party to the held channel party.
  7403. *
  7404. * \param active_ch Channel currently connected.
  7405. * \param held_ch Channel currently on hold.
  7406. *
  7407. * \retval 0 on success.
  7408. * \retval -1 on error.
  7409. */
  7410. static int misdn_attempt_transfer(struct chan_list *active_ch, struct chan_list *held_ch)
  7411. {
  7412. int retval;
  7413. struct ast_channel *target;
  7414. struct ast_channel *transferee;
  7415. struct ast_party_connected_line target_colp;
  7416. struct ast_party_connected_line transferee_colp;
  7417. switch (active_ch->state) {
  7418. case MISDN_PROCEEDING:
  7419. case MISDN_PROGRESS:
  7420. case MISDN_ALERTING:
  7421. case MISDN_CONNECTED:
  7422. break;
  7423. default:
  7424. return -1;
  7425. }
  7426. ast_channel_lock(held_ch->ast);
  7427. while (ast_channel_trylock(active_ch->ast)) {
  7428. CHANNEL_DEADLOCK_AVOIDANCE(held_ch->ast);
  7429. }
  7430. transferee = ast_bridged_channel(held_ch->ast);
  7431. if (!transferee) {
  7432. /*
  7433. * Could not transfer. Held channel is not bridged anymore.
  7434. * Held party probably got tired of waiting and hung up.
  7435. */
  7436. ast_channel_unlock(held_ch->ast);
  7437. ast_channel_unlock(active_ch->ast);
  7438. return -1;
  7439. }
  7440. target = active_ch->ast;
  7441. chan_misdn_log(1, held_ch->hold.port, "TRANSFERRING %s to %s\n",
  7442. held_ch->ast->name, target->name);
  7443. ast_party_connected_line_init(&target_colp);
  7444. ast_party_connected_line_copy(&target_colp, &target->connected);
  7445. ast_party_connected_line_init(&transferee_colp);
  7446. ast_party_connected_line_copy(&transferee_colp, &held_ch->ast->connected);
  7447. held_ch->hold.state = MISDN_HOLD_TRANSFER;
  7448. /*
  7449. * Before starting a masquerade, all channel and pvt locks must
  7450. * be unlocked. Any recursive channel locks held before
  7451. * ast_channel_transfer_masquerade() invalidates deadlock
  7452. * avoidance. Since we are unlocking both the pvt and its owner
  7453. * channel it is possible for "target" and "transferee" to be
  7454. * destroyed by their pbx threads. To prevent this we must give
  7455. * "target" and "transferee" a reference before any unlocking
  7456. * takes place.
  7457. */
  7458. ao2_ref(target, +1);
  7459. ao2_ref(transferee, +1);
  7460. ast_channel_unlock(held_ch->ast);
  7461. ast_channel_unlock(active_ch->ast);
  7462. /* Setup transfer masquerade. */
  7463. retval = ast_channel_transfer_masquerade(target, &target_colp, 0,
  7464. transferee, &transferee_colp, 1);
  7465. ast_party_connected_line_free(&target_colp);
  7466. ast_party_connected_line_free(&transferee_colp);
  7467. ao2_ref(target, -1);
  7468. ao2_ref(transferee, -1);
  7469. return retval;
  7470. }
  7471. static void do_immediate_setup(struct misdn_bchannel *bc, struct chan_list *ch, struct ast_channel *ast)
  7472. {
  7473. char *predial;
  7474. struct ast_frame fr;
  7475. predial = ast_strdupa(ast->exten);
  7476. ch->state = MISDN_DIALING;
  7477. if (!ch->noautorespond_on_setup) {
  7478. if (bc->nt) {
  7479. misdn_lib_send_event(bc, EVENT_SETUP_ACKNOWLEDGE);
  7480. } else {
  7481. if (misdn_lib_is_ptp(bc->port)) {
  7482. misdn_lib_send_event(bc, EVENT_SETUP_ACKNOWLEDGE);
  7483. } else {
  7484. misdn_lib_send_event(bc, EVENT_PROCEEDING);
  7485. }
  7486. }
  7487. } else {
  7488. ch->state = MISDN_INCOMING_SETUP;
  7489. }
  7490. chan_misdn_log(1, bc->port,
  7491. "* Starting Ast context:%s dialed:%s caller:\"%s\" <%s> with 's' extension\n",
  7492. ast->context,
  7493. ast->exten,
  7494. (ast->caller.id.name.valid && ast->caller.id.name.str)
  7495. ? ast->caller.id.name.str : "",
  7496. (ast->caller.id.number.valid && ast->caller.id.number.str)
  7497. ? ast->caller.id.number.str : "");
  7498. strcpy(ast->exten, "s");
  7499. if (!ast_canmatch_extension(ast, ast->context, ast->exten, 1, bc->caller.number) || pbx_start_chan(ch) < 0) {
  7500. ast = NULL;
  7501. bc->out_cause = AST_CAUSE_UNALLOCATED;
  7502. hangup_chan(ch, bc);
  7503. hanguptone_indicate(ch);
  7504. misdn_lib_send_event(bc, bc->nt ? EVENT_RELEASE_COMPLETE : EVENT_DISCONNECT);
  7505. }
  7506. while (!ast_strlen_zero(predial)) {
  7507. fr.frametype = AST_FRAME_DTMF;
  7508. fr.subclass.integer = *predial;
  7509. fr.src = NULL;
  7510. fr.data.ptr = NULL;
  7511. fr.datalen = 0;
  7512. fr.samples = 0;
  7513. fr.mallocd = 0;
  7514. fr.offset = 0;
  7515. fr.delivery = ast_tv(0,0);
  7516. if (ch->ast && MISDN_ASTERISK_TECH_PVT(ch->ast)) {
  7517. ast_queue_frame(ch->ast, &fr);
  7518. }
  7519. predial++;
  7520. }
  7521. }
  7522. /*!
  7523. * \retval -1 if can hangup after calling.
  7524. * \retval 0 if cannot hangup after calling.
  7525. */
  7526. static int send_cause2ast(struct ast_channel *ast, struct misdn_bchannel *bc, struct chan_list *ch)
  7527. {
  7528. int can_hangup;
  7529. if (!ast) {
  7530. chan_misdn_log(1, 0, "send_cause2ast: No Ast\n");
  7531. return 0;
  7532. }
  7533. if (!bc) {
  7534. chan_misdn_log(1, 0, "send_cause2ast: No BC\n");
  7535. return 0;
  7536. }
  7537. if (!ch) {
  7538. chan_misdn_log(1, 0, "send_cause2ast: No Ch\n");
  7539. return 0;
  7540. }
  7541. ast->hangupcause = bc->cause;
  7542. can_hangup = -1;
  7543. switch (bc->cause) {
  7544. case AST_CAUSE_UNALLOCATED:
  7545. case AST_CAUSE_NO_ROUTE_TRANSIT_NET:
  7546. case AST_CAUSE_NO_ROUTE_DESTINATION:
  7547. case 4: /* Send special information tone */
  7548. case AST_CAUSE_NUMBER_CHANGED:
  7549. case AST_CAUSE_DESTINATION_OUT_OF_ORDER:
  7550. /* Congestion Cases */
  7551. /*
  7552. * Not Queueing the Congestion anymore, since we want to hear
  7553. * the inband message
  7554. *
  7555. chan_misdn_log(1, bc ? bc->port : 0, " --> * SEND: Queue Congestion pid:%d\n", bc ? bc->pid : -1);
  7556. ch->state = MISDN_BUSY;
  7557. ast_queue_control(ast, AST_CONTROL_CONGESTION);
  7558. */
  7559. break;
  7560. case AST_CAUSE_CALL_REJECTED:
  7561. case AST_CAUSE_USER_BUSY:
  7562. ch->state = MISDN_BUSY;
  7563. if (!ch->need_busy) {
  7564. chan_misdn_log(1, bc ? bc->port : 0, "Queued busy already\n");
  7565. break;
  7566. }
  7567. ch->need_busy = 0;
  7568. chan_misdn_log(1, bc ? bc->port : 0, " --> * SEND: Queue Busy pid:%d\n", bc ? bc->pid : -1);
  7569. ast_queue_control(ast, AST_CONTROL_BUSY);
  7570. /* The BUSY is likely to cause a hangup or the user needs to hear it. */
  7571. can_hangup = 0;
  7572. break;
  7573. }
  7574. return can_hangup;
  7575. }
  7576. /*! \brief Import parameters from the dialplan environment variables */
  7577. void import_ch(struct ast_channel *chan, struct misdn_bchannel *bc, struct chan_list *ch)
  7578. {
  7579. const char *tmp;
  7580. ast_channel_lock(chan);
  7581. tmp = pbx_builtin_getvar_helper(chan, "MISDN_ADDRESS_COMPLETE");
  7582. if (tmp && (atoi(tmp) == 1)) {
  7583. bc->sending_complete = 1;
  7584. }
  7585. tmp = pbx_builtin_getvar_helper(chan, "MISDN_USERUSER");
  7586. if (tmp) {
  7587. ast_log(LOG_NOTICE, "MISDN_USERUSER: %s\n", tmp);
  7588. ast_copy_string(bc->uu, tmp, sizeof(bc->uu));
  7589. bc->uulen = strlen(bc->uu);
  7590. }
  7591. tmp = pbx_builtin_getvar_helper(chan, "MISDN_KEYPAD");
  7592. if (tmp) {
  7593. ast_copy_string(bc->keypad, tmp, sizeof(bc->keypad));
  7594. }
  7595. ast_channel_unlock(chan);
  7596. }
  7597. /*! \brief Export parameters to the dialplan environment variables */
  7598. void export_ch(struct ast_channel *chan, struct misdn_bchannel *bc, struct chan_list *ch)
  7599. {
  7600. char tmp[32];
  7601. /*
  7602. * The only use for MISDN_PID is if there is a problem and you
  7603. * have to use the "misdn restart pid" CLI command. Otherwise,
  7604. * the pid is not used by anyone. The internal use of MISDN_PID
  7605. * has been deleted.
  7606. */
  7607. chan_misdn_log(3, bc->port, " --> EXPORT_PID: pid:%d\n", bc->pid);
  7608. snprintf(tmp, sizeof(tmp), "%d", bc->pid);
  7609. pbx_builtin_setvar_helper(chan, "_MISDN_PID", tmp);
  7610. if (bc->sending_complete) {
  7611. snprintf(tmp, sizeof(tmp), "%d", bc->sending_complete);
  7612. pbx_builtin_setvar_helper(chan, "MISDN_ADDRESS_COMPLETE", tmp);
  7613. }
  7614. if (bc->urate) {
  7615. snprintf(tmp, sizeof(tmp), "%d", bc->urate);
  7616. pbx_builtin_setvar_helper(chan, "MISDN_URATE", tmp);
  7617. }
  7618. if (bc->uulen) {
  7619. pbx_builtin_setvar_helper(chan, "MISDN_USERUSER", bc->uu);
  7620. }
  7621. if (!ast_strlen_zero(bc->keypad)) {
  7622. pbx_builtin_setvar_helper(chan, "MISDN_KEYPAD", bc->keypad);
  7623. }
  7624. }
  7625. int add_in_calls(int port)
  7626. {
  7627. int max_in_calls;
  7628. misdn_cfg_get(port, MISDN_CFG_MAX_IN, &max_in_calls, sizeof(max_in_calls));
  7629. misdn_in_calls[port]++;
  7630. if (max_in_calls >= 0 && max_in_calls < misdn_in_calls[port]) {
  7631. ast_log(LOG_NOTICE, "Marking Incoming Call on port[%d]\n", port);
  7632. return misdn_in_calls[port] - max_in_calls;
  7633. }
  7634. return 0;
  7635. }
  7636. int add_out_calls(int port)
  7637. {
  7638. int max_out_calls;
  7639. misdn_cfg_get(port, MISDN_CFG_MAX_OUT, &max_out_calls, sizeof(max_out_calls));
  7640. if (max_out_calls >= 0 && max_out_calls <= misdn_out_calls[port]) {
  7641. ast_log(LOG_NOTICE, "Rejecting Outgoing Call on port[%d]\n", port);
  7642. return (misdn_out_calls[port] + 1) - max_out_calls;
  7643. }
  7644. misdn_out_calls[port]++;
  7645. return 0;
  7646. }
  7647. static void start_pbx(struct chan_list *ch, struct misdn_bchannel *bc, struct ast_channel *chan)
  7648. {
  7649. if (pbx_start_chan(ch) < 0) {
  7650. hangup_chan(ch, bc);
  7651. chan_misdn_log(-1, bc->port, "ast_pbx_start returned <0 in SETUP\n");
  7652. if (bc->nt) {
  7653. hanguptone_indicate(ch);
  7654. misdn_lib_send_event(bc, EVENT_RELEASE_COMPLETE);
  7655. } else {
  7656. misdn_lib_send_event(bc, EVENT_RELEASE);
  7657. }
  7658. }
  7659. }
  7660. static void wait_for_digits(struct chan_list *ch, struct misdn_bchannel *bc, struct ast_channel *chan)
  7661. {
  7662. ch->state = MISDN_WAITING4DIGS;
  7663. misdn_lib_send_event(bc, EVENT_SETUP_ACKNOWLEDGE);
  7664. if (bc->nt && !bc->dialed.number[0]) {
  7665. dialtone_indicate(ch);
  7666. }
  7667. }
  7668. #if defined(AST_MISDN_ENHANCEMENTS)
  7669. /*!
  7670. * \internal
  7671. * \brief Handle the FACILITY CCBSStatusRequest message.
  7672. *
  7673. * \param port Logical port number.
  7674. * \param facility Facility ie contents.
  7675. *
  7676. * \return Nothing
  7677. */
  7678. static void misdn_cc_handle_ccbs_status_request(int port, const struct FacParm *facility)
  7679. {
  7680. struct misdn_cc_record *cc_record;
  7681. struct misdn_bchannel dummy;
  7682. switch (facility->u.CCBSStatusRequest.ComponentType) {
  7683. case FacComponent_Invoke:
  7684. /* Build message */
  7685. misdn_make_dummy(&dummy, port, 0, misdn_lib_port_is_nt(port), 0);
  7686. dummy.fac_out.Function = Fac_CCBSStatusRequest;
  7687. dummy.fac_out.u.CCBSStatusRequest.InvokeID = facility->u.CCBSStatusRequest.InvokeID;
  7688. dummy.fac_out.u.CCBSStatusRequest.ComponentType = FacComponent_Result;
  7689. /* Answer User-A free question */
  7690. AST_LIST_LOCK(&misdn_cc_records_db);
  7691. cc_record = misdn_cc_find_by_reference(port, facility->u.CCBSStatusRequest.Component.Invoke.CCBSReference);
  7692. if (cc_record) {
  7693. dummy.fac_out.u.CCBSStatusRequest.Component.Result.Free = cc_record->party_a_free;
  7694. } else {
  7695. /* No record so say User-A is free */
  7696. dummy.fac_out.u.CCBSStatusRequest.Component.Result.Free = 1;
  7697. }
  7698. AST_LIST_UNLOCK(&misdn_cc_records_db);
  7699. /* Send message */
  7700. print_facility(&dummy.fac_out, &dummy);
  7701. misdn_lib_send_event(&dummy, EVENT_FACILITY);
  7702. break;
  7703. default:
  7704. chan_misdn_log(0, port, " --> not yet handled: facility type:0x%04X\n", facility->Function);
  7705. break;
  7706. }
  7707. }
  7708. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  7709. #if defined(AST_MISDN_ENHANCEMENTS)
  7710. /*!
  7711. * \internal
  7712. * \brief Start a PBX to notify that User-B is available.
  7713. *
  7714. * \param record_id Call completion record ID
  7715. * \param notify Dialplan location to start processing.
  7716. *
  7717. * \return Nothing
  7718. */
  7719. static void misdn_cc_pbx_notify(long record_id, const struct misdn_cc_notify *notify)
  7720. {
  7721. struct ast_channel *chan;
  7722. char id_str[32];
  7723. static unsigned short sequence = 0;
  7724. /* Create a channel to notify with */
  7725. snprintf(id_str, sizeof(id_str), "%ld", record_id);
  7726. chan = ast_channel_alloc(0, AST_STATE_DOWN, id_str, NULL, NULL,
  7727. notify->exten, notify->context, NULL, 0,
  7728. "mISDN-CC/%ld-%X", record_id, (unsigned) ++sequence);
  7729. if (!chan) {
  7730. ast_log(LOG_ERROR, "Unable to allocate channel!\n");
  7731. return;
  7732. }
  7733. chan->priority = notify->priority;
  7734. ast_free(chan->dialed.number.str);
  7735. chan->dialed.number.str = ast_strdup(notify->exten);
  7736. if (ast_pbx_start(chan)) {
  7737. ast_log(LOG_WARNING, "Unable to start pbx channel %s!\n", chan->name);
  7738. ast_channel_release(chan);
  7739. } else {
  7740. ast_verb(1, "Started pbx for call completion notify channel %s\n", chan->name);
  7741. }
  7742. }
  7743. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  7744. #if defined(AST_MISDN_ENHANCEMENTS)
  7745. /*!
  7746. * \internal
  7747. * \brief Handle the FACILITY CCBS_T_RemoteUserFree message.
  7748. *
  7749. * \param bc B channel control structure message came in on
  7750. *
  7751. * \return Nothing
  7752. */
  7753. static void misdn_cc_handle_T_remote_user_free(struct misdn_bchannel *bc)
  7754. {
  7755. struct misdn_cc_record *cc_record;
  7756. struct misdn_cc_notify notify;
  7757. long record_id;
  7758. AST_LIST_LOCK(&misdn_cc_records_db);
  7759. cc_record = misdn_cc_find_by_bc(bc);
  7760. if (cc_record) {
  7761. if (cc_record->party_a_free) {
  7762. notify = cc_record->remote_user_free;
  7763. } else {
  7764. /* Send CCBS_T_Suspend message */
  7765. bc->fac_out.Function = Fac_CCBS_T_Suspend;
  7766. bc->fac_out.u.CCBS_T_Suspend.InvokeID = ++misdn_invoke_id;
  7767. print_facility(&bc->fac_out, bc);
  7768. misdn_lib_send_event(bc, EVENT_FACILITY);
  7769. notify = cc_record->b_free;
  7770. }
  7771. record_id = cc_record->record_id;
  7772. AST_LIST_UNLOCK(&misdn_cc_records_db);
  7773. if (notify.context[0]) {
  7774. /* Party A is free or B-Free notify has been setup. */
  7775. misdn_cc_pbx_notify(record_id, &notify);
  7776. }
  7777. } else {
  7778. AST_LIST_UNLOCK(&misdn_cc_records_db);
  7779. }
  7780. }
  7781. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  7782. #if defined(AST_MISDN_ENHANCEMENTS)
  7783. /*!
  7784. * \internal
  7785. * \brief Handle the FACILITY CCBSRemoteUserFree message.
  7786. *
  7787. * \param port Logical port number.
  7788. * \param facility Facility ie contents.
  7789. *
  7790. * \return Nothing
  7791. */
  7792. static void misdn_cc_handle_remote_user_free(int port, const struct FacParm *facility)
  7793. {
  7794. struct misdn_cc_record *cc_record;
  7795. struct misdn_cc_notify notify;
  7796. long record_id;
  7797. AST_LIST_LOCK(&misdn_cc_records_db);
  7798. cc_record = misdn_cc_find_by_reference(port, facility->u.CCBSRemoteUserFree.CCBSReference);
  7799. if (cc_record) {
  7800. notify = cc_record->remote_user_free;
  7801. record_id = cc_record->record_id;
  7802. AST_LIST_UNLOCK(&misdn_cc_records_db);
  7803. misdn_cc_pbx_notify(record_id, &notify);
  7804. } else {
  7805. AST_LIST_UNLOCK(&misdn_cc_records_db);
  7806. }
  7807. }
  7808. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  7809. #if defined(AST_MISDN_ENHANCEMENTS)
  7810. /*!
  7811. * \internal
  7812. * \brief Handle the FACILITY CCBSBFree message.
  7813. *
  7814. * \param port Logical port number.
  7815. * \param facility Facility ie contents.
  7816. *
  7817. * \return Nothing
  7818. */
  7819. static void misdn_cc_handle_b_free(int port, const struct FacParm *facility)
  7820. {
  7821. struct misdn_cc_record *cc_record;
  7822. struct misdn_cc_notify notify;
  7823. long record_id;
  7824. AST_LIST_LOCK(&misdn_cc_records_db);
  7825. cc_record = misdn_cc_find_by_reference(port, facility->u.CCBSBFree.CCBSReference);
  7826. if (cc_record && cc_record->b_free.context[0]) {
  7827. /* B-Free notify has been setup. */
  7828. notify = cc_record->b_free;
  7829. record_id = cc_record->record_id;
  7830. AST_LIST_UNLOCK(&misdn_cc_records_db);
  7831. misdn_cc_pbx_notify(record_id, &notify);
  7832. } else {
  7833. AST_LIST_UNLOCK(&misdn_cc_records_db);
  7834. }
  7835. }
  7836. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  7837. /*!
  7838. * \internal
  7839. * \brief Handle the incoming facility ie contents
  7840. *
  7841. * \param event Message type facility ie came in on
  7842. * \param bc B channel control structure message came in on
  7843. * \param ch Associated channel call record
  7844. *
  7845. * \return Nothing
  7846. */
  7847. static void misdn_facility_ie_handler(enum event_e event, struct misdn_bchannel *bc, struct chan_list *ch)
  7848. {
  7849. #if defined(AST_MISDN_ENHANCEMENTS)
  7850. const char *diagnostic_msg;
  7851. struct misdn_cc_record *cc_record;
  7852. char buf[32];
  7853. struct misdn_party_id party_id;
  7854. long new_record_id;
  7855. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  7856. print_facility(&bc->fac_in, bc);
  7857. switch (bc->fac_in.Function) {
  7858. #if defined(AST_MISDN_ENHANCEMENTS)
  7859. case Fac_ActivationDiversion:
  7860. switch (bc->fac_in.u.ActivationDiversion.ComponentType) {
  7861. case FacComponent_Result:
  7862. /* Positive ACK to activation */
  7863. /* We don't handle this yet */
  7864. break;
  7865. default:
  7866. chan_misdn_log(0, bc->port," --> not yet handled: facility type:0x%04X\n",
  7867. bc->fac_in.Function);
  7868. break;
  7869. }
  7870. break;
  7871. case Fac_DeactivationDiversion:
  7872. switch (bc->fac_in.u.DeactivationDiversion.ComponentType) {
  7873. case FacComponent_Result:
  7874. /* Positive ACK to deactivation */
  7875. /* We don't handle this yet */
  7876. break;
  7877. default:
  7878. chan_misdn_log(0, bc->port," --> not yet handled: facility type:0x%04X\n",
  7879. bc->fac_in.Function);
  7880. break;
  7881. }
  7882. break;
  7883. case Fac_ActivationStatusNotificationDiv:
  7884. /* Sent to other MSN numbers on the line when a user activates call forwarding. */
  7885. /* Sent in the first call control message of an outgoing call from the served user. */
  7886. /* We do not have anything to do for this message. */
  7887. break;
  7888. case Fac_DeactivationStatusNotificationDiv:
  7889. /* Sent to other MSN numbers on the line when a user deactivates call forwarding. */
  7890. /* We do not have anything to do for this message. */
  7891. break;
  7892. #if 0 /* We don't handle this yet */
  7893. case Fac_InterrogationDiversion:
  7894. /* We don't handle this yet */
  7895. break;
  7896. case Fac_InterrogateServedUserNumbers:
  7897. /* We don't handle this yet */
  7898. break;
  7899. #endif /* We don't handle this yet */
  7900. case Fac_DiversionInformation:
  7901. /* Sent to the served user when a call is forwarded. */
  7902. /* We do not have anything to do for this message. */
  7903. break;
  7904. case Fac_CallDeflection:
  7905. if (ch && ch->ast) {
  7906. switch (bc->fac_in.u.CallDeflection.ComponentType) {
  7907. case FacComponent_Invoke:
  7908. ast_copy_string(bc->redirecting.from.number, bc->dialed.number,
  7909. sizeof(bc->redirecting.from.number));
  7910. bc->redirecting.from.name[0] = 0;
  7911. bc->redirecting.from.number_plan = bc->dialed.number_plan;
  7912. bc->redirecting.from.number_type = bc->dialed.number_type;
  7913. bc->redirecting.from.screening = 0;/* Unscreened */
  7914. if (bc->fac_in.u.CallDeflection.Component.Invoke.PresentationAllowedToDivertedToUserPresent) {
  7915. bc->redirecting.from.presentation =
  7916. bc->fac_in.u.CallDeflection.Component.Invoke.PresentationAllowedToDivertedToUser
  7917. ? 0 /* Allowed */ : 1 /* Restricted */;
  7918. } else {
  7919. bc->redirecting.from.presentation = 0;/* Allowed */
  7920. }
  7921. /* Add configured prefix to the call deflection number */
  7922. memset(&party_id, 0, sizeof(party_id));
  7923. misdn_PartyNumber_extract(&party_id,
  7924. &bc->fac_in.u.CallDeflection.Component.Invoke.Deflection.Party);
  7925. misdn_add_number_prefix(bc->port, party_id.number_type,
  7926. party_id.number, sizeof(party_id.number));
  7927. //party_id.presentation = 0;/* Allowed */
  7928. //party_id.screening = 0;/* Unscreened */
  7929. bc->redirecting.to = party_id;
  7930. ++bc->redirecting.count;
  7931. bc->redirecting.reason = mISDN_REDIRECTING_REASON_DEFLECTION;
  7932. misdn_copy_redirecting_to_ast(ch->ast, &bc->redirecting, bc->incoming_cid_tag);
  7933. ast_string_field_set(ch->ast, call_forward, bc->redirecting.to.number);
  7934. /* Send back positive ACK */
  7935. #if 1
  7936. /*
  7937. * Since there are no return result arguments it must be a
  7938. * generic result message. ETSI 300-196
  7939. */
  7940. bc->fac_out.Function = Fac_RESULT;
  7941. bc->fac_out.u.RESULT.InvokeID = bc->fac_in.u.CallDeflection.InvokeID;
  7942. #else
  7943. bc->fac_out.Function = Fac_CallDeflection;
  7944. bc->fac_out.u.CallDeflection.InvokeID = bc->fac_in.u.CallDeflection.InvokeID;
  7945. bc->fac_out.u.CallDeflection.ComponentType = FacComponent_Result;
  7946. #endif
  7947. print_facility(&bc->fac_out, bc);
  7948. misdn_lib_send_event(bc, EVENT_DISCONNECT);
  7949. /* This line is BUSY to further attempts by this dialing attempt. */
  7950. ast_queue_control(ch->ast, AST_CONTROL_BUSY);
  7951. break;
  7952. case FacComponent_Result:
  7953. /* Positive ACK to call deflection */
  7954. /*
  7955. * Sent in DISCONNECT or FACILITY message depending upon network option.
  7956. * It is in the FACILITY message if the call is still offered to the user
  7957. * while trying to alert the deflected to party.
  7958. */
  7959. /* Ignore the ACK */
  7960. break;
  7961. default:
  7962. break;
  7963. }
  7964. }
  7965. break;
  7966. #if 0 /* We don't handle this yet */
  7967. case Fac_CallRerouteing:
  7968. /* Private-Public ISDN interworking message */
  7969. /* We don't handle this yet */
  7970. break;
  7971. #endif /* We don't handle this yet */
  7972. case Fac_DivertingLegInformation1:
  7973. /* Private-Public ISDN interworking message */
  7974. bc->div_leg_3_rx_wanted = 0;
  7975. if (ch && ch->ast) {
  7976. bc->redirecting.reason =
  7977. diversion_reason_to_misdn(bc->fac_in.u.DivertingLegInformation1.DiversionReason);
  7978. if (bc->fac_in.u.DivertingLegInformation1.DivertedToPresent) {
  7979. misdn_PresentedNumberUnscreened_extract(&bc->redirecting.to,
  7980. &bc->fac_in.u.DivertingLegInformation1.DivertedTo);
  7981. /* Add configured prefix to redirecting.to.number */
  7982. misdn_add_number_prefix(bc->port, bc->redirecting.to.number_type,
  7983. bc->redirecting.to.number, sizeof(bc->redirecting.to.number));
  7984. } else {
  7985. bc->redirecting.to.number[0] = '\0';
  7986. bc->redirecting.to.number_plan = NUMPLAN_ISDN;
  7987. bc->redirecting.to.number_type = NUMTYPE_UNKNOWN;
  7988. bc->redirecting.to.presentation = 1;/* restricted */
  7989. bc->redirecting.to.screening = 0;/* unscreened */
  7990. }
  7991. misdn_copy_redirecting_to_ast(ch->ast, &bc->redirecting, bc->incoming_cid_tag);
  7992. bc->div_leg_3_rx_wanted = 1;
  7993. }
  7994. break;
  7995. case Fac_DivertingLegInformation2:
  7996. /* Private-Public ISDN interworking message */
  7997. switch (event) {
  7998. case EVENT_SETUP:
  7999. /* Comes in on a SETUP with redirecting.from information */
  8000. bc->div_leg_3_tx_pending = 1;
  8001. if (ch && ch->ast) {
  8002. /*
  8003. * Setup the redirecting.to informtion so we can identify
  8004. * if the user wants to manually supply the COLR for this
  8005. * redirected to number if further redirects could happen.
  8006. *
  8007. * All the user needs to do is set the REDIRECTING(to-pres)
  8008. * to the COLR and REDIRECTING(to-num) = ${EXTEN} to be safe
  8009. * after determining that the incoming call was redirected by
  8010. * checking if there is a REDIRECTING(from-num).
  8011. */
  8012. ast_copy_string(bc->redirecting.to.number, bc->dialed.number,
  8013. sizeof(bc->redirecting.to.number));
  8014. bc->redirecting.to.number_plan = bc->dialed.number_plan;
  8015. bc->redirecting.to.number_type = bc->dialed.number_type;
  8016. bc->redirecting.to.presentation = 1;/* restricted */
  8017. bc->redirecting.to.screening = 0;/* unscreened */
  8018. bc->redirecting.reason =
  8019. diversion_reason_to_misdn(bc->fac_in.u.DivertingLegInformation2.DiversionReason);
  8020. bc->redirecting.count = bc->fac_in.u.DivertingLegInformation2.DiversionCounter;
  8021. if (bc->fac_in.u.DivertingLegInformation2.DivertingPresent) {
  8022. /* This information is redundant if there was a redirecting ie in the SETUP. */
  8023. misdn_PresentedNumberUnscreened_extract(&bc->redirecting.from,
  8024. &bc->fac_in.u.DivertingLegInformation2.Diverting);
  8025. /* Add configured prefix to redirecting.from.number */
  8026. misdn_add_number_prefix(bc->port, bc->redirecting.from.number_type,
  8027. bc->redirecting.from.number, sizeof(bc->redirecting.from.number));
  8028. }
  8029. #if 0
  8030. if (bc->fac_in.u.DivertingLegInformation2.OriginalCalledPresent) {
  8031. /* We have no place to put the OriginalCalled number */
  8032. }
  8033. #endif
  8034. misdn_copy_redirecting_to_ast(ch->ast, &bc->redirecting, bc->incoming_cid_tag);
  8035. }
  8036. break;
  8037. default:
  8038. chan_misdn_log(0, bc->port," --> Expected in a SETUP message: facility type:0x%04X\n",
  8039. bc->fac_in.Function);
  8040. break;
  8041. }
  8042. break;
  8043. case Fac_DivertingLegInformation3:
  8044. /* Private-Public ISDN interworking message */
  8045. if (bc->div_leg_3_rx_wanted) {
  8046. bc->div_leg_3_rx_wanted = 0;
  8047. if (ch && ch->ast) {
  8048. ch->ast->redirecting.to.number.presentation =
  8049. bc->fac_in.u.DivertingLegInformation3.PresentationAllowedIndicator
  8050. ? AST_PRES_ALLOWED | AST_PRES_USER_NUMBER_UNSCREENED
  8051. : AST_PRES_RESTRICTED | AST_PRES_USER_NUMBER_UNSCREENED;
  8052. ast_channel_queue_redirecting_update(ch->ast, &ch->ast->redirecting, NULL);
  8053. }
  8054. }
  8055. break;
  8056. #else /* !defined(AST_MISDN_ENHANCEMENTS) */
  8057. case Fac_CD:
  8058. if (ch && ch->ast) {
  8059. ast_copy_string(bc->redirecting.from.number, bc->dialed.number,
  8060. sizeof(bc->redirecting.from.number));
  8061. bc->redirecting.from.name[0] = 0;
  8062. bc->redirecting.from.number_plan = bc->dialed.number_plan;
  8063. bc->redirecting.from.number_type = bc->dialed.number_type;
  8064. bc->redirecting.from.screening = 0;/* Unscreened */
  8065. bc->redirecting.from.presentation =
  8066. bc->fac_in.u.CDeflection.PresentationAllowed
  8067. ? 0 /* Allowed */ : 1 /* Restricted */;
  8068. ast_copy_string(bc->redirecting.to.number,
  8069. (char *) bc->fac_in.u.CDeflection.DeflectedToNumber,
  8070. sizeof(bc->redirecting.to.number));
  8071. bc->redirecting.to.name[0] = 0;
  8072. bc->redirecting.to.number_plan = NUMPLAN_UNKNOWN;
  8073. bc->redirecting.to.number_type = NUMTYPE_UNKNOWN;
  8074. bc->redirecting.to.presentation = 0;/* Allowed */
  8075. bc->redirecting.to.screening = 0;/* Unscreened */
  8076. ++bc->redirecting.count;
  8077. bc->redirecting.reason = mISDN_REDIRECTING_REASON_DEFLECTION;
  8078. misdn_copy_redirecting_to_ast(ch->ast, &bc->redirecting, bc->incoming_cid_tag);
  8079. ast_string_field_set(ch->ast, call_forward, bc->redirecting.to.number);
  8080. misdn_lib_send_event(bc, EVENT_DISCONNECT);
  8081. /* This line is BUSY to further attempts by this dialing attempt. */
  8082. ast_queue_control(ch->ast, AST_CONTROL_BUSY);
  8083. }
  8084. break;
  8085. #endif /* !defined(AST_MISDN_ENHANCEMENTS) */
  8086. case Fac_AOCDCurrency:
  8087. if (ch && ch->ast) {
  8088. bc->AOCDtype = Fac_AOCDCurrency;
  8089. memcpy(&bc->AOCD.currency, &bc->fac_in.u.AOCDcur, sizeof(bc->AOCD.currency));
  8090. bc->AOCD_need_export = 1;
  8091. export_aoc_vars(ch->originator, ch->ast, bc);
  8092. }
  8093. break;
  8094. case Fac_AOCDChargingUnit:
  8095. if (ch && ch->ast) {
  8096. bc->AOCDtype = Fac_AOCDChargingUnit;
  8097. memcpy(&bc->AOCD.chargingUnit, &bc->fac_in.u.AOCDchu, sizeof(bc->AOCD.chargingUnit));
  8098. bc->AOCD_need_export = 1;
  8099. export_aoc_vars(ch->originator, ch->ast, bc);
  8100. }
  8101. break;
  8102. #if defined(AST_MISDN_ENHANCEMENTS)
  8103. case Fac_ERROR:
  8104. diagnostic_msg = misdn_to_str_error_code(bc->fac_in.u.ERROR.errorValue);
  8105. chan_misdn_log(1, bc->port, " --> Facility error code: %s\n", diagnostic_msg);
  8106. switch (event) {
  8107. case EVENT_DISCONNECT:
  8108. case EVENT_RELEASE:
  8109. case EVENT_RELEASE_COMPLETE:
  8110. /* Possible call failure as a result of Fac_CCBSCall/Fac_CCBS_T_Call */
  8111. if (ch && ch->peer) {
  8112. misdn_cc_set_peer_var(ch->peer, MISDN_ERROR_MSG, diagnostic_msg);
  8113. }
  8114. break;
  8115. default:
  8116. break;
  8117. }
  8118. AST_LIST_LOCK(&misdn_cc_records_db);
  8119. cc_record = misdn_cc_find_by_invoke(bc->port, bc->fac_in.u.ERROR.invokeId);
  8120. if (cc_record) {
  8121. cc_record->outstanding_message = 0;
  8122. cc_record->error_code = bc->fac_in.u.ERROR.errorValue;
  8123. }
  8124. AST_LIST_UNLOCK(&misdn_cc_records_db);
  8125. break;
  8126. case Fac_REJECT:
  8127. diagnostic_msg = misdn_to_str_reject_code(bc->fac_in.u.REJECT.Code);
  8128. chan_misdn_log(1, bc->port, " --> Facility reject code: %s\n", diagnostic_msg);
  8129. switch (event) {
  8130. case EVENT_DISCONNECT:
  8131. case EVENT_RELEASE:
  8132. case EVENT_RELEASE_COMPLETE:
  8133. /* Possible call failure as a result of Fac_CCBSCall/Fac_CCBS_T_Call */
  8134. if (ch && ch->peer) {
  8135. misdn_cc_set_peer_var(ch->peer, MISDN_ERROR_MSG, diagnostic_msg);
  8136. }
  8137. break;
  8138. default:
  8139. break;
  8140. }
  8141. if (bc->fac_in.u.REJECT.InvokeIDPresent) {
  8142. AST_LIST_LOCK(&misdn_cc_records_db);
  8143. cc_record = misdn_cc_find_by_invoke(bc->port, bc->fac_in.u.REJECT.InvokeID);
  8144. if (cc_record) {
  8145. cc_record->outstanding_message = 0;
  8146. cc_record->reject_code = bc->fac_in.u.REJECT.Code;
  8147. }
  8148. AST_LIST_UNLOCK(&misdn_cc_records_db);
  8149. }
  8150. break;
  8151. case Fac_RESULT:
  8152. AST_LIST_LOCK(&misdn_cc_records_db);
  8153. cc_record = misdn_cc_find_by_invoke(bc->port, bc->fac_in.u.RESULT.InvokeID);
  8154. if (cc_record) {
  8155. cc_record->outstanding_message = 0;
  8156. }
  8157. AST_LIST_UNLOCK(&misdn_cc_records_db);
  8158. break;
  8159. #if 0 /* We don't handle this yet */
  8160. case Fac_EctExecute:
  8161. /* We don't handle this yet */
  8162. break;
  8163. case Fac_ExplicitEctExecute:
  8164. /* We don't handle this yet */
  8165. break;
  8166. case Fac_EctLinkIdRequest:
  8167. /* We don't handle this yet */
  8168. break;
  8169. #endif /* We don't handle this yet */
  8170. case Fac_SubaddressTransfer:
  8171. /* We do not have anything to do for this message since we do not handle subaddreses. */
  8172. break;
  8173. case Fac_RequestSubaddress:
  8174. /*
  8175. * We do not have anything to do for this message since we do not handle subaddreses.
  8176. * However, we do care about some other ie's that should be present.
  8177. */
  8178. if (bc->redirecting.to_changed) {
  8179. /* Add configured prefix to redirecting.to.number */
  8180. misdn_add_number_prefix(bc->port, bc->redirecting.to.number_type,
  8181. bc->redirecting.to.number, sizeof(bc->redirecting.to.number));
  8182. }
  8183. switch (bc->notify_description_code) {
  8184. case mISDN_NOTIFY_CODE_INVALID:
  8185. /* Notify ie was not present. */
  8186. bc->redirecting.to_changed = 0;
  8187. break;
  8188. case mISDN_NOTIFY_CODE_CALL_TRANSFER_ALERTING:
  8189. /*
  8190. * It would be preferable to update the connected line information
  8191. * only when the message callStatus is active. However, the
  8192. * optional redirection number may not be present in the active
  8193. * message if an alerting message were received earlier.
  8194. *
  8195. * The consequences if we wind up sending two updates is benign.
  8196. * The other end will think that it got transferred twice.
  8197. */
  8198. if (!bc->redirecting.to_changed) {
  8199. break;
  8200. }
  8201. bc->redirecting.to_changed = 0;
  8202. if (!ch || !ch->ast) {
  8203. break;
  8204. }
  8205. misdn_update_remote_party(ch->ast, &bc->redirecting.to,
  8206. AST_CONNECTED_LINE_UPDATE_SOURCE_TRANSFER_ALERTING,
  8207. bc->incoming_cid_tag);
  8208. break;
  8209. case mISDN_NOTIFY_CODE_CALL_TRANSFER_ACTIVE:
  8210. if (!bc->redirecting.to_changed) {
  8211. break;
  8212. }
  8213. bc->redirecting.to_changed = 0;
  8214. if (!ch || !ch->ast) {
  8215. break;
  8216. }
  8217. misdn_update_remote_party(ch->ast, &bc->redirecting.to,
  8218. AST_CONNECTED_LINE_UPDATE_SOURCE_TRANSFER, bc->incoming_cid_tag);
  8219. break;
  8220. default:
  8221. bc->redirecting.to_changed = 0;
  8222. chan_misdn_log(0, bc->port," --> not yet handled: notify code:0x%02X\n",
  8223. bc->notify_description_code);
  8224. break;
  8225. }
  8226. bc->notify_description_code = mISDN_NOTIFY_CODE_INVALID;
  8227. break;
  8228. case Fac_EctInform:
  8229. /* Private-Public ISDN interworking message */
  8230. if (ch && ch->ast && bc->fac_in.u.EctInform.RedirectionPresent) {
  8231. /* Add configured prefix to the redirection number */
  8232. memset(&party_id, 0, sizeof(party_id));
  8233. misdn_PresentedNumberUnscreened_extract(&party_id,
  8234. &bc->fac_in.u.EctInform.Redirection);
  8235. misdn_add_number_prefix(bc->port, party_id.number_type,
  8236. party_id.number, sizeof(party_id.number));
  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. misdn_update_remote_party(ch->ast, &party_id,
  8247. (bc->fac_in.u.EctInform.Status == 0 /* alerting */)
  8248. ? AST_CONNECTED_LINE_UPDATE_SOURCE_TRANSFER_ALERTING
  8249. : AST_CONNECTED_LINE_UPDATE_SOURCE_TRANSFER,
  8250. bc->incoming_cid_tag);
  8251. }
  8252. break;
  8253. #if 0 /* We don't handle this yet */
  8254. case Fac_EctLoopTest:
  8255. /* The use of this message is unclear on how it works to detect loops. */
  8256. /* We don't handle this yet */
  8257. break;
  8258. #endif /* We don't handle this yet */
  8259. case Fac_CallInfoRetain:
  8260. switch (event) {
  8261. case EVENT_ALERTING:
  8262. case EVENT_DISCONNECT:
  8263. /* CCBS/CCNR is available */
  8264. if (ch && ch->peer) {
  8265. AST_LIST_LOCK(&misdn_cc_records_db);
  8266. if (ch->record_id == -1) {
  8267. cc_record = misdn_cc_new();
  8268. } else {
  8269. /*
  8270. * We are doing a call-completion attempt
  8271. * or the switch is sending us extra call-completion
  8272. * availability indications (erroneously?).
  8273. *
  8274. * Assume that the network request retention option
  8275. * is not on and that the current call-completion
  8276. * request is disabled.
  8277. */
  8278. cc_record = misdn_cc_find_by_id(ch->record_id);
  8279. if (cc_record) {
  8280. if (cc_record->ptp && cc_record->mode.ptp.bc) {
  8281. /*
  8282. * What? We are getting mixed messages from the
  8283. * switch. We are currently setup for
  8284. * point-to-point. Now we are switching to
  8285. * point-to-multipoint.
  8286. *
  8287. * Close the call-completion signaling link
  8288. */
  8289. cc_record->mode.ptp.bc->fac_out.Function = Fac_None;
  8290. cc_record->mode.ptp.bc->out_cause = AST_CAUSE_NORMAL_CLEARING;
  8291. misdn_lib_send_event(cc_record->mode.ptp.bc, EVENT_RELEASE_COMPLETE);
  8292. }
  8293. /*
  8294. * Resetup the existing record for a possible new
  8295. * call-completion request.
  8296. */
  8297. new_record_id = misdn_cc_record_id_new();
  8298. if (new_record_id < 0) {
  8299. /* Looks like we must keep the old id anyway. */
  8300. } else {
  8301. cc_record->record_id = new_record_id;
  8302. ch->record_id = new_record_id;
  8303. }
  8304. cc_record->ptp = 0;
  8305. cc_record->port = bc->port;
  8306. memset(&cc_record->mode, 0, sizeof(cc_record->mode));
  8307. cc_record->mode.ptmp.linkage_id = bc->fac_in.u.CallInfoRetain.CallLinkageID;
  8308. cc_record->invoke_id = ++misdn_invoke_id;
  8309. cc_record->activated = 0;
  8310. cc_record->outstanding_message = 0;
  8311. cc_record->activation_requested = 0;
  8312. cc_record->error_code = FacError_None;
  8313. cc_record->reject_code = FacReject_None;
  8314. memset(&cc_record->remote_user_free, 0, sizeof(cc_record->remote_user_free));
  8315. memset(&cc_record->b_free, 0, sizeof(cc_record->b_free));
  8316. cc_record->time_created = time(NULL);
  8317. cc_record = NULL;
  8318. } else {
  8319. /*
  8320. * Where did the record go? We will have to recapture
  8321. * the call setup information. Unfortunately, some
  8322. * setup information may have been changed.
  8323. */
  8324. ch->record_id = -1;
  8325. cc_record = misdn_cc_new();
  8326. }
  8327. }
  8328. if (cc_record) {
  8329. ch->record_id = cc_record->record_id;
  8330. cc_record->ptp = 0;
  8331. cc_record->port = bc->port;
  8332. cc_record->mode.ptmp.linkage_id = bc->fac_in.u.CallInfoRetain.CallLinkageID;
  8333. /* Record call information for possible call-completion attempt. */
  8334. cc_record->redial.caller = bc->caller;
  8335. cc_record->redial.dialed = bc->dialed;
  8336. cc_record->redial.setup_bc_hlc_llc = bc->setup_bc_hlc_llc;
  8337. cc_record->redial.capability = bc->capability;
  8338. cc_record->redial.hdlc = bc->hdlc;
  8339. }
  8340. AST_LIST_UNLOCK(&misdn_cc_records_db);
  8341. /* Set MISDN_CC_RECORD_ID in original channel */
  8342. if (ch->record_id != -1) {
  8343. snprintf(buf, sizeof(buf), "%ld", ch->record_id);
  8344. } else {
  8345. buf[0] = 0;
  8346. }
  8347. misdn_cc_set_peer_var(ch->peer, MISDN_CC_RECORD_ID, buf);
  8348. }
  8349. break;
  8350. default:
  8351. chan_misdn_log(0, bc->port,
  8352. " --> Expected in a DISCONNECT or ALERTING message: facility type:0x%04X\n",
  8353. bc->fac_in.Function);
  8354. break;
  8355. }
  8356. break;
  8357. case Fac_CCBS_T_Call:
  8358. case Fac_CCBSCall:
  8359. switch (event) {
  8360. case EVENT_SETUP:
  8361. /*
  8362. * This is a call completion retry call.
  8363. * If we had anything to do we would do it here.
  8364. */
  8365. break;
  8366. default:
  8367. chan_misdn_log(0, bc->port, " --> Expected in a SETUP message: facility type:0x%04X\n",
  8368. bc->fac_in.Function);
  8369. break;
  8370. }
  8371. break;
  8372. case Fac_CCBSDeactivate:
  8373. switch (bc->fac_in.u.CCBSDeactivate.ComponentType) {
  8374. case FacComponent_Result:
  8375. AST_LIST_LOCK(&misdn_cc_records_db);
  8376. cc_record = misdn_cc_find_by_invoke(bc->port, bc->fac_in.u.CCBSDeactivate.InvokeID);
  8377. if (cc_record) {
  8378. cc_record->outstanding_message = 0;
  8379. }
  8380. AST_LIST_UNLOCK(&misdn_cc_records_db);
  8381. break;
  8382. default:
  8383. chan_misdn_log(0, bc->port, " --> not yet handled: facility type:0x%04X\n",
  8384. bc->fac_in.Function);
  8385. break;
  8386. }
  8387. break;
  8388. case Fac_CCBSErase:
  8389. AST_LIST_LOCK(&misdn_cc_records_db);
  8390. cc_record = misdn_cc_find_by_reference(bc->port, bc->fac_in.u.CCBSErase.CCBSReference);
  8391. if (cc_record) {
  8392. misdn_cc_delete(cc_record);
  8393. }
  8394. AST_LIST_UNLOCK(&misdn_cc_records_db);
  8395. break;
  8396. case Fac_CCBSRemoteUserFree:
  8397. misdn_cc_handle_remote_user_free(bc->port, &bc->fac_in);
  8398. break;
  8399. case Fac_CCBSBFree:
  8400. misdn_cc_handle_b_free(bc->port, &bc->fac_in);
  8401. break;
  8402. case Fac_CCBSStatusRequest:
  8403. misdn_cc_handle_ccbs_status_request(bc->port, &bc->fac_in);
  8404. break;
  8405. case Fac_EraseCallLinkageID:
  8406. AST_LIST_LOCK(&misdn_cc_records_db);
  8407. cc_record = misdn_cc_find_by_linkage(bc->port,
  8408. bc->fac_in.u.EraseCallLinkageID.CallLinkageID);
  8409. if (cc_record && !cc_record->activation_requested) {
  8410. /*
  8411. * The T-RETENTION timer expired before we requested
  8412. * call completion activation. Call completion is no
  8413. * longer available.
  8414. */
  8415. misdn_cc_delete(cc_record);
  8416. }
  8417. AST_LIST_UNLOCK(&misdn_cc_records_db);
  8418. break;
  8419. case Fac_CCBSStopAlerting:
  8420. /* We do not have anything to do for this message. */
  8421. break;
  8422. case Fac_CCBSRequest:
  8423. case Fac_CCNRRequest:
  8424. switch (bc->fac_in.u.CCBSRequest.ComponentType) {
  8425. case FacComponent_Result:
  8426. AST_LIST_LOCK(&misdn_cc_records_db);
  8427. cc_record = misdn_cc_find_by_invoke(bc->port, bc->fac_in.u.CCBSRequest.InvokeID);
  8428. if (cc_record && !cc_record->ptp) {
  8429. cc_record->outstanding_message = 0;
  8430. cc_record->activated = 1;
  8431. cc_record->mode.ptmp.recall_mode = bc->fac_in.u.CCBSRequest.Component.Result.RecallMode;
  8432. cc_record->mode.ptmp.reference_id = bc->fac_in.u.CCBSRequest.Component.Result.CCBSReference;
  8433. }
  8434. AST_LIST_UNLOCK(&misdn_cc_records_db);
  8435. break;
  8436. default:
  8437. chan_misdn_log(0, bc->port, " --> not yet handled: facility type:0x%04X\n",
  8438. bc->fac_in.Function);
  8439. break;
  8440. }
  8441. break;
  8442. #if 0 /* We don't handle this yet */
  8443. case Fac_CCBSInterrogate:
  8444. case Fac_CCNRInterrogate:
  8445. /* We don't handle this yet */
  8446. break;
  8447. case Fac_StatusRequest:
  8448. /* We don't handle this yet */
  8449. break;
  8450. #endif /* We don't handle this yet */
  8451. #if 0 /* We don't handle this yet */
  8452. case Fac_CCBS_T_Suspend:
  8453. case Fac_CCBS_T_Resume:
  8454. /* We don't handle this yet */
  8455. break;
  8456. #endif /* We don't handle this yet */
  8457. case Fac_CCBS_T_RemoteUserFree:
  8458. misdn_cc_handle_T_remote_user_free(bc);
  8459. break;
  8460. case Fac_CCBS_T_Available:
  8461. switch (event) {
  8462. case EVENT_ALERTING:
  8463. case EVENT_DISCONNECT:
  8464. /* CCBS-T/CCNR-T is available */
  8465. if (ch && ch->peer) {
  8466. int set_id = 1;
  8467. AST_LIST_LOCK(&misdn_cc_records_db);
  8468. if (ch->record_id == -1) {
  8469. cc_record = misdn_cc_new();
  8470. } else {
  8471. /*
  8472. * We are doing a call-completion attempt
  8473. * or the switch is sending us extra call-completion
  8474. * availability indications (erroneously?).
  8475. */
  8476. cc_record = misdn_cc_find_by_id(ch->record_id);
  8477. if (cc_record) {
  8478. if (cc_record->ptp && cc_record->mode.ptp.retention_enabled) {
  8479. /*
  8480. * Call-completion is still activated.
  8481. * The user does not have to request it again.
  8482. */
  8483. chan_misdn_log(1, bc->port, " --> Call-completion request retention option is enabled\n");
  8484. set_id = 0;
  8485. } else {
  8486. if (cc_record->ptp && cc_record->mode.ptp.bc) {
  8487. /*
  8488. * The network request retention option
  8489. * is not on and the current call-completion
  8490. * request is to be disabled.
  8491. *
  8492. * We should get here only if EVENT_DISCONNECT
  8493. *
  8494. * Close the call-completion signaling link
  8495. */
  8496. cc_record->mode.ptp.bc->fac_out.Function = Fac_None;
  8497. cc_record->mode.ptp.bc->out_cause = AST_CAUSE_NORMAL_CLEARING;
  8498. misdn_lib_send_event(cc_record->mode.ptp.bc, EVENT_RELEASE_COMPLETE);
  8499. }
  8500. /*
  8501. * Resetup the existing record for a possible new
  8502. * call-completion request.
  8503. */
  8504. new_record_id = misdn_cc_record_id_new();
  8505. if (new_record_id < 0) {
  8506. /* Looks like we must keep the old id anyway. */
  8507. } else {
  8508. cc_record->record_id = new_record_id;
  8509. ch->record_id = new_record_id;
  8510. }
  8511. cc_record->ptp = 1;
  8512. cc_record->port = bc->port;
  8513. memset(&cc_record->mode, 0, sizeof(cc_record->mode));
  8514. cc_record->invoke_id = ++misdn_invoke_id;
  8515. cc_record->activated = 0;
  8516. cc_record->outstanding_message = 0;
  8517. cc_record->activation_requested = 0;
  8518. cc_record->error_code = FacError_None;
  8519. cc_record->reject_code = FacReject_None;
  8520. memset(&cc_record->remote_user_free, 0, sizeof(cc_record->remote_user_free));
  8521. memset(&cc_record->b_free, 0, sizeof(cc_record->b_free));
  8522. cc_record->time_created = time(NULL);
  8523. }
  8524. cc_record = NULL;
  8525. } else {
  8526. /*
  8527. * Where did the record go? We will have to recapture
  8528. * the call setup information. Unfortunately, some
  8529. * setup information may have been changed.
  8530. */
  8531. ch->record_id = -1;
  8532. cc_record = misdn_cc_new();
  8533. }
  8534. }
  8535. if (cc_record) {
  8536. ch->record_id = cc_record->record_id;
  8537. cc_record->ptp = 1;
  8538. cc_record->port = bc->port;
  8539. /* Record call information for possible call-completion attempt. */
  8540. cc_record->redial.caller = bc->caller;
  8541. cc_record->redial.dialed = bc->dialed;
  8542. cc_record->redial.setup_bc_hlc_llc = bc->setup_bc_hlc_llc;
  8543. cc_record->redial.capability = bc->capability;
  8544. cc_record->redial.hdlc = bc->hdlc;
  8545. }
  8546. AST_LIST_UNLOCK(&misdn_cc_records_db);
  8547. /* Set MISDN_CC_RECORD_ID in original channel */
  8548. if (ch->record_id != -1 && set_id) {
  8549. snprintf(buf, sizeof(buf), "%ld", ch->record_id);
  8550. } else {
  8551. buf[0] = 0;
  8552. }
  8553. misdn_cc_set_peer_var(ch->peer, MISDN_CC_RECORD_ID, buf);
  8554. }
  8555. break;
  8556. default:
  8557. chan_misdn_log(0, bc->port,
  8558. " --> Expected in a DISCONNECT or ALERTING message: facility type:0x%04X\n",
  8559. bc->fac_in.Function);
  8560. break;
  8561. }
  8562. break;
  8563. case Fac_CCBS_T_Request:
  8564. case Fac_CCNR_T_Request:
  8565. switch (bc->fac_in.u.CCBS_T_Request.ComponentType) {
  8566. case FacComponent_Result:
  8567. AST_LIST_LOCK(&misdn_cc_records_db);
  8568. cc_record = misdn_cc_find_by_invoke(bc->port, bc->fac_in.u.CCBS_T_Request.InvokeID);
  8569. if (cc_record && cc_record->ptp) {
  8570. cc_record->outstanding_message = 0;
  8571. cc_record->activated = 1;
  8572. cc_record->mode.ptp.retention_enabled =
  8573. cc_record->mode.ptp.requested_retention
  8574. ? bc->fac_in.u.CCBS_T_Request.Component.Result.RetentionSupported
  8575. ? 1 : 0
  8576. : 0;
  8577. }
  8578. AST_LIST_UNLOCK(&misdn_cc_records_db);
  8579. break;
  8580. case FacComponent_Invoke:
  8581. /* We cannot be User-B in ptp mode. */
  8582. default:
  8583. chan_misdn_log(0, bc->port, " --> not yet handled: facility type:0x%04X\n",
  8584. bc->fac_in.Function);
  8585. break;
  8586. }
  8587. break;
  8588. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  8589. case Fac_None:
  8590. break;
  8591. default:
  8592. chan_misdn_log(0, bc->port, " --> not yet handled: facility type:0x%04X\n",
  8593. bc->fac_in.Function);
  8594. break;
  8595. }
  8596. }
  8597. /*!
  8598. * \internal
  8599. * \brief Determine if the given dialed party matches our MSN.
  8600. * \since 1.8
  8601. *
  8602. * \param port ISDN port
  8603. * \param dialed Dialed party information of incoming call.
  8604. *
  8605. * \retval non-zero if MSN is valid.
  8606. * \retval 0 if MSN invalid.
  8607. */
  8608. static int misdn_is_msn_valid(int port, const struct misdn_party_dialing *dialed)
  8609. {
  8610. char number[sizeof(dialed->number)];
  8611. ast_copy_string(number, dialed->number, sizeof(number));
  8612. misdn_add_number_prefix(port, dialed->number_type, number, sizeof(number));
  8613. return misdn_cfg_is_msn_valid(port, number);
  8614. }
  8615. /************************************************************/
  8616. /* Receive Events from isdn_lib here */
  8617. /************************************************************/
  8618. static enum event_response_e
  8619. cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
  8620. {
  8621. #if defined(AST_MISDN_ENHANCEMENTS)
  8622. struct misdn_cc_record *cc_record;
  8623. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  8624. struct chan_list *held_ch;
  8625. struct chan_list *ch = find_chan_by_bc(bc);
  8626. if (event != EVENT_BCHAN_DATA && event != EVENT_TONE_GENERATE) {
  8627. int debuglevel = 1;
  8628. /* Debug Only Non-Bchan */
  8629. if (event == EVENT_CLEANUP && !user_data) {
  8630. debuglevel = 5;
  8631. }
  8632. chan_misdn_log(debuglevel, bc->port,
  8633. "I IND :%s caller:\"%s\" <%s> dialed:%s pid:%d state:%s\n",
  8634. manager_isdn_get_info(event),
  8635. bc->caller.name,
  8636. bc->caller.number,
  8637. bc->dialed.number,
  8638. bc->pid,
  8639. ch ? misdn_get_ch_state(ch) : "none");
  8640. if (debuglevel == 1) {
  8641. misdn_lib_log_ies(bc);
  8642. chan_misdn_log(4, bc->port, " --> bc_state:%s\n", bc_state2str(bc->bc_state));
  8643. }
  8644. }
  8645. if (!ch) {
  8646. switch(event) {
  8647. case EVENT_SETUP:
  8648. case EVENT_DISCONNECT:
  8649. case EVENT_RELEASE:
  8650. case EVENT_RELEASE_COMPLETE:
  8651. case EVENT_PORT_ALARM:
  8652. case EVENT_RETRIEVE:
  8653. case EVENT_NEW_BC:
  8654. case EVENT_FACILITY:
  8655. case EVENT_REGISTER:
  8656. break;
  8657. case EVENT_CLEANUP:
  8658. case EVENT_TONE_GENERATE:
  8659. case EVENT_BCHAN_DATA:
  8660. return -1;
  8661. default:
  8662. 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);
  8663. return -1;
  8664. }
  8665. } else {
  8666. switch (event) {
  8667. case EVENT_TONE_GENERATE:
  8668. break;
  8669. case EVENT_DISCONNECT:
  8670. case EVENT_RELEASE:
  8671. case EVENT_RELEASE_COMPLETE:
  8672. case EVENT_CLEANUP:
  8673. case EVENT_TIMEOUT:
  8674. if (!ch->ast) {
  8675. 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));
  8676. }
  8677. break;
  8678. default:
  8679. if (!ch->ast || !MISDN_ASTERISK_TECH_PVT(ch->ast)) {
  8680. if (event != EVENT_BCHAN_DATA) {
  8681. ast_log(LOG_NOTICE, "No Ast or No private Pointer in Event (%d:%s)\n", event, manager_isdn_get_info(event));
  8682. }
  8683. chan_list_unref(ch, "No Ast or Ast private pointer");
  8684. return -1;
  8685. }
  8686. break;
  8687. }
  8688. }
  8689. switch (event) {
  8690. case EVENT_PORT_ALARM:
  8691. {
  8692. int boa = 0;
  8693. misdn_cfg_get(bc->port, MISDN_CFG_ALARM_BLOCK, &boa, sizeof(boa));
  8694. if (boa) {
  8695. cb_log(1, bc->port, " --> blocking\n");
  8696. misdn_lib_port_block(bc->port);
  8697. }
  8698. }
  8699. break;
  8700. case EVENT_BCHAN_ACTIVATED:
  8701. break;
  8702. case EVENT_NEW_CHANNEL:
  8703. update_name(ch->ast,bc->port,bc->channel);
  8704. break;
  8705. case EVENT_NEW_L3ID:
  8706. ch->l3id=bc->l3_id;
  8707. ch->addr=bc->addr;
  8708. break;
  8709. case EVENT_NEW_BC:
  8710. if (!ch) {
  8711. ch = find_hold_call(bc);
  8712. }
  8713. if (!ch) {
  8714. ast_log(LOG_WARNING, "NEW_BC without chan_list?\n");
  8715. break;
  8716. }
  8717. if (bc) {
  8718. ch->bc = (struct misdn_bchannel *) user_data;
  8719. }
  8720. break;
  8721. case EVENT_DTMF_TONE:
  8722. {
  8723. /* sending INFOS as DTMF-Frames :) */
  8724. struct ast_frame fr;
  8725. memset(&fr, 0, sizeof(fr));
  8726. fr.frametype = AST_FRAME_DTMF;
  8727. fr.subclass.integer = bc->dtmf ;
  8728. fr.src = NULL;
  8729. fr.data.ptr = NULL;
  8730. fr.datalen = 0;
  8731. fr.samples = 0;
  8732. fr.mallocd = 0;
  8733. fr.offset = 0;
  8734. fr.delivery = ast_tv(0,0);
  8735. if (!ch->ignore_dtmf) {
  8736. chan_misdn_log(2, bc->port, " --> DTMF:%c\n", bc->dtmf);
  8737. ast_queue_frame(ch->ast, &fr);
  8738. } else {
  8739. chan_misdn_log(2, bc->port, " --> Ignoring DTMF:%c due to bridge flags\n", bc->dtmf);
  8740. }
  8741. break;
  8742. }
  8743. case EVENT_STATUS:
  8744. break;
  8745. case EVENT_INFORMATION:
  8746. if (ch->state != MISDN_CONNECTED) {
  8747. stop_indicate(ch);
  8748. }
  8749. if (!ch->ast) {
  8750. break;
  8751. }
  8752. if (ch->state == MISDN_WAITING4DIGS) {
  8753. /* Ok, incomplete Setup, waiting till extension exists */
  8754. if (ast_strlen_zero(bc->info_dad) && ! ast_strlen_zero(bc->keypad)) {
  8755. chan_misdn_log(1, bc->port, " --> using keypad as info\n");
  8756. ast_copy_string(bc->info_dad, bc->keypad, sizeof(bc->info_dad));
  8757. }
  8758. strncat(bc->dialed.number, bc->info_dad, sizeof(bc->dialed.number) - strlen(bc->dialed.number) - 1);
  8759. ast_copy_string(ch->ast->exten, bc->dialed.number, sizeof(ch->ast->exten));
  8760. /* Check for Pickup Request first */
  8761. if (!strcmp(ch->ast->exten, ast_pickup_ext())) {
  8762. if (ast_pickup_call(ch->ast)) {
  8763. hangup_chan(ch, bc);
  8764. } else {
  8765. ch->state = MISDN_CALLING_ACKNOWLEDGE;
  8766. hangup_chan(ch, bc);
  8767. ch->ast = NULL;
  8768. break;
  8769. }
  8770. }
  8771. if (!ast_canmatch_extension(ch->ast, ch->context, bc->dialed.number, 1, bc->caller.number)) {
  8772. if (ast_exists_extension(ch->ast, ch->context, "i", 1, bc->caller.number)) {
  8773. ast_log(LOG_WARNING,
  8774. "Extension '%s@%s' can never match. Jumping to 'i' extension. port:%d\n",
  8775. bc->dialed.number, ch->context, bc->port);
  8776. strcpy(ch->ast->exten, "i");
  8777. ch->state = MISDN_DIALING;
  8778. start_pbx(ch, bc, ch->ast);
  8779. break;
  8780. }
  8781. ast_log(LOG_WARNING,
  8782. "Extension '%s@%s' can never match. Disconnecting. port:%d\n"
  8783. "\tMaybe you want to add an 'i' extension to catch this case.\n",
  8784. bc->dialed.number, ch->context, bc->port);
  8785. if (bc->nt) {
  8786. hanguptone_indicate(ch);
  8787. }
  8788. ch->state = MISDN_EXTCANTMATCH;
  8789. bc->out_cause = AST_CAUSE_UNALLOCATED;
  8790. misdn_lib_send_event(bc, EVENT_DISCONNECT);
  8791. break;
  8792. }
  8793. if (ch->overlap_dial) {
  8794. ast_mutex_lock(&ch->overlap_tv_lock);
  8795. ch->overlap_tv = ast_tvnow();
  8796. ast_mutex_unlock(&ch->overlap_tv_lock);
  8797. if (ch->overlap_dial_task == -1) {
  8798. ch->overlap_dial_task =
  8799. misdn_tasks_add_variable(ch->overlap_dial, misdn_overlap_dial_task, ch);
  8800. }
  8801. break;
  8802. }
  8803. if (ast_exists_extension(ch->ast, ch->context, bc->dialed.number, 1, bc->caller.number)) {
  8804. ch->state = MISDN_DIALING;
  8805. start_pbx(ch, bc, ch->ast);
  8806. }
  8807. } else {
  8808. /* sending INFOS as DTMF-Frames :) */
  8809. struct ast_frame fr;
  8810. int digits;
  8811. memset(&fr, 0, sizeof(fr));
  8812. fr.frametype = AST_FRAME_DTMF;
  8813. fr.subclass.integer = bc->info_dad[0] ;
  8814. fr.src = NULL;
  8815. fr.data.ptr = NULL;
  8816. fr.datalen = 0;
  8817. fr.samples = 0;
  8818. fr.mallocd = 0;
  8819. fr.offset = 0;
  8820. fr.delivery = ast_tv(0,0);
  8821. misdn_cfg_get(0, MISDN_GEN_APPEND_DIGITS2EXTEN, &digits, sizeof(digits));
  8822. if (ch->state != MISDN_CONNECTED) {
  8823. if (digits) {
  8824. strncat(bc->dialed.number, bc->info_dad, sizeof(bc->dialed.number) - strlen(bc->dialed.number) - 1);
  8825. ast_copy_string(ch->ast->exten, bc->dialed.number, sizeof(ch->ast->exten));
  8826. ast_cdr_update(ch->ast);
  8827. }
  8828. ast_queue_frame(ch->ast, &fr);
  8829. }
  8830. }
  8831. break;
  8832. case EVENT_SETUP:
  8833. {
  8834. struct ast_channel *chan;
  8835. int exceed;
  8836. int ai;
  8837. int im;
  8838. int append_msn = 0;
  8839. if (ch) {
  8840. switch (ch->state) {
  8841. case MISDN_NOTHING:
  8842. chan_list_unref(ch, "Ignore found ch. Is it for an outgoing call?");
  8843. ch = NULL;
  8844. break;
  8845. default:
  8846. chan_list_unref(ch, "Already have a call.");
  8847. chan_misdn_log(1, bc->port, " --> Ignoring Call we have already one\n");
  8848. return RESPONSE_IGNORE_SETUP_WITHOUT_CLOSE; /* Ignore MSNs which are not in our List */
  8849. }
  8850. }
  8851. if (!bc->nt && !misdn_is_msn_valid(bc->port, &bc->dialed)) {
  8852. chan_misdn_log(1, bc->port, " --> Ignoring Call, its not in our MSN List\n");
  8853. return RESPONSE_IGNORE_SETUP; /* Ignore MSNs which are not in our List */
  8854. }
  8855. if (bc->cw) {
  8856. int cause;
  8857. chan_misdn_log(0, bc->port, " --> Call Waiting on PMP sending RELEASE_COMPLETE\n");
  8858. misdn_cfg_get(bc->port, MISDN_CFG_REJECT_CAUSE, &cause, sizeof(cause));
  8859. bc->out_cause = cause ? cause : AST_CAUSE_NORMAL_CLEARING;
  8860. return RESPONSE_RELEASE_SETUP;
  8861. }
  8862. print_bearer(bc);
  8863. ch = chan_list_init(ORG_MISDN);
  8864. if (!ch) {
  8865. chan_misdn_log(-1, bc->port, "cb_events: malloc for chan_list failed!\n");
  8866. return RESPONSE_RELEASE_SETUP;
  8867. }
  8868. ch->bc = bc;
  8869. ch->l3id = bc->l3_id;
  8870. ch->addr = bc->addr;
  8871. {
  8872. struct ast_format_cap *cap = ast_format_cap_alloc_nolock();
  8873. struct ast_format tmpfmt;
  8874. if (!(cap)) {
  8875. return RESPONSE_ERR;
  8876. }
  8877. ast_format_cap_add(cap, ast_format_set(&tmpfmt, AST_FORMAT_ALAW, 0));
  8878. chan = misdn_new(ch, AST_STATE_RESERVED, bc->dialed.number, bc->caller.number, cap, NULL, bc->port, bc->channel);
  8879. cap = ast_format_cap_destroy(cap);
  8880. }
  8881. if (!chan) {
  8882. chan_list_unref(ch, "Failed to create a new channel");
  8883. ast_log(LOG_ERROR, "cb_events: misdn_new failed!\n");
  8884. return RESPONSE_RELEASE_SETUP;
  8885. }
  8886. if ((exceed = add_in_calls(bc->port))) {
  8887. char tmp[16];
  8888. snprintf(tmp, sizeof(tmp), "%d", exceed);
  8889. pbx_builtin_setvar_helper(chan, "MAX_OVERFLOW", tmp);
  8890. }
  8891. read_config(ch);
  8892. export_ch(chan, bc, ch);
  8893. ch->ast->rings = 1;
  8894. ast_setstate(ch->ast, AST_STATE_RINGING);
  8895. /* Update asterisk channel caller information */
  8896. chan_misdn_log(2, bc->port, " --> TON: %s(%d)\n", misdn_to_str_ton(bc->caller.number_type), bc->caller.number_type);
  8897. chan_misdn_log(2, bc->port, " --> PLAN: %s(%d)\n", misdn_to_str_plan(bc->caller.number_plan), bc->caller.number_plan);
  8898. chan->caller.id.number.plan = misdn_to_ast_ton(bc->caller.number_type)
  8899. | misdn_to_ast_plan(bc->caller.number_plan);
  8900. chan_misdn_log(2, bc->port, " --> PRES: %s(%d)\n", misdn_to_str_pres(bc->caller.presentation), bc->caller.presentation);
  8901. chan_misdn_log(2, bc->port, " --> SCREEN: %s(%d)\n", misdn_to_str_screen(bc->caller.screening), bc->caller.screening);
  8902. chan->caller.id.number.presentation = misdn_to_ast_pres(bc->caller.presentation)
  8903. | misdn_to_ast_screen(bc->caller.screening);
  8904. ast_set_callerid(chan, bc->caller.number, NULL, bc->caller.number);
  8905. misdn_cfg_get(bc->port, MISDN_CFG_APPEND_MSN_TO_CALLERID_TAG, &append_msn, sizeof(append_msn));
  8906. if (append_msn) {
  8907. strncat(bc->incoming_cid_tag, "_", sizeof(bc->incoming_cid_tag) - strlen(bc->incoming_cid_tag) - 1);
  8908. strncat(bc->incoming_cid_tag, bc->dialed.number, sizeof(bc->incoming_cid_tag) - strlen(bc->incoming_cid_tag) - 1);
  8909. }
  8910. ast_channel_lock(chan);
  8911. chan->caller.id.tag = ast_strdup(bc->incoming_cid_tag);
  8912. ast_channel_unlock(chan);
  8913. if (!ast_strlen_zero(bc->redirecting.from.number)) {
  8914. /* Add configured prefix to redirecting.from.number */
  8915. misdn_add_number_prefix(bc->port, bc->redirecting.from.number_type, bc->redirecting.from.number, sizeof(bc->redirecting.from.number));
  8916. /* Update asterisk channel redirecting information */
  8917. misdn_copy_redirecting_to_ast(chan, &bc->redirecting, bc->incoming_cid_tag);
  8918. }
  8919. pbx_builtin_setvar_helper(chan, "TRANSFERCAPABILITY", ast_transfercapability2str(bc->capability));
  8920. chan->transfercapability = bc->capability;
  8921. switch (bc->capability) {
  8922. case INFO_CAPABILITY_DIGITAL_UNRESTRICTED:
  8923. pbx_builtin_setvar_helper(chan, "CALLTYPE", "DIGITAL");
  8924. break;
  8925. default:
  8926. pbx_builtin_setvar_helper(chan, "CALLTYPE", "SPEECH");
  8927. break;
  8928. }
  8929. if (!strstr(ch->allowed_bearers, "all")) {
  8930. int i;
  8931. for (i = 0; i < ARRAY_LEN(allowed_bearers_array); ++i) {
  8932. if (allowed_bearers_array[i].cap == bc->capability) {
  8933. if (strstr(ch->allowed_bearers, allowed_bearers_array[i].name)) {
  8934. /* The bearer capability is allowed */
  8935. if (allowed_bearers_array[i].deprecated) {
  8936. chan_misdn_log(0, bc->port, "%s in allowed_bearers list is deprecated\n",
  8937. allowed_bearers_array[i].name);
  8938. }
  8939. break;
  8940. }
  8941. }
  8942. }
  8943. if (i == ARRAY_LEN(allowed_bearers_array)) {
  8944. /* We did not find the bearer capability */
  8945. chan_misdn_log(0, bc->port, "Bearer capability not allowed: %s(%d)\n",
  8946. bearer2str(bc->capability), bc->capability);
  8947. ch->state = MISDN_EXTCANTMATCH;
  8948. chan_list_unref(ch, "BC not allowed, releasing call");
  8949. bc->out_cause = AST_CAUSE_INCOMPATIBLE_DESTINATION;
  8950. return RESPONSE_RELEASE_SETUP;
  8951. }
  8952. }
  8953. /** queue new chan **/
  8954. cl_queue_chan(ch);
  8955. if (bc->fac_in.Function != Fac_None) {
  8956. misdn_facility_ie_handler(event, bc, ch);
  8957. }
  8958. /* Check for Pickup Request first */
  8959. if (!strcmp(chan->exten, ast_pickup_ext())) {
  8960. if (!ch->noautorespond_on_setup) {
  8961. /* Sending SETUP_ACK */
  8962. misdn_lib_send_event(bc, EVENT_SETUP_ACKNOWLEDGE);
  8963. } else {
  8964. ch->state = MISDN_INCOMING_SETUP;
  8965. }
  8966. if (ast_pickup_call(chan)) {
  8967. hangup_chan(ch, bc);
  8968. } else {
  8969. ch->state = MISDN_CALLING_ACKNOWLEDGE;
  8970. hangup_chan(ch, bc);
  8971. ch->ast = NULL;
  8972. break;
  8973. }
  8974. }
  8975. /*
  8976. * added support for s extension hope it will help those poor cretains
  8977. * which haven't overlap dial.
  8978. */
  8979. misdn_cfg_get(bc->port, MISDN_CFG_ALWAYS_IMMEDIATE, &ai, sizeof(ai));
  8980. if (ai) {
  8981. do_immediate_setup(bc, ch, chan);
  8982. break;
  8983. }
  8984. /* check if we should jump into s when we have no dialed.number */
  8985. misdn_cfg_get(bc->port, MISDN_CFG_IMMEDIATE, &im, sizeof(im));
  8986. if (im && ast_strlen_zero(bc->dialed.number)) {
  8987. do_immediate_setup(bc, ch, chan);
  8988. break;
  8989. }
  8990. chan_misdn_log(5, bc->port, "CONTEXT:%s\n", ch->context);
  8991. if (!ast_canmatch_extension(ch->ast, ch->context, bc->dialed.number, 1, bc->caller.number)) {
  8992. if (ast_exists_extension(ch->ast, ch->context, "i", 1, bc->caller.number)) {
  8993. ast_log(LOG_WARNING,
  8994. "Extension '%s@%s' can never match. Jumping to 'i' extension. port:%d\n",
  8995. bc->dialed.number, ch->context, bc->port);
  8996. strcpy(ch->ast->exten, "i");
  8997. misdn_lib_send_event(bc, EVENT_SETUP_ACKNOWLEDGE);
  8998. ch->state = MISDN_DIALING;
  8999. start_pbx(ch, bc, chan);
  9000. break;
  9001. }
  9002. ast_log(LOG_WARNING,
  9003. "Extension '%s@%s' can never match. Disconnecting. port:%d\n"
  9004. "\tMaybe you want to add an 'i' extension to catch this case.\n",
  9005. bc->dialed.number, ch->context, bc->port);
  9006. if (bc->nt) {
  9007. hanguptone_indicate(ch);
  9008. }
  9009. ch->state = MISDN_EXTCANTMATCH;
  9010. bc->out_cause = AST_CAUSE_UNALLOCATED;
  9011. misdn_lib_send_event(bc, bc->nt ? EVENT_RELEASE_COMPLETE : EVENT_RELEASE);
  9012. break;
  9013. }
  9014. /* Whatever happens, when sending_complete is set or we are PTMP TE, we will definitely
  9015. * jump into the dialplan, when the dialed extension does not exist, the 's' extension
  9016. * will be used by Asterisk automatically. */
  9017. if (bc->sending_complete || (!bc->nt && !misdn_lib_is_ptp(bc->port))) {
  9018. if (!ch->noautorespond_on_setup) {
  9019. ch->state=MISDN_DIALING;
  9020. misdn_lib_send_event(bc, EVENT_PROCEEDING);
  9021. } else {
  9022. ch->state = MISDN_INCOMING_SETUP;
  9023. }
  9024. start_pbx(ch, bc, chan);
  9025. break;
  9026. }
  9027. /*
  9028. * When we are NT and overlapdial is set and if
  9029. * the number is empty, we wait for the ISDN timeout
  9030. * instead of our own timer.
  9031. */
  9032. if (ch->overlap_dial && bc->nt && !bc->dialed.number[0]) {
  9033. wait_for_digits(ch, bc, chan);
  9034. break;
  9035. }
  9036. /*
  9037. * If overlapdial we will definitely send a SETUP_ACKNOWLEDGE and wait for more
  9038. * Infos with a Interdigit Timeout.
  9039. * */
  9040. if (ch->overlap_dial) {
  9041. ast_mutex_lock(&ch->overlap_tv_lock);
  9042. ch->overlap_tv = ast_tvnow();
  9043. ast_mutex_unlock(&ch->overlap_tv_lock);
  9044. wait_for_digits(ch, bc, chan);
  9045. if (ch->overlap_dial_task == -1) {
  9046. ch->overlap_dial_task =
  9047. misdn_tasks_add_variable(ch->overlap_dial, misdn_overlap_dial_task, ch);
  9048. }
  9049. break;
  9050. }
  9051. /* If the extension does not exist and we're not TE_PTMP we wait for more digits
  9052. * without interdigit timeout.
  9053. * */
  9054. if (!ast_exists_extension(ch->ast, ch->context, bc->dialed.number, 1, bc->caller.number)) {
  9055. wait_for_digits(ch, bc, chan);
  9056. break;
  9057. }
  9058. /*
  9059. * If the extension exists let's just jump into it.
  9060. * */
  9061. if (ast_exists_extension(ch->ast, ch->context, bc->dialed.number, 1, bc->caller.number)) {
  9062. misdn_lib_send_event(bc, bc->need_more_infos ? EVENT_SETUP_ACKNOWLEDGE : EVENT_PROCEEDING);
  9063. ch->state = MISDN_DIALING;
  9064. start_pbx(ch, bc, chan);
  9065. break;
  9066. }
  9067. break;
  9068. }
  9069. #if defined(AST_MISDN_ENHANCEMENTS)
  9070. case EVENT_REGISTER:
  9071. if (bc->fac_in.Function != Fac_None) {
  9072. misdn_facility_ie_handler(event, bc, ch);
  9073. }
  9074. /*
  9075. * Shut down this connection immediately.
  9076. * The current design of chan_misdn data structures
  9077. * does not allow the proper handling of inbound call records
  9078. * without an assigned B channel. Therefore, we cannot
  9079. * be the CCBS User-B party in a point-to-point setup.
  9080. */
  9081. bc->fac_out.Function = Fac_None;
  9082. bc->out_cause = AST_CAUSE_NORMAL_CLEARING;
  9083. misdn_lib_send_event(bc, EVENT_RELEASE_COMPLETE);
  9084. break;
  9085. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  9086. case EVENT_SETUP_ACKNOWLEDGE:
  9087. ch->state = MISDN_CALLING_ACKNOWLEDGE;
  9088. if (bc->channel) {
  9089. update_name(ch->ast,bc->port,bc->channel);
  9090. }
  9091. if (bc->fac_in.Function != Fac_None) {
  9092. misdn_facility_ie_handler(event, bc, ch);
  9093. }
  9094. if (!ast_strlen_zero(bc->infos_pending)) {
  9095. /* TX Pending Infos */
  9096. strncat(bc->dialed.number, bc->infos_pending, sizeof(bc->dialed.number) - strlen(bc->dialed.number) - 1);
  9097. if (!ch->ast) {
  9098. break;
  9099. }
  9100. ast_copy_string(ch->ast->exten, bc->dialed.number, sizeof(ch->ast->exten));
  9101. ast_copy_string(bc->info_dad, bc->infos_pending, sizeof(bc->info_dad));
  9102. ast_copy_string(bc->infos_pending, "", sizeof(bc->infos_pending));
  9103. misdn_lib_send_event(bc, EVENT_INFORMATION);
  9104. }
  9105. break;
  9106. case EVENT_PROCEEDING:
  9107. if (misdn_cap_is_speech(bc->capability) &&
  9108. misdn_inband_avail(bc)) {
  9109. start_bc_tones(ch);
  9110. }
  9111. ch->state = MISDN_PROCEEDING;
  9112. if (bc->fac_in.Function != Fac_None) {
  9113. misdn_facility_ie_handler(event, bc, ch);
  9114. }
  9115. if (!ch->ast) {
  9116. break;
  9117. }
  9118. ast_queue_control(ch->ast, AST_CONTROL_PROCEEDING);
  9119. break;
  9120. case EVENT_PROGRESS:
  9121. if (bc->channel) {
  9122. update_name(ch->ast, bc->port, bc->channel);
  9123. }
  9124. if (bc->fac_in.Function != Fac_None) {
  9125. misdn_facility_ie_handler(event, bc, ch);
  9126. }
  9127. if (!bc->nt) {
  9128. if (misdn_cap_is_speech(bc->capability) &&
  9129. misdn_inband_avail(bc)) {
  9130. start_bc_tones(ch);
  9131. }
  9132. ch->state = MISDN_PROGRESS;
  9133. if (!ch->ast) {
  9134. break;
  9135. }
  9136. ast_queue_control(ch->ast, AST_CONTROL_PROGRESS);
  9137. }
  9138. break;
  9139. case EVENT_ALERTING:
  9140. ch->state = MISDN_ALERTING;
  9141. if (!ch->ast) {
  9142. break;
  9143. }
  9144. if (bc->fac_in.Function != Fac_None) {
  9145. misdn_facility_ie_handler(event, bc, ch);
  9146. }
  9147. ast_queue_control(ch->ast, AST_CONTROL_RINGING);
  9148. ast_setstate(ch->ast, AST_STATE_RINGING);
  9149. cb_log(7, bc->port, " --> Set State Ringing\n");
  9150. if (misdn_cap_is_speech(bc->capability) && misdn_inband_avail(bc)) {
  9151. cb_log(1, bc->port, "Starting Tones, we have inband Data\n");
  9152. start_bc_tones(ch);
  9153. } else {
  9154. cb_log(3, bc->port, " --> We have no inband Data, the other end must create ringing\n");
  9155. if (ch->far_alerting) {
  9156. cb_log(1, bc->port, " --> The other end can not do ringing eh ?.. we must do all ourself..");
  9157. start_bc_tones(ch);
  9158. /*tone_indicate(ch, TONE_FAR_ALERTING);*/
  9159. }
  9160. }
  9161. break;
  9162. case EVENT_CONNECT:
  9163. if (bc->fac_in.Function != Fac_None) {
  9164. misdn_facility_ie_handler(event, bc, ch);
  9165. }
  9166. #if defined(AST_MISDN_ENHANCEMENTS)
  9167. if (bc->div_leg_3_rx_wanted) {
  9168. bc->div_leg_3_rx_wanted = 0;
  9169. if (ch->ast) {
  9170. ch->ast->redirecting.to.number.presentation =
  9171. AST_PRES_RESTRICTED | AST_PRES_USER_NUMBER_UNSCREENED;
  9172. ast_channel_queue_redirecting_update(ch->ast, &ch->ast->redirecting, NULL);
  9173. }
  9174. }
  9175. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  9176. /* we answer when we've got our very new L3 ID from the NT stack */
  9177. misdn_lib_send_event(bc, EVENT_CONNECT_ACKNOWLEDGE);
  9178. if (!ch->ast) {
  9179. break;
  9180. }
  9181. stop_indicate(ch);
  9182. #if defined(AST_MISDN_ENHANCEMENTS)
  9183. if (ch->record_id != -1) {
  9184. /*
  9185. * We will delete the associated call completion
  9186. * record since we now have a completed call.
  9187. * We will not wait/depend on the network to tell
  9188. * us to delete it.
  9189. */
  9190. AST_LIST_LOCK(&misdn_cc_records_db);
  9191. cc_record = misdn_cc_find_by_id(ch->record_id);
  9192. if (cc_record) {
  9193. if (cc_record->ptp && cc_record->mode.ptp.bc) {
  9194. /* Close the call-completion signaling link */
  9195. cc_record->mode.ptp.bc->fac_out.Function = Fac_None;
  9196. cc_record->mode.ptp.bc->out_cause = AST_CAUSE_NORMAL_CLEARING;
  9197. misdn_lib_send_event(cc_record->mode.ptp.bc, EVENT_RELEASE_COMPLETE);
  9198. }
  9199. misdn_cc_delete(cc_record);
  9200. }
  9201. AST_LIST_UNLOCK(&misdn_cc_records_db);
  9202. ch->record_id = -1;
  9203. if (ch->peer) {
  9204. misdn_cc_set_peer_var(ch->peer, MISDN_CC_RECORD_ID, "");
  9205. ao2_ref(ch->peer, -1);
  9206. ch->peer = NULL;
  9207. }
  9208. }
  9209. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  9210. if (!ast_strlen_zero(bc->connected.number)) {
  9211. /* Add configured prefix to connected.number */
  9212. misdn_add_number_prefix(bc->port, bc->connected.number_type, bc->connected.number, sizeof(bc->connected.number));
  9213. /* Update the connected line information on the other channel */
  9214. misdn_update_remote_party(ch->ast, &bc->connected, AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER, bc->incoming_cid_tag);
  9215. }
  9216. ch->l3id = bc->l3_id;
  9217. ch->addr = bc->addr;
  9218. start_bc_tones(ch);
  9219. ch->state = MISDN_CONNECTED;
  9220. ast_queue_control(ch->ast, AST_CONTROL_ANSWER);
  9221. break;
  9222. case EVENT_CONNECT_ACKNOWLEDGE:
  9223. ch->l3id = bc->l3_id;
  9224. ch->addr = bc->addr;
  9225. start_bc_tones(ch);
  9226. ch->state = MISDN_CONNECTED;
  9227. break;
  9228. case EVENT_DISCONNECT:
  9229. /* we might not have an ch->ast ptr here anymore */
  9230. if (ch) {
  9231. if (bc->fac_in.Function != Fac_None) {
  9232. misdn_facility_ie_handler(event, bc, ch);
  9233. }
  9234. 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);
  9235. if (ch->originator == ORG_AST && !bc->nt && misdn_inband_avail(bc) && ch->state != MISDN_CONNECTED) {
  9236. /* If there's inband information available (e.g. a
  9237. recorded message saying what was wrong with the
  9238. dialled number, or perhaps even giving an
  9239. alternative number, then play it instead of
  9240. immediately releasing the call */
  9241. chan_misdn_log(1, bc->port, " --> Inband Info Avail, not sending RELEASE\n");
  9242. ch->state = MISDN_DISCONNECTED;
  9243. start_bc_tones(ch);
  9244. if (ch->ast) {
  9245. ch->ast->hangupcause = bc->cause;
  9246. if (bc->cause == AST_CAUSE_USER_BUSY) {
  9247. ast_queue_control(ch->ast, AST_CONTROL_BUSY);
  9248. }
  9249. }
  9250. ch->need_busy = 0;
  9251. break;
  9252. }
  9253. bc->need_disconnect = 0;
  9254. stop_bc_tones(ch);
  9255. /* Check for held channel, to implement transfer */
  9256. held_ch = find_hold_call(bc);
  9257. if (!held_ch || !ch->ast || misdn_attempt_transfer(ch, held_ch)) {
  9258. hangup_chan(ch, bc);
  9259. }
  9260. } else {
  9261. held_ch = find_hold_call_l3(bc->l3_id);
  9262. if (held_ch) {
  9263. if (bc->fac_in.Function != Fac_None) {
  9264. misdn_facility_ie_handler(event, bc, held_ch);
  9265. }
  9266. if (held_ch->hold.state == MISDN_HOLD_ACTIVE) {
  9267. bc->need_disconnect = 0;
  9268. #if defined(TRANSFER_ON_HELD_CALL_HANGUP)
  9269. /*
  9270. * Some phones disconnect the held call and the active call at the
  9271. * same time to do the transfer. Unfortunately, either call could
  9272. * be disconnected first.
  9273. */
  9274. ch = find_hold_active_call(bc);
  9275. if (!ch || misdn_attempt_transfer(ch, held_ch)) {
  9276. held_ch->hold.state = MISDN_HOLD_DISCONNECT;
  9277. hangup_chan(held_ch, bc);
  9278. }
  9279. #else
  9280. hangup_chan(held_ch, bc);
  9281. #endif /* defined(TRANSFER_ON_HELD_CALL_HANGUP) */
  9282. }
  9283. }
  9284. }
  9285. if (held_ch) {
  9286. chan_list_unref(held_ch, "Done with held call");
  9287. }
  9288. bc->out_cause = -1;
  9289. if (bc->need_release) {
  9290. misdn_lib_send_event(bc, EVENT_RELEASE);
  9291. }
  9292. break;
  9293. case EVENT_RELEASE:
  9294. if (!ch) {
  9295. ch = find_hold_call_l3(bc->l3_id);
  9296. if (!ch) {
  9297. chan_misdn_log(1, bc->port,
  9298. " --> no Ch, so we've already released. (%s)\n",
  9299. manager_isdn_get_info(event));
  9300. return -1;
  9301. }
  9302. }
  9303. if (bc->fac_in.Function != Fac_None) {
  9304. misdn_facility_ie_handler(event, bc, ch);
  9305. }
  9306. bc->need_disconnect = 0;
  9307. bc->need_release = 0;
  9308. hangup_chan(ch, bc);
  9309. release_chan(ch, bc);
  9310. break;
  9311. case EVENT_RELEASE_COMPLETE:
  9312. if (!ch) {
  9313. ch = find_hold_call_l3(bc->l3_id);
  9314. }
  9315. bc->need_disconnect = 0;
  9316. bc->need_release = 0;
  9317. bc->need_release_complete = 0;
  9318. if (ch) {
  9319. if (bc->fac_in.Function != Fac_None) {
  9320. misdn_facility_ie_handler(event, bc, ch);
  9321. }
  9322. stop_bc_tones(ch);
  9323. hangup_chan(ch, bc);
  9324. release_chan(ch, bc);
  9325. } else {
  9326. #if defined(AST_MISDN_ENHANCEMENTS)
  9327. /*
  9328. * A call-completion signaling link established with
  9329. * REGISTER does not have a struct chan_list record
  9330. * associated with it.
  9331. */
  9332. AST_LIST_LOCK(&misdn_cc_records_db);
  9333. cc_record = misdn_cc_find_by_bc(bc);
  9334. if (cc_record) {
  9335. /* The call-completion signaling link is closed. */
  9336. misdn_cc_delete(cc_record);
  9337. }
  9338. AST_LIST_UNLOCK(&misdn_cc_records_db);
  9339. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  9340. chan_misdn_log(1, bc->port,
  9341. " --> no Ch, so we've already released. (%s)\n",
  9342. manager_isdn_get_info(event));
  9343. }
  9344. break;
  9345. case EVENT_BCHAN_ERROR:
  9346. case EVENT_CLEANUP:
  9347. stop_bc_tones(ch);
  9348. switch (ch->state) {
  9349. case MISDN_CALLING:
  9350. bc->cause = AST_CAUSE_DESTINATION_OUT_OF_ORDER;
  9351. break;
  9352. default:
  9353. break;
  9354. }
  9355. hangup_chan(ch, bc);
  9356. release_chan(ch, bc);
  9357. break;
  9358. case EVENT_TONE_GENERATE:
  9359. {
  9360. int tone_len = bc->tone_cnt;
  9361. struct ast_channel *ast = ch->ast;
  9362. void *tmp;
  9363. int res;
  9364. int (*generate)(struct ast_channel *chan, void *tmp, int datalen, int samples);
  9365. chan_misdn_log(9, bc->port, "TONE_GEN: len:%d\n", tone_len);
  9366. if (!ast) {
  9367. break;
  9368. }
  9369. if (!ast->generator) {
  9370. break;
  9371. }
  9372. tmp = ast->generatordata;
  9373. ast->generatordata = NULL;
  9374. generate = ast->generator->generate;
  9375. if (tone_len < 0 || tone_len > 512) {
  9376. ast_log(LOG_NOTICE, "TONE_GEN: len was %d, set to 128\n", tone_len);
  9377. tone_len = 128;
  9378. }
  9379. res = generate(ast, tmp, tone_len, tone_len);
  9380. ast->generatordata = tmp;
  9381. if (res) {
  9382. ast_log(LOG_WARNING, "Auto-deactivating generator\n");
  9383. ast_deactivate_generator(ast);
  9384. } else {
  9385. bc->tone_cnt = 0;
  9386. }
  9387. break;
  9388. }
  9389. case EVENT_BCHAN_DATA:
  9390. if (ch->bc->AOCD_need_export) {
  9391. export_aoc_vars(ch->originator, ch->ast, ch->bc);
  9392. }
  9393. if (!misdn_cap_is_speech(ch->bc->capability)) {
  9394. struct ast_frame frame;
  9395. /* In Data Modes we queue frames */
  9396. memset(&frame, 0, sizeof(frame));
  9397. frame.frametype = AST_FRAME_VOICE; /* we have no data frames yet */
  9398. ast_format_set(&frame.subclass.format, AST_FORMAT_ALAW, 0);
  9399. frame.datalen = bc->bframe_len;
  9400. frame.samples = bc->bframe_len;
  9401. frame.mallocd = 0;
  9402. frame.offset = 0;
  9403. frame.delivery = ast_tv(0, 0);
  9404. frame.src = NULL;
  9405. frame.data.ptr = bc->bframe;
  9406. if (ch->ast) {
  9407. ast_queue_frame(ch->ast, &frame);
  9408. }
  9409. } else {
  9410. struct pollfd pfd = { .fd = ch->pipe[1], .events = POLLOUT };
  9411. int t;
  9412. t = ast_poll(&pfd, 1, 0);
  9413. if (t < 0) {
  9414. chan_misdn_log(-1, bc->port, "poll() error (err=%s)\n", strerror(errno));
  9415. break;
  9416. }
  9417. if (!t) {
  9418. chan_misdn_log(9, bc->port, "poll() timed out\n");
  9419. break;
  9420. }
  9421. if (pfd.revents & POLLOUT) {
  9422. chan_misdn_log(9, bc->port, "writing %d bytes to asterisk\n", bc->bframe_len);
  9423. if (write(ch->pipe[1], bc->bframe, bc->bframe_len) <= 0) {
  9424. chan_misdn_log(0, bc->port, "Write returned <=0 (err=%s) --> hanging up channel\n", strerror(errno));
  9425. stop_bc_tones(ch);
  9426. hangup_chan(ch, bc);
  9427. release_chan(ch, bc);
  9428. }
  9429. } else {
  9430. chan_misdn_log(1, bc->port, "Write Pipe full!\n");
  9431. }
  9432. }
  9433. break;
  9434. case EVENT_TIMEOUT:
  9435. if (ch && bc) {
  9436. chan_misdn_log(1, bc->port, "--> state: %s\n", misdn_get_ch_state(ch));
  9437. }
  9438. switch (ch->state) {
  9439. case MISDN_DIALING:
  9440. case MISDN_PROGRESS:
  9441. if (bc->nt && !ch->nttimeout) {
  9442. break;
  9443. }
  9444. /* fall-through */
  9445. case MISDN_CALLING:
  9446. case MISDN_ALERTING:
  9447. case MISDN_PROCEEDING:
  9448. case MISDN_CALLING_ACKNOWLEDGE:
  9449. if (bc->nt) {
  9450. bc->progress_indicator = INFO_PI_INBAND_AVAILABLE;
  9451. hanguptone_indicate(ch);
  9452. }
  9453. bc->out_cause = AST_CAUSE_UNALLOCATED;
  9454. misdn_lib_send_event(bc, EVENT_DISCONNECT);
  9455. break;
  9456. case MISDN_WAITING4DIGS:
  9457. if (bc->nt) {
  9458. bc->progress_indicator = INFO_PI_INBAND_AVAILABLE;
  9459. bc->out_cause = AST_CAUSE_UNALLOCATED;
  9460. hanguptone_indicate(ch);
  9461. misdn_lib_send_event(bc, EVENT_DISCONNECT);
  9462. } else {
  9463. bc->out_cause = AST_CAUSE_NORMAL_CLEARING;
  9464. misdn_lib_send_event(bc, EVENT_RELEASE);
  9465. }
  9466. break;
  9467. case MISDN_CLEANING:
  9468. chan_misdn_log(1, bc->port, " --> in state cleaning .. so ignoring, the stack should clean it for us\n");
  9469. break;
  9470. default:
  9471. misdn_lib_send_event(bc, EVENT_RELEASE_COMPLETE);
  9472. break;
  9473. }
  9474. break;
  9475. /****************************/
  9476. /** Supplementary Services **/
  9477. /****************************/
  9478. case EVENT_RETRIEVE:
  9479. if (!ch) {
  9480. chan_misdn_log(4, bc->port, " --> no CH, searching for held call\n");
  9481. ch = find_hold_call_l3(bc->l3_id);
  9482. if (!ch || ch->hold.state != MISDN_HOLD_ACTIVE) {
  9483. ast_log(LOG_WARNING, "No held call found, cannot Retrieve\n");
  9484. misdn_lib_send_event(bc, EVENT_RETRIEVE_REJECT);
  9485. break;
  9486. }
  9487. }
  9488. /* remember the channel again */
  9489. ch->bc = bc;
  9490. ch->hold.state = MISDN_HOLD_IDLE;
  9491. ch->hold.port = 0;
  9492. ch->hold.channel = 0;
  9493. ast_queue_control(ch->ast, AST_CONTROL_UNHOLD);
  9494. if (misdn_lib_send_event(bc, EVENT_RETRIEVE_ACKNOWLEDGE) < 0) {
  9495. chan_misdn_log(4, bc->port, " --> RETRIEVE_ACK failed\n");
  9496. misdn_lib_send_event(bc, EVENT_RETRIEVE_REJECT);
  9497. }
  9498. break;
  9499. case EVENT_HOLD:
  9500. {
  9501. int hold_allowed;
  9502. struct ast_channel *bridged;
  9503. misdn_cfg_get(bc->port, MISDN_CFG_HOLD_ALLOWED, &hold_allowed, sizeof(hold_allowed));
  9504. if (!hold_allowed) {
  9505. chan_misdn_log(-1, bc->port, "Hold not allowed this port.\n");
  9506. misdn_lib_send_event(bc, EVENT_HOLD_REJECT);
  9507. break;
  9508. }
  9509. bridged = ast_bridged_channel(ch->ast);
  9510. if (bridged) {
  9511. chan_misdn_log(2, bc->port, "Bridge Partner is of type: %s\n", bridged->tech->type);
  9512. ch->l3id = bc->l3_id;
  9513. /* forget the channel now */
  9514. ch->bc = NULL;
  9515. ch->hold.state = MISDN_HOLD_ACTIVE;
  9516. ch->hold.port = bc->port;
  9517. ch->hold.channel = bc->channel;
  9518. ast_queue_control(ch->ast, AST_CONTROL_HOLD);
  9519. misdn_lib_send_event(bc, EVENT_HOLD_ACKNOWLEDGE);
  9520. } else {
  9521. misdn_lib_send_event(bc, EVENT_HOLD_REJECT);
  9522. chan_misdn_log(0, bc->port, "We aren't bridged to anybody\n");
  9523. }
  9524. break;
  9525. }
  9526. case EVENT_NOTIFY:
  9527. if (bc->redirecting.to_changed) {
  9528. /* Add configured prefix to redirecting.to.number */
  9529. misdn_add_number_prefix(bc->port, bc->redirecting.to.number_type,
  9530. bc->redirecting.to.number, sizeof(bc->redirecting.to.number));
  9531. }
  9532. switch (bc->notify_description_code) {
  9533. case mISDN_NOTIFY_CODE_DIVERSION_ACTIVATED:
  9534. /* Ignore for now. */
  9535. bc->redirecting.to_changed = 0;
  9536. break;
  9537. case mISDN_NOTIFY_CODE_CALL_IS_DIVERTING:
  9538. if (!bc->redirecting.to_changed) {
  9539. break;
  9540. }
  9541. bc->redirecting.to_changed = 0;
  9542. if (!ch || !ch->ast) {
  9543. break;
  9544. }
  9545. switch (ch->state) {
  9546. case MISDN_ALERTING:
  9547. /* Call is deflecting after we have seen an ALERTING message */
  9548. bc->redirecting.reason = mISDN_REDIRECTING_REASON_NO_REPLY;
  9549. break;
  9550. default:
  9551. /* Call is deflecting for call forwarding unconditional or busy reason. */
  9552. bc->redirecting.reason = mISDN_REDIRECTING_REASON_UNKNOWN;
  9553. break;
  9554. }
  9555. misdn_copy_redirecting_to_ast(ch->ast, &bc->redirecting, bc->incoming_cid_tag);
  9556. ast_channel_queue_redirecting_update(ch->ast, &ch->ast->redirecting, NULL);
  9557. break;
  9558. case mISDN_NOTIFY_CODE_CALL_TRANSFER_ALERTING:
  9559. /*
  9560. * It would be preferable to update the connected line information
  9561. * only when the message callStatus is active. However, the
  9562. * optional redirection number may not be present in the active
  9563. * message if an alerting message were received earlier.
  9564. *
  9565. * The consequences if we wind up sending two updates is benign.
  9566. * The other end will think that it got transferred twice.
  9567. */
  9568. if (!bc->redirecting.to_changed) {
  9569. break;
  9570. }
  9571. bc->redirecting.to_changed = 0;
  9572. if (!ch || !ch->ast) {
  9573. break;
  9574. }
  9575. misdn_update_remote_party(ch->ast, &bc->redirecting.to,
  9576. AST_CONNECTED_LINE_UPDATE_SOURCE_TRANSFER_ALERTING,
  9577. bc->incoming_cid_tag);
  9578. break;
  9579. case mISDN_NOTIFY_CODE_CALL_TRANSFER_ACTIVE:
  9580. if (!bc->redirecting.to_changed) {
  9581. break;
  9582. }
  9583. bc->redirecting.to_changed = 0;
  9584. if (!ch || !ch->ast) {
  9585. break;
  9586. }
  9587. misdn_update_remote_party(ch->ast, &bc->redirecting.to,
  9588. AST_CONNECTED_LINE_UPDATE_SOURCE_TRANSFER, bc->incoming_cid_tag);
  9589. break;
  9590. default:
  9591. bc->redirecting.to_changed = 0;
  9592. chan_misdn_log(0, bc->port," --> not yet handled: notify code:0x%02X\n",
  9593. bc->notify_description_code);
  9594. break;
  9595. }
  9596. bc->notify_description_code = mISDN_NOTIFY_CODE_INVALID;
  9597. break;
  9598. case EVENT_FACILITY:
  9599. if (bc->fac_in.Function == Fac_None) {
  9600. /* This is a FACILITY message so we MUST have a facility ie */
  9601. chan_misdn_log(0, bc->port," --> Missing facility ie or unknown facility ie contents.\n");
  9602. } else {
  9603. misdn_facility_ie_handler(event, bc, ch);
  9604. }
  9605. /* In case it came in on a FACILITY message and we did not handle it. */
  9606. bc->redirecting.to_changed = 0;
  9607. bc->notify_description_code = mISDN_NOTIFY_CODE_INVALID;
  9608. break;
  9609. case EVENT_RESTART:
  9610. if (!bc->dummy) {
  9611. stop_bc_tones(ch);
  9612. release_chan(ch, bc);
  9613. }
  9614. break;
  9615. default:
  9616. chan_misdn_log(1, 0, "Got Unknown Event\n");
  9617. break;
  9618. }
  9619. if (ch) {
  9620. chan_list_unref(ch, "cb_event complete OK");
  9621. }
  9622. return RESPONSE_OK;
  9623. }
  9624. /** TE STUFF END **/
  9625. #if defined(AST_MISDN_ENHANCEMENTS)
  9626. /*!
  9627. * \internal
  9628. * \brief Get call completion record information.
  9629. *
  9630. * \param chan Asterisk channel to operate upon. (Not used)
  9631. * \param function_name Name of the function that called us.
  9632. * \param function_args Argument string passed to function (Could be NULL)
  9633. * \param buf Buffer to put returned string.
  9634. * \param size Size of the supplied buffer including the null terminator.
  9635. *
  9636. * \retval 0 on success.
  9637. * \retval -1 on error.
  9638. */
  9639. static int misdn_cc_read(struct ast_channel *chan, const char *function_name,
  9640. char *function_args, char *buf, size_t size)
  9641. {
  9642. char *parse;
  9643. struct misdn_cc_record *cc_record;
  9644. AST_DECLARE_APP_ARGS(args,
  9645. AST_APP_ARG(cc_id); /* Call completion record ID value. */
  9646. AST_APP_ARG(get_name); /* Name of what to get */
  9647. AST_APP_ARG(other); /* Any extraneous garbage arguments */
  9648. );
  9649. /* Ensure that the buffer is empty */
  9650. *buf = 0;
  9651. if (ast_strlen_zero(function_args)) {
  9652. ast_log(LOG_ERROR, "Function '%s' requires arguments.\n", function_name);
  9653. return -1;
  9654. }
  9655. parse = ast_strdupa(function_args);
  9656. AST_STANDARD_APP_ARGS(args, parse);
  9657. if (!args.argc || ast_strlen_zero(args.cc_id)) {
  9658. ast_log(LOG_ERROR, "Function '%s' missing call completion record ID.\n",
  9659. function_name);
  9660. return -1;
  9661. }
  9662. if (!isdigit(*args.cc_id)) {
  9663. ast_log(LOG_ERROR, "Function '%s' call completion record ID must be numeric.\n",
  9664. function_name);
  9665. return -1;
  9666. }
  9667. if (ast_strlen_zero(args.get_name)) {
  9668. ast_log(LOG_ERROR, "Function '%s' missing what-to-get parameter.\n",
  9669. function_name);
  9670. return -1;
  9671. }
  9672. AST_LIST_LOCK(&misdn_cc_records_db);
  9673. cc_record = misdn_cc_find_by_id(atoi(args.cc_id));
  9674. if (cc_record) {
  9675. if (!strcasecmp("a-all", args.get_name)) {
  9676. snprintf(buf, size, "\"%s\" <%s>", cc_record->redial.caller.name,
  9677. cc_record->redial.caller.number);
  9678. } else if (!strcasecmp("a-name", args.get_name)) {
  9679. ast_copy_string(buf, cc_record->redial.caller.name, size);
  9680. } else if (!strncasecmp("a-num", args.get_name, 5)) {
  9681. ast_copy_string(buf, cc_record->redial.caller.number, size);
  9682. } else if (!strcasecmp("a-ton", args.get_name)) {
  9683. snprintf(buf, size, "%d",
  9684. misdn_to_ast_plan(cc_record->redial.caller.number_plan)
  9685. | misdn_to_ast_ton(cc_record->redial.caller.number_type));
  9686. } else if (!strncasecmp("a-pres", args.get_name, 6)) {
  9687. ast_copy_string(buf, ast_named_caller_presentation(
  9688. misdn_to_ast_pres(cc_record->redial.caller.presentation)
  9689. | misdn_to_ast_screen(cc_record->redial.caller.screening)), size);
  9690. } else if (!strcasecmp("a-busy", args.get_name)) {
  9691. ast_copy_string(buf, cc_record->party_a_free ? "no" : "yes", size);
  9692. } else if (!strncasecmp("b-num", args.get_name, 5)) {
  9693. ast_copy_string(buf, cc_record->redial.dialed.number, size);
  9694. } else if (!strcasecmp("b-ton", args.get_name)) {
  9695. snprintf(buf, size, "%d",
  9696. misdn_to_ast_plan(cc_record->redial.dialed.number_plan)
  9697. | misdn_to_ast_ton(cc_record->redial.dialed.number_type));
  9698. } else if (!strcasecmp("port", args.get_name)) {
  9699. snprintf(buf, size, "%d", cc_record->port);
  9700. } else if (!strcasecmp("available-notify-priority", args.get_name)) {
  9701. snprintf(buf, size, "%d", cc_record->remote_user_free.priority);
  9702. } else if (!strcasecmp("available-notify-exten", args.get_name)) {
  9703. ast_copy_string(buf, cc_record->remote_user_free.exten, size);
  9704. } else if (!strcasecmp("available-notify-context", args.get_name)) {
  9705. ast_copy_string(buf, cc_record->remote_user_free.context, size);
  9706. } else if (!strcasecmp("busy-notify-priority", args.get_name)) {
  9707. snprintf(buf, size, "%d", cc_record->b_free.priority);
  9708. } else if (!strcasecmp("busy-notify-exten", args.get_name)) {
  9709. ast_copy_string(buf, cc_record->b_free.exten, size);
  9710. } else if (!strcasecmp("busy-notify-context", args.get_name)) {
  9711. ast_copy_string(buf, cc_record->b_free.context, size);
  9712. } else {
  9713. AST_LIST_UNLOCK(&misdn_cc_records_db);
  9714. ast_log(LOG_ERROR, "Function '%s': Unknown what-to-get '%s'.\n", function_name, args.get_name);
  9715. return -1;
  9716. }
  9717. }
  9718. AST_LIST_UNLOCK(&misdn_cc_records_db);
  9719. return 0;
  9720. }
  9721. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  9722. #if defined(AST_MISDN_ENHANCEMENTS)
  9723. static struct ast_custom_function misdn_cc_function = {
  9724. .name = "mISDN_CC",
  9725. .synopsis = "Get call completion record information.",
  9726. .syntax = "mISDN_CC(${MISDN_CC_RECORD_ID},<what-to-get>)",
  9727. .desc =
  9728. "mISDN_CC(${MISDN_CC_RECORD_ID},<what-to-get>)\n"
  9729. "The following can be retrieved:\n"
  9730. "\"a-num\", \"a-name\", \"a-all\", \"a-ton\", \"a-pres\", \"a-busy\",\n"
  9731. "\"b-num\", \"b-ton\", \"port\",\n"
  9732. " User-A is available for call completion:\n"
  9733. " \"available-notify-priority\",\n"
  9734. " \"available-notify-exten\",\n"
  9735. " \"available-notify-context\",\n"
  9736. " User-A is busy:\n"
  9737. " \"busy-notify-priority\",\n"
  9738. " \"busy-notify-exten\",\n"
  9739. " \"busy-notify-context\"\n",
  9740. .read = misdn_cc_read,
  9741. };
  9742. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  9743. /******************************************
  9744. *
  9745. * Asterisk Channel Endpoint END
  9746. *
  9747. *
  9748. *******************************************/
  9749. static int unload_module(void)
  9750. {
  9751. /* First, take us out of the channel loop */
  9752. ast_log(LOG_VERBOSE, "-- Unregistering mISDN Channel Driver --\n");
  9753. misdn_tasks_destroy();
  9754. if (!g_config_initialized) {
  9755. return 0;
  9756. }
  9757. ast_cli_unregister_multiple(chan_misdn_clis, sizeof(chan_misdn_clis) / sizeof(struct ast_cli_entry));
  9758. /* ast_unregister_application("misdn_crypt"); */
  9759. ast_unregister_application("misdn_set_opt");
  9760. ast_unregister_application("misdn_facility");
  9761. ast_unregister_application("misdn_check_l2l1");
  9762. #if defined(AST_MISDN_ENHANCEMENTS)
  9763. ast_unregister_application(misdn_command_name);
  9764. ast_custom_function_unregister(&misdn_cc_function);
  9765. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  9766. ast_channel_unregister(&misdn_tech);
  9767. free_robin_list();
  9768. misdn_cfg_destroy();
  9769. misdn_lib_destroy();
  9770. ast_free(misdn_out_calls);
  9771. ast_free(misdn_in_calls);
  9772. ast_free(misdn_debug_only);
  9773. ast_free(misdn_ports);
  9774. ast_free(misdn_debug);
  9775. #if defined(AST_MISDN_ENHANCEMENTS)
  9776. misdn_cc_destroy();
  9777. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  9778. misdn_tech.capabilities = ast_format_cap_destroy(misdn_tech.capabilities);
  9779. misdn_tech_wo_bridge.capabilities = ast_format_cap_destroy(misdn_tech_wo_bridge.capabilities);
  9780. return 0;
  9781. }
  9782. static int load_module(void)
  9783. {
  9784. int i, port;
  9785. int ntflags = 0, ntkc = 0;
  9786. char ports[256] = "";
  9787. char tempbuf[BUFFERSIZE + 1];
  9788. char ntfile[BUFFERSIZE + 1];
  9789. struct misdn_lib_iface iface = {
  9790. .cb_event = cb_events,
  9791. .cb_log = chan_misdn_log,
  9792. .cb_jb_empty = chan_misdn_jb_empty,
  9793. };
  9794. if (!(misdn_tech.capabilities = ast_format_cap_alloc())) {
  9795. return AST_MODULE_LOAD_DECLINE;
  9796. }
  9797. if (!(misdn_tech_wo_bridge.capabilities = ast_format_cap_alloc())) {
  9798. return AST_MODULE_LOAD_DECLINE;
  9799. }
  9800. ast_format_set(&prefformat, AST_FORMAT_ALAW, 0);
  9801. ast_format_cap_add(misdn_tech.capabilities, &prefformat);
  9802. ast_format_cap_add(misdn_tech_wo_bridge.capabilities, &prefformat);
  9803. max_ports = misdn_lib_maxports_get();
  9804. if (max_ports <= 0) {
  9805. ast_log(LOG_ERROR, "Unable to initialize mISDN\n");
  9806. return AST_MODULE_LOAD_DECLINE;
  9807. }
  9808. if (misdn_cfg_init(max_ports, 0)) {
  9809. ast_log(LOG_ERROR, "Unable to initialize misdn_config.\n");
  9810. return AST_MODULE_LOAD_DECLINE;
  9811. }
  9812. g_config_initialized = 1;
  9813. #if defined(AST_MISDN_ENHANCEMENTS)
  9814. misdn_cc_init();
  9815. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  9816. misdn_debug = ast_malloc(sizeof(int) * (max_ports + 1));
  9817. if (!misdn_debug) {
  9818. ast_log(LOG_ERROR, "Out of memory for misdn_debug\n");
  9819. return AST_MODULE_LOAD_DECLINE;
  9820. }
  9821. misdn_ports = ast_malloc(sizeof(int) * (max_ports + 1));
  9822. if (!misdn_ports) {
  9823. ast_free(misdn_debug);
  9824. ast_log(LOG_ERROR, "Out of memory for misdn_ports\n");
  9825. return AST_MODULE_LOAD_DECLINE;
  9826. }
  9827. misdn_cfg_get(0, MISDN_GEN_DEBUG, &misdn_debug[0], sizeof(misdn_debug[0]));
  9828. for (i = 1; i <= max_ports; i++) {
  9829. misdn_debug[i] = misdn_debug[0];
  9830. misdn_ports[i] = i;
  9831. }
  9832. *misdn_ports = 0;
  9833. misdn_debug_only = ast_calloc(max_ports + 1, sizeof(int));
  9834. if (!misdn_debug_only) {
  9835. ast_free(misdn_ports);
  9836. ast_free(misdn_debug);
  9837. ast_log(LOG_ERROR, "Out of memory for misdn_debug_only\n");
  9838. return AST_MODULE_LOAD_DECLINE;
  9839. }
  9840. misdn_cfg_get(0, MISDN_GEN_TRACEFILE, tempbuf, sizeof(tempbuf));
  9841. if (!ast_strlen_zero(tempbuf)) {
  9842. tracing = 1;
  9843. }
  9844. misdn_in_calls = ast_malloc(sizeof(int) * (max_ports + 1));
  9845. if (!misdn_in_calls) {
  9846. ast_free(misdn_debug_only);
  9847. ast_free(misdn_ports);
  9848. ast_free(misdn_debug);
  9849. ast_log(LOG_ERROR, "Out of memory for misdn_in_calls\n");
  9850. return AST_MODULE_LOAD_DECLINE;
  9851. }
  9852. misdn_out_calls = ast_malloc(sizeof(int) * (max_ports + 1));
  9853. if (!misdn_out_calls) {
  9854. ast_free(misdn_in_calls);
  9855. ast_free(misdn_debug_only);
  9856. ast_free(misdn_ports);
  9857. ast_free(misdn_debug);
  9858. ast_log(LOG_ERROR, "Out of memory for misdn_out_calls\n");
  9859. return AST_MODULE_LOAD_DECLINE;
  9860. }
  9861. for (i = 1; i <= max_ports; i++) {
  9862. misdn_in_calls[i] = 0;
  9863. misdn_out_calls[i] = 0;
  9864. }
  9865. ast_mutex_init(&cl_te_lock);
  9866. ast_mutex_init(&release_lock);
  9867. misdn_cfg_update_ptp();
  9868. misdn_cfg_get_ports_string(ports);
  9869. if (!ast_strlen_zero(ports)) {
  9870. chan_misdn_log(0, 0, "Got: %s from get_ports\n", ports);
  9871. }
  9872. if (misdn_lib_init(ports, &iface, NULL)) {
  9873. chan_misdn_log(0, 0, "No te ports initialized\n");
  9874. }
  9875. misdn_cfg_get(0, MISDN_GEN_NTDEBUGFLAGS, &ntflags, sizeof(ntflags));
  9876. misdn_cfg_get(0, MISDN_GEN_NTDEBUGFILE, &ntfile, sizeof(ntfile));
  9877. misdn_cfg_get(0, MISDN_GEN_NTKEEPCALLS, &ntkc, sizeof(ntkc));
  9878. misdn_lib_nt_keepcalls(ntkc);
  9879. misdn_lib_nt_debug_init(ntflags, ntfile);
  9880. if (ast_channel_register(&misdn_tech)) {
  9881. ast_log(LOG_ERROR, "Unable to register channel class %s\n", misdn_type);
  9882. unload_module();
  9883. return AST_MODULE_LOAD_DECLINE;
  9884. }
  9885. ast_cli_register_multiple(chan_misdn_clis, sizeof(chan_misdn_clis) / sizeof(struct ast_cli_entry));
  9886. ast_register_application("misdn_set_opt", misdn_set_opt_exec, "misdn_set_opt",
  9887. "misdn_set_opt(:<opt><optarg>:<opt><optarg>...):\n"
  9888. "Sets mISDN opts. and optargs\n"
  9889. "\n"
  9890. "The available options are:\n"
  9891. " a - Have Asterisk detect DTMF tones on called channel\n"
  9892. " c - Make crypted outgoing call, optarg is keyindex\n"
  9893. " d - Send display text to called phone, text is the optarg\n"
  9894. " e - Perform echo cancellation on this channel,\n"
  9895. " takes taps as optarg (32,64,128,256)\n"
  9896. " e! - Disable echo cancellation on this channel\n"
  9897. " f - Enable fax detection\n"
  9898. " h - Make digital outgoing call\n"
  9899. " h1 - Make HDLC mode digital outgoing call\n"
  9900. " i - Ignore detected DTMF tones, don't signal them to Asterisk,\n"
  9901. " they will be transported inband.\n"
  9902. " jb - Set jitter buffer length, optarg is length\n"
  9903. " jt - Set jitter buffer upper threshold, optarg is threshold\n"
  9904. " jn - Disable jitter buffer\n"
  9905. " n - Disable mISDN DSP on channel.\n"
  9906. " Disables: echo cancel, DTMF detection, and volume control.\n"
  9907. " p - Caller ID presentation,\n"
  9908. " optarg is either 'allowed' or 'restricted'\n"
  9909. " s - Send Non-inband DTMF as inband\n"
  9910. " vr - Rx gain control, optarg is gain\n"
  9911. " vt - Tx gain control, optarg is gain\n"
  9912. );
  9913. ast_register_application("misdn_facility", misdn_facility_exec, "misdn_facility",
  9914. "misdn_facility(<FACILITY_TYPE>|<ARG1>|..)\n"
  9915. "Sends the Facility Message FACILITY_TYPE with \n"
  9916. "the given Arguments to the current ISDN Channel\n"
  9917. "Supported Facilities are:\n"
  9918. "\n"
  9919. "type=calldeflect args=Nr where to deflect\n"
  9920. #if defined(AST_MISDN_ENHANCEMENTS)
  9921. "type=callrerouting args=Nr where to deflect\n"
  9922. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  9923. );
  9924. ast_register_application("misdn_check_l2l1", misdn_check_l2l1, "misdn_check_l2l1",
  9925. "misdn_check_l2l1(<port>||g:<groupname>,timeout)\n"
  9926. "Checks if the L2 and L1 are up on either the given <port> or\n"
  9927. "on the ports in the group with <groupname>\n"
  9928. "If the L1/L2 are down, check_l2l1 gets up the L1/L2 and waits\n"
  9929. "for <timeout> seconds that this happens. Otherwise, nothing happens\n"
  9930. "\n"
  9931. "This application, ensures the L1/L2 state of the Ports in a group\n"
  9932. "it is intended to make the pmp_l1_check option redundant and to\n"
  9933. "fix a buggy switch config from your provider\n"
  9934. "\n"
  9935. "a sample dialplan would look like:\n\n"
  9936. "exten => _X.,1,misdn_check_l2l1(g:out|2)\n"
  9937. "exten => _X.,n,dial(mISDN/g:out/${EXTEN})\n"
  9938. );
  9939. #if defined(AST_MISDN_ENHANCEMENTS)
  9940. ast_register_application(misdn_command_name, misdn_command_exec, misdn_command_name,
  9941. "misdn_command(<command>[,<options>])\n"
  9942. "The following commands are defined:\n"
  9943. "cc-initialize\n"
  9944. " Setup mISDN support for call completion\n"
  9945. " Must call before doing any Dial() involving call completion.\n"
  9946. "ccnr-request,${MISDN_CC_RECORD_ID},<notify-context>,<user-a-extension>,<priority>\n"
  9947. " Request Call Completion No Reply activation\n"
  9948. "ccbs-request,${MISDN_CC_RECORD_ID},<notify-context>,<user-a-extension>,<priority>\n"
  9949. " Request Call Completion Busy Subscriber activation\n"
  9950. "cc-b-free,${MISDN_CC_RECORD_ID},<notify-context>,<user-a-extension>,<priority>\n"
  9951. " Set the dialplan location to notify when User-B is available but User-A is busy.\n"
  9952. " Setting this dialplan location is optional.\n"
  9953. "cc-a-busy,${MISDN_CC_RECORD_ID},<yes/no>\n"
  9954. " Set the busy status of call completion User-A\n"
  9955. "cc-deactivate,${MISDN_CC_RECORD_ID}\n"
  9956. " Deactivate the identified call completion request\n"
  9957. "\n"
  9958. "MISDN_CC_RECORD_ID is set when Dial() returns and call completion is possible\n"
  9959. "MISDN_CC_STATUS is set to ACTIVATED or ERROR after the call completion\n"
  9960. "activation request.\n"
  9961. "MISDN_ERROR_MSG is set to a descriptive message on error.\n"
  9962. );
  9963. ast_custom_function_register(&misdn_cc_function);
  9964. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  9965. misdn_cfg_get(0, MISDN_GEN_TRACEFILE, global_tracefile, sizeof(global_tracefile));
  9966. /* start the l1 watchers */
  9967. for (port = misdn_cfg_get_next_port(0); port >= 0; port = misdn_cfg_get_next_port(port)) {
  9968. int l1timeout;
  9969. misdn_cfg_get(port, MISDN_CFG_L1_TIMEOUT, &l1timeout, sizeof(l1timeout));
  9970. if (l1timeout) {
  9971. chan_misdn_log(4, 0, "Adding L1watcher task: port:%d timeout:%ds\n", port, l1timeout);
  9972. misdn_tasks_add(l1timeout * 1000, misdn_l1_task, &misdn_ports[port]);
  9973. }
  9974. }
  9975. chan_misdn_log(0, 0, "-- mISDN Channel Driver Registered --\n");
  9976. return 0;
  9977. }
  9978. static int reload(void)
  9979. {
  9980. reload_config();
  9981. return 0;
  9982. }
  9983. /*** SOME APPS ;)***/
  9984. #if defined(AST_MISDN_ENHANCEMENTS)
  9985. /*!
  9986. * \brief misdn_command arguments container.
  9987. */
  9988. AST_DEFINE_APP_ARGS_TYPE(misdn_command_args,
  9989. AST_APP_ARG(name); /* Subcommand name */
  9990. AST_APP_ARG(arg)[10 + 1]; /* Subcommand arguments */
  9991. );
  9992. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  9993. #if defined(AST_MISDN_ENHANCEMENTS)
  9994. static void misdn_cc_caller_destroy(void *obj)
  9995. {
  9996. /* oh snap! */
  9997. }
  9998. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  9999. #if defined(AST_MISDN_ENHANCEMENTS)
  10000. static struct misdn_cc_caller *misdn_cc_caller_alloc(struct ast_channel *chan)
  10001. {
  10002. struct misdn_cc_caller *cc_caller;
  10003. if (!(cc_caller = ao2_alloc(sizeof(*cc_caller), misdn_cc_caller_destroy))) {
  10004. return NULL;
  10005. }
  10006. cc_caller->chan = chan;
  10007. return cc_caller;
  10008. }
  10009. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  10010. #if defined(AST_MISDN_ENHANCEMENTS)
  10011. /*!
  10012. * \internal
  10013. * \brief misdn_command(cc-initialize) subcommand handler
  10014. *
  10015. * \param chan Asterisk channel to operate upon.
  10016. * \param subcommand Arguments for the subcommand
  10017. *
  10018. * \retval 0 on success.
  10019. * \retval -1 on error.
  10020. */
  10021. static int misdn_command_cc_initialize(struct ast_channel *chan, struct misdn_command_args *subcommand)
  10022. {
  10023. struct misdn_cc_caller *cc_caller;
  10024. struct ast_datastore *datastore;
  10025. if (!(cc_caller = misdn_cc_caller_alloc(chan))) {
  10026. return -1;
  10027. }
  10028. if (!(datastore = ast_datastore_alloc(&misdn_cc_ds_info, NULL))) {
  10029. ao2_ref(cc_caller, -1);
  10030. return -1;
  10031. }
  10032. ast_channel_lock(chan);
  10033. /* Inherit reference */
  10034. datastore->data = cc_caller;
  10035. cc_caller = NULL;
  10036. datastore->inheritance = DATASTORE_INHERIT_FOREVER;
  10037. ast_channel_datastore_add(chan, datastore);
  10038. ast_channel_unlock(chan);
  10039. return 0;
  10040. }
  10041. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  10042. #if defined(AST_MISDN_ENHANCEMENTS)
  10043. /*!
  10044. * \internal
  10045. * \brief misdn_command(cc-deactivate) subcommand handler
  10046. *
  10047. * \details
  10048. * misdn_command(cc-deactivate,${MISDN_CC_RECORD_ID})
  10049. * Deactivate a call completion service instance.
  10050. *
  10051. * \param chan Asterisk channel to operate upon.
  10052. * \param subcommand Arguments for the subcommand
  10053. *
  10054. * \retval 0 on success.
  10055. * \retval -1 on error.
  10056. */
  10057. static int misdn_command_cc_deactivate(struct ast_channel *chan, struct misdn_command_args *subcommand)
  10058. {
  10059. long record_id;
  10060. const char *error_str;
  10061. struct misdn_cc_record *cc_record;
  10062. struct misdn_bchannel *bc;
  10063. struct misdn_bchannel dummy;
  10064. static const char cmd_help[] = "%s(%s,${MISDN_CC_RECORD_ID})\n";
  10065. if (ast_strlen_zero(subcommand->arg[0]) || !isdigit(*subcommand->arg[0])) {
  10066. ast_log(LOG_WARNING, cmd_help, misdn_command_name, subcommand->name);
  10067. return -1;
  10068. }
  10069. record_id = atol(subcommand->arg[0]);
  10070. AST_LIST_LOCK(&misdn_cc_records_db);
  10071. cc_record = misdn_cc_find_by_id(record_id);
  10072. if (cc_record && 0 <= cc_record->port) {
  10073. if (cc_record->ptp) {
  10074. if (cc_record->mode.ptp.bc) {
  10075. /* Close the call-completion signaling link */
  10076. bc = cc_record->mode.ptp.bc;
  10077. bc->fac_out.Function = Fac_None;
  10078. bc->out_cause = AST_CAUSE_NORMAL_CLEARING;
  10079. misdn_lib_send_event(bc, EVENT_RELEASE_COMPLETE);
  10080. }
  10081. misdn_cc_delete(cc_record);
  10082. } else if (cc_record->activated) {
  10083. cc_record->error_code = FacError_None;
  10084. cc_record->reject_code = FacReject_None;
  10085. cc_record->invoke_id = ++misdn_invoke_id;
  10086. cc_record->outstanding_message = 1;
  10087. /* Build message */
  10088. misdn_make_dummy(&dummy, cc_record->port, 0, misdn_lib_port_is_nt(cc_record->port), 0);
  10089. dummy.fac_out.Function = Fac_CCBSDeactivate;
  10090. dummy.fac_out.u.CCBSDeactivate.InvokeID = cc_record->invoke_id;
  10091. dummy.fac_out.u.CCBSDeactivate.ComponentType = FacComponent_Invoke;
  10092. dummy.fac_out.u.CCBSDeactivate.Component.Invoke.CCBSReference = cc_record->mode.ptmp.reference_id;
  10093. /* Send message */
  10094. print_facility(&dummy.fac_out, &dummy);
  10095. misdn_lib_send_event(&dummy, EVENT_FACILITY);
  10096. }
  10097. }
  10098. AST_LIST_UNLOCK(&misdn_cc_records_db);
  10099. /* Wait for the response to the call completion deactivation request. */
  10100. misdn_cc_response_wait(chan, MISDN_CC_REQUEST_WAIT_MAX, record_id);
  10101. AST_LIST_LOCK(&misdn_cc_records_db);
  10102. cc_record = misdn_cc_find_by_id(record_id);
  10103. if (cc_record) {
  10104. if (cc_record->port < 0) {
  10105. /* The network did not tell us that call completion was available. */
  10106. error_str = NULL;
  10107. } else if (cc_record->outstanding_message) {
  10108. cc_record->outstanding_message = 0;
  10109. error_str = misdn_no_response_from_network;
  10110. } else if (cc_record->reject_code != FacReject_None) {
  10111. error_str = misdn_to_str_reject_code(cc_record->reject_code);
  10112. } else if (cc_record->error_code != FacError_None) {
  10113. error_str = misdn_to_str_error_code(cc_record->error_code);
  10114. } else {
  10115. error_str = NULL;
  10116. }
  10117. misdn_cc_delete(cc_record);
  10118. } else {
  10119. error_str = NULL;
  10120. }
  10121. AST_LIST_UNLOCK(&misdn_cc_records_db);
  10122. if (error_str) {
  10123. ast_verb(1, "%s(%s) diagnostic '%s' on channel %s\n",
  10124. misdn_command_name, subcommand->name, error_str, chan->name);
  10125. pbx_builtin_setvar_helper(chan, MISDN_ERROR_MSG, error_str);
  10126. }
  10127. return 0;
  10128. }
  10129. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  10130. #if defined(AST_MISDN_ENHANCEMENTS)
  10131. /*!
  10132. * \internal
  10133. * \brief misdn_command(cc-a-busy) subcommand handler
  10134. *
  10135. * \details
  10136. * misdn_command(cc-a-busy,${MISDN_CC_RECORD_ID},<yes/no>)
  10137. * Set the status of User-A for a call completion service instance.
  10138. *
  10139. * \param chan Asterisk channel to operate upon.
  10140. * \param subcommand Arguments for the subcommand
  10141. *
  10142. * \retval 0 on success.
  10143. * \retval -1 on error.
  10144. */
  10145. static int misdn_command_cc_a_busy(struct ast_channel *chan, struct misdn_command_args *subcommand)
  10146. {
  10147. long record_id;
  10148. int party_a_free;
  10149. struct misdn_cc_record *cc_record;
  10150. struct misdn_bchannel *bc;
  10151. static const char cmd_help[] = "%s(%s,${MISDN_CC_RECORD_ID},<yes/no>)\n";
  10152. if (ast_strlen_zero(subcommand->arg[0]) || !isdigit(*subcommand->arg[0])) {
  10153. ast_log(LOG_WARNING, cmd_help, misdn_command_name, subcommand->name);
  10154. return -1;
  10155. }
  10156. record_id = atol(subcommand->arg[0]);
  10157. if (ast_true(subcommand->arg[1])) {
  10158. party_a_free = 0;
  10159. } else if (ast_false(subcommand->arg[1])) {
  10160. party_a_free = 1;
  10161. } else {
  10162. ast_log(LOG_WARNING, cmd_help, misdn_command_name, subcommand->name);
  10163. return -1;
  10164. }
  10165. AST_LIST_LOCK(&misdn_cc_records_db);
  10166. cc_record = misdn_cc_find_by_id(record_id);
  10167. if (cc_record && cc_record->party_a_free != party_a_free) {
  10168. /* User-A's status has changed */
  10169. cc_record->party_a_free = party_a_free;
  10170. if (cc_record->ptp && cc_record->mode.ptp.bc) {
  10171. cc_record->error_code = FacError_None;
  10172. cc_record->reject_code = FacReject_None;
  10173. /* Build message */
  10174. bc = cc_record->mode.ptp.bc;
  10175. if (cc_record->party_a_free) {
  10176. bc->fac_out.Function = Fac_CCBS_T_Resume;
  10177. bc->fac_out.u.CCBS_T_Resume.InvokeID = ++misdn_invoke_id;
  10178. } else {
  10179. bc->fac_out.Function = Fac_CCBS_T_Suspend;
  10180. bc->fac_out.u.CCBS_T_Suspend.InvokeID = ++misdn_invoke_id;
  10181. }
  10182. /* Send message */
  10183. print_facility(&bc->fac_out, bc);
  10184. misdn_lib_send_event(bc, EVENT_FACILITY);
  10185. }
  10186. }
  10187. AST_LIST_UNLOCK(&misdn_cc_records_db);
  10188. return 0;
  10189. }
  10190. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  10191. #if defined(AST_MISDN_ENHANCEMENTS)
  10192. /*!
  10193. * \internal
  10194. * \brief misdn_command(cc-b-free) subcommand handler
  10195. *
  10196. * \details
  10197. * misdn_command(cc-b-free,${MISDN_CC_RECORD_ID},<notify-context>,<user-a-extension>,<priority>)
  10198. * Set the dialplan location to notify when User-B is free and User-A is busy.
  10199. *
  10200. * \param chan Asterisk channel to operate upon.
  10201. * \param subcommand Arguments for the subcommand
  10202. *
  10203. * \retval 0 on success.
  10204. * \retval -1 on error.
  10205. */
  10206. static int misdn_command_cc_b_free(struct ast_channel *chan, struct misdn_command_args *subcommand)
  10207. {
  10208. unsigned index;
  10209. long record_id;
  10210. int priority;
  10211. char *context;
  10212. char *exten;
  10213. struct misdn_cc_record *cc_record;
  10214. static const char cmd_help[] = "%s(%s,${MISDN_CC_RECORD_ID},<notify-context>,<user-a-extension>,<priority>)\n";
  10215. /* Check that all arguments are present */
  10216. for (index = 0; index < 4; ++index) {
  10217. if (ast_strlen_zero(subcommand->arg[index])) {
  10218. ast_log(LOG_WARNING, cmd_help, misdn_command_name, subcommand->name);
  10219. return -1;
  10220. }
  10221. }
  10222. /* These must be numeric */
  10223. if (!isdigit(*subcommand->arg[0]) || !isdigit(*subcommand->arg[3])) {
  10224. ast_log(LOG_WARNING, cmd_help, misdn_command_name, subcommand->name);
  10225. return -1;
  10226. }
  10227. record_id = atol(subcommand->arg[0]);
  10228. context = subcommand->arg[1];
  10229. exten = subcommand->arg[2];
  10230. priority = atoi(subcommand->arg[3]);
  10231. AST_LIST_LOCK(&misdn_cc_records_db);
  10232. cc_record = misdn_cc_find_by_id(record_id);
  10233. if (cc_record) {
  10234. /* Save User-B free information */
  10235. ast_copy_string(cc_record->b_free.context, context, sizeof(cc_record->b_free.context));
  10236. ast_copy_string(cc_record->b_free.exten, exten, sizeof(cc_record->b_free.exten));
  10237. cc_record->b_free.priority = priority;
  10238. }
  10239. AST_LIST_UNLOCK(&misdn_cc_records_db);
  10240. return 0;
  10241. }
  10242. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  10243. #if defined(AST_MISDN_ENHANCEMENTS)
  10244. struct misdn_cc_request {
  10245. enum FacFunction ptmp;
  10246. enum FacFunction ptp;
  10247. };
  10248. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  10249. #if defined(AST_MISDN_ENHANCEMENTS)
  10250. /*!
  10251. * \internal
  10252. * \brief misdn_command(ccbs-request/ccnr-request) subcommand handler helper
  10253. *
  10254. * \details
  10255. * misdn_command(ccbs-request,${MISDN_CC_RECORD_ID},<notify-context>,<user-a-extension>,<priority>)
  10256. * misdn_command(ccnr-request,${MISDN_CC_RECORD_ID},<notify-context>,<user-a-extension>,<priority>)
  10257. * Set the dialplan location to notify when User-B is free and User-A is free.
  10258. *
  10259. * \param chan Asterisk channel to operate upon.
  10260. * \param subcommand Arguments for the subcommand
  10261. * \param request Which call-completion request message to generate.
  10262. *
  10263. * \retval 0 on success.
  10264. * \retval -1 on error.
  10265. */
  10266. static int misdn_command_cc_request(struct ast_channel *chan, struct misdn_command_args *subcommand, const struct misdn_cc_request *request)
  10267. {
  10268. unsigned index;
  10269. int request_retention;
  10270. long record_id;
  10271. int priority;
  10272. char *context;
  10273. char *exten;
  10274. const char *error_str;
  10275. struct misdn_cc_record *cc_record;
  10276. struct misdn_bchannel *bc;
  10277. struct misdn_bchannel dummy;
  10278. struct misdn_party_id id;
  10279. static const char cmd_help[] = "%s(%s,${MISDN_CC_RECORD_ID},<notify-context>,<user-a-extension>,<priority>)\n";
  10280. /* Check that all arguments are present */
  10281. for (index = 0; index < 4; ++index) {
  10282. if (ast_strlen_zero(subcommand->arg[index])) {
  10283. ast_log(LOG_WARNING, cmd_help, misdn_command_name, subcommand->name);
  10284. return -1;
  10285. }
  10286. }
  10287. /* These must be numeric */
  10288. if (!isdigit(*subcommand->arg[0]) || !isdigit(*subcommand->arg[3])) {
  10289. ast_log(LOG_WARNING, cmd_help, misdn_command_name, subcommand->name);
  10290. return -1;
  10291. }
  10292. record_id = atol(subcommand->arg[0]);
  10293. context = subcommand->arg[1];
  10294. exten = subcommand->arg[2];
  10295. priority = atoi(subcommand->arg[3]);
  10296. AST_LIST_LOCK(&misdn_cc_records_db);
  10297. cc_record = misdn_cc_find_by_id(record_id);
  10298. if (cc_record) {
  10299. /* Save User-B free information */
  10300. ast_copy_string(cc_record->remote_user_free.context, context,
  10301. sizeof(cc_record->remote_user_free.context));
  10302. ast_copy_string(cc_record->remote_user_free.exten, exten,
  10303. sizeof(cc_record->remote_user_free.exten));
  10304. cc_record->remote_user_free.priority = priority;
  10305. if (0 <= cc_record->port) {
  10306. if (cc_record->ptp) {
  10307. if (!cc_record->mode.ptp.bc) {
  10308. bc = misdn_lib_get_register_bc(cc_record->port);
  10309. if (bc) {
  10310. cc_record->mode.ptp.bc = bc;
  10311. cc_record->error_code = FacError_None;
  10312. cc_record->reject_code = FacReject_None;
  10313. cc_record->invoke_id = ++misdn_invoke_id;
  10314. cc_record->outstanding_message = 1;
  10315. cc_record->activation_requested = 1;
  10316. misdn_cfg_get(bc->port, MISDN_CFG_CC_REQUEST_RETENTION,
  10317. &request_retention, sizeof(request_retention));
  10318. cc_record->mode.ptp.requested_retention = request_retention ? 1 : 0;
  10319. /* Build message */
  10320. bc->fac_out.Function = request->ptp;
  10321. bc->fac_out.u.CCBS_T_Request.InvokeID = cc_record->invoke_id;
  10322. bc->fac_out.u.CCBS_T_Request.ComponentType = FacComponent_Invoke;
  10323. bc->fac_out.u.CCBS_T_Request.Component.Invoke.Q931ie =
  10324. cc_record->redial.setup_bc_hlc_llc;
  10325. memset(&id, 0, sizeof(id));
  10326. id.number_plan = cc_record->redial.dialed.number_plan;
  10327. id.number_type = cc_record->redial.dialed.number_type;
  10328. ast_copy_string(id.number, cc_record->redial.dialed.number,
  10329. sizeof(id.number));
  10330. misdn_Address_fill(
  10331. &bc->fac_out.u.CCBS_T_Request.Component.Invoke.Destination,
  10332. &id);
  10333. misdn_Address_fill(
  10334. &bc->fac_out.u.CCBS_T_Request.Component.Invoke.Originating,
  10335. &cc_record->redial.caller);
  10336. bc->fac_out.u.CCBS_T_Request.Component.Invoke.PresentationAllowedIndicatorPresent = 1;
  10337. bc->fac_out.u.CCBS_T_Request.Component.Invoke.PresentationAllowedIndicator =
  10338. (cc_record->redial.caller.presentation != 0) ? 0 : 1;
  10339. bc->fac_out.u.CCBS_T_Request.Component.Invoke.RetentionSupported =
  10340. request_retention ? 1 : 0;
  10341. /* Send message */
  10342. print_facility(&bc->fac_out, bc);
  10343. misdn_lib_send_event(bc, EVENT_REGISTER);
  10344. }
  10345. }
  10346. } else {
  10347. cc_record->error_code = FacError_None;
  10348. cc_record->reject_code = FacReject_None;
  10349. cc_record->invoke_id = ++misdn_invoke_id;
  10350. cc_record->outstanding_message = 1;
  10351. cc_record->activation_requested = 1;
  10352. /* Build message */
  10353. misdn_make_dummy(&dummy, cc_record->port, 0,
  10354. misdn_lib_port_is_nt(cc_record->port), 0);
  10355. dummy.fac_out.Function = request->ptmp;
  10356. dummy.fac_out.u.CCBSRequest.InvokeID = cc_record->invoke_id;
  10357. dummy.fac_out.u.CCBSRequest.ComponentType = FacComponent_Invoke;
  10358. dummy.fac_out.u.CCBSRequest.Component.Invoke.CallLinkageID =
  10359. cc_record->mode.ptmp.linkage_id;
  10360. /* Send message */
  10361. print_facility(&dummy.fac_out, &dummy);
  10362. misdn_lib_send_event(&dummy, EVENT_FACILITY);
  10363. }
  10364. }
  10365. }
  10366. AST_LIST_UNLOCK(&misdn_cc_records_db);
  10367. /* Wait for the response to the call completion request. */
  10368. misdn_cc_response_wait(chan, MISDN_CC_REQUEST_WAIT_MAX, record_id);
  10369. AST_LIST_LOCK(&misdn_cc_records_db);
  10370. cc_record = misdn_cc_find_by_id(record_id);
  10371. if (cc_record) {
  10372. if (!cc_record->activated) {
  10373. if (cc_record->port < 0) {
  10374. /* The network did not tell us that call completion was available. */
  10375. error_str = "No port number";
  10376. } else if (cc_record->outstanding_message) {
  10377. cc_record->outstanding_message = 0;
  10378. error_str = misdn_no_response_from_network;
  10379. } else if (cc_record->reject_code != FacReject_None) {
  10380. error_str = misdn_to_str_reject_code(cc_record->reject_code);
  10381. } else if (cc_record->error_code != FacError_None) {
  10382. error_str = misdn_to_str_error_code(cc_record->error_code);
  10383. } else if (cc_record->ptp) {
  10384. if (cc_record->mode.ptp.bc) {
  10385. error_str = "Call-completion already requested";
  10386. } else {
  10387. error_str = "Could not allocate call-completion signaling link";
  10388. }
  10389. } else {
  10390. /* Should never happen. */
  10391. error_str = "Unexpected error";
  10392. }
  10393. /* No need to keep the call completion record. */
  10394. if (cc_record->ptp && cc_record->mode.ptp.bc) {
  10395. /* Close the call-completion signaling link */
  10396. bc = cc_record->mode.ptp.bc;
  10397. bc->fac_out.Function = Fac_None;
  10398. bc->out_cause = AST_CAUSE_NORMAL_CLEARING;
  10399. misdn_lib_send_event(bc, EVENT_RELEASE_COMPLETE);
  10400. }
  10401. misdn_cc_delete(cc_record);
  10402. } else {
  10403. error_str = NULL;
  10404. }
  10405. } else {
  10406. error_str = misdn_cc_record_not_found;
  10407. }
  10408. AST_LIST_UNLOCK(&misdn_cc_records_db);
  10409. if (error_str) {
  10410. ast_verb(1, "%s(%s) diagnostic '%s' on channel %s\n",
  10411. misdn_command_name, subcommand->name, error_str, chan->name);
  10412. pbx_builtin_setvar_helper(chan, MISDN_ERROR_MSG, error_str);
  10413. pbx_builtin_setvar_helper(chan, MISDN_CC_STATUS, "ERROR");
  10414. } else {
  10415. pbx_builtin_setvar_helper(chan, MISDN_CC_STATUS, "ACTIVATED");
  10416. }
  10417. return 0;
  10418. }
  10419. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  10420. #if defined(AST_MISDN_ENHANCEMENTS)
  10421. /*!
  10422. * \internal
  10423. * \brief misdn_command(ccbs-request) subcommand handler
  10424. *
  10425. * \details
  10426. * misdn_command(ccbs-request,${MISDN_CC_RECORD_ID},<notify-context>,<user-a-extension>,<priority>)
  10427. * Set the dialplan location to notify when User-B is free and User-A is free.
  10428. *
  10429. * \param chan Asterisk channel to operate upon.
  10430. * \param subcommand Arguments for the subcommand
  10431. *
  10432. * \retval 0 on success.
  10433. * \retval -1 on error.
  10434. */
  10435. static int misdn_command_ccbs_request(struct ast_channel *chan, struct misdn_command_args *subcommand)
  10436. {
  10437. static const struct misdn_cc_request request = {
  10438. .ptmp = Fac_CCBSRequest,
  10439. .ptp = Fac_CCBS_T_Request
  10440. };
  10441. return misdn_command_cc_request(chan, subcommand, &request);
  10442. }
  10443. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  10444. #if defined(AST_MISDN_ENHANCEMENTS)
  10445. /*!
  10446. * \internal
  10447. * \brief misdn_command(ccnr-request) subcommand handler
  10448. *
  10449. * \details
  10450. * misdn_command(ccnr-request,${MISDN_CC_RECORD_ID},<notify-context>,<user-a-extension>,<priority>)
  10451. * Set the dialplan location to notify when User-B is free and User-A is free.
  10452. *
  10453. * \param chan Asterisk channel to operate upon.
  10454. * \param subcommand Arguments for the subcommand
  10455. *
  10456. * \retval 0 on success.
  10457. * \retval -1 on error.
  10458. */
  10459. static int misdn_command_ccnr_request(struct ast_channel *chan, struct misdn_command_args *subcommand)
  10460. {
  10461. static const struct misdn_cc_request request = {
  10462. .ptmp = Fac_CCNRRequest,
  10463. .ptp = Fac_CCNR_T_Request
  10464. };
  10465. return misdn_command_cc_request(chan, subcommand, &request);
  10466. }
  10467. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  10468. #if defined(AST_MISDN_ENHANCEMENTS)
  10469. struct misdn_command_table {
  10470. /*! \brief subcommand name */
  10471. const char *name;
  10472. /*! \brief subcommand handler */
  10473. int (*func)(struct ast_channel *chan, struct misdn_command_args *subcommand);
  10474. /*! \brief TRUE if the subcommand can only be executed on mISDN channels */
  10475. int misdn_only;
  10476. };
  10477. static const struct misdn_command_table misdn_commands[] = {
  10478. /* *INDENT-OFF* */
  10479. /* subcommand-name subcommand-handler mISDN only */
  10480. { "cc-initialize", misdn_command_cc_initialize, 0 },
  10481. { "cc-deactivate", misdn_command_cc_deactivate, 0 },
  10482. { "cc-a-busy", misdn_command_cc_a_busy, 0 },
  10483. { "cc-b-free", misdn_command_cc_b_free, 0 },
  10484. { "ccbs-request", misdn_command_ccbs_request, 0 },
  10485. { "ccnr-request", misdn_command_ccnr_request, 0 },
  10486. /* *INDENT-ON* */
  10487. };
  10488. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  10489. #if defined(AST_MISDN_ENHANCEMENTS)
  10490. /*!
  10491. * \internal
  10492. * \brief misdn_command() dialplan application.
  10493. *
  10494. * \param chan Asterisk channel to operate upon.
  10495. * \param data Application options string.
  10496. *
  10497. * \retval 0 on success.
  10498. * \retval -1 on error.
  10499. */
  10500. static int misdn_command_exec(struct ast_channel *chan, const char *data)
  10501. {
  10502. char *parse;
  10503. unsigned index;
  10504. struct misdn_command_args subcommand;
  10505. if (ast_strlen_zero((char *) data)) {
  10506. ast_log(LOG_ERROR, "%s requires arguments\n", misdn_command_name);
  10507. return -1;
  10508. }
  10509. ast_debug(1, "%s(%s)\n", misdn_command_name, (char *) data);
  10510. parse = ast_strdupa(data);
  10511. AST_STANDARD_APP_ARGS(subcommand, parse);
  10512. if (!subcommand.argc || ast_strlen_zero(subcommand.name)) {
  10513. ast_log(LOG_ERROR, "%s requires a subcommand\n", misdn_command_name);
  10514. return -1;
  10515. }
  10516. for (index = 0; index < ARRAY_LEN(misdn_commands); ++index) {
  10517. if (strcasecmp(misdn_commands[index].name, subcommand.name) == 0) {
  10518. strcpy(subcommand.name, misdn_commands[index].name);
  10519. if (misdn_commands[index].misdn_only
  10520. && strcasecmp(chan->tech->type, misdn_type) != 0) {
  10521. ast_log(LOG_WARNING,
  10522. "%s(%s) only makes sense with %s channels!\n",
  10523. misdn_command_name, subcommand.name, misdn_type);
  10524. return -1;
  10525. }
  10526. return misdn_commands[index].func(chan, &subcommand);
  10527. }
  10528. }
  10529. ast_log(LOG_WARNING, "%s(%s) subcommand is unknown\n", misdn_command_name,
  10530. subcommand.name);
  10531. return -1;
  10532. }
  10533. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  10534. static int misdn_facility_exec(struct ast_channel *chan, const char *data)
  10535. {
  10536. struct chan_list *ch = MISDN_ASTERISK_TECH_PVT(chan);
  10537. char *parse;
  10538. unsigned max_len;
  10539. AST_DECLARE_APP_ARGS(args,
  10540. AST_APP_ARG(facility_type);
  10541. AST_APP_ARG(arg)[99];
  10542. );
  10543. chan_misdn_log(0, 0, "TYPE: %s\n", chan->tech->type);
  10544. if (strcasecmp(chan->tech->type, misdn_type)) {
  10545. ast_log(LOG_WARNING, "misdn_facility only makes sense with %s channels!\n", misdn_type);
  10546. return -1;
  10547. }
  10548. if (ast_strlen_zero((char *) data)) {
  10549. ast_log(LOG_WARNING, "misdn_facility requires arguments: facility_type[,<args>]\n");
  10550. return -1;
  10551. }
  10552. parse = ast_strdupa(data);
  10553. AST_STANDARD_APP_ARGS(args, parse);
  10554. if (ast_strlen_zero(args.facility_type)) {
  10555. ast_log(LOG_WARNING, "misdn_facility requires arguments: facility_type[,<args>]\n");
  10556. return -1;
  10557. }
  10558. if (!strcasecmp(args.facility_type, "calldeflect")) {
  10559. if (ast_strlen_zero(args.arg[0])) {
  10560. ast_log(LOG_WARNING, "Facility: Call Deflection requires an argument: Number\n");
  10561. }
  10562. #if defined(AST_MISDN_ENHANCEMENTS)
  10563. max_len = sizeof(ch->bc->fac_out.u.CallDeflection.Component.Invoke.Deflection.Party.Number) - 1;
  10564. if (max_len < strlen(args.arg[0])) {
  10565. ast_log(LOG_WARNING,
  10566. "Facility: Number argument too long (up to %u digits are allowed). Ignoring.\n",
  10567. max_len);
  10568. return 0;
  10569. }
  10570. ch->bc->fac_out.Function = Fac_CallDeflection;
  10571. ch->bc->fac_out.u.CallDeflection.InvokeID = ++misdn_invoke_id;
  10572. ch->bc->fac_out.u.CallDeflection.ComponentType = FacComponent_Invoke;
  10573. ch->bc->fac_out.u.CallDeflection.Component.Invoke.PresentationAllowedToDivertedToUserPresent = 1;
  10574. ch->bc->fac_out.u.CallDeflection.Component.Invoke.PresentationAllowedToDivertedToUser = 0;
  10575. ch->bc->fac_out.u.CallDeflection.Component.Invoke.Deflection.Party.Type = 0;/* unknown */
  10576. ch->bc->fac_out.u.CallDeflection.Component.Invoke.Deflection.Party.LengthOfNumber = strlen(args.arg[0]);
  10577. strcpy((char *) ch->bc->fac_out.u.CallDeflection.Component.Invoke.Deflection.Party.Number, args.arg[0]);
  10578. ch->bc->fac_out.u.CallDeflection.Component.Invoke.Deflection.Subaddress.Length = 0;
  10579. #else /* !defined(AST_MISDN_ENHANCEMENTS) */
  10580. max_len = sizeof(ch->bc->fac_out.u.CDeflection.DeflectedToNumber) - 1;
  10581. if (max_len < strlen(args.arg[0])) {
  10582. ast_log(LOG_WARNING,
  10583. "Facility: Number argument too long (up to %u digits are allowed). Ignoring.\n",
  10584. max_len);
  10585. return 0;
  10586. }
  10587. ch->bc->fac_out.Function = Fac_CD;
  10588. ch->bc->fac_out.u.CDeflection.PresentationAllowed = 0;
  10589. //ch->bc->fac_out.u.CDeflection.DeflectedToSubaddress[0] = 0;
  10590. strcpy((char *) ch->bc->fac_out.u.CDeflection.DeflectedToNumber, args.arg[0]);
  10591. #endif /* !defined(AST_MISDN_ENHANCEMENTS) */
  10592. /* Send message */
  10593. print_facility(&ch->bc->fac_out, ch->bc);
  10594. misdn_lib_send_event(ch->bc, EVENT_FACILITY);
  10595. #if defined(AST_MISDN_ENHANCEMENTS)
  10596. } else if (!strcasecmp(args.facility_type, "callrerouteing")
  10597. || !strcasecmp(args.facility_type, "callrerouting")) {
  10598. if (ast_strlen_zero(args.arg[0])) {
  10599. ast_log(LOG_WARNING, "Facility: Call rerouting requires an argument: Number\n");
  10600. }
  10601. max_len = sizeof(ch->bc->fac_out.u.CallRerouteing.Component.Invoke.CalledAddress.Party.Number) - 1;
  10602. if (max_len < strlen(args.arg[0])) {
  10603. ast_log(LOG_WARNING,
  10604. "Facility: Number argument too long (up to %u digits are allowed). Ignoring.\n",
  10605. max_len);
  10606. return 0;
  10607. }
  10608. ch->bc->fac_out.Function = Fac_CallRerouteing;
  10609. ch->bc->fac_out.u.CallRerouteing.InvokeID = ++misdn_invoke_id;
  10610. ch->bc->fac_out.u.CallRerouteing.ComponentType = FacComponent_Invoke;
  10611. ch->bc->fac_out.u.CallRerouteing.Component.Invoke.ReroutingReason = 0;/* unknown */
  10612. ch->bc->fac_out.u.CallRerouteing.Component.Invoke.ReroutingCounter = 1;
  10613. ch->bc->fac_out.u.CallRerouteing.Component.Invoke.CalledAddress.Party.Type = 0;/* unknown */
  10614. ch->bc->fac_out.u.CallRerouteing.Component.Invoke.CalledAddress.Party.LengthOfNumber = strlen(args.arg[0]);
  10615. strcpy((char *) ch->bc->fac_out.u.CallRerouteing.Component.Invoke.CalledAddress.Party.Number, args.arg[0]);
  10616. ch->bc->fac_out.u.CallRerouteing.Component.Invoke.CalledAddress.Subaddress.Length = 0;
  10617. ch->bc->fac_out.u.CallRerouteing.Component.Invoke.CallingPartySubaddress.Length = 0;
  10618. /* 0x90 0x90 0xa3 3.1 kHz audio, circuit mode, 64kbit/sec, level1/a-Law */
  10619. ch->bc->fac_out.u.CallRerouteing.Component.Invoke.Q931ie.Bc.Length = 3;
  10620. ch->bc->fac_out.u.CallRerouteing.Component.Invoke.Q931ie.Bc.Contents[0] = 0x90;
  10621. ch->bc->fac_out.u.CallRerouteing.Component.Invoke.Q931ie.Bc.Contents[1] = 0x90;
  10622. ch->bc->fac_out.u.CallRerouteing.Component.Invoke.Q931ie.Bc.Contents[2] = 0xa3;
  10623. ch->bc->fac_out.u.CallRerouteing.Component.Invoke.Q931ie.Hlc.Length = 0;
  10624. ch->bc->fac_out.u.CallRerouteing.Component.Invoke.Q931ie.Llc.Length = 0;
  10625. ch->bc->fac_out.u.CallRerouteing.Component.Invoke.Q931ie.UserInfo.Length = 0;
  10626. ch->bc->fac_out.u.CallRerouteing.Component.Invoke.LastRerouting.Type = 1;/* presentationRestricted */
  10627. ch->bc->fac_out.u.CallRerouteing.Component.Invoke.SubscriptionOption = 0;/* no notification to caller */
  10628. /* Send message */
  10629. print_facility(&ch->bc->fac_out, ch->bc);
  10630. misdn_lib_send_event(ch->bc, EVENT_FACILITY);
  10631. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  10632. } else {
  10633. chan_misdn_log(1, ch->bc->port, "Unknown Facility: %s\n", args.facility_type);
  10634. }
  10635. return 0;
  10636. }
  10637. static int misdn_check_l2l1(struct ast_channel *chan, const char *data)
  10638. {
  10639. char *parse;
  10640. char group[BUFFERSIZE + 1];
  10641. char *port_str;
  10642. int port = 0;
  10643. int timeout;
  10644. int dowait = 0;
  10645. int port_up;
  10646. AST_DECLARE_APP_ARGS(args,
  10647. AST_APP_ARG(grouppar);
  10648. AST_APP_ARG(timeout);
  10649. );
  10650. if (ast_strlen_zero((char *) data)) {
  10651. ast_log(LOG_WARNING, "misdn_check_l2l1 Requires arguments\n");
  10652. return -1;
  10653. }
  10654. parse = ast_strdupa(data);
  10655. AST_STANDARD_APP_ARGS(args, parse);
  10656. if (args.argc != 2) {
  10657. ast_log(LOG_WARNING, "Wrong argument count\n");
  10658. return 0;
  10659. }
  10660. /*ast_log(LOG_NOTICE, "Arguments: group/port '%s' timeout '%s'\n", args.grouppar, args.timeout);*/
  10661. timeout = atoi(args.timeout);
  10662. port_str = args.grouppar;
  10663. if (port_str[0] == 'g' && port_str[1] == ':') {
  10664. /* We make a group call lets checkout which ports are in my group */
  10665. port_str += 2;
  10666. ast_copy_string(group, port_str, sizeof(group));
  10667. chan_misdn_log(2, 0, "Checking Ports in group: %s\n", group);
  10668. for (port = misdn_cfg_get_next_port(port);
  10669. port > 0;
  10670. port = misdn_cfg_get_next_port(port)) {
  10671. char cfg_group[BUFFERSIZE + 1];
  10672. chan_misdn_log(2, 0, "trying port %d\n", port);
  10673. misdn_cfg_get(port, MISDN_CFG_GROUPNAME, cfg_group, sizeof(cfg_group));
  10674. if (!strcasecmp(cfg_group, group)) {
  10675. port_up = misdn_lib_port_up(port, 1);
  10676. if (!port_up) {
  10677. chan_misdn_log(2, 0, " --> port '%d'\n", port);
  10678. misdn_lib_get_port_up(port);
  10679. dowait = 1;
  10680. }
  10681. }
  10682. }
  10683. } else {
  10684. port = atoi(port_str);
  10685. chan_misdn_log(2, 0, "Checking Port: %d\n", port);
  10686. port_up = misdn_lib_port_up(port, 1);
  10687. if (!port_up) {
  10688. misdn_lib_get_port_up(port);
  10689. dowait = 1;
  10690. }
  10691. }
  10692. if (dowait) {
  10693. chan_misdn_log(2, 0, "Waiting for '%d' seconds\n", timeout);
  10694. ast_safe_sleep(chan, timeout * 1000);
  10695. }
  10696. return 0;
  10697. }
  10698. static int misdn_set_opt_exec(struct ast_channel *chan, const char *data)
  10699. {
  10700. struct chan_list *ch = MISDN_ASTERISK_TECH_PVT(chan);
  10701. char *tok;
  10702. char *tokb;
  10703. char *parse;
  10704. int keyidx = 0;
  10705. int rxgain = 0;
  10706. int txgain = 0;
  10707. int change_jitter = 0;
  10708. if (strcasecmp(chan->tech->type, misdn_type)) {
  10709. ast_log(LOG_WARNING, "misdn_set_opt makes sense only with %s channels!\n", misdn_type);
  10710. return -1;
  10711. }
  10712. if (ast_strlen_zero((char *) data)) {
  10713. ast_log(LOG_WARNING, "misdn_set_opt Requires arguments\n");
  10714. return -1;
  10715. }
  10716. parse = ast_strdupa(data);
  10717. for (tok = strtok_r(parse, ":", &tokb);
  10718. tok;
  10719. tok = strtok_r(NULL, ":", &tokb)) {
  10720. int neglect = 0;
  10721. if (tok[0] == '!') {
  10722. neglect = 1;
  10723. tok++;
  10724. }
  10725. switch(tok[0]) {
  10726. case 'd' :
  10727. ast_copy_string(ch->bc->display, ++tok, sizeof(ch->bc->display));
  10728. chan_misdn_log(1, ch->bc->port, "SETOPT: Display:%s\n", ch->bc->display);
  10729. break;
  10730. case 'n':
  10731. chan_misdn_log(1, ch->bc->port, "SETOPT: No DSP\n");
  10732. ch->bc->nodsp = 1;
  10733. break;
  10734. case 'j':
  10735. chan_misdn_log(1, ch->bc->port, "SETOPT: jitter\n");
  10736. tok++;
  10737. change_jitter = 1;
  10738. switch (tok[0]) {
  10739. case 'b':
  10740. ch->jb_len = atoi(++tok);
  10741. chan_misdn_log(1, ch->bc->port, " --> buffer_len:%d\n", ch->jb_len);
  10742. break;
  10743. case 't' :
  10744. ch->jb_upper_threshold = atoi(++tok);
  10745. chan_misdn_log(1, ch->bc->port, " --> upper_threshold:%d\n", ch->jb_upper_threshold);
  10746. break;
  10747. case 'n':
  10748. ch->bc->nojitter = 1;
  10749. chan_misdn_log(1, ch->bc->port, " --> nojitter\n");
  10750. break;
  10751. default:
  10752. ch->jb_len = 4000;
  10753. ch->jb_upper_threshold = 0;
  10754. chan_misdn_log(1, ch->bc->port, " --> buffer_len:%d (default)\n", ch->jb_len);
  10755. chan_misdn_log(1, ch->bc->port, " --> upper_threshold:%d (default)\n", ch->jb_upper_threshold);
  10756. break;
  10757. }
  10758. break;
  10759. case 'v':
  10760. tok++;
  10761. switch (tok[0]) {
  10762. case 'r' :
  10763. rxgain = atoi(++tok);
  10764. if (rxgain < -8) {
  10765. rxgain = -8;
  10766. }
  10767. if (rxgain > 8) {
  10768. rxgain = 8;
  10769. }
  10770. ch->bc->rxgain = rxgain;
  10771. chan_misdn_log(1, ch->bc->port, "SETOPT: Volume:%d\n", rxgain);
  10772. break;
  10773. case 't':
  10774. txgain = atoi(++tok);
  10775. if (txgain < -8) {
  10776. txgain = -8;
  10777. }
  10778. if (txgain > 8) {
  10779. txgain = 8;
  10780. }
  10781. ch->bc->txgain = txgain;
  10782. chan_misdn_log(1, ch->bc->port, "SETOPT: Volume:%d\n", txgain);
  10783. break;
  10784. }
  10785. break;
  10786. case 'c':
  10787. keyidx = atoi(++tok);
  10788. {
  10789. char keys[4096];
  10790. char *key = NULL;
  10791. char *tmp = keys;
  10792. int i;
  10793. misdn_cfg_get(0, MISDN_GEN_CRYPT_KEYS, keys, sizeof(keys));
  10794. for (i = 0; i < keyidx; i++) {
  10795. key = strsep(&tmp, ",");
  10796. }
  10797. if (key) {
  10798. ast_copy_string(ch->bc->crypt_key, key, sizeof(ch->bc->crypt_key));
  10799. }
  10800. chan_misdn_log(0, ch->bc->port, "SETOPT: crypt with key:%s\n", ch->bc->crypt_key);
  10801. break;
  10802. }
  10803. case 'e':
  10804. chan_misdn_log(1, ch->bc->port, "SETOPT: EchoCancel\n");
  10805. if (neglect) {
  10806. chan_misdn_log(1, ch->bc->port, " --> disabled\n");
  10807. #ifdef MISDN_1_2
  10808. *ch->bc->pipeline = 0;
  10809. #else
  10810. ch->bc->ec_enable = 0;
  10811. #endif
  10812. } else {
  10813. #ifdef MISDN_1_2
  10814. update_pipeline_config(ch->bc);
  10815. #else
  10816. ch->bc->ec_enable = 1;
  10817. ch->bc->orig = ch->originator;
  10818. tok++;
  10819. if (*tok) {
  10820. ch->bc->ec_deftaps = atoi(tok);
  10821. }
  10822. #endif
  10823. }
  10824. break;
  10825. case 'h':
  10826. chan_misdn_log(1, ch->bc->port, "SETOPT: Digital\n");
  10827. if (strlen(tok) > 1 && tok[1] == '1') {
  10828. chan_misdn_log(1, ch->bc->port, "SETOPT: HDLC \n");
  10829. if (!ch->bc->hdlc) {
  10830. ch->bc->hdlc = 1;
  10831. }
  10832. }
  10833. ch->bc->capability = INFO_CAPABILITY_DIGITAL_UNRESTRICTED;
  10834. break;
  10835. case 's':
  10836. chan_misdn_log(1, ch->bc->port, "SETOPT: Send DTMF\n");
  10837. ch->bc->send_dtmf = 1;
  10838. break;
  10839. case 'f':
  10840. chan_misdn_log(1, ch->bc->port, "SETOPT: Faxdetect\n");
  10841. ch->faxdetect = 1;
  10842. misdn_cfg_get(ch->bc->port, MISDN_CFG_FAXDETECT_TIMEOUT, &ch->faxdetect_timeout, sizeof(ch->faxdetect_timeout));
  10843. break;
  10844. case 'a':
  10845. chan_misdn_log(1, ch->bc->port, "SETOPT: AST_DSP (for DTMF)\n");
  10846. ch->ast_dsp = 1;
  10847. break;
  10848. case 'p':
  10849. chan_misdn_log(1, ch->bc->port, "SETOPT: callerpres: %s\n", &tok[1]);
  10850. /* CRICH: callingpres!!! */
  10851. if (strstr(tok, "allowed")) {
  10852. ch->bc->presentation = 0;
  10853. ch->bc->set_presentation = 1;
  10854. } else if (strstr(tok, "restricted")) {
  10855. ch->bc->presentation = 1;
  10856. ch->bc->set_presentation = 1;
  10857. } else if (strstr(tok, "not_screened")) {
  10858. chan_misdn_log(0, ch->bc->port, "SETOPT: callerpres: not_screened is deprecated\n");
  10859. ch->bc->presentation = 1;
  10860. ch->bc->set_presentation = 1;
  10861. }
  10862. break;
  10863. case 'i' :
  10864. chan_misdn_log(1, ch->bc->port, "Ignoring dtmf tones, just use them inband\n");
  10865. ch->ignore_dtmf = 1;
  10866. break;
  10867. default:
  10868. break;
  10869. }
  10870. }
  10871. if (change_jitter) {
  10872. config_jitterbuffer(ch);
  10873. }
  10874. if (ch->faxdetect || ch->ast_dsp) {
  10875. if (!ch->dsp) {
  10876. ch->dsp = ast_dsp_new();
  10877. }
  10878. if (ch->dsp) {
  10879. ast_dsp_set_features(ch->dsp, DSP_FEATURE_DIGIT_DETECT | DSP_FEATURE_FAX_DETECT);
  10880. }
  10881. }
  10882. if (ch->ast_dsp) {
  10883. chan_misdn_log(1, ch->bc->port, "SETOPT: with AST_DSP we deactivate mISDN_dsp\n");
  10884. ch->bc->nodsp = 1;
  10885. }
  10886. return 0;
  10887. }
  10888. int chan_misdn_jb_empty(struct misdn_bchannel *bc, char *buf, int len)
  10889. {
  10890. struct chan_list *ch;
  10891. int res;
  10892. ch = find_chan_by_bc(bc);
  10893. if (!ch) {
  10894. return 0;
  10895. }
  10896. if (ch->jb) {
  10897. res = misdn_jb_empty(ch->jb, buf, len);
  10898. } else {
  10899. res = 0;
  10900. }
  10901. chan_list_unref(ch, "Done emptying jb");
  10902. return res;
  10903. }
  10904. /*******************************************************/
  10905. /***************** JITTERBUFFER ************************/
  10906. /*******************************************************/
  10907. /* allocates the jb-structure and initialize the elements*/
  10908. struct misdn_jb *misdn_jb_init(int size, int upper_threshold)
  10909. {
  10910. struct misdn_jb *jb;
  10911. jb = ast_calloc(1, sizeof(*jb));
  10912. if (!jb) {
  10913. chan_misdn_log(-1, 0, "No free Mem for jb\n");
  10914. return NULL;
  10915. }
  10916. jb->size = size;
  10917. jb->upper_threshold = upper_threshold;
  10918. //jb->wp = 0;
  10919. //jb->rp = 0;
  10920. //jb->state_full = 0;
  10921. //jb->state_empty = 0;
  10922. //jb->bytes_wrote = 0;
  10923. jb->samples = ast_calloc(size, sizeof(*jb->samples));
  10924. if (!jb->samples) {
  10925. ast_free(jb);
  10926. chan_misdn_log(-1, 0, "No free Mem for jb->samples\n");
  10927. return NULL;
  10928. }
  10929. jb->ok = ast_calloc(size, sizeof(*jb->ok));
  10930. if (!jb->ok) {
  10931. ast_free(jb->samples);
  10932. ast_free(jb);
  10933. chan_misdn_log(-1, 0, "No free Mem for jb->ok\n");
  10934. return NULL;
  10935. }
  10936. ast_mutex_init(&jb->mutexjb);
  10937. return jb;
  10938. }
  10939. /* frees the data and destroys the given jitterbuffer struct */
  10940. void misdn_jb_destroy(struct misdn_jb *jb)
  10941. {
  10942. ast_mutex_destroy(&jb->mutexjb);
  10943. ast_free(jb->ok);
  10944. ast_free(jb->samples);
  10945. ast_free(jb);
  10946. }
  10947. /* fills the jitterbuffer with len data returns < 0 if there was an
  10948. error (buffer overflow). */
  10949. int misdn_jb_fill(struct misdn_jb *jb, const char *data, int len)
  10950. {
  10951. int i;
  10952. int j;
  10953. int rp;
  10954. int wp;
  10955. if (!jb || ! data) {
  10956. return 0;
  10957. }
  10958. ast_mutex_lock(&jb->mutexjb);
  10959. wp = jb->wp;
  10960. rp = jb->rp;
  10961. for (i = 0; i < len; i++) {
  10962. jb->samples[wp] = data[i];
  10963. jb->ok[wp] = 1;
  10964. wp = (wp != jb->size - 1) ? wp + 1 : 0;
  10965. if (wp == jb->rp) {
  10966. jb->state_full = 1;
  10967. }
  10968. }
  10969. if (wp >= rp) {
  10970. jb->state_buffer = wp - rp;
  10971. } else {
  10972. jb->state_buffer = jb->size - rp + wp;
  10973. }
  10974. chan_misdn_log(9, 0, "misdn_jb_fill: written:%d | Buffer status:%d p:%p\n", len, jb->state_buffer, jb);
  10975. if (jb->state_full) {
  10976. jb->wp = wp;
  10977. rp = wp;
  10978. for (j = 0; j < jb->upper_threshold; j++) {
  10979. rp = (rp != 0) ? rp - 1 : jb->size - 1;
  10980. }
  10981. jb->rp = rp;
  10982. jb->state_full = 0;
  10983. jb->state_empty = 1;
  10984. ast_mutex_unlock(&jb->mutexjb);
  10985. return -1;
  10986. }
  10987. if (!jb->state_empty) {
  10988. jb->bytes_wrote += len;
  10989. if (jb->bytes_wrote >= jb->upper_threshold) {
  10990. jb->state_empty = 1;
  10991. jb->bytes_wrote = 0;
  10992. }
  10993. }
  10994. jb->wp = wp;
  10995. ast_mutex_unlock(&jb->mutexjb);
  10996. return 0;
  10997. }
  10998. /* gets len bytes out of the jitterbuffer if available, else only the
  10999. available data is returned and the return value indicates the number
  11000. of data. */
  11001. int misdn_jb_empty(struct misdn_jb *jb, char *data, int len)
  11002. {
  11003. int i;
  11004. int wp;
  11005. int rp;
  11006. int read = 0;
  11007. ast_mutex_lock(&jb->mutexjb);
  11008. rp = jb->rp;
  11009. wp = jb->wp;
  11010. if (jb->state_empty) {
  11011. for (i = 0; i < len; i++) {
  11012. if (wp == rp) {
  11013. jb->rp = rp;
  11014. jb->state_empty = 0;
  11015. ast_mutex_unlock(&jb->mutexjb);
  11016. return read;
  11017. } else {
  11018. if (jb->ok[rp] == 1) {
  11019. data[i] = jb->samples[rp];
  11020. jb->ok[rp] = 0;
  11021. rp = (rp != jb->size - 1) ? rp + 1 : 0;
  11022. read += 1;
  11023. }
  11024. }
  11025. }
  11026. if (wp >= rp) {
  11027. jb->state_buffer = wp - rp;
  11028. } else {
  11029. jb->state_buffer = jb->size - rp + wp;
  11030. }
  11031. chan_misdn_log(9, 0, "misdn_jb_empty: read:%d | Buffer status:%d p:%p\n", len, jb->state_buffer, jb);
  11032. jb->rp = rp;
  11033. } else {
  11034. chan_misdn_log(9, 0, "misdn_jb_empty: Wait...requested:%d p:%p\n", len, jb);
  11035. }
  11036. ast_mutex_unlock(&jb->mutexjb);
  11037. return read;
  11038. }
  11039. /*******************************************************/
  11040. /*************** JITTERBUFFER END *********************/
  11041. /*******************************************************/
  11042. static void chan_misdn_log(int level, int port, char *tmpl, ...)
  11043. {
  11044. va_list ap;
  11045. char buf[1024];
  11046. char port_buf[8];
  11047. if (!(0 <= port && port <= max_ports)) {
  11048. ast_log(LOG_WARNING, "chan_misdn_log called with out-of-range port number! (%d)\n", port);
  11049. port = 0;
  11050. level = -1;
  11051. } else if (!(level == -1
  11052. || (misdn_debug_only[port]
  11053. ? (level == 1 && misdn_debug[port]) || level == misdn_debug[port]
  11054. : level <= misdn_debug[port])
  11055. || (level <= misdn_debug[0] && !ast_strlen_zero(global_tracefile)))) {
  11056. /*
  11057. * We are not going to print anything so lets not
  11058. * go to all the work of generating a string.
  11059. */
  11060. return;
  11061. }
  11062. snprintf(port_buf, sizeof(port_buf), "P[%2d] ", port);
  11063. va_start(ap, tmpl);
  11064. vsnprintf(buf, sizeof(buf), tmpl, ap);
  11065. va_end(ap);
  11066. if (level == -1) {
  11067. ast_log(LOG_WARNING, "%s", buf);
  11068. } else if (misdn_debug_only[port]
  11069. ? (level == 1 && misdn_debug[port]) || level == misdn_debug[port]
  11070. : level <= misdn_debug[port]) {
  11071. ast_console_puts(port_buf);
  11072. ast_console_puts(buf);
  11073. }
  11074. if (level <= misdn_debug[0] && !ast_strlen_zero(global_tracefile)) {
  11075. char ctimebuf[30];
  11076. time_t tm;
  11077. char *tmp;
  11078. char *p;
  11079. FILE *fp;
  11080. fp = fopen(global_tracefile, "a+");
  11081. if (!fp) {
  11082. ast_console_puts("Error opening Tracefile: [ ");
  11083. ast_console_puts(global_tracefile);
  11084. ast_console_puts(" ] ");
  11085. ast_console_puts(strerror(errno));
  11086. ast_console_puts("\n");
  11087. return;
  11088. }
  11089. tm = time(NULL);
  11090. tmp = ctime_r(&tm, ctimebuf);
  11091. p = strchr(tmp, '\n');
  11092. if (p) {
  11093. *p = ':';
  11094. }
  11095. fputs(tmp, fp);
  11096. fputs(" ", fp);
  11097. fputs(port_buf, fp);
  11098. fputs(" ", fp);
  11099. fputs(buf, fp);
  11100. fclose(fp);
  11101. }
  11102. }
  11103. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Channel driver for mISDN Support (BRI/PRI)",
  11104. .load = load_module,
  11105. .unload = unload_module,
  11106. .reload = reload,
  11107. .load_pri = AST_MODPRI_CHANNEL_DRIVER,
  11108. );