chan_misdn.c 385 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2004 - 2006, Christian Richter
  5. *
  6. * Christian Richter <crich@beronet.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. *
  18. */
  19. /*!
  20. * \file
  21. *
  22. * \brief the chan_misdn channel driver for Asterisk
  23. *
  24. * \author Christian Richter <crich@beronet.com>
  25. *
  26. * MISDN http://www.misdn.org/
  27. *
  28. * \ingroup channel_drivers
  29. */
  30. /*! \li \ref chan_misdn.c uses the configuration file \ref misdn.conf
  31. * \addtogroup configuration_file
  32. */
  33. /*! \page misdn.conf misdn.conf
  34. * \verbinclude misdn.conf.sample
  35. */
  36. /*!
  37. * \note
  38. * To use the CCBS/CCNR supplementary service feature and other
  39. * supplementary services using FACILITY messages requires a
  40. * modified version of mISDN.
  41. *
  42. * \note
  43. * The latest modified mISDN v1.1.x based version is available at:
  44. * http://svn.digium.com/svn/thirdparty/mISDN/trunk
  45. * http://svn.digium.com/svn/thirdparty/mISDNuser/trunk
  46. *
  47. * \note
  48. * Taged versions of the modified mISDN code are available under:
  49. * http://svn.digium.com/svn/thirdparty/mISDN/tags
  50. * http://svn.digium.com/svn/thirdparty/mISDNuser/tags
  51. */
  52. /* Define to enable cli commands to generate canned CCBS messages. */
  53. // #define CCBS_TEST_MESSAGES 1
  54. /*
  55. * XXX The mISDN channel driver needs its native bridge code
  56. * converted to the new bridge technology scheme. The
  57. * chan_dahdi native bridge code can be used as an example. It
  58. * is unlikely that this will ever get done. Support for this
  59. * channel driver is dwindling because the supported version of
  60. * mISDN does not support newer kernels.
  61. *
  62. * Without native bridge support, the following config file
  63. * parameters have no effect: bridging.
  64. *
  65. * The existing native bridge code is marked with the
  66. * mISDN_NATIVE_BRIDGING conditional.
  67. */
  68. /*** MODULEINFO
  69. <depend>isdnnet</depend>
  70. <depend>misdn</depend>
  71. <depend>suppserv</depend>
  72. <support_level>extended</support_level>
  73. ***/
  74. #include "asterisk.h"
  75. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  76. #include <pthread.h>
  77. #include <sys/socket.h>
  78. #include <sys/time.h>
  79. #include <arpa/inet.h>
  80. #include <fcntl.h>
  81. #include <sys/ioctl.h>
  82. #include <signal.h>
  83. #include <sys/file.h>
  84. #include <semaphore.h>
  85. #include <ctype.h>
  86. #include <time.h>
  87. #include "asterisk/channel.h"
  88. #include "asterisk/config.h"
  89. #include "asterisk/module.h"
  90. #include "asterisk/pbx.h"
  91. #include "asterisk/io.h"
  92. #include "asterisk/frame.h"
  93. #include "asterisk/translate.h"
  94. #include "asterisk/cli.h"
  95. #include "asterisk/musiconhold.h"
  96. #include "asterisk/dsp.h"
  97. #include "asterisk/file.h"
  98. #include "asterisk/callerid.h"
  99. #include "asterisk/indications.h"
  100. #include "asterisk/app.h"
  101. #include "asterisk/features.h"
  102. #include "asterisk/term.h"
  103. #include "asterisk/sched.h"
  104. #include "asterisk/stringfields.h"
  105. #include "asterisk/abstract_jb.h"
  106. #include "asterisk/causes.h"
  107. #include "asterisk/format.h"
  108. #include "asterisk/format_cap.h"
  109. #include "asterisk/features_config.h"
  110. #include "asterisk/bridge.h"
  111. #include "asterisk/pickup.h"
  112. #include "chan_misdn_config.h"
  113. #include "isdn_lib.h"
  114. static char global_tracefile[BUFFERSIZE + 1];
  115. static int g_config_initialized = 0;
  116. struct misdn_jb{
  117. int size;
  118. int upper_threshold;
  119. char *samples, *ok;
  120. int wp,rp;
  121. int state_empty;
  122. int state_full;
  123. int state_buffer;
  124. int bytes_wrote;
  125. ast_mutex_t mutexjb;
  126. };
  127. /*! \brief allocates the jb-structure and initialize the elements */
  128. struct misdn_jb *misdn_jb_init(int size, int upper_threshold);
  129. /*! \brief frees the data and destroys the given jitterbuffer struct */
  130. void misdn_jb_destroy(struct misdn_jb *jb);
  131. /*! \brief fills the jitterbuffer with len data returns < 0 if there was an
  132. error (buffer overrun). */
  133. int misdn_jb_fill(struct misdn_jb *jb, const char *data, int len);
  134. /*! \brief gets len bytes out of the jitterbuffer if available, else only the
  135. available data is returned and the return value indicates the number
  136. of data. */
  137. int misdn_jb_empty(struct misdn_jb *jb, char *data, int len);
  138. static char *complete_ch(struct ast_cli_args *a);
  139. static char *complete_debug_port(struct ast_cli_args *a);
  140. static char *complete_show_config(struct ast_cli_args *a);
  141. /* BEGIN: chan_misdn.h */
  142. #if defined(AST_MISDN_ENHANCEMENTS)
  143. /*
  144. * This timeout duration is to clean up any call completion records that
  145. * are forgotten about by the switch.
  146. */
  147. #define MISDN_CC_RECORD_AGE_MAX (6UL * 60 * 60) /* seconds */
  148. #define MISDN_CC_REQUEST_WAIT_MAX 5 /* seconds */
  149. /*!
  150. * \brief Caller that initialized call completion services
  151. *
  152. * \details
  153. * This data is the payload for a datastore that is put on the channel that
  154. * initializes call completion services. This datastore is set to be inherited
  155. * by the outbound mISDN channel. When one of these channels hangs up, the
  156. * channel pointer will be set to NULL. That way, we can ensure that we do not
  157. * touch this channel after it gets destroyed.
  158. */
  159. struct misdn_cc_caller {
  160. /*! \brief The channel that initialized call completion services */
  161. struct ast_channel *chan;
  162. };
  163. struct misdn_cc_notify {
  164. /*! \brief Dialplan: Notify extension priority */
  165. int priority;
  166. /*! \brief Dialplan: Notify extension context */
  167. char context[AST_MAX_CONTEXT];
  168. /*! \brief Dialplan: Notify extension number (User-A) */
  169. char exten[AST_MAX_EXTENSION];
  170. };
  171. /*! \brief mISDN call completion record */
  172. struct misdn_cc_record {
  173. /*! \brief Call completion record linked list */
  174. AST_LIST_ENTRY(misdn_cc_record) list;
  175. /*! \brief Time the record was created. */
  176. time_t time_created;
  177. /*! \brief MISDN_CC_RECORD_ID value */
  178. long record_id;
  179. /*!
  180. * \brief Logical Layer 1 port associated with this
  181. * call completion record
  182. */
  183. int port;
  184. /*! \brief TRUE if point-to-point mode (CCBS-T/CCNR-T mode) */
  185. int ptp;
  186. /*! \brief Mode specific parameters */
  187. union {
  188. /*! \brief point-to-point specific parameters. */
  189. struct {
  190. /*!
  191. * \brief Call-completion signaling link.
  192. * NULL if signaling link not established.
  193. */
  194. struct misdn_bchannel *bc;
  195. /*!
  196. * \brief TRUE if we requested the request retention option
  197. * to be enabled.
  198. */
  199. int requested_retention;
  200. /*!
  201. * \brief TRUE if the request retention option is enabled.
  202. */
  203. int retention_enabled;
  204. } ptp;
  205. /*! \brief point-to-multi-point specific parameters. */
  206. struct {
  207. /*! \brief CallLinkageID (valid when port determined) */
  208. int linkage_id;
  209. /*! \breif CCBSReference (valid when activated is TRUE) */
  210. int reference_id;
  211. /*! \brief globalRecall(0), specificRecall(1) */
  212. int recall_mode;
  213. } ptmp;
  214. } mode;
  215. /*! \brief TRUE if call completion activated */
  216. int activated;
  217. /*! \brief Outstanding message ID (valid when outstanding_message) */
  218. int invoke_id;
  219. /*! \brief TRUE if waiting for a response from a message (invoke_id is valid) */
  220. int outstanding_message;
  221. /*! \brief TRUE if activation has been requested */
  222. int activation_requested;
  223. /*!
  224. * \brief TRUE if User-A is free
  225. * \note PTMP - Used to answer CCBSStatusRequest.
  226. * PTP - Determines how to respond to CCBS_T_RemoteUserFree.
  227. */
  228. int party_a_free;
  229. /*! \brief Error code received from last outstanding message. */
  230. enum FacErrorCode error_code;
  231. /*! \brief Reject code received from last outstanding message. */
  232. enum FacRejectCode reject_code;
  233. /*!
  234. * \brief Saved struct misdn_bchannel call information when
  235. * attempted to call User-B
  236. */
  237. struct {
  238. /*! \brief User-A caller id information */
  239. struct misdn_party_id caller;
  240. /*! \brief User-B number information */
  241. struct misdn_party_dialing dialed;
  242. /*! \brief The BC, HLC (optional) and LLC (optional) contents from the SETUP message. */
  243. struct Q931_Bc_Hlc_Llc setup_bc_hlc_llc;
  244. /*! \brief SETUP message bearer capability field code value */
  245. int capability;
  246. /*! \brief TRUE if call made in digital HDLC mode */
  247. int hdlc;
  248. } redial;
  249. /*! \brief Dialplan location to indicate User-B free and User-A is free */
  250. struct misdn_cc_notify remote_user_free;
  251. /*! \brief Dialplan location to indicate User-B free and User-A is busy */
  252. struct misdn_cc_notify b_free;
  253. };
  254. /*! \brief mISDN call completion record database */
  255. static AST_LIST_HEAD_STATIC(misdn_cc_records_db, misdn_cc_record);
  256. /*! \brief Next call completion record ID to use */
  257. static __u16 misdn_cc_record_id;
  258. /*! \brief Next invoke ID to use */
  259. static __s16 misdn_invoke_id;
  260. static const char misdn_no_response_from_network[] = "No response from network";
  261. static const char misdn_cc_record_not_found[] = "Call completion record not found";
  262. /* mISDN channel variable names */
  263. #define MISDN_CC_RECORD_ID "MISDN_CC_RECORD_ID"
  264. #define MISDN_CC_STATUS "MISDN_CC_STATUS"
  265. #define MISDN_ERROR_MSG "MISDN_ERROR_MSG"
  266. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  267. static ast_mutex_t release_lock;
  268. enum misdn_chan_state {
  269. MISDN_NOTHING = 0, /*!< at beginning */
  270. MISDN_WAITING4DIGS, /*!< when waiting for info */
  271. MISDN_EXTCANTMATCH, /*!< when asterisk couldn't match our ext */
  272. MISDN_INCOMING_SETUP, /*!< for incoming setup */
  273. MISDN_DIALING, /*!< when pbx_start */
  274. MISDN_PROGRESS, /*!< we have progress */
  275. MISDN_PROCEEDING, /*!< we have progress */
  276. MISDN_CALLING, /*!< when misdn_call is called */
  277. MISDN_CALLING_ACKNOWLEDGE, /*!< when we get SETUP_ACK */
  278. MISDN_ALERTING, /*!< when Alerting */
  279. MISDN_BUSY, /*!< when BUSY */
  280. MISDN_CONNECTED, /*!< when connected */
  281. MISDN_DISCONNECTED, /*!< when connected */
  282. MISDN_CLEANING, /*!< when hangup from * but we were connected before */
  283. };
  284. /*! Asterisk created the channel (outgoing call) */
  285. #define ORG_AST 1
  286. /*! mISDN created the channel (incoming call) */
  287. #define ORG_MISDN 2
  288. enum misdn_hold_state {
  289. MISDN_HOLD_IDLE, /*!< HOLD not active */
  290. MISDN_HOLD_ACTIVE, /*!< Call is held */
  291. MISDN_HOLD_TRANSFER, /*!< Held call is being transferred */
  292. MISDN_HOLD_DISCONNECT, /*!< Held call is being disconnected */
  293. };
  294. struct hold_info {
  295. /*!
  296. * \brief Call HOLD state.
  297. */
  298. enum misdn_hold_state state;
  299. /*!
  300. * \brief Logical port the channel call record is HELD on
  301. * because the B channel is no longer associated.
  302. */
  303. int port;
  304. /*!
  305. * \brief Original B channel number the HELD call was using.
  306. * \note Used only for debug display messages.
  307. */
  308. int channel;
  309. };
  310. #define chan_list_ref(obj, debug) (ao2_t_ref((obj), +1, (debug)), (obj))
  311. #define chan_list_unref(obj, debug) (ao2_t_ref((obj), -1, (debug)), NULL)
  312. /*!
  313. * \brief Channel call record structure
  314. */
  315. struct chan_list {
  316. /*!
  317. * \brief The "allowed_bearers" string read in from /etc/asterisk/misdn.conf
  318. */
  319. char allowed_bearers[BUFFERSIZE + 1];
  320. /*!
  321. * \brief State of the channel
  322. */
  323. enum misdn_chan_state state;
  324. /*!
  325. * \brief TRUE if a hangup needs to be queued
  326. * \note This is a debug flag only used to catch calls to hangup_chan() that are already hungup.
  327. */
  328. int need_queue_hangup;
  329. /*!
  330. * \brief TRUE if a channel can be hung up by calling asterisk directly when done.
  331. */
  332. int need_hangup;
  333. /*!
  334. * \brief TRUE if we could send an AST_CONTROL_BUSY if needed.
  335. */
  336. int need_busy;
  337. /*!
  338. * \brief Who originally created this channel. ORG_AST or ORG_MISDN
  339. */
  340. int originator;
  341. /*!
  342. * \brief TRUE of we are not to respond immediately to a SETUP message. Check the dialplan first.
  343. * \note The "noautorespond_on_setup" boolean read in from /etc/asterisk/misdn.conf
  344. */
  345. int noautorespond_on_setup;
  346. int norxtone; /*!< Boolean assigned values but the value is not used. */
  347. /*!
  348. * \brief TRUE if we are not to generate tones (Playtones)
  349. */
  350. int notxtone;
  351. /*!
  352. * \brief TRUE if echo canceller is enabled. Value is toggled.
  353. */
  354. int toggle_ec;
  355. /*!
  356. * \brief TRUE if you want to send Tone Indications to an incoming
  357. * ISDN channel on a TE Port.
  358. * \note The "incoming_early_audio" boolean read in from /etc/asterisk/misdn.conf
  359. */
  360. int incoming_early_audio;
  361. /*!
  362. * \brief TRUE if DTMF digits are to be passed inband only.
  363. * \note It is settable by the misdn_set_opt() application.
  364. */
  365. int ignore_dtmf;
  366. /*!
  367. * \brief Pipe file descriptor handles array.
  368. * Read from pipe[0], write to pipe[1]
  369. */
  370. int pipe[2];
  371. /*!
  372. * \brief Read buffer for inbound audio from pipe[0]
  373. */
  374. char ast_rd_buf[4096];
  375. /*!
  376. * \brief Inbound audio frame returned by misdn_read().
  377. */
  378. struct ast_frame frame;
  379. /*!
  380. * \brief Fax detection option. (0:no 1:yes 2:yes+nojump)
  381. * \note The "faxdetect" option string read in from /etc/asterisk/misdn.conf
  382. * \note It is settable by the misdn_set_opt() application.
  383. */
  384. int faxdetect;
  385. /*!
  386. * \brief Number of seconds to detect a Fax machine when detection enabled.
  387. * \note 0 disables the timeout.
  388. * \note The "faxdetect_timeout" value read in from /etc/asterisk/misdn.conf
  389. */
  390. int faxdetect_timeout;
  391. /*!
  392. * \brief Starting time of fax detection with timeout when nonzero.
  393. */
  394. struct timeval faxdetect_tv;
  395. /*!
  396. * \brief TRUE if a fax has been detected.
  397. */
  398. int faxhandled;
  399. /*!
  400. * \brief TRUE if we will use the Asterisk DSP to detect DTMF/Fax
  401. * \note The "astdtmf" boolean read in from /etc/asterisk/misdn.conf
  402. */
  403. int ast_dsp;
  404. /*!
  405. * \brief Jitterbuffer length
  406. * \note The "jitterbuffer" value read in from /etc/asterisk/misdn.conf
  407. */
  408. int jb_len;
  409. /*!
  410. * \brief Jitterbuffer upper threshold
  411. * \note The "jitterbuffer_upper_threshold" value read in from /etc/asterisk/misdn.conf
  412. */
  413. int jb_upper_threshold;
  414. /*!
  415. * \brief Allocated jitterbuffer controller
  416. * \note misdn_jb_init() creates the jitterbuffer.
  417. * \note Must use misdn_jb_destroy() to clean up.
  418. */
  419. struct misdn_jb *jb;
  420. /*!
  421. * \brief Allocated DSP controller
  422. * \note ast_dsp_new() creates the DSP controller.
  423. * \note Must use ast_dsp_free() to clean up.
  424. */
  425. struct ast_dsp *dsp;
  426. /*!
  427. * \brief Associated Asterisk channel structure.
  428. */
  429. struct ast_channel * ast;
  430. /*!
  431. * \brief Associated B channel structure.
  432. */
  433. struct misdn_bchannel *bc;
  434. #if defined(AST_MISDN_ENHANCEMENTS)
  435. /*!
  436. * \brief Peer channel for which call completion was initialized.
  437. */
  438. struct misdn_cc_caller *peer;
  439. /*! \brief Associated call completion record ID (-1 if not associated) */
  440. long record_id;
  441. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  442. /*!
  443. * \brief HELD channel call information
  444. */
  445. struct hold_info hold;
  446. /*!
  447. * \brief From associated B channel: Layer 3 process ID
  448. * \note Used to find the HELD channel call record when retrieving a call.
  449. */
  450. unsigned int l3id;
  451. /*!
  452. * \brief From associated B channel: B Channel mISDN driver layer ID from mISDN_get_layerid()
  453. * \note Used only for debug display messages.
  454. */
  455. int addr;
  456. /*!
  457. * \brief Incoming call dialplan context identifier.
  458. * \note The "context" string read in from /etc/asterisk/misdn.conf
  459. */
  460. char context[AST_MAX_CONTEXT];
  461. /*!
  462. * \brief The configured music-on-hold class to use for this call.
  463. * \note The "musicclass" string read in from /etc/asterisk/misdn.conf
  464. */
  465. char mohinterpret[MAX_MUSICCLASS];
  466. /*!
  467. * \brief Number of outgoing audio frames dropped since last debug gripe message.
  468. */
  469. int dropped_frame_cnt;
  470. /*!
  471. * \brief TRUE if we must do the ringback tones.
  472. * \note The "far_alerting" boolean read in from /etc/asterisk/misdn.conf
  473. */
  474. int far_alerting;
  475. /*!
  476. * \brief TRUE if NT should disconnect an overlap dialing call when a timeout occurs.
  477. * \note The "nttimeout" boolean read in from /etc/asterisk/misdn.conf
  478. */
  479. int nttimeout;
  480. /*!
  481. * \brief Tone zone sound used for dialtone generation.
  482. * \note Used as a boolean. Non-NULL to prod generation if enabled.
  483. */
  484. struct ast_tone_zone_sound *ts;
  485. /*!
  486. * \brief Enables overlap dialing for the set amount of seconds. (0 = Disabled)
  487. * \note The "overlapdial" value read in from /etc/asterisk/misdn.conf
  488. */
  489. int overlap_dial;
  490. /*!
  491. * \brief Overlap dialing timeout Task ID. -1 if not running.
  492. */
  493. int overlap_dial_task;
  494. /*!
  495. * \brief overlap_tv access lock.
  496. */
  497. ast_mutex_t overlap_tv_lock;
  498. /*!
  499. * \brief Overlap timer start time. Timer restarted for every digit received.
  500. */
  501. struct timeval overlap_tv;
  502. /*!
  503. * \brief Next channel call record in the list.
  504. */
  505. struct chan_list *next;
  506. };
  507. int MAXTICS = 8;
  508. void export_ch(struct ast_channel *chan, struct misdn_bchannel *bc, struct chan_list *ch);
  509. void import_ch(struct ast_channel *chan, struct misdn_bchannel *bc, struct chan_list *ch);
  510. static struct ast_frame *process_ast_dsp(struct chan_list *tmp, struct ast_frame *frame);
  511. struct robin_list {
  512. char *group;
  513. int port;
  514. int channel;
  515. struct robin_list *next;
  516. struct robin_list *prev;
  517. };
  518. static struct robin_list *robin = NULL;
  519. static void free_robin_list(void)
  520. {
  521. struct robin_list *r;
  522. struct robin_list *next;
  523. for (r = robin, robin = NULL; r; r = next) {
  524. next = r->next;
  525. ast_free(r->group);
  526. ast_free(r);
  527. }
  528. }
  529. static struct robin_list *get_robin_position(char *group)
  530. {
  531. struct robin_list *new;
  532. struct robin_list *iter = robin;
  533. for (; iter; iter = iter->next) {
  534. if (!strcasecmp(iter->group, group)) {
  535. return iter;
  536. }
  537. }
  538. new = ast_calloc(1, sizeof(*new));
  539. if (!new) {
  540. return NULL;
  541. }
  542. new->group = ast_strdup(group);
  543. if (!new->group) {
  544. ast_free(new);
  545. return NULL;
  546. }
  547. new->channel = 1;
  548. if (robin) {
  549. new->next = robin;
  550. robin->prev = new;
  551. }
  552. robin = new;
  553. return robin;
  554. }
  555. /*! \brief the main schedule context for stuff like l1 watcher, overlap dial, ... */
  556. static struct ast_sched_context *misdn_tasks = NULL;
  557. static pthread_t misdn_tasks_thread;
  558. static int *misdn_ports;
  559. static void chan_misdn_log(int level, int port, char *tmpl, ...)
  560. __attribute__((format(printf, 3, 4)));
  561. static struct ast_channel *misdn_new(struct chan_list *cl, int state, char *exten, char *callerid, struct ast_format_cap *cap, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, int port, int c);
  562. static void send_digit_to_chan(struct chan_list *cl, char digit);
  563. static int pbx_start_chan(struct chan_list *ch);
  564. #define MISDN_ASTERISK_TECH_PVT(ast) ast_channel_tech_pvt(ast)
  565. #define MISDN_ASTERISK_TECH_PVT_SET(ast, value) ast_channel_tech_pvt_set(ast, value)
  566. #include "asterisk/strings.h"
  567. /* #define MISDN_DEBUG 1 */
  568. static const char misdn_type[] = "mISDN";
  569. static int tracing = 0;
  570. /*! \brief Only alaw and mulaw is allowed for now */
  571. static struct ast_format prefformat; /* AST_FORMAT_SLINEAR ; AST_FORMAT_ULAW | */
  572. static int *misdn_debug;
  573. static int *misdn_debug_only;
  574. static int max_ports;
  575. static int *misdn_in_calls;
  576. static int *misdn_out_calls;
  577. /*!
  578. * \brief Global channel call record list head.
  579. */
  580. static struct chan_list *cl_te=NULL;
  581. static ast_mutex_t cl_te_lock;
  582. static enum event_response_e
  583. cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data);
  584. static int send_cause2ast(struct ast_channel *ast, struct misdn_bchannel *bc, struct chan_list *ch);
  585. static void cl_queue_chan(struct chan_list *chan);
  586. static int dialtone_indicate(struct chan_list *cl);
  587. static void hanguptone_indicate(struct chan_list *cl);
  588. static int stop_indicate(struct chan_list *cl);
  589. static int start_bc_tones(struct chan_list *cl);
  590. static int stop_bc_tones(struct chan_list *cl);
  591. static void release_chan_early(struct chan_list *ch);
  592. static void release_chan(struct chan_list *ch, struct misdn_bchannel *bc);
  593. #if defined(AST_MISDN_ENHANCEMENTS)
  594. static const char misdn_command_name[] = "misdn_command";
  595. static int misdn_command_exec(struct ast_channel *chan, const char *data);
  596. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  597. static int misdn_check_l2l1(struct ast_channel *chan, const char *data);
  598. static int misdn_set_opt_exec(struct ast_channel *chan, const char *data);
  599. static int misdn_facility_exec(struct ast_channel *chan, const char *data);
  600. int chan_misdn_jb_empty(struct misdn_bchannel *bc, char *buf, int len);
  601. void debug_numtype(int port, int numtype, char *type);
  602. int add_out_calls(int port);
  603. int add_in_calls(int port);
  604. #ifdef MISDN_1_2
  605. static int update_pipeline_config(struct misdn_bchannel *bc);
  606. #else
  607. static int update_ec_config(struct misdn_bchannel *bc);
  608. #endif
  609. /*************** Helpers *****************/
  610. static int misdn_chan_is_valid(struct chan_list *ch)
  611. {
  612. struct chan_list *list;
  613. ast_mutex_lock(&cl_te_lock);
  614. for (list = cl_te; list; list = list->next) {
  615. if (list == ch) {
  616. ast_mutex_unlock(&cl_te_lock);
  617. return 1;
  618. }
  619. }
  620. ast_mutex_unlock(&cl_te_lock);
  621. return 0;
  622. }
  623. #if defined(mISDN_NATIVE_BRIDGING)
  624. /*! Returns a reference to the found chan_list. */
  625. static struct chan_list *get_chan_by_ast(struct ast_channel *ast)
  626. {
  627. struct chan_list *tmp;
  628. ast_mutex_lock(&cl_te_lock);
  629. for (tmp = cl_te; tmp; tmp = tmp->next) {
  630. if (tmp->ast == ast) {
  631. chan_list_ref(tmp, "Found chan_list by ast");
  632. ast_mutex_unlock(&cl_te_lock);
  633. return tmp;
  634. }
  635. }
  636. ast_mutex_unlock(&cl_te_lock);
  637. return NULL;
  638. }
  639. #endif /* defined(mISDN_NATIVE_BRIDGING) */
  640. /*! Returns a reference to the found chan_list. */
  641. static struct chan_list *get_chan_by_ast_name(const char *name)
  642. {
  643. struct chan_list *tmp;
  644. ast_mutex_lock(&cl_te_lock);
  645. for (tmp = cl_te; tmp; tmp = tmp->next) {
  646. if (tmp->ast && strcmp(ast_channel_name(tmp->ast), name) == 0) {
  647. chan_list_ref(tmp, "Found chan_list by ast name");
  648. ast_mutex_unlock(&cl_te_lock);
  649. return tmp;
  650. }
  651. }
  652. ast_mutex_unlock(&cl_te_lock);
  653. return NULL;
  654. }
  655. #if defined(AST_MISDN_ENHANCEMENTS)
  656. /*!
  657. * \internal
  658. * \brief Destroy the misdn_cc_ds_info datastore payload
  659. *
  660. * \param[in] data the datastore payload, a reference to an misdn_cc_caller
  661. *
  662. * \details
  663. * Since the payload is a reference to an astobj2 object, we just decrement its
  664. * reference count. Before doing so, we NULL out the channel pointer inside of
  665. * the misdn_cc_caller instance. This function will be called in one of two
  666. * cases. In both cases, we no longer need the channel pointer:
  667. *
  668. * - The original channel that initialized call completion services, the same
  669. * channel that is stored here, has been destroyed early. This could happen
  670. * if it transferred the mISDN channel, for example.
  671. *
  672. * - The mISDN channel that had this datastore inherited on to it is now being
  673. * destroyed. If this is the case, then the call completion events have
  674. * already occurred and the appropriate channel variables have already been
  675. * set on the original channel that requested call completion services.
  676. *
  677. * \return Nothing
  678. */
  679. static void misdn_cc_ds_destroy(void *data)
  680. {
  681. struct misdn_cc_caller *cc_caller = data;
  682. ao2_lock(cc_caller);
  683. cc_caller->chan = NULL;
  684. ao2_unlock(cc_caller);
  685. ao2_ref(cc_caller, -1);
  686. }
  687. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  688. #if defined(AST_MISDN_ENHANCEMENTS)
  689. /*!
  690. * \internal
  691. * \brief Duplicate the misdn_cc_ds_info datastore payload
  692. *
  693. * \param[in] data the datastore payload, a reference to an misdn_cc_caller
  694. *
  695. * \details
  696. * All we need to do is bump the reference count and return the same instance.
  697. *
  698. * \return A reference to an instance of a misdn_cc_caller
  699. */
  700. static void *misdn_cc_ds_duplicate(void *data)
  701. {
  702. struct misdn_cc_caller *cc_caller = data;
  703. ao2_ref(cc_caller, +1);
  704. return cc_caller;
  705. }
  706. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  707. #if defined(AST_MISDN_ENHANCEMENTS)
  708. static const struct ast_datastore_info misdn_cc_ds_info = {
  709. .type = "misdn_cc",
  710. .destroy = misdn_cc_ds_destroy,
  711. .duplicate = misdn_cc_ds_duplicate,
  712. };
  713. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  714. #if defined(AST_MISDN_ENHANCEMENTS)
  715. /*!
  716. * \internal
  717. * \brief Set a channel var on the peer channel for call completion services
  718. *
  719. * \param[in] peer The peer that initialized call completion services
  720. * \param[in] var The variable name to set
  721. * \param[in] value The variable value to set
  722. *
  723. * This function may be called from outside of the channel thread. It handles
  724. * the fact that the peer channel may be hung up and destroyed at any time.
  725. *
  726. * \return nothing
  727. */
  728. static void misdn_cc_set_peer_var(struct misdn_cc_caller *peer, const char *var,
  729. const char *value)
  730. {
  731. ao2_lock(peer);
  732. /*! \todo XXX This nastiness can go away once ast_channel is ref counted! */
  733. while (peer->chan && ast_channel_trylock(peer->chan)) {
  734. ao2_unlock(peer);
  735. sched_yield();
  736. ao2_lock(peer);
  737. }
  738. if (peer->chan) {
  739. pbx_builtin_setvar_helper(peer->chan, var, value);
  740. ast_channel_unlock(peer->chan);
  741. }
  742. ao2_unlock(peer);
  743. }
  744. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  745. #if defined(AST_MISDN_ENHANCEMENTS)
  746. /*!
  747. * \internal
  748. * \brief Get a reference to the CC caller if it exists
  749. */
  750. static struct misdn_cc_caller *misdn_cc_caller_get(struct ast_channel *chan)
  751. {
  752. struct ast_datastore *datastore;
  753. struct misdn_cc_caller *cc_caller;
  754. ast_channel_lock(chan);
  755. if (!(datastore = ast_channel_datastore_find(chan, &misdn_cc_ds_info, NULL))) {
  756. ast_channel_unlock(chan);
  757. return NULL;
  758. }
  759. ao2_ref(datastore->data, +1);
  760. cc_caller = datastore->data;
  761. ast_channel_unlock(chan);
  762. return cc_caller;
  763. }
  764. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  765. #if defined(AST_MISDN_ENHANCEMENTS)
  766. /*!
  767. * \internal
  768. * \brief Find the call completion record given the record id.
  769. *
  770. * \param record_id
  771. *
  772. * \retval pointer to found call completion record
  773. * \retval NULL if not found
  774. *
  775. * \note Assumes the misdn_cc_records_db lock is already obtained.
  776. */
  777. static struct misdn_cc_record *misdn_cc_find_by_id(long record_id)
  778. {
  779. struct misdn_cc_record *current;
  780. AST_LIST_TRAVERSE(&misdn_cc_records_db, current, list) {
  781. if (current->record_id == record_id) {
  782. /* Found the record */
  783. break;
  784. }
  785. }
  786. return current;
  787. }
  788. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  789. #if defined(AST_MISDN_ENHANCEMENTS)
  790. /*!
  791. * \internal
  792. * \brief Find the call completion record given the port and call linkage id.
  793. *
  794. * \param port Logical port number
  795. * \param linkage_id Call linkage ID number from switch.
  796. *
  797. * \retval pointer to found call completion record
  798. * \retval NULL if not found
  799. *
  800. * \note Assumes the misdn_cc_records_db lock is already obtained.
  801. */
  802. static struct misdn_cc_record *misdn_cc_find_by_linkage(int port, int linkage_id)
  803. {
  804. struct misdn_cc_record *current;
  805. AST_LIST_TRAVERSE(&misdn_cc_records_db, current, list) {
  806. if (current->port == port
  807. && !current->ptp
  808. && current->mode.ptmp.linkage_id == linkage_id) {
  809. /* Found the record */
  810. break;
  811. }
  812. }
  813. return current;
  814. }
  815. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  816. #if defined(AST_MISDN_ENHANCEMENTS)
  817. /*!
  818. * \internal
  819. * \brief Find the call completion record given the port and outstanding invocation id.
  820. *
  821. * \param port Logical port number
  822. * \param invoke_id Outstanding message invocation ID number.
  823. *
  824. * \retval pointer to found call completion record
  825. * \retval NULL if not found
  826. *
  827. * \note Assumes the misdn_cc_records_db lock is already obtained.
  828. */
  829. static struct misdn_cc_record *misdn_cc_find_by_invoke(int port, int invoke_id)
  830. {
  831. struct misdn_cc_record *current;
  832. AST_LIST_TRAVERSE(&misdn_cc_records_db, current, list) {
  833. if (current->outstanding_message
  834. && current->invoke_id == invoke_id
  835. && current->port == port) {
  836. /* Found the record */
  837. break;
  838. }
  839. }
  840. return current;
  841. }
  842. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  843. #if defined(AST_MISDN_ENHANCEMENTS)
  844. /*!
  845. * \internal
  846. * \brief Find the call completion record given the port and CCBS reference id.
  847. *
  848. * \param port Logical port number
  849. * \param reference_id CCBS reference ID number from switch.
  850. *
  851. * \retval pointer to found call completion record
  852. * \retval NULL if not found
  853. *
  854. * \note Assumes the misdn_cc_records_db lock is already obtained.
  855. */
  856. static struct misdn_cc_record *misdn_cc_find_by_reference(int port, int reference_id)
  857. {
  858. struct misdn_cc_record *current;
  859. AST_LIST_TRAVERSE(&misdn_cc_records_db, current, list) {
  860. if (current->activated
  861. && current->port == port
  862. && !current->ptp
  863. && current->mode.ptmp.reference_id == reference_id) {
  864. /* Found the record */
  865. break;
  866. }
  867. }
  868. return current;
  869. }
  870. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  871. #if defined(AST_MISDN_ENHANCEMENTS)
  872. /*!
  873. * \internal
  874. * \brief Find the call completion record given the B channel pointer
  875. *
  876. * \param bc B channel control structure pointer.
  877. *
  878. * \retval pointer to found call completion record
  879. * \retval NULL if not found
  880. *
  881. * \note Assumes the misdn_cc_records_db lock is already obtained.
  882. */
  883. static struct misdn_cc_record *misdn_cc_find_by_bc(const struct misdn_bchannel *bc)
  884. {
  885. struct misdn_cc_record *current;
  886. if (bc) {
  887. AST_LIST_TRAVERSE(&misdn_cc_records_db, current, list) {
  888. if (current->ptp
  889. && current->mode.ptp.bc == bc) {
  890. /* Found the record */
  891. break;
  892. }
  893. }
  894. } else {
  895. current = NULL;
  896. }
  897. return current;
  898. }
  899. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  900. #if defined(AST_MISDN_ENHANCEMENTS)
  901. /*!
  902. * \internal
  903. * \brief Delete the given call completion record
  904. *
  905. * \param doomed Call completion record to destroy
  906. *
  907. * \return Nothing
  908. *
  909. * \note Assumes the misdn_cc_records_db lock is already obtained.
  910. */
  911. static void misdn_cc_delete(struct misdn_cc_record *doomed)
  912. {
  913. struct misdn_cc_record *current;
  914. AST_LIST_TRAVERSE_SAFE_BEGIN(&misdn_cc_records_db, current, list) {
  915. if (current == doomed) {
  916. AST_LIST_REMOVE_CURRENT(list);
  917. ast_free(current);
  918. return;
  919. }
  920. }
  921. AST_LIST_TRAVERSE_SAFE_END;
  922. /* The doomed node is not in the call completion database */
  923. }
  924. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  925. #if defined(AST_MISDN_ENHANCEMENTS)
  926. /*!
  927. * \internal
  928. * \brief Delete all old call completion records
  929. *
  930. * \return Nothing
  931. *
  932. * \note Assumes the misdn_cc_records_db lock is already obtained.
  933. */
  934. static void misdn_cc_remove_old(void)
  935. {
  936. struct misdn_cc_record *current;
  937. time_t now;
  938. now = time(NULL);
  939. AST_LIST_TRAVERSE_SAFE_BEGIN(&misdn_cc_records_db, current, list) {
  940. if (MISDN_CC_RECORD_AGE_MAX < now - current->time_created) {
  941. if (current->ptp && current->mode.ptp.bc) {
  942. /* Close the old call-completion signaling link */
  943. current->mode.ptp.bc->fac_out.Function = Fac_None;
  944. current->mode.ptp.bc->out_cause = AST_CAUSE_NORMAL_CLEARING;
  945. misdn_lib_send_event(current->mode.ptp.bc, EVENT_RELEASE_COMPLETE);
  946. }
  947. /* Remove the old call completion record */
  948. AST_LIST_REMOVE_CURRENT(list);
  949. ast_free(current);
  950. }
  951. }
  952. AST_LIST_TRAVERSE_SAFE_END;
  953. }
  954. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  955. #if defined(AST_MISDN_ENHANCEMENTS)
  956. /*!
  957. * \internal
  958. * \brief Allocate the next record id.
  959. *
  960. * \retval New record id on success.
  961. * \retval -1 on error.
  962. *
  963. * \note Assumes the misdn_cc_records_db lock is already obtained.
  964. */
  965. static long misdn_cc_record_id_new(void)
  966. {
  967. long record_id;
  968. long first_id;
  969. record_id = ++misdn_cc_record_id;
  970. first_id = record_id;
  971. while (misdn_cc_find_by_id(record_id)) {
  972. record_id = ++misdn_cc_record_id;
  973. if (record_id == first_id) {
  974. /*
  975. * We have a resource leak.
  976. * We should never need to allocate 64k records.
  977. */
  978. chan_misdn_log(0, 0, " --> ERROR Too many call completion records!\n");
  979. record_id = -1;
  980. break;
  981. }
  982. }
  983. return record_id;
  984. }
  985. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  986. #if defined(AST_MISDN_ENHANCEMENTS)
  987. /*!
  988. * \internal
  989. * \brief Create a new call completion record
  990. *
  991. * \retval pointer to new call completion record
  992. * \retval NULL if failed
  993. *
  994. * \note Assumes the misdn_cc_records_db lock is already obtained.
  995. */
  996. static struct misdn_cc_record *misdn_cc_new(void)
  997. {
  998. struct misdn_cc_record *cc_record;
  999. long record_id;
  1000. misdn_cc_remove_old();
  1001. cc_record = ast_calloc(1, sizeof(*cc_record));
  1002. if (cc_record) {
  1003. record_id = misdn_cc_record_id_new();
  1004. if (record_id < 0) {
  1005. ast_free(cc_record);
  1006. return NULL;
  1007. }
  1008. /* Initialize the new record */
  1009. cc_record->record_id = record_id;
  1010. cc_record->port = -1;/* Invalid port so it will never be found this way */
  1011. cc_record->invoke_id = ++misdn_invoke_id;
  1012. cc_record->party_a_free = 1;/* Default User-A as free */
  1013. cc_record->error_code = FacError_None;
  1014. cc_record->reject_code = FacReject_None;
  1015. cc_record->time_created = time(NULL);
  1016. /* Insert the new record into the database */
  1017. AST_LIST_INSERT_HEAD(&misdn_cc_records_db, cc_record, list);
  1018. }
  1019. return cc_record;
  1020. }
  1021. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  1022. #if defined(AST_MISDN_ENHANCEMENTS)
  1023. /*!
  1024. * \internal
  1025. * \brief Destroy the call completion record database
  1026. *
  1027. * \return Nothing
  1028. */
  1029. static void misdn_cc_destroy(void)
  1030. {
  1031. struct misdn_cc_record *current;
  1032. while ((current = AST_LIST_REMOVE_HEAD(&misdn_cc_records_db, list))) {
  1033. /* Do a misdn_cc_delete(current) inline */
  1034. ast_free(current);
  1035. }
  1036. }
  1037. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  1038. #if defined(AST_MISDN_ENHANCEMENTS)
  1039. /*!
  1040. * \internal
  1041. * \brief Initialize the call completion record database
  1042. *
  1043. * \return Nothing
  1044. */
  1045. static void misdn_cc_init(void)
  1046. {
  1047. misdn_cc_record_id = 0;
  1048. }
  1049. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  1050. #if defined(AST_MISDN_ENHANCEMENTS)
  1051. /*!
  1052. * \internal
  1053. * \brief Check the status of an outstanding invocation request.
  1054. *
  1055. * \param data Points to an integer containing the call completion record id.
  1056. *
  1057. * \retval 0 if got a response.
  1058. * \retval -1 if no response yet.
  1059. */
  1060. static int misdn_cc_response_check(void *data)
  1061. {
  1062. int not_responded;
  1063. struct misdn_cc_record *cc_record;
  1064. AST_LIST_LOCK(&misdn_cc_records_db);
  1065. cc_record = misdn_cc_find_by_id(*(long *) data);
  1066. if (cc_record) {
  1067. if (cc_record->outstanding_message) {
  1068. not_responded = -1;
  1069. } else {
  1070. not_responded = 0;
  1071. }
  1072. } else {
  1073. /* No record so there is no response to check. */
  1074. not_responded = 0;
  1075. }
  1076. AST_LIST_UNLOCK(&misdn_cc_records_db);
  1077. return not_responded;
  1078. }
  1079. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  1080. #if defined(AST_MISDN_ENHANCEMENTS)
  1081. /*!
  1082. * \internal
  1083. * \brief Wait for a response from the switch for an outstanding
  1084. * invocation request.
  1085. *
  1086. * \param chan Asterisk channel to operate upon.
  1087. * \param wait_seconds Number of seconds to wait
  1088. * \param record_id Call completion record ID.
  1089. *
  1090. * \return Nothing
  1091. */
  1092. static void misdn_cc_response_wait(struct ast_channel *chan, int wait_seconds, long record_id)
  1093. {
  1094. unsigned count;
  1095. for (count = 2 * MISDN_CC_REQUEST_WAIT_MAX; count--;) {
  1096. /* Sleep in 500 ms increments */
  1097. if (ast_safe_sleep_conditional(chan, 500, misdn_cc_response_check, &record_id) != 0) {
  1098. /* We got hung up or our response came in. */
  1099. break;
  1100. }
  1101. }
  1102. }
  1103. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  1104. #if defined(AST_MISDN_ENHANCEMENTS)
  1105. /*!
  1106. * \internal
  1107. * \brief Convert the mISDN reject code to a string
  1108. *
  1109. * \param code mISDN reject code.
  1110. *
  1111. * \return The mISDN reject code as a string
  1112. */
  1113. static const char *misdn_to_str_reject_code(enum FacRejectCode code)
  1114. {
  1115. static const struct {
  1116. enum FacRejectCode code;
  1117. char *name;
  1118. } arr[] = {
  1119. /* *INDENT-OFF* */
  1120. { FacReject_None, "No reject occurred" },
  1121. { FacReject_Unknown, "Unknown reject code" },
  1122. { FacReject_Gen_UnrecognizedComponent, "General: Unrecognized Component" },
  1123. { FacReject_Gen_MistypedComponent, "General: Mistyped Component" },
  1124. { FacReject_Gen_BadlyStructuredComponent, "General: Badly Structured Component" },
  1125. { FacReject_Inv_DuplicateInvocation, "Invoke: Duplicate Invocation" },
  1126. { FacReject_Inv_UnrecognizedOperation, "Invoke: Unrecognized Operation" },
  1127. { FacReject_Inv_MistypedArgument, "Invoke: Mistyped Argument" },
  1128. { FacReject_Inv_ResourceLimitation, "Invoke: Resource Limitation" },
  1129. { FacReject_Inv_InitiatorReleasing, "Invoke: Initiator Releasing" },
  1130. { FacReject_Inv_UnrecognizedLinkedID, "Invoke: Unrecognized Linked ID" },
  1131. { FacReject_Inv_LinkedResponseUnexpected, "Invoke: Linked Response Unexpected" },
  1132. { FacReject_Inv_UnexpectedChildOperation, "Invoke: Unexpected Child Operation" },
  1133. { FacReject_Res_UnrecognizedInvocation, "Result: Unrecognized Invocation" },
  1134. { FacReject_Res_ResultResponseUnexpected, "Result: Result Response Unexpected" },
  1135. { FacReject_Res_MistypedResult, "Result: Mistyped Result" },
  1136. { FacReject_Err_UnrecognizedInvocation, "Error: Unrecognized Invocation" },
  1137. { FacReject_Err_ErrorResponseUnexpected, "Error: Error Response Unexpected" },
  1138. { FacReject_Err_UnrecognizedError, "Error: Unrecognized Error" },
  1139. { FacReject_Err_UnexpectedError, "Error: Unexpected Error" },
  1140. { FacReject_Err_MistypedParameter, "Error: Mistyped Parameter" },
  1141. /* *INDENT-ON* */
  1142. };
  1143. unsigned index;
  1144. for (index = 0; index < ARRAY_LEN(arr); ++index) {
  1145. if (arr[index].code == code) {
  1146. return arr[index].name;
  1147. }
  1148. }
  1149. return "unknown";
  1150. }
  1151. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  1152. #if defined(AST_MISDN_ENHANCEMENTS)
  1153. /*!
  1154. * \internal
  1155. * \brief Convert the mISDN error code to a string
  1156. *
  1157. * \param code mISDN error code.
  1158. *
  1159. * \return The mISDN error code as a string
  1160. */
  1161. static const char *misdn_to_str_error_code(enum FacErrorCode code)
  1162. {
  1163. static const struct {
  1164. enum FacErrorCode code;
  1165. char *name;
  1166. } arr[] = {
  1167. /* *INDENT-OFF* */
  1168. { FacError_None, "No error occurred" },
  1169. { FacError_Unknown, "Unknown OID error code" },
  1170. { FacError_Gen_NotSubscribed, "General: Not Subscribed" },
  1171. { FacError_Gen_NotAvailable, "General: Not Available" },
  1172. { FacError_Gen_NotImplemented, "General: Not Implemented" },
  1173. { FacError_Gen_InvalidServedUserNr, "General: Invalid Served User Number" },
  1174. { FacError_Gen_InvalidCallState, "General: Invalid Call State" },
  1175. { FacError_Gen_BasicServiceNotProvided, "General: Basic Service Not Provided" },
  1176. { FacError_Gen_NotIncomingCall, "General: Not Incoming Call" },
  1177. { FacError_Gen_SupplementaryServiceInteractionNotAllowed,"General: Supplementary Service Interaction Not Allowed" },
  1178. { FacError_Gen_ResourceUnavailable, "General: Resource Unavailable" },
  1179. { FacError_Div_InvalidDivertedToNr, "Diversion: Invalid Diverted To Number" },
  1180. { FacError_Div_SpecialServiceNr, "Diversion: Special Service Number" },
  1181. { FacError_Div_DiversionToServedUserNr, "Diversion: Diversion To Served User Number" },
  1182. { FacError_Div_IncomingCallAccepted, "Diversion: Incoming Call Accepted" },
  1183. { FacError_Div_NumberOfDiversionsExceeded, "Diversion: Number Of Diversions Exceeded" },
  1184. { FacError_Div_NotActivated, "Diversion: Not Activated" },
  1185. { FacError_Div_RequestAlreadyAccepted, "Diversion: Request Already Accepted" },
  1186. { FacError_AOC_NoChargingInfoAvailable, "AOC: No Charging Info Available" },
  1187. { FacError_CCBS_InvalidCallLinkageID, "CCBS: Invalid Call Linkage ID" },
  1188. { FacError_CCBS_InvalidCCBSReference, "CCBS: Invalid CCBS Reference" },
  1189. { FacError_CCBS_LongTermDenial, "CCBS: Long Term Denial" },
  1190. { FacError_CCBS_ShortTermDenial, "CCBS: Short Term Denial" },
  1191. { FacError_CCBS_IsAlreadyActivated, "CCBS: Is Already Activated" },
  1192. { FacError_CCBS_AlreadyAccepted, "CCBS: Already Accepted" },
  1193. { FacError_CCBS_OutgoingCCBSQueueFull, "CCBS: Outgoing CCBS Queue Full" },
  1194. { FacError_CCBS_CallFailureReasonNotBusy, "CCBS: Call Failure Reason Not Busy" },
  1195. { FacError_CCBS_NotReadyForCall, "CCBS: Not Ready For Call" },
  1196. { FacError_CCBS_T_LongTermDenial, "CCBS-T: Long Term Denial" },
  1197. { FacError_CCBS_T_ShortTermDenial, "CCBS-T: Short Term Denial" },
  1198. { FacError_ECT_LinkIdNotAssignedByNetwork, "ECT: Link ID Not Assigned By Network" },
  1199. /* *INDENT-ON* */
  1200. };
  1201. unsigned index;
  1202. for (index = 0; index < ARRAY_LEN(arr); ++index) {
  1203. if (arr[index].code == code) {
  1204. return arr[index].name;
  1205. }
  1206. }
  1207. return "unknown";
  1208. }
  1209. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  1210. #if defined(AST_MISDN_ENHANCEMENTS)
  1211. /*!
  1212. * \internal
  1213. * \brief Convert mISDN redirecting reason to diversion reason.
  1214. *
  1215. * \param reason mISDN redirecting reason code.
  1216. *
  1217. * \return Supported diversion reason code.
  1218. */
  1219. static unsigned misdn_to_diversion_reason(enum mISDN_REDIRECTING_REASON reason)
  1220. {
  1221. unsigned diversion_reason;
  1222. switch (reason) {
  1223. case mISDN_REDIRECTING_REASON_CALL_FWD:
  1224. diversion_reason = 1;/* cfu */
  1225. break;
  1226. case mISDN_REDIRECTING_REASON_CALL_FWD_BUSY:
  1227. diversion_reason = 2;/* cfb */
  1228. break;
  1229. case mISDN_REDIRECTING_REASON_NO_REPLY:
  1230. diversion_reason = 3;/* cfnr */
  1231. break;
  1232. default:
  1233. diversion_reason = 0;/* unknown */
  1234. break;
  1235. }
  1236. return diversion_reason;
  1237. }
  1238. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  1239. #if defined(AST_MISDN_ENHANCEMENTS)
  1240. /*!
  1241. * \internal
  1242. * \brief Convert diversion reason to mISDN redirecting reason
  1243. *
  1244. * \param diversion_reason Diversion reason to convert
  1245. *
  1246. * \return Supported redirecting reason code.
  1247. */
  1248. static enum mISDN_REDIRECTING_REASON diversion_reason_to_misdn(unsigned diversion_reason)
  1249. {
  1250. enum mISDN_REDIRECTING_REASON reason;
  1251. switch (diversion_reason) {
  1252. case 1:/* cfu */
  1253. reason = mISDN_REDIRECTING_REASON_CALL_FWD;
  1254. break;
  1255. case 2:/* cfb */
  1256. reason = mISDN_REDIRECTING_REASON_CALL_FWD_BUSY;
  1257. break;
  1258. case 3:/* cfnr */
  1259. reason = mISDN_REDIRECTING_REASON_NO_REPLY;
  1260. break;
  1261. default:
  1262. reason = mISDN_REDIRECTING_REASON_UNKNOWN;
  1263. break;
  1264. }
  1265. return reason;
  1266. }
  1267. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  1268. #if defined(AST_MISDN_ENHANCEMENTS)
  1269. /*!
  1270. * \internal
  1271. * \brief Convert the mISDN presentation to PresentedNumberUnscreened type
  1272. *
  1273. * \param presentation mISDN presentation to convert
  1274. * \param number_present TRUE if the number is present
  1275. *
  1276. * \return PresentedNumberUnscreened type
  1277. */
  1278. static unsigned misdn_to_PresentedNumberUnscreened_type(int presentation, int number_present)
  1279. {
  1280. unsigned type;
  1281. switch (presentation) {
  1282. case 0:/* allowed */
  1283. if (number_present) {
  1284. type = 0;/* presentationAllowedNumber */
  1285. } else {
  1286. type = 2;/* numberNotAvailableDueToInterworking */
  1287. }
  1288. break;
  1289. case 1:/* restricted */
  1290. if (number_present) {
  1291. type = 3;/* presentationRestrictedNumber */
  1292. } else {
  1293. type = 1;/* presentationRestricted */
  1294. }
  1295. break;
  1296. default:
  1297. type = 2;/* numberNotAvailableDueToInterworking */
  1298. break;
  1299. }
  1300. return type;
  1301. }
  1302. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  1303. #if defined(AST_MISDN_ENHANCEMENTS)
  1304. /*!
  1305. * \internal
  1306. * \brief Convert the PresentedNumberUnscreened type to mISDN presentation
  1307. *
  1308. * \param type PresentedNumberUnscreened type
  1309. *
  1310. * \return mISDN presentation
  1311. */
  1312. static int PresentedNumberUnscreened_to_misdn_pres(unsigned type)
  1313. {
  1314. int presentation;
  1315. switch (type) {
  1316. default:
  1317. case 0:/* presentationAllowedNumber */
  1318. presentation = 0;/* allowed */
  1319. break;
  1320. case 1:/* presentationRestricted */
  1321. case 3:/* presentationRestrictedNumber */
  1322. presentation = 1;/* restricted */
  1323. break;
  1324. case 2:/* numberNotAvailableDueToInterworking */
  1325. presentation = 2;/* unavailable */
  1326. break;
  1327. }
  1328. return presentation;
  1329. }
  1330. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  1331. #if defined(AST_MISDN_ENHANCEMENTS)
  1332. /*!
  1333. * \internal
  1334. * \brief Convert the mISDN numbering plan to PartyNumber numbering plan
  1335. *
  1336. * \param number_plan mISDN numbering plan
  1337. *
  1338. * \return PartyNumber numbering plan
  1339. */
  1340. static unsigned misdn_to_PartyNumber_plan(enum mISDN_NUMBER_PLAN number_plan)
  1341. {
  1342. unsigned party_plan;
  1343. switch (number_plan) {
  1344. default:
  1345. case NUMPLAN_UNKNOWN:
  1346. party_plan = 0;/* unknown */
  1347. break;
  1348. case NUMPLAN_ISDN:
  1349. party_plan = 1;/* public */
  1350. break;
  1351. case NUMPLAN_DATA:
  1352. party_plan = 3;/* data */
  1353. break;
  1354. case NUMPLAN_TELEX:
  1355. party_plan = 4;/* telex */
  1356. break;
  1357. case NUMPLAN_NATIONAL:
  1358. party_plan = 8;/* nationalStandard */
  1359. break;
  1360. case NUMPLAN_PRIVATE:
  1361. party_plan = 5;/* private */
  1362. break;
  1363. }
  1364. return party_plan;
  1365. }
  1366. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  1367. #if defined(AST_MISDN_ENHANCEMENTS)
  1368. /*!
  1369. * \internal
  1370. * \brief Convert PartyNumber numbering plan to mISDN numbering plan
  1371. *
  1372. * \param party_plan PartyNumber numbering plan
  1373. *
  1374. * \return mISDN numbering plan
  1375. */
  1376. static enum mISDN_NUMBER_PLAN PartyNumber_to_misdn_plan(unsigned party_plan)
  1377. {
  1378. enum mISDN_NUMBER_PLAN number_plan;
  1379. switch (party_plan) {
  1380. default:
  1381. case 0:/* unknown */
  1382. number_plan = NUMPLAN_UNKNOWN;
  1383. break;
  1384. case 1:/* public */
  1385. number_plan = NUMPLAN_ISDN;
  1386. break;
  1387. case 3:/* data */
  1388. number_plan = NUMPLAN_DATA;
  1389. break;
  1390. case 4:/* telex */
  1391. number_plan = NUMPLAN_TELEX;
  1392. break;
  1393. case 8:/* nationalStandard */
  1394. number_plan = NUMPLAN_NATIONAL;
  1395. break;
  1396. case 5:/* private */
  1397. number_plan = NUMPLAN_PRIVATE;
  1398. break;
  1399. }
  1400. return number_plan;
  1401. }
  1402. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  1403. #if defined(AST_MISDN_ENHANCEMENTS)
  1404. /*!
  1405. * \internal
  1406. * \brief Convert mISDN type-of-number to PartyNumber public type-of-number
  1407. *
  1408. * \param ton mISDN type-of-number
  1409. *
  1410. * \return PartyNumber public type-of-number
  1411. */
  1412. static unsigned misdn_to_PartyNumber_ton_public(enum mISDN_NUMBER_TYPE ton)
  1413. {
  1414. unsigned party_ton;
  1415. switch (ton) {
  1416. default:
  1417. case NUMTYPE_UNKNOWN:
  1418. party_ton = 0;/* unknown */
  1419. break;
  1420. case NUMTYPE_INTERNATIONAL:
  1421. party_ton = 1;/* internationalNumber */
  1422. break;
  1423. case NUMTYPE_NATIONAL:
  1424. party_ton = 2;/* nationalNumber */
  1425. break;
  1426. case NUMTYPE_NETWORK_SPECIFIC:
  1427. party_ton = 3;/* networkSpecificNumber */
  1428. break;
  1429. case NUMTYPE_SUBSCRIBER:
  1430. party_ton = 4;/* subscriberNumber */
  1431. break;
  1432. case NUMTYPE_ABBREVIATED:
  1433. party_ton = 6;/* abbreviatedNumber */
  1434. break;
  1435. }
  1436. return party_ton;
  1437. }
  1438. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  1439. #if defined(AST_MISDN_ENHANCEMENTS)
  1440. /*!
  1441. * \internal
  1442. * \brief Convert the PartyNumber public type-of-number to mISDN type-of-number
  1443. *
  1444. * \param party_ton PartyNumber public type-of-number
  1445. *
  1446. * \return mISDN type-of-number
  1447. */
  1448. static enum mISDN_NUMBER_TYPE PartyNumber_to_misdn_ton_public(unsigned party_ton)
  1449. {
  1450. enum mISDN_NUMBER_TYPE ton;
  1451. switch (party_ton) {
  1452. default:
  1453. case 0:/* unknown */
  1454. ton = NUMTYPE_UNKNOWN;
  1455. break;
  1456. case 1:/* internationalNumber */
  1457. ton = NUMTYPE_INTERNATIONAL;
  1458. break;
  1459. case 2:/* nationalNumber */
  1460. ton = NUMTYPE_NATIONAL;
  1461. break;
  1462. case 3:/* networkSpecificNumber */
  1463. ton = NUMTYPE_NETWORK_SPECIFIC;
  1464. break;
  1465. case 4:/* subscriberNumber */
  1466. ton = NUMTYPE_SUBSCRIBER;
  1467. break;
  1468. case 6:/* abbreviatedNumber */
  1469. ton = NUMTYPE_ABBREVIATED;
  1470. break;
  1471. }
  1472. return ton;
  1473. }
  1474. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  1475. #if defined(AST_MISDN_ENHANCEMENTS)
  1476. /*!
  1477. * \internal
  1478. * \brief Convert mISDN type-of-number to PartyNumber private type-of-number
  1479. *
  1480. * \param ton mISDN type-of-number
  1481. *
  1482. * \return PartyNumber private type-of-number
  1483. */
  1484. static unsigned misdn_to_PartyNumber_ton_private(enum mISDN_NUMBER_TYPE ton)
  1485. {
  1486. unsigned party_ton;
  1487. switch (ton) {
  1488. default:
  1489. case NUMTYPE_UNKNOWN:
  1490. party_ton = 0;/* unknown */
  1491. break;
  1492. case NUMTYPE_INTERNATIONAL:
  1493. party_ton = 1;/* level2RegionalNumber */
  1494. break;
  1495. case NUMTYPE_NATIONAL:
  1496. party_ton = 2;/* level1RegionalNumber */
  1497. break;
  1498. case NUMTYPE_NETWORK_SPECIFIC:
  1499. party_ton = 3;/* pTNSpecificNumber */
  1500. break;
  1501. case NUMTYPE_SUBSCRIBER:
  1502. party_ton = 4;/* localNumber */
  1503. break;
  1504. case NUMTYPE_ABBREVIATED:
  1505. party_ton = 6;/* abbreviatedNumber */
  1506. break;
  1507. }
  1508. return party_ton;
  1509. }
  1510. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  1511. #if defined(AST_MISDN_ENHANCEMENTS)
  1512. /*!
  1513. * \internal
  1514. * \brief Convert the PartyNumber private type-of-number to mISDN type-of-number
  1515. *
  1516. * \param party_ton PartyNumber private type-of-number
  1517. *
  1518. * \return mISDN type-of-number
  1519. */
  1520. static enum mISDN_NUMBER_TYPE PartyNumber_to_misdn_ton_private(unsigned party_ton)
  1521. {
  1522. enum mISDN_NUMBER_TYPE ton;
  1523. switch (party_ton) {
  1524. default:
  1525. case 0:/* unknown */
  1526. ton = NUMTYPE_UNKNOWN;
  1527. break;
  1528. case 1:/* level2RegionalNumber */
  1529. ton = NUMTYPE_INTERNATIONAL;
  1530. break;
  1531. case 2:/* level1RegionalNumber */
  1532. ton = NUMTYPE_NATIONAL;
  1533. break;
  1534. case 3:/* pTNSpecificNumber */
  1535. ton = NUMTYPE_NETWORK_SPECIFIC;
  1536. break;
  1537. case 4:/* localNumber */
  1538. ton = NUMTYPE_SUBSCRIBER;
  1539. break;
  1540. case 6:/* abbreviatedNumber */
  1541. ton = NUMTYPE_ABBREVIATED;
  1542. break;
  1543. }
  1544. return ton;
  1545. }
  1546. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  1547. /*!
  1548. * \internal
  1549. * \brief Convert the mISDN type of number code to a string
  1550. *
  1551. * \param number_type mISDN type of number code.
  1552. *
  1553. * \return The mISDN type of number code as a string
  1554. */
  1555. static const char *misdn_to_str_ton(enum mISDN_NUMBER_TYPE number_type)
  1556. {
  1557. const char *str;
  1558. switch (number_type) {
  1559. default:
  1560. case NUMTYPE_UNKNOWN:
  1561. str = "Unknown";
  1562. break;
  1563. case NUMTYPE_INTERNATIONAL:
  1564. str = "International";
  1565. break;
  1566. case NUMTYPE_NATIONAL:
  1567. str = "National";
  1568. break;
  1569. case NUMTYPE_NETWORK_SPECIFIC:
  1570. str = "Network Specific";
  1571. break;
  1572. case NUMTYPE_SUBSCRIBER:
  1573. str = "Subscriber";
  1574. break;
  1575. case NUMTYPE_ABBREVIATED:
  1576. str = "Abbreviated";
  1577. break;
  1578. }
  1579. return str;
  1580. }
  1581. /*!
  1582. * \internal
  1583. * \brief Convert the mISDN type of number code to Asterisk type of number code
  1584. *
  1585. * \param number_type mISDN type of number code.
  1586. *
  1587. * \return Asterisk type of number code
  1588. */
  1589. static int misdn_to_ast_ton(enum mISDN_NUMBER_TYPE number_type)
  1590. {
  1591. int ast_number_type;
  1592. switch (number_type) {
  1593. default:
  1594. case NUMTYPE_UNKNOWN:
  1595. ast_number_type = NUMTYPE_UNKNOWN << 4;
  1596. break;
  1597. case NUMTYPE_INTERNATIONAL:
  1598. ast_number_type = NUMTYPE_INTERNATIONAL << 4;
  1599. break;
  1600. case NUMTYPE_NATIONAL:
  1601. ast_number_type = NUMTYPE_NATIONAL << 4;
  1602. break;
  1603. case NUMTYPE_NETWORK_SPECIFIC:
  1604. ast_number_type = NUMTYPE_NETWORK_SPECIFIC << 4;
  1605. break;
  1606. case NUMTYPE_SUBSCRIBER:
  1607. ast_number_type = NUMTYPE_SUBSCRIBER << 4;
  1608. break;
  1609. case NUMTYPE_ABBREVIATED:
  1610. ast_number_type = NUMTYPE_ABBREVIATED << 4;
  1611. break;
  1612. }
  1613. return ast_number_type;
  1614. }
  1615. /*!
  1616. * \internal
  1617. * \brief Convert the Asterisk type of number code to mISDN type of number code
  1618. *
  1619. * \param ast_number_type Asterisk type of number code.
  1620. *
  1621. * \return mISDN type of number code
  1622. */
  1623. static enum mISDN_NUMBER_TYPE ast_to_misdn_ton(unsigned ast_number_type)
  1624. {
  1625. enum mISDN_NUMBER_TYPE number_type;
  1626. switch ((ast_number_type >> 4) & 0x07) {
  1627. default:
  1628. case NUMTYPE_UNKNOWN:
  1629. number_type = NUMTYPE_UNKNOWN;
  1630. break;
  1631. case NUMTYPE_INTERNATIONAL:
  1632. number_type = NUMTYPE_INTERNATIONAL;
  1633. break;
  1634. case NUMTYPE_NATIONAL:
  1635. number_type = NUMTYPE_NATIONAL;
  1636. break;
  1637. case NUMTYPE_NETWORK_SPECIFIC:
  1638. number_type = NUMTYPE_NETWORK_SPECIFIC;
  1639. break;
  1640. case NUMTYPE_SUBSCRIBER:
  1641. number_type = NUMTYPE_SUBSCRIBER;
  1642. break;
  1643. case NUMTYPE_ABBREVIATED:
  1644. number_type = NUMTYPE_ABBREVIATED;
  1645. break;
  1646. }
  1647. return number_type;
  1648. }
  1649. /*!
  1650. * \internal
  1651. * \brief Convert the mISDN numbering plan code to a string
  1652. *
  1653. * \param number_plan mISDN numbering plan code.
  1654. *
  1655. * \return The mISDN numbering plan code as a string
  1656. */
  1657. static const char *misdn_to_str_plan(enum mISDN_NUMBER_PLAN number_plan)
  1658. {
  1659. const char *str;
  1660. switch (number_plan) {
  1661. default:
  1662. case NUMPLAN_UNKNOWN:
  1663. str = "Unknown";
  1664. break;
  1665. case NUMPLAN_ISDN:
  1666. str = "ISDN";
  1667. break;
  1668. case NUMPLAN_DATA:
  1669. str = "Data";
  1670. break;
  1671. case NUMPLAN_TELEX:
  1672. str = "Telex";
  1673. break;
  1674. case NUMPLAN_NATIONAL:
  1675. str = "National";
  1676. break;
  1677. case NUMPLAN_PRIVATE:
  1678. str = "Private";
  1679. break;
  1680. }
  1681. return str;
  1682. }
  1683. /*!
  1684. * \internal
  1685. * \brief Convert the mISDN numbering plan code to Asterisk numbering plan code
  1686. *
  1687. * \param number_plan mISDN numbering plan code.
  1688. *
  1689. * \return Asterisk numbering plan code
  1690. */
  1691. static int misdn_to_ast_plan(enum mISDN_NUMBER_PLAN number_plan)
  1692. {
  1693. int ast_number_plan;
  1694. switch (number_plan) {
  1695. default:
  1696. case NUMPLAN_UNKNOWN:
  1697. ast_number_plan = NUMPLAN_UNKNOWN;
  1698. break;
  1699. case NUMPLAN_ISDN:
  1700. ast_number_plan = NUMPLAN_ISDN;
  1701. break;
  1702. case NUMPLAN_DATA:
  1703. ast_number_plan = NUMPLAN_DATA;
  1704. break;
  1705. case NUMPLAN_TELEX:
  1706. ast_number_plan = NUMPLAN_TELEX;
  1707. break;
  1708. case NUMPLAN_NATIONAL:
  1709. ast_number_plan = NUMPLAN_NATIONAL;
  1710. break;
  1711. case NUMPLAN_PRIVATE:
  1712. ast_number_plan = NUMPLAN_PRIVATE;
  1713. break;
  1714. }
  1715. return ast_number_plan;
  1716. }
  1717. /*!
  1718. * \internal
  1719. * \brief Convert the Asterisk numbering plan code to mISDN numbering plan code
  1720. *
  1721. * \param ast_number_plan Asterisk numbering plan code.
  1722. *
  1723. * \return mISDN numbering plan code
  1724. */
  1725. static enum mISDN_NUMBER_PLAN ast_to_misdn_plan(unsigned ast_number_plan)
  1726. {
  1727. enum mISDN_NUMBER_PLAN number_plan;
  1728. switch (ast_number_plan & 0x0F) {
  1729. default:
  1730. case NUMPLAN_UNKNOWN:
  1731. number_plan = NUMPLAN_UNKNOWN;
  1732. break;
  1733. case NUMPLAN_ISDN:
  1734. number_plan = NUMPLAN_ISDN;
  1735. break;
  1736. case NUMPLAN_DATA:
  1737. number_plan = NUMPLAN_DATA;
  1738. break;
  1739. case NUMPLAN_TELEX:
  1740. number_plan = NUMPLAN_TELEX;
  1741. break;
  1742. case NUMPLAN_NATIONAL:
  1743. number_plan = NUMPLAN_NATIONAL;
  1744. break;
  1745. case NUMPLAN_PRIVATE:
  1746. number_plan = NUMPLAN_PRIVATE;
  1747. break;
  1748. }
  1749. return number_plan;
  1750. }
  1751. /*!
  1752. * \internal
  1753. * \brief Convert the mISDN presentation code to a string
  1754. *
  1755. * \param presentation mISDN number presentation restriction code.
  1756. *
  1757. * \return The mISDN presentation code as a string
  1758. */
  1759. static const char *misdn_to_str_pres(int presentation)
  1760. {
  1761. const char *str;
  1762. switch (presentation) {
  1763. case 0:
  1764. str = "Allowed";
  1765. break;
  1766. case 1:
  1767. str = "Restricted";
  1768. break;
  1769. case 2:
  1770. str = "Unavailable";
  1771. break;
  1772. default:
  1773. str = "Unknown";
  1774. break;
  1775. }
  1776. return str;
  1777. }
  1778. /*!
  1779. * \internal
  1780. * \brief Convert the mISDN presentation code to Asterisk presentation code
  1781. *
  1782. * \param presentation mISDN number presentation restriction code.
  1783. *
  1784. * \return Asterisk presentation code
  1785. */
  1786. static int misdn_to_ast_pres(int presentation)
  1787. {
  1788. switch (presentation) {
  1789. default:
  1790. case 0:
  1791. presentation = AST_PRES_ALLOWED;
  1792. break;
  1793. case 1:
  1794. presentation = AST_PRES_RESTRICTED;
  1795. break;
  1796. case 2:
  1797. presentation = AST_PRES_UNAVAILABLE;
  1798. break;
  1799. }
  1800. return presentation;
  1801. }
  1802. /*!
  1803. * \internal
  1804. * \brief Convert the Asterisk presentation code to mISDN presentation code
  1805. *
  1806. * \param presentation Asterisk number presentation restriction code.
  1807. *
  1808. * \return mISDN presentation code
  1809. */
  1810. static int ast_to_misdn_pres(int presentation)
  1811. {
  1812. switch (presentation & AST_PRES_RESTRICTION) {
  1813. default:
  1814. case AST_PRES_ALLOWED:
  1815. presentation = 0;
  1816. break;
  1817. case AST_PRES_RESTRICTED:
  1818. presentation = 1;
  1819. break;
  1820. case AST_PRES_UNAVAILABLE:
  1821. presentation = 2;
  1822. break;
  1823. }
  1824. return presentation;
  1825. }
  1826. /*!
  1827. * \internal
  1828. * \brief Convert the mISDN screening code to a string
  1829. *
  1830. * \param screening mISDN number screening code.
  1831. *
  1832. * \return The mISDN screening code as a string
  1833. */
  1834. static const char *misdn_to_str_screen(int screening)
  1835. {
  1836. const char *str;
  1837. switch (screening) {
  1838. case 0:
  1839. str = "Unscreened";
  1840. break;
  1841. case 1:
  1842. str = "Passed Screen";
  1843. break;
  1844. case 2:
  1845. str = "Failed Screen";
  1846. break;
  1847. case 3:
  1848. str = "Network Number";
  1849. break;
  1850. default:
  1851. str = "Unknown";
  1852. break;
  1853. }
  1854. return str;
  1855. }
  1856. /*!
  1857. * \internal
  1858. * \brief Convert the mISDN screening code to Asterisk screening code
  1859. *
  1860. * \param screening mISDN number screening code.
  1861. *
  1862. * \return Asterisk screening code
  1863. */
  1864. static int misdn_to_ast_screen(int screening)
  1865. {
  1866. switch (screening) {
  1867. default:
  1868. case 0:
  1869. screening = AST_PRES_USER_NUMBER_UNSCREENED;
  1870. break;
  1871. case 1:
  1872. screening = AST_PRES_USER_NUMBER_PASSED_SCREEN;
  1873. break;
  1874. case 2:
  1875. screening = AST_PRES_USER_NUMBER_FAILED_SCREEN;
  1876. break;
  1877. case 3:
  1878. screening = AST_PRES_NETWORK_NUMBER;
  1879. break;
  1880. }
  1881. return screening;
  1882. }
  1883. /*!
  1884. * \internal
  1885. * \brief Convert the Asterisk screening code to mISDN screening code
  1886. *
  1887. * \param screening Asterisk number screening code.
  1888. *
  1889. * \return mISDN screening code
  1890. */
  1891. static int ast_to_misdn_screen(int screening)
  1892. {
  1893. switch (screening & AST_PRES_NUMBER_TYPE) {
  1894. default:
  1895. case AST_PRES_USER_NUMBER_UNSCREENED:
  1896. screening = 0;
  1897. break;
  1898. case AST_PRES_USER_NUMBER_PASSED_SCREEN:
  1899. screening = 1;
  1900. break;
  1901. case AST_PRES_USER_NUMBER_FAILED_SCREEN:
  1902. screening = 2;
  1903. break;
  1904. case AST_PRES_NETWORK_NUMBER:
  1905. screening = 3;
  1906. break;
  1907. }
  1908. return screening;
  1909. }
  1910. /*!
  1911. * \internal
  1912. * \brief Convert Asterisk redirecting reason to mISDN redirecting reason code.
  1913. *
  1914. * \param ast Asterisk redirecting reason code.
  1915. *
  1916. * \return mISDN reason code
  1917. */
  1918. static enum mISDN_REDIRECTING_REASON ast_to_misdn_reason(const enum AST_REDIRECTING_REASON ast)
  1919. {
  1920. unsigned index;
  1921. static const struct misdn_reasons {
  1922. enum AST_REDIRECTING_REASON ast;
  1923. enum mISDN_REDIRECTING_REASON q931;
  1924. } misdn_reason_table[] = {
  1925. /* *INDENT-OFF* */
  1926. { AST_REDIRECTING_REASON_UNKNOWN, mISDN_REDIRECTING_REASON_UNKNOWN },
  1927. { AST_REDIRECTING_REASON_USER_BUSY, mISDN_REDIRECTING_REASON_CALL_FWD_BUSY },
  1928. { AST_REDIRECTING_REASON_NO_ANSWER, mISDN_REDIRECTING_REASON_NO_REPLY },
  1929. { AST_REDIRECTING_REASON_UNAVAILABLE, mISDN_REDIRECTING_REASON_NO_REPLY },
  1930. { AST_REDIRECTING_REASON_UNCONDITIONAL, mISDN_REDIRECTING_REASON_CALL_FWD },
  1931. { AST_REDIRECTING_REASON_TIME_OF_DAY, mISDN_REDIRECTING_REASON_UNKNOWN },
  1932. { AST_REDIRECTING_REASON_DO_NOT_DISTURB, mISDN_REDIRECTING_REASON_UNKNOWN },
  1933. { AST_REDIRECTING_REASON_DEFLECTION, mISDN_REDIRECTING_REASON_DEFLECTION },
  1934. { AST_REDIRECTING_REASON_FOLLOW_ME, mISDN_REDIRECTING_REASON_UNKNOWN },
  1935. { AST_REDIRECTING_REASON_OUT_OF_ORDER, mISDN_REDIRECTING_REASON_OUT_OF_ORDER },
  1936. { AST_REDIRECTING_REASON_AWAY, mISDN_REDIRECTING_REASON_UNKNOWN },
  1937. { AST_REDIRECTING_REASON_CALL_FWD_DTE, mISDN_REDIRECTING_REASON_CALL_FWD_DTE }
  1938. /* *INDENT-ON* */
  1939. };
  1940. for (index = 0; index < ARRAY_LEN(misdn_reason_table); ++index) {
  1941. if (misdn_reason_table[index].ast == ast) {
  1942. return misdn_reason_table[index].q931;
  1943. }
  1944. }
  1945. return mISDN_REDIRECTING_REASON_UNKNOWN;
  1946. }
  1947. /*!
  1948. * \internal
  1949. * \brief Convert the mISDN redirecting reason to Asterisk redirecting reason code
  1950. *
  1951. * \param q931 mISDN redirecting reason code.
  1952. *
  1953. * \return Asterisk redirecting reason code
  1954. */
  1955. static enum AST_REDIRECTING_REASON misdn_to_ast_reason(const enum mISDN_REDIRECTING_REASON q931)
  1956. {
  1957. enum AST_REDIRECTING_REASON ast;
  1958. switch (q931) {
  1959. default:
  1960. case mISDN_REDIRECTING_REASON_UNKNOWN:
  1961. ast = AST_REDIRECTING_REASON_UNKNOWN;
  1962. break;
  1963. case mISDN_REDIRECTING_REASON_CALL_FWD_BUSY:
  1964. ast = AST_REDIRECTING_REASON_USER_BUSY;
  1965. break;
  1966. case mISDN_REDIRECTING_REASON_NO_REPLY:
  1967. ast = AST_REDIRECTING_REASON_NO_ANSWER;
  1968. break;
  1969. case mISDN_REDIRECTING_REASON_DEFLECTION:
  1970. ast = AST_REDIRECTING_REASON_DEFLECTION;
  1971. break;
  1972. case mISDN_REDIRECTING_REASON_OUT_OF_ORDER:
  1973. ast = AST_REDIRECTING_REASON_OUT_OF_ORDER;
  1974. break;
  1975. case mISDN_REDIRECTING_REASON_CALL_FWD_DTE:
  1976. ast = AST_REDIRECTING_REASON_CALL_FWD_DTE;
  1977. break;
  1978. case mISDN_REDIRECTING_REASON_CALL_FWD:
  1979. ast = AST_REDIRECTING_REASON_UNCONDITIONAL;
  1980. break;
  1981. }
  1982. return ast;
  1983. }
  1984. struct allowed_bearers {
  1985. char *name; /*!< Bearer capability name string used in /etc/misdn.conf allowed_bearers */
  1986. char *display; /*!< Bearer capability displayable name */
  1987. int cap; /*!< SETUP message bearer capability field code value */
  1988. int deprecated; /*!< TRUE if this entry is deprecated. (Misspelled or bad name to use) */
  1989. };
  1990. /* *INDENT-OFF* */
  1991. static const struct allowed_bearers allowed_bearers_array[] = {
  1992. /* Name, Displayable Name Bearer Capability, Deprecated */
  1993. { "speech", "Speech", INFO_CAPABILITY_SPEECH, 0 },
  1994. { "3_1khz", "3.1KHz Audio", INFO_CAPABILITY_AUDIO_3_1K, 0 },
  1995. { "digital_unrestricted", "Unrestricted Digital", INFO_CAPABILITY_DIGITAL_UNRESTRICTED, 0 },
  1996. { "digital_restricted", "Restricted Digital", INFO_CAPABILITY_DIGITAL_RESTRICTED, 0 },
  1997. { "digital_restriced", "Restricted Digital", INFO_CAPABILITY_DIGITAL_RESTRICTED, 1 }, /* Allow misspelling for backwards compatibility */
  1998. { "video", "Video", INFO_CAPABILITY_VIDEO, 0 }
  1999. };
  2000. /* *INDENT-ON* */
  2001. static const char *bearer2str(int cap)
  2002. {
  2003. unsigned index;
  2004. for (index = 0; index < ARRAY_LEN(allowed_bearers_array); ++index) {
  2005. if (allowed_bearers_array[index].cap == cap) {
  2006. return allowed_bearers_array[index].display;
  2007. }
  2008. }
  2009. return "Unknown Bearer";
  2010. }
  2011. #if defined(AST_MISDN_ENHANCEMENTS)
  2012. /*!
  2013. * \internal
  2014. * \brief Fill in facility PartyNumber information
  2015. *
  2016. * \param party PartyNumber structure to fill in.
  2017. * \param id Information to put in PartyNumber structure.
  2018. *
  2019. * \return Nothing
  2020. */
  2021. static void misdn_PartyNumber_fill(struct FacPartyNumber *party, const struct misdn_party_id *id)
  2022. {
  2023. ast_copy_string((char *) party->Number, id->number, sizeof(party->Number));
  2024. party->LengthOfNumber = strlen((char *) party->Number);
  2025. party->Type = misdn_to_PartyNumber_plan(id->number_plan);
  2026. switch (party->Type) {
  2027. case 1:/* public */
  2028. party->TypeOfNumber = misdn_to_PartyNumber_ton_public(id->number_type);
  2029. break;
  2030. case 5:/* private */
  2031. party->TypeOfNumber = misdn_to_PartyNumber_ton_private(id->number_type);
  2032. break;
  2033. default:
  2034. party->TypeOfNumber = 0;/* Don't care */
  2035. break;
  2036. }
  2037. }
  2038. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2039. #if defined(AST_MISDN_ENHANCEMENTS)
  2040. /*!
  2041. * \internal
  2042. * \brief Extract the information from PartyNumber
  2043. *
  2044. * \param id Where to put extracted PartyNumber information
  2045. * \param party PartyNumber information to extract
  2046. *
  2047. * \return Nothing
  2048. */
  2049. static void misdn_PartyNumber_extract(struct misdn_party_id *id, const struct FacPartyNumber *party)
  2050. {
  2051. if (party->LengthOfNumber) {
  2052. ast_copy_string(id->number, (char *) party->Number, sizeof(id->number));
  2053. id->number_plan = PartyNumber_to_misdn_plan(party->Type);
  2054. switch (party->Type) {
  2055. case 1:/* public */
  2056. id->number_type = PartyNumber_to_misdn_ton_public(party->TypeOfNumber);
  2057. break;
  2058. case 5:/* private */
  2059. id->number_type = PartyNumber_to_misdn_ton_private(party->TypeOfNumber);
  2060. break;
  2061. default:
  2062. id->number_type = NUMTYPE_UNKNOWN;
  2063. break;
  2064. }
  2065. } else {
  2066. /* Number not present */
  2067. id->number_type = NUMTYPE_UNKNOWN;
  2068. id->number_plan = NUMPLAN_ISDN;
  2069. id->number[0] = 0;
  2070. }
  2071. }
  2072. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2073. #if defined(AST_MISDN_ENHANCEMENTS)
  2074. /*!
  2075. * \internal
  2076. * \brief Fill in facility Address information
  2077. *
  2078. * \param Address Address structure to fill in.
  2079. * \param id Information to put in Address structure.
  2080. *
  2081. * \return Nothing
  2082. */
  2083. static void misdn_Address_fill(struct FacAddress *Address, const struct misdn_party_id *id)
  2084. {
  2085. misdn_PartyNumber_fill(&Address->Party, id);
  2086. /* Subaddresses are not supported yet */
  2087. Address->Subaddress.Length = 0;
  2088. }
  2089. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2090. #if defined(AST_MISDN_ENHANCEMENTS)
  2091. /*!
  2092. * \internal
  2093. * \brief Fill in facility PresentedNumberUnscreened information
  2094. *
  2095. * \param presented PresentedNumberUnscreened structure to fill in.
  2096. * \param id Information to put in PresentedNumberUnscreened structure.
  2097. *
  2098. * \return Nothing
  2099. */
  2100. static void misdn_PresentedNumberUnscreened_fill(struct FacPresentedNumberUnscreened *presented, const struct misdn_party_id *id)
  2101. {
  2102. presented->Type = misdn_to_PresentedNumberUnscreened_type(id->presentation, id->number[0] ? 1 : 0);
  2103. misdn_PartyNumber_fill(&presented->Unscreened, id);
  2104. }
  2105. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2106. #if defined(AST_MISDN_ENHANCEMENTS)
  2107. /*!
  2108. * \internal
  2109. * \brief Extract the information from PartyNumber
  2110. *
  2111. * \param id Where to put extracted PresentedNumberUnscreened information
  2112. * \param presented PresentedNumberUnscreened information to extract
  2113. *
  2114. * \return Nothing
  2115. */
  2116. static void misdn_PresentedNumberUnscreened_extract(struct misdn_party_id *id, const struct FacPresentedNumberUnscreened *presented)
  2117. {
  2118. id->presentation = PresentedNumberUnscreened_to_misdn_pres(presented->Type);
  2119. id->screening = 0;/* unscreened */
  2120. switch (presented->Type) {
  2121. case 0:/* presentationAllowedNumber */
  2122. case 3:/* presentationRestrictedNumber */
  2123. misdn_PartyNumber_extract(id, &presented->Unscreened);
  2124. break;
  2125. case 1:/* presentationRestricted */
  2126. case 2:/* numberNotAvailableDueToInterworking */
  2127. default:
  2128. /* Number not present (And uninitialized so do not even look at it!) */
  2129. id->number_type = NUMTYPE_UNKNOWN;
  2130. id->number_plan = NUMPLAN_ISDN;
  2131. id->number[0] = 0;
  2132. break;
  2133. }
  2134. }
  2135. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2136. #if defined(AST_MISDN_ENHANCEMENTS)
  2137. static const char Level_Spacing[] = " ";/* Work for up to 10 levels */
  2138. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2139. #if defined(AST_MISDN_ENHANCEMENTS)
  2140. static void print_facility_PartyNumber(unsigned Level, const struct FacPartyNumber *Party, const struct misdn_bchannel *bc)
  2141. {
  2142. if (Party->LengthOfNumber) {
  2143. const char *Spacing;
  2144. Spacing = &Level_Spacing[sizeof(Level_Spacing) - 1 - Level];
  2145. chan_misdn_log(1, bc->port, " -->%s PartyNumber: Type:%d\n",
  2146. Spacing, Party->Type);
  2147. switch (Party->Type) {
  2148. case 0: /* Unknown PartyNumber */
  2149. chan_misdn_log(1, bc->port, " -->%s Unknown: %s\n",
  2150. Spacing, Party->Number);
  2151. break;
  2152. case 1: /* Public PartyNumber */
  2153. chan_misdn_log(1, bc->port, " -->%s Public TON:%d %s\n",
  2154. Spacing, Party->TypeOfNumber, Party->Number);
  2155. break;
  2156. case 2: /* NSAP encoded PartyNumber */
  2157. chan_misdn_log(1, bc->port, " -->%s NSAP: %s\n",
  2158. Spacing, Party->Number);
  2159. break;
  2160. case 3: /* Data PartyNumber (Not used) */
  2161. chan_misdn_log(1, bc->port, " -->%s Data: %s\n",
  2162. Spacing, Party->Number);
  2163. break;
  2164. case 4: /* Telex PartyNumber (Not used) */
  2165. chan_misdn_log(1, bc->port, " -->%s Telex: %s\n",
  2166. Spacing, Party->Number);
  2167. break;
  2168. case 5: /* Private PartyNumber */
  2169. chan_misdn_log(1, bc->port, " -->%s Private TON:%d %s\n",
  2170. Spacing, Party->TypeOfNumber, Party->Number);
  2171. break;
  2172. case 8: /* National Standard PartyNumber (Not used) */
  2173. chan_misdn_log(1, bc->port, " -->%s National: %s\n",
  2174. Spacing, Party->Number);
  2175. break;
  2176. default:
  2177. break;
  2178. }
  2179. }
  2180. }
  2181. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2182. #if defined(AST_MISDN_ENHANCEMENTS)
  2183. static void print_facility_Subaddress(unsigned Level, const struct FacPartySubaddress *Subaddress, const struct misdn_bchannel *bc)
  2184. {
  2185. if (Subaddress->Length) {
  2186. const char *Spacing;
  2187. Spacing = &Level_Spacing[sizeof(Level_Spacing) - 1 - Level];
  2188. chan_misdn_log(1, bc->port, " -->%s Subaddress: Type:%d\n",
  2189. Spacing, Subaddress->Type);
  2190. switch (Subaddress->Type) {
  2191. case 0: /* UserSpecified */
  2192. if (Subaddress->u.UserSpecified.OddCountPresent) {
  2193. chan_misdn_log(1, bc->port, " -->%s User BCD OddCount:%d NumOctets:%d\n",
  2194. Spacing, Subaddress->u.UserSpecified.OddCount, Subaddress->Length);
  2195. } else {
  2196. chan_misdn_log(1, bc->port, " -->%s User: %s\n",
  2197. Spacing, Subaddress->u.UserSpecified.Information);
  2198. }
  2199. break;
  2200. case 1: /* NSAP */
  2201. chan_misdn_log(1, bc->port, " -->%s NSAP: %s\n",
  2202. Spacing, Subaddress->u.Nsap);
  2203. break;
  2204. default:
  2205. break;
  2206. }
  2207. }
  2208. }
  2209. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2210. #if defined(AST_MISDN_ENHANCEMENTS)
  2211. static void print_facility_Address(unsigned Level, const struct FacAddress *Address, const struct misdn_bchannel *bc)
  2212. {
  2213. print_facility_PartyNumber(Level, &Address->Party, bc);
  2214. print_facility_Subaddress(Level, &Address->Subaddress, bc);
  2215. }
  2216. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2217. #if defined(AST_MISDN_ENHANCEMENTS)
  2218. static void print_facility_PresentedNumberUnscreened(unsigned Level, const struct FacPresentedNumberUnscreened *Presented, 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 Unscreened Type:%d\n", Spacing, Presented->Type);
  2223. switch (Presented->Type) {
  2224. case 0: /* presentationAllowedNumber */
  2225. chan_misdn_log(1, bc->port, " -->%s Allowed:\n", Spacing);
  2226. print_facility_PartyNumber(Level + 2, &Presented->Unscreened, bc);
  2227. break;
  2228. case 1: /* presentationRestricted */
  2229. chan_misdn_log(1, bc->port, " -->%s Restricted\n", Spacing);
  2230. break;
  2231. case 2: /* numberNotAvailableDueToInterworking */
  2232. chan_misdn_log(1, bc->port, " -->%s Not Available\n", Spacing);
  2233. break;
  2234. case 3: /* presentationRestrictedNumber */
  2235. chan_misdn_log(1, bc->port, " -->%s Restricted:\n", Spacing);
  2236. print_facility_PartyNumber(Level + 2, &Presented->Unscreened, bc);
  2237. break;
  2238. default:
  2239. break;
  2240. }
  2241. }
  2242. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2243. #if defined(AST_MISDN_ENHANCEMENTS)
  2244. static void print_facility_AddressScreened(unsigned Level, const struct FacAddressScreened *Address, const struct misdn_bchannel *bc)
  2245. {
  2246. const char *Spacing;
  2247. Spacing = &Level_Spacing[sizeof(Level_Spacing) - 1 - Level];
  2248. chan_misdn_log(1, bc->port, " -->%s ScreeningIndicator:%d\n", Spacing, Address->ScreeningIndicator);
  2249. print_facility_PartyNumber(Level, &Address->Party, bc);
  2250. print_facility_Subaddress(Level, &Address->Subaddress, bc);
  2251. }
  2252. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2253. #if defined(AST_MISDN_ENHANCEMENTS)
  2254. static void print_facility_PresentedAddressScreened(unsigned Level, const struct FacPresentedAddressScreened *Presented, 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 Screened Type:%d\n", Spacing, Presented->Type);
  2259. switch (Presented->Type) {
  2260. case 0: /* presentationAllowedAddress */
  2261. chan_misdn_log(1, bc->port, " -->%s Allowed:\n", Spacing);
  2262. print_facility_AddressScreened(Level + 2, &Presented->Address, bc);
  2263. break;
  2264. case 1: /* presentationRestricted */
  2265. chan_misdn_log(1, bc->port, " -->%s Restricted\n", Spacing);
  2266. break;
  2267. case 2: /* numberNotAvailableDueToInterworking */
  2268. chan_misdn_log(1, bc->port, " -->%s Not Available\n", Spacing);
  2269. break;
  2270. case 3: /* presentationRestrictedAddress */
  2271. chan_misdn_log(1, bc->port, " -->%s Restricted:\n", Spacing);
  2272. print_facility_AddressScreened(Level + 2, &Presented->Address, bc);
  2273. break;
  2274. default:
  2275. break;
  2276. }
  2277. }
  2278. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2279. #if defined(AST_MISDN_ENHANCEMENTS)
  2280. static void print_facility_Q931_Bc_Hlc_Llc(unsigned Level, const struct Q931_Bc_Hlc_Llc *Q931ie, const struct misdn_bchannel *bc)
  2281. {
  2282. const char *Spacing;
  2283. Spacing = &Level_Spacing[sizeof(Level_Spacing) - 1 - Level];
  2284. chan_misdn_log(1, bc->port, " -->%s Q931ie:\n", Spacing);
  2285. if (Q931ie->Bc.Length) {
  2286. chan_misdn_log(1, bc->port, " -->%s Bc Len:%d\n", Spacing, Q931ie->Bc.Length);
  2287. }
  2288. if (Q931ie->Hlc.Length) {
  2289. chan_misdn_log(1, bc->port, " -->%s Hlc Len:%d\n", Spacing, Q931ie->Hlc.Length);
  2290. }
  2291. if (Q931ie->Llc.Length) {
  2292. chan_misdn_log(1, bc->port, " -->%s Llc Len:%d\n", Spacing, Q931ie->Llc.Length);
  2293. }
  2294. }
  2295. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2296. #if defined(AST_MISDN_ENHANCEMENTS)
  2297. static void print_facility_Q931_Bc_Hlc_Llc_Uu(unsigned Level, const struct Q931_Bc_Hlc_Llc_Uu *Q931ie, const struct misdn_bchannel *bc)
  2298. {
  2299. const char *Spacing;
  2300. Spacing = &Level_Spacing[sizeof(Level_Spacing) - 1 - Level];
  2301. chan_misdn_log(1, bc->port, " -->%s Q931ie:\n", Spacing);
  2302. if (Q931ie->Bc.Length) {
  2303. chan_misdn_log(1, bc->port, " -->%s Bc Len:%d\n", Spacing, Q931ie->Bc.Length);
  2304. }
  2305. if (Q931ie->Hlc.Length) {
  2306. chan_misdn_log(1, bc->port, " -->%s Hlc Len:%d\n", Spacing, Q931ie->Hlc.Length);
  2307. }
  2308. if (Q931ie->Llc.Length) {
  2309. chan_misdn_log(1, bc->port, " -->%s Llc Len:%d\n", Spacing, Q931ie->Llc.Length);
  2310. }
  2311. if (Q931ie->UserInfo.Length) {
  2312. chan_misdn_log(1, bc->port, " -->%s UserInfo Len:%d\n", Spacing, Q931ie->UserInfo.Length);
  2313. }
  2314. }
  2315. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2316. #if defined(AST_MISDN_ENHANCEMENTS)
  2317. static void print_facility_CallInformation(unsigned Level, const struct FacCallInformation *CallInfo, const struct misdn_bchannel *bc)
  2318. {
  2319. const char *Spacing;
  2320. Spacing = &Level_Spacing[sizeof(Level_Spacing) - 1 - Level];
  2321. chan_misdn_log(1, bc->port, " -->%s CCBSReference:%d\n",
  2322. Spacing, CallInfo->CCBSReference);
  2323. chan_misdn_log(1, bc->port, " -->%s AddressOfB:\n", Spacing);
  2324. print_facility_Address(Level + 1, &CallInfo->AddressOfB, bc);
  2325. print_facility_Q931_Bc_Hlc_Llc(Level, &CallInfo->Q931ie, bc);
  2326. if (CallInfo->SubaddressOfA.Length) {
  2327. chan_misdn_log(1, bc->port, " -->%s SubaddressOfA:\n", Spacing);
  2328. print_facility_Subaddress(Level + 1, &CallInfo->SubaddressOfA, bc);
  2329. }
  2330. }
  2331. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2332. #if defined(AST_MISDN_ENHANCEMENTS)
  2333. static void print_facility_ServedUserNr(unsigned Level, const struct FacPartyNumber *Party, const struct misdn_bchannel *bc)
  2334. {
  2335. const char *Spacing;
  2336. Spacing = &Level_Spacing[sizeof(Level_Spacing) - 1 - Level];
  2337. if (Party->LengthOfNumber) {
  2338. print_facility_PartyNumber(Level, Party, bc);
  2339. } else {
  2340. chan_misdn_log(1, bc->port, " -->%s All Numbers\n", Spacing);
  2341. }
  2342. }
  2343. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2344. #if defined(AST_MISDN_ENHANCEMENTS)
  2345. static void print_facility_IntResult(unsigned Level, const struct FacForwardingRecord *ForwardingRecord, const struct misdn_bchannel *bc)
  2346. {
  2347. const char *Spacing;
  2348. Spacing = &Level_Spacing[sizeof(Level_Spacing) - 1 - Level];
  2349. chan_misdn_log(1, bc->port, " -->%s Procedure:%d BasicService:%d\n",
  2350. Spacing,
  2351. ForwardingRecord->Procedure,
  2352. ForwardingRecord->BasicService);
  2353. chan_misdn_log(1, bc->port, " -->%s ForwardedTo:\n", Spacing);
  2354. print_facility_Address(Level + 1, &ForwardingRecord->ForwardedTo, bc);
  2355. chan_misdn_log(1, bc->port, " -->%s ServedUserNr:\n", Spacing);
  2356. print_facility_ServedUserNr(Level + 1, &ForwardingRecord->ServedUser, bc);
  2357. }
  2358. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2359. static void print_facility(const struct FacParm *fac, const struct misdn_bchannel *bc)
  2360. {
  2361. #if defined(AST_MISDN_ENHANCEMENTS)
  2362. unsigned Index;
  2363. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2364. switch (fac->Function) {
  2365. #if defined(AST_MISDN_ENHANCEMENTS)
  2366. case Fac_ActivationDiversion:
  2367. chan_misdn_log(1, bc->port, " --> ActivationDiversion: InvokeID:%d\n",
  2368. fac->u.ActivationDiversion.InvokeID);
  2369. switch (fac->u.ActivationDiversion.ComponentType) {
  2370. case FacComponent_Invoke:
  2371. chan_misdn_log(1, bc->port, " --> Invoke: Procedure:%d BasicService:%d\n",
  2372. fac->u.ActivationDiversion.Component.Invoke.Procedure,
  2373. fac->u.ActivationDiversion.Component.Invoke.BasicService);
  2374. chan_misdn_log(1, bc->port, " --> ForwardedTo:\n");
  2375. print_facility_Address(3, &fac->u.ActivationDiversion.Component.Invoke.ForwardedTo, bc);
  2376. chan_misdn_log(1, bc->port, " --> ServedUserNr:\n");
  2377. print_facility_ServedUserNr(3, &fac->u.ActivationDiversion.Component.Invoke.ServedUser, bc);
  2378. break;
  2379. case FacComponent_Result:
  2380. chan_misdn_log(1, bc->port, " --> Result\n");
  2381. break;
  2382. default:
  2383. break;
  2384. }
  2385. break;
  2386. case Fac_DeactivationDiversion:
  2387. chan_misdn_log(1, bc->port, " --> DeactivationDiversion: InvokeID:%d\n",
  2388. fac->u.DeactivationDiversion.InvokeID);
  2389. switch (fac->u.DeactivationDiversion.ComponentType) {
  2390. case FacComponent_Invoke:
  2391. chan_misdn_log(1, bc->port, " --> Invoke: Procedure:%d BasicService:%d\n",
  2392. fac->u.DeactivationDiversion.Component.Invoke.Procedure,
  2393. fac->u.DeactivationDiversion.Component.Invoke.BasicService);
  2394. chan_misdn_log(1, bc->port, " --> ServedUserNr:\n");
  2395. print_facility_ServedUserNr(3, &fac->u.DeactivationDiversion.Component.Invoke.ServedUser, bc);
  2396. break;
  2397. case FacComponent_Result:
  2398. chan_misdn_log(1, bc->port, " --> Result\n");
  2399. break;
  2400. default:
  2401. break;
  2402. }
  2403. break;
  2404. case Fac_ActivationStatusNotificationDiv:
  2405. chan_misdn_log(1, bc->port, " --> ActivationStatusNotificationDiv: InvokeID:%d Procedure:%d BasicService:%d\n",
  2406. fac->u.ActivationStatusNotificationDiv.InvokeID,
  2407. fac->u.ActivationStatusNotificationDiv.Procedure,
  2408. fac->u.ActivationStatusNotificationDiv.BasicService);
  2409. chan_misdn_log(1, bc->port, " --> ForwardedTo:\n");
  2410. print_facility_Address(2, &fac->u.ActivationStatusNotificationDiv.ForwardedTo, bc);
  2411. chan_misdn_log(1, bc->port, " --> ServedUserNr:\n");
  2412. print_facility_ServedUserNr(2, &fac->u.ActivationStatusNotificationDiv.ServedUser, bc);
  2413. break;
  2414. case Fac_DeactivationStatusNotificationDiv:
  2415. chan_misdn_log(1, bc->port, " --> DeactivationStatusNotificationDiv: InvokeID:%d Procedure:%d BasicService:%d\n",
  2416. fac->u.DeactivationStatusNotificationDiv.InvokeID,
  2417. fac->u.DeactivationStatusNotificationDiv.Procedure,
  2418. fac->u.DeactivationStatusNotificationDiv.BasicService);
  2419. chan_misdn_log(1, bc->port, " --> ServedUserNr:\n");
  2420. print_facility_ServedUserNr(2, &fac->u.DeactivationStatusNotificationDiv.ServedUser, bc);
  2421. break;
  2422. case Fac_InterrogationDiversion:
  2423. chan_misdn_log(1, bc->port, " --> InterrogationDiversion: InvokeID:%d\n",
  2424. fac->u.InterrogationDiversion.InvokeID);
  2425. switch (fac->u.InterrogationDiversion.ComponentType) {
  2426. case FacComponent_Invoke:
  2427. chan_misdn_log(1, bc->port, " --> Invoke: Procedure:%d BasicService:%d\n",
  2428. fac->u.InterrogationDiversion.Component.Invoke.Procedure,
  2429. fac->u.InterrogationDiversion.Component.Invoke.BasicService);
  2430. chan_misdn_log(1, bc->port, " --> ServedUserNr:\n");
  2431. print_facility_ServedUserNr(3, &fac->u.InterrogationDiversion.Component.Invoke.ServedUser, bc);
  2432. break;
  2433. case FacComponent_Result:
  2434. chan_misdn_log(1, bc->port, " --> Result:\n");
  2435. if (fac->u.InterrogationDiversion.Component.Result.NumRecords) {
  2436. for (Index = 0; Index < fac->u.InterrogationDiversion.Component.Result.NumRecords; ++Index) {
  2437. chan_misdn_log(1, bc->port, " --> IntResult[%d]:\n", Index);
  2438. print_facility_IntResult(3, &fac->u.InterrogationDiversion.Component.Result.List[Index], bc);
  2439. }
  2440. }
  2441. break;
  2442. default:
  2443. break;
  2444. }
  2445. break;
  2446. case Fac_DiversionInformation:
  2447. chan_misdn_log(1, bc->port, " --> DiversionInformation: InvokeID:%d Reason:%d BasicService:%d\n",
  2448. fac->u.DiversionInformation.InvokeID,
  2449. fac->u.DiversionInformation.DiversionReason,
  2450. fac->u.DiversionInformation.BasicService);
  2451. if (fac->u.DiversionInformation.ServedUserSubaddress.Length) {
  2452. chan_misdn_log(1, bc->port, " --> ServedUserSubaddress:\n");
  2453. print_facility_Subaddress(2, &fac->u.DiversionInformation.ServedUserSubaddress, bc);
  2454. }
  2455. if (fac->u.DiversionInformation.CallingAddressPresent) {
  2456. chan_misdn_log(1, bc->port, " --> CallingAddress:\n");
  2457. print_facility_PresentedAddressScreened(2, &fac->u.DiversionInformation.CallingAddress, bc);
  2458. }
  2459. if (fac->u.DiversionInformation.OriginalCalledPresent) {
  2460. chan_misdn_log(1, bc->port, " --> OriginalCalledNr:\n");
  2461. print_facility_PresentedNumberUnscreened(2, &fac->u.DiversionInformation.OriginalCalled, bc);
  2462. }
  2463. if (fac->u.DiversionInformation.LastDivertingPresent) {
  2464. chan_misdn_log(1, bc->port, " --> LastDivertingNr:\n");
  2465. print_facility_PresentedNumberUnscreened(2, &fac->u.DiversionInformation.LastDiverting, bc);
  2466. }
  2467. if (fac->u.DiversionInformation.LastDivertingReasonPresent) {
  2468. chan_misdn_log(1, bc->port, " --> LastDivertingReason:%d\n", fac->u.DiversionInformation.LastDivertingReason);
  2469. }
  2470. if (fac->u.DiversionInformation.UserInfo.Length) {
  2471. chan_misdn_log(1, bc->port, " --> UserInfo Length:%d\n", fac->u.DiversionInformation.UserInfo.Length);
  2472. }
  2473. break;
  2474. case Fac_CallDeflection:
  2475. chan_misdn_log(1, bc->port, " --> CallDeflection: InvokeID:%d\n",
  2476. fac->u.CallDeflection.InvokeID);
  2477. switch (fac->u.CallDeflection.ComponentType) {
  2478. case FacComponent_Invoke:
  2479. chan_misdn_log(1, bc->port, " --> Invoke:\n");
  2480. if (fac->u.CallDeflection.Component.Invoke.PresentationAllowedToDivertedToUserPresent) {
  2481. chan_misdn_log(1, bc->port, " --> PresentationAllowed:%d\n",
  2482. fac->u.CallDeflection.Component.Invoke.PresentationAllowedToDivertedToUser);
  2483. }
  2484. chan_misdn_log(1, bc->port, " --> DeflectionAddress:\n");
  2485. print_facility_Address(3, &fac->u.CallDeflection.Component.Invoke.Deflection, bc);
  2486. break;
  2487. case FacComponent_Result:
  2488. chan_misdn_log(1, bc->port, " --> Result\n");
  2489. break;
  2490. default:
  2491. break;
  2492. }
  2493. break;
  2494. case Fac_CallRerouteing:
  2495. chan_misdn_log(1, bc->port, " --> CallRerouteing: InvokeID:%d\n",
  2496. fac->u.CallRerouteing.InvokeID);
  2497. switch (fac->u.CallRerouteing.ComponentType) {
  2498. case FacComponent_Invoke:
  2499. chan_misdn_log(1, bc->port, " --> Invoke: Reason:%d Counter:%d\n",
  2500. fac->u.CallRerouteing.Component.Invoke.ReroutingReason,
  2501. fac->u.CallRerouteing.Component.Invoke.ReroutingCounter);
  2502. chan_misdn_log(1, bc->port, " --> CalledAddress:\n");
  2503. print_facility_Address(3, &fac->u.CallRerouteing.Component.Invoke.CalledAddress, bc);
  2504. print_facility_Q931_Bc_Hlc_Llc_Uu(2, &fac->u.CallRerouteing.Component.Invoke.Q931ie, bc);
  2505. chan_misdn_log(1, bc->port, " --> LastReroutingNr:\n");
  2506. print_facility_PresentedNumberUnscreened(3, &fac->u.CallRerouteing.Component.Invoke.LastRerouting, bc);
  2507. chan_misdn_log(1, bc->port, " --> SubscriptionOption:%d\n",
  2508. fac->u.CallRerouteing.Component.Invoke.SubscriptionOption);
  2509. if (fac->u.CallRerouteing.Component.Invoke.CallingPartySubaddress.Length) {
  2510. chan_misdn_log(1, bc->port, " --> CallingParty:\n");
  2511. print_facility_Subaddress(3, &fac->u.CallRerouteing.Component.Invoke.CallingPartySubaddress, bc);
  2512. }
  2513. break;
  2514. case FacComponent_Result:
  2515. chan_misdn_log(1, bc->port, " --> Result\n");
  2516. break;
  2517. default:
  2518. break;
  2519. }
  2520. break;
  2521. case Fac_InterrogateServedUserNumbers:
  2522. chan_misdn_log(1, bc->port, " --> InterrogateServedUserNumbers: InvokeID:%d\n",
  2523. fac->u.InterrogateServedUserNumbers.InvokeID);
  2524. switch (fac->u.InterrogateServedUserNumbers.ComponentType) {
  2525. case FacComponent_Invoke:
  2526. chan_misdn_log(1, bc->port, " --> Invoke\n");
  2527. break;
  2528. case FacComponent_Result:
  2529. chan_misdn_log(1, bc->port, " --> Result:\n");
  2530. if (fac->u.InterrogateServedUserNumbers.Component.Result.NumRecords) {
  2531. for (Index = 0; Index < fac->u.InterrogateServedUserNumbers.Component.Result.NumRecords; ++Index) {
  2532. chan_misdn_log(1, bc->port, " --> ServedUserNr[%d]:\n", Index);
  2533. print_facility_PartyNumber(3, &fac->u.InterrogateServedUserNumbers.Component.Result.List[Index], bc);
  2534. }
  2535. }
  2536. break;
  2537. default:
  2538. break;
  2539. }
  2540. break;
  2541. case Fac_DivertingLegInformation1:
  2542. chan_misdn_log(1, bc->port, " --> DivertingLegInformation1: InvokeID:%d Reason:%d SubscriptionOption:%d\n",
  2543. fac->u.DivertingLegInformation1.InvokeID,
  2544. fac->u.DivertingLegInformation1.DiversionReason,
  2545. fac->u.DivertingLegInformation1.SubscriptionOption);
  2546. if (fac->u.DivertingLegInformation1.DivertedToPresent) {
  2547. chan_misdn_log(1, bc->port, " --> DivertedToNr:\n");
  2548. print_facility_PresentedNumberUnscreened(2, &fac->u.DivertingLegInformation1.DivertedTo, bc);
  2549. }
  2550. break;
  2551. case Fac_DivertingLegInformation2:
  2552. chan_misdn_log(1, bc->port, " --> DivertingLegInformation2: InvokeID:%d Reason:%d Count:%d\n",
  2553. fac->u.DivertingLegInformation2.InvokeID,
  2554. fac->u.DivertingLegInformation2.DiversionReason,
  2555. fac->u.DivertingLegInformation2.DiversionCounter);
  2556. if (fac->u.DivertingLegInformation2.DivertingPresent) {
  2557. chan_misdn_log(1, bc->port, " --> DivertingNr:\n");
  2558. print_facility_PresentedNumberUnscreened(2, &fac->u.DivertingLegInformation2.Diverting, bc);
  2559. }
  2560. if (fac->u.DivertingLegInformation2.OriginalCalledPresent) {
  2561. chan_misdn_log(1, bc->port, " --> OriginalCalledNr:\n");
  2562. print_facility_PresentedNumberUnscreened(2, &fac->u.DivertingLegInformation2.OriginalCalled, bc);
  2563. }
  2564. break;
  2565. case Fac_DivertingLegInformation3:
  2566. chan_misdn_log(1, bc->port, " --> DivertingLegInformation3: InvokeID:%d PresentationAllowed:%d\n",
  2567. fac->u.DivertingLegInformation3.InvokeID,
  2568. fac->u.DivertingLegInformation3.PresentationAllowedIndicator);
  2569. break;
  2570. #else /* !defined(AST_MISDN_ENHANCEMENTS) */
  2571. case Fac_CD:
  2572. chan_misdn_log(1, bc->port, " --> calldeflect to: %s, presentable: %s\n", fac->u.CDeflection.DeflectedToNumber,
  2573. fac->u.CDeflection.PresentationAllowed ? "yes" : "no");
  2574. break;
  2575. #endif /* !defined(AST_MISDN_ENHANCEMENTS) */
  2576. case Fac_AOCDCurrency:
  2577. if (fac->u.AOCDcur.chargeNotAvailable) {
  2578. chan_misdn_log(1, bc->port, " --> AOCD currency: charge not available\n");
  2579. } else if (fac->u.AOCDcur.freeOfCharge) {
  2580. chan_misdn_log(1, bc->port, " --> AOCD currency: free of charge\n");
  2581. } else if (fac->u.AOCDchu.billingId >= 0) {
  2582. chan_misdn_log(1, bc->port, " --> AOCD currency: currency:%s amount:%d multiplier:%d typeOfChargingInfo:%s billingId:%d\n",
  2583. fac->u.AOCDcur.currency, fac->u.AOCDcur.currencyAmount, fac->u.AOCDcur.multiplier,
  2584. (fac->u.AOCDcur.typeOfChargingInfo == 0) ? "subTotal" : "total", fac->u.AOCDcur.billingId);
  2585. } else {
  2586. chan_misdn_log(1, bc->port, " --> AOCD currency: currency:%s amount:%d multiplier:%d typeOfChargingInfo:%s\n",
  2587. fac->u.AOCDcur.currency, fac->u.AOCDcur.currencyAmount, fac->u.AOCDcur.multiplier,
  2588. (fac->u.AOCDcur.typeOfChargingInfo == 0) ? "subTotal" : "total");
  2589. }
  2590. break;
  2591. case Fac_AOCDChargingUnit:
  2592. if (fac->u.AOCDchu.chargeNotAvailable) {
  2593. chan_misdn_log(1, bc->port, " --> AOCD charging unit: charge not available\n");
  2594. } else if (fac->u.AOCDchu.freeOfCharge) {
  2595. chan_misdn_log(1, bc->port, " --> AOCD charging unit: free of charge\n");
  2596. } else if (fac->u.AOCDchu.billingId >= 0) {
  2597. chan_misdn_log(1, bc->port, " --> AOCD charging unit: recordedUnits:%d typeOfChargingInfo:%s billingId:%d\n",
  2598. fac->u.AOCDchu.recordedUnits, (fac->u.AOCDchu.typeOfChargingInfo == 0) ? "subTotal" : "total", fac->u.AOCDchu.billingId);
  2599. } else {
  2600. chan_misdn_log(1, bc->port, " --> AOCD charging unit: recordedUnits:%d typeOfChargingInfo:%s\n",
  2601. fac->u.AOCDchu.recordedUnits, (fac->u.AOCDchu.typeOfChargingInfo == 0) ? "subTotal" : "total");
  2602. }
  2603. break;
  2604. #if defined(AST_MISDN_ENHANCEMENTS)
  2605. case Fac_ERROR:
  2606. chan_misdn_log(1, bc->port, " --> ERROR: InvokeID:%d, Code:0x%02x\n",
  2607. fac->u.ERROR.invokeId, fac->u.ERROR.errorValue);
  2608. break;
  2609. case Fac_RESULT:
  2610. chan_misdn_log(1, bc->port, " --> RESULT: InvokeID:%d\n",
  2611. fac->u.RESULT.InvokeID);
  2612. break;
  2613. case Fac_REJECT:
  2614. if (fac->u.REJECT.InvokeIDPresent) {
  2615. chan_misdn_log(1, bc->port, " --> REJECT: InvokeID:%d, Code:0x%02x\n",
  2616. fac->u.REJECT.InvokeID, fac->u.REJECT.Code);
  2617. } else {
  2618. chan_misdn_log(1, bc->port, " --> REJECT: Code:0x%02x\n",
  2619. fac->u.REJECT.Code);
  2620. }
  2621. break;
  2622. case Fac_EctExecute:
  2623. chan_misdn_log(1, bc->port, " --> EctExecute: InvokeID:%d\n",
  2624. fac->u.EctExecute.InvokeID);
  2625. break;
  2626. case Fac_ExplicitEctExecute:
  2627. chan_misdn_log(1, bc->port, " --> ExplicitEctExecute: InvokeID:%d LinkID:%d\n",
  2628. fac->u.ExplicitEctExecute.InvokeID,
  2629. fac->u.ExplicitEctExecute.LinkID);
  2630. break;
  2631. case Fac_RequestSubaddress:
  2632. chan_misdn_log(1, bc->port, " --> RequestSubaddress: InvokeID:%d\n",
  2633. fac->u.RequestSubaddress.InvokeID);
  2634. break;
  2635. case Fac_SubaddressTransfer:
  2636. chan_misdn_log(1, bc->port, " --> SubaddressTransfer: InvokeID:%d\n",
  2637. fac->u.SubaddressTransfer.InvokeID);
  2638. print_facility_Subaddress(1, &fac->u.SubaddressTransfer.Subaddress, bc);
  2639. break;
  2640. case Fac_EctLinkIdRequest:
  2641. chan_misdn_log(1, bc->port, " --> EctLinkIdRequest: InvokeID:%d\n",
  2642. fac->u.EctLinkIdRequest.InvokeID);
  2643. switch (fac->u.EctLinkIdRequest.ComponentType) {
  2644. case FacComponent_Invoke:
  2645. chan_misdn_log(1, bc->port, " --> Invoke\n");
  2646. break;
  2647. case FacComponent_Result:
  2648. chan_misdn_log(1, bc->port, " --> Result: LinkID:%d\n",
  2649. fac->u.EctLinkIdRequest.Component.Result.LinkID);
  2650. break;
  2651. default:
  2652. break;
  2653. }
  2654. break;
  2655. case Fac_EctInform:
  2656. chan_misdn_log(1, bc->port, " --> EctInform: InvokeID:%d Status:%d\n",
  2657. fac->u.EctInform.InvokeID,
  2658. fac->u.EctInform.Status);
  2659. if (fac->u.EctInform.RedirectionPresent) {
  2660. chan_misdn_log(1, bc->port, " --> Redirection Number\n");
  2661. print_facility_PresentedNumberUnscreened(2, &fac->u.EctInform.Redirection, bc);
  2662. }
  2663. break;
  2664. case Fac_EctLoopTest:
  2665. chan_misdn_log(1, bc->port, " --> EctLoopTest: InvokeID:%d\n",
  2666. fac->u.EctLoopTest.InvokeID);
  2667. switch (fac->u.EctLoopTest.ComponentType) {
  2668. case FacComponent_Invoke:
  2669. chan_misdn_log(1, bc->port, " --> Invoke: CallTransferID:%d\n",
  2670. fac->u.EctLoopTest.Component.Invoke.CallTransferID);
  2671. break;
  2672. case FacComponent_Result:
  2673. chan_misdn_log(1, bc->port, " --> Result: LoopResult:%d\n",
  2674. fac->u.EctLoopTest.Component.Result.LoopResult);
  2675. break;
  2676. default:
  2677. break;
  2678. }
  2679. break;
  2680. case Fac_StatusRequest:
  2681. chan_misdn_log(1, bc->port, " --> StatusRequest: InvokeID:%d\n",
  2682. fac->u.StatusRequest.InvokeID);
  2683. switch (fac->u.StatusRequest.ComponentType) {
  2684. case FacComponent_Invoke:
  2685. chan_misdn_log(1, bc->port, " --> Invoke: Compatibility:%d\n",
  2686. fac->u.StatusRequest.Component.Invoke.CompatibilityMode);
  2687. break;
  2688. case FacComponent_Result:
  2689. chan_misdn_log(1, bc->port, " --> Result: Status:%d\n",
  2690. fac->u.StatusRequest.Component.Result.Status);
  2691. break;
  2692. default:
  2693. break;
  2694. }
  2695. break;
  2696. case Fac_CallInfoRetain:
  2697. chan_misdn_log(1, bc->port, " --> CallInfoRetain: InvokeID:%d, LinkageID:%d\n",
  2698. fac->u.CallInfoRetain.InvokeID, fac->u.CallInfoRetain.CallLinkageID);
  2699. break;
  2700. case Fac_CCBSDeactivate:
  2701. chan_misdn_log(1, bc->port, " --> CCBSDeactivate: InvokeID:%d\n",
  2702. fac->u.CCBSDeactivate.InvokeID);
  2703. switch (fac->u.CCBSDeactivate.ComponentType) {
  2704. case FacComponent_Invoke:
  2705. chan_misdn_log(1, bc->port, " --> Invoke: CCBSReference:%d\n",
  2706. fac->u.CCBSDeactivate.Component.Invoke.CCBSReference);
  2707. break;
  2708. case FacComponent_Result:
  2709. chan_misdn_log(1, bc->port, " --> Result\n");
  2710. break;
  2711. default:
  2712. break;
  2713. }
  2714. break;
  2715. case Fac_CCBSErase:
  2716. chan_misdn_log(1, bc->port, " --> CCBSErase: InvokeID:%d, CCBSReference:%d RecallMode:%d, Reason:%d\n",
  2717. fac->u.CCBSErase.InvokeID, fac->u.CCBSErase.CCBSReference,
  2718. fac->u.CCBSErase.RecallMode, fac->u.CCBSErase.Reason);
  2719. chan_misdn_log(1, bc->port, " --> AddressOfB\n");
  2720. print_facility_Address(2, &fac->u.CCBSErase.AddressOfB, bc);
  2721. print_facility_Q931_Bc_Hlc_Llc(1, &fac->u.CCBSErase.Q931ie, bc);
  2722. break;
  2723. case Fac_CCBSRemoteUserFree:
  2724. chan_misdn_log(1, bc->port, " --> CCBSRemoteUserFree: InvokeID:%d, CCBSReference:%d RecallMode:%d\n",
  2725. fac->u.CCBSRemoteUserFree.InvokeID, fac->u.CCBSRemoteUserFree.CCBSReference,
  2726. fac->u.CCBSRemoteUserFree.RecallMode);
  2727. chan_misdn_log(1, bc->port, " --> AddressOfB\n");
  2728. print_facility_Address(2, &fac->u.CCBSRemoteUserFree.AddressOfB, bc);
  2729. print_facility_Q931_Bc_Hlc_Llc(1, &fac->u.CCBSRemoteUserFree.Q931ie, bc);
  2730. break;
  2731. case Fac_CCBSCall:
  2732. chan_misdn_log(1, bc->port, " --> CCBSCall: InvokeID:%d, CCBSReference:%d\n",
  2733. fac->u.CCBSCall.InvokeID, fac->u.CCBSCall.CCBSReference);
  2734. break;
  2735. case Fac_CCBSStatusRequest:
  2736. chan_misdn_log(1, bc->port, " --> CCBSStatusRequest: InvokeID:%d\n",
  2737. fac->u.CCBSStatusRequest.InvokeID);
  2738. switch (fac->u.CCBSStatusRequest.ComponentType) {
  2739. case FacComponent_Invoke:
  2740. chan_misdn_log(1, bc->port, " --> Invoke: CCBSReference:%d RecallMode:%d\n",
  2741. fac->u.CCBSStatusRequest.Component.Invoke.CCBSReference,
  2742. fac->u.CCBSStatusRequest.Component.Invoke.RecallMode);
  2743. print_facility_Q931_Bc_Hlc_Llc(2, &fac->u.CCBSStatusRequest.Component.Invoke.Q931ie, bc);
  2744. break;
  2745. case FacComponent_Result:
  2746. chan_misdn_log(1, bc->port, " --> Result: Free:%d\n",
  2747. fac->u.CCBSStatusRequest.Component.Result.Free);
  2748. break;
  2749. default:
  2750. break;
  2751. }
  2752. break;
  2753. case Fac_CCBSBFree:
  2754. chan_misdn_log(1, bc->port, " --> CCBSBFree: InvokeID:%d, CCBSReference:%d RecallMode:%d\n",
  2755. fac->u.CCBSBFree.InvokeID, fac->u.CCBSBFree.CCBSReference,
  2756. fac->u.CCBSBFree.RecallMode);
  2757. chan_misdn_log(1, bc->port, " --> AddressOfB\n");
  2758. print_facility_Address(2, &fac->u.CCBSBFree.AddressOfB, bc);
  2759. print_facility_Q931_Bc_Hlc_Llc(1, &fac->u.CCBSBFree.Q931ie, bc);
  2760. break;
  2761. case Fac_EraseCallLinkageID:
  2762. chan_misdn_log(1, bc->port, " --> EraseCallLinkageID: InvokeID:%d, LinkageID:%d\n",
  2763. fac->u.EraseCallLinkageID.InvokeID, fac->u.EraseCallLinkageID.CallLinkageID);
  2764. break;
  2765. case Fac_CCBSStopAlerting:
  2766. chan_misdn_log(1, bc->port, " --> CCBSStopAlerting: InvokeID:%d, CCBSReference:%d\n",
  2767. fac->u.CCBSStopAlerting.InvokeID, fac->u.CCBSStopAlerting.CCBSReference);
  2768. break;
  2769. case Fac_CCBSRequest:
  2770. chan_misdn_log(1, bc->port, " --> CCBSRequest: InvokeID:%d\n",
  2771. fac->u.CCBSRequest.InvokeID);
  2772. switch (fac->u.CCBSRequest.ComponentType) {
  2773. case FacComponent_Invoke:
  2774. chan_misdn_log(1, bc->port, " --> Invoke: LinkageID:%d\n",
  2775. fac->u.CCBSRequest.Component.Invoke.CallLinkageID);
  2776. break;
  2777. case FacComponent_Result:
  2778. chan_misdn_log(1, bc->port, " --> Result: CCBSReference:%d RecallMode:%d\n",
  2779. fac->u.CCBSRequest.Component.Result.CCBSReference,
  2780. fac->u.CCBSRequest.Component.Result.RecallMode);
  2781. break;
  2782. default:
  2783. break;
  2784. }
  2785. break;
  2786. case Fac_CCBSInterrogate:
  2787. chan_misdn_log(1, bc->port, " --> CCBSInterrogate: InvokeID:%d\n",
  2788. fac->u.CCBSInterrogate.InvokeID);
  2789. switch (fac->u.CCBSInterrogate.ComponentType) {
  2790. case FacComponent_Invoke:
  2791. chan_misdn_log(1, bc->port, " --> Invoke\n");
  2792. if (fac->u.CCBSInterrogate.Component.Invoke.CCBSReferencePresent) {
  2793. chan_misdn_log(1, bc->port, " --> CCBSReference:%d\n",
  2794. fac->u.CCBSInterrogate.Component.Invoke.CCBSReference);
  2795. }
  2796. if (fac->u.CCBSInterrogate.Component.Invoke.AParty.LengthOfNumber) {
  2797. chan_misdn_log(1, bc->port, " --> AParty\n");
  2798. print_facility_PartyNumber(3, &fac->u.CCBSInterrogate.Component.Invoke.AParty, bc);
  2799. }
  2800. break;
  2801. case FacComponent_Result:
  2802. chan_misdn_log(1, bc->port, " --> Result: RecallMode:%d\n",
  2803. fac->u.CCBSInterrogate.Component.Result.RecallMode);
  2804. if (fac->u.CCBSInterrogate.Component.Result.NumRecords) {
  2805. for (Index = 0; Index < fac->u.CCBSInterrogate.Component.Result.NumRecords; ++Index) {
  2806. chan_misdn_log(1, bc->port, " --> CallDetails[%d]:\n", Index);
  2807. print_facility_CallInformation(3, &fac->u.CCBSInterrogate.Component.Result.CallDetails[Index], bc);
  2808. }
  2809. }
  2810. break;
  2811. default:
  2812. break;
  2813. }
  2814. break;
  2815. case Fac_CCNRRequest:
  2816. chan_misdn_log(1, bc->port, " --> CCNRRequest: InvokeID:%d\n",
  2817. fac->u.CCNRRequest.InvokeID);
  2818. switch (fac->u.CCNRRequest.ComponentType) {
  2819. case FacComponent_Invoke:
  2820. chan_misdn_log(1, bc->port, " --> Invoke: LinkageID:%d\n",
  2821. fac->u.CCNRRequest.Component.Invoke.CallLinkageID);
  2822. break;
  2823. case FacComponent_Result:
  2824. chan_misdn_log(1, bc->port, " --> Result: CCBSReference:%d RecallMode:%d\n",
  2825. fac->u.CCNRRequest.Component.Result.CCBSReference,
  2826. fac->u.CCNRRequest.Component.Result.RecallMode);
  2827. break;
  2828. default:
  2829. break;
  2830. }
  2831. break;
  2832. case Fac_CCNRInterrogate:
  2833. chan_misdn_log(1, bc->port, " --> CCNRInterrogate: InvokeID:%d\n",
  2834. fac->u.CCNRInterrogate.InvokeID);
  2835. switch (fac->u.CCNRInterrogate.ComponentType) {
  2836. case FacComponent_Invoke:
  2837. chan_misdn_log(1, bc->port, " --> Invoke\n");
  2838. if (fac->u.CCNRInterrogate.Component.Invoke.CCBSReferencePresent) {
  2839. chan_misdn_log(1, bc->port, " --> CCBSReference:%d\n",
  2840. fac->u.CCNRInterrogate.Component.Invoke.CCBSReference);
  2841. }
  2842. if (fac->u.CCNRInterrogate.Component.Invoke.AParty.LengthOfNumber) {
  2843. chan_misdn_log(1, bc->port, " --> AParty\n");
  2844. print_facility_PartyNumber(3, &fac->u.CCNRInterrogate.Component.Invoke.AParty, bc);
  2845. }
  2846. break;
  2847. case FacComponent_Result:
  2848. chan_misdn_log(1, bc->port, " --> Result: RecallMode:%d\n",
  2849. fac->u.CCNRInterrogate.Component.Result.RecallMode);
  2850. if (fac->u.CCNRInterrogate.Component.Result.NumRecords) {
  2851. for (Index = 0; Index < fac->u.CCNRInterrogate.Component.Result.NumRecords; ++Index) {
  2852. chan_misdn_log(1, bc->port, " --> CallDetails[%d]:\n", Index);
  2853. print_facility_CallInformation(3, &fac->u.CCNRInterrogate.Component.Result.CallDetails[Index], bc);
  2854. }
  2855. }
  2856. break;
  2857. default:
  2858. break;
  2859. }
  2860. break;
  2861. case Fac_CCBS_T_Call:
  2862. chan_misdn_log(1, bc->port, " --> CCBS_T_Call: InvokeID:%d\n",
  2863. fac->u.CCBS_T_Call.InvokeID);
  2864. break;
  2865. case Fac_CCBS_T_Suspend:
  2866. chan_misdn_log(1, bc->port, " --> CCBS_T_Suspend: InvokeID:%d\n",
  2867. fac->u.CCBS_T_Suspend.InvokeID);
  2868. break;
  2869. case Fac_CCBS_T_Resume:
  2870. chan_misdn_log(1, bc->port, " --> CCBS_T_Resume: InvokeID:%d\n",
  2871. fac->u.CCBS_T_Resume.InvokeID);
  2872. break;
  2873. case Fac_CCBS_T_RemoteUserFree:
  2874. chan_misdn_log(1, bc->port, " --> CCBS_T_RemoteUserFree: InvokeID:%d\n",
  2875. fac->u.CCBS_T_RemoteUserFree.InvokeID);
  2876. break;
  2877. case Fac_CCBS_T_Available:
  2878. chan_misdn_log(1, bc->port, " --> CCBS_T_Available: InvokeID:%d\n",
  2879. fac->u.CCBS_T_Available.InvokeID);
  2880. break;
  2881. case Fac_CCBS_T_Request:
  2882. chan_misdn_log(1, bc->port, " --> CCBS_T_Request: InvokeID:%d\n",
  2883. fac->u.CCBS_T_Request.InvokeID);
  2884. switch (fac->u.CCBS_T_Request.ComponentType) {
  2885. case FacComponent_Invoke:
  2886. chan_misdn_log(1, bc->port, " --> Invoke\n");
  2887. chan_misdn_log(1, bc->port, " --> DestinationAddress:\n");
  2888. print_facility_Address(3, &fac->u.CCBS_T_Request.Component.Invoke.Destination, bc);
  2889. print_facility_Q931_Bc_Hlc_Llc(2, &fac->u.CCBS_T_Request.Component.Invoke.Q931ie, bc);
  2890. if (fac->u.CCBS_T_Request.Component.Invoke.RetentionSupported) {
  2891. chan_misdn_log(1, bc->port, " --> RetentionSupported:1\n");
  2892. }
  2893. if (fac->u.CCBS_T_Request.Component.Invoke.PresentationAllowedIndicatorPresent) {
  2894. chan_misdn_log(1, bc->port, " --> PresentationAllowed:%d\n",
  2895. fac->u.CCBS_T_Request.Component.Invoke.PresentationAllowedIndicator);
  2896. }
  2897. if (fac->u.CCBS_T_Request.Component.Invoke.Originating.Party.LengthOfNumber) {
  2898. chan_misdn_log(1, bc->port, " --> OriginatingAddress:\n");
  2899. print_facility_Address(3, &fac->u.CCBS_T_Request.Component.Invoke.Originating, bc);
  2900. }
  2901. break;
  2902. case FacComponent_Result:
  2903. chan_misdn_log(1, bc->port, " --> Result: RetentionSupported:%d\n",
  2904. fac->u.CCBS_T_Request.Component.Result.RetentionSupported);
  2905. break;
  2906. default:
  2907. break;
  2908. }
  2909. break;
  2910. case Fac_CCNR_T_Request:
  2911. chan_misdn_log(1, bc->port, " --> CCNR_T_Request: InvokeID:%d\n",
  2912. fac->u.CCNR_T_Request.InvokeID);
  2913. switch (fac->u.CCNR_T_Request.ComponentType) {
  2914. case FacComponent_Invoke:
  2915. chan_misdn_log(1, bc->port, " --> Invoke\n");
  2916. chan_misdn_log(1, bc->port, " --> DestinationAddress:\n");
  2917. print_facility_Address(3, &fac->u.CCNR_T_Request.Component.Invoke.Destination, bc);
  2918. print_facility_Q931_Bc_Hlc_Llc(2, &fac->u.CCNR_T_Request.Component.Invoke.Q931ie, bc);
  2919. if (fac->u.CCNR_T_Request.Component.Invoke.RetentionSupported) {
  2920. chan_misdn_log(1, bc->port, " --> RetentionSupported:1\n");
  2921. }
  2922. if (fac->u.CCNR_T_Request.Component.Invoke.PresentationAllowedIndicatorPresent) {
  2923. chan_misdn_log(1, bc->port, " --> PresentationAllowed:%d\n",
  2924. fac->u.CCNR_T_Request.Component.Invoke.PresentationAllowedIndicator);
  2925. }
  2926. if (fac->u.CCNR_T_Request.Component.Invoke.Originating.Party.LengthOfNumber) {
  2927. chan_misdn_log(1, bc->port, " --> OriginatingAddress:\n");
  2928. print_facility_Address(3, &fac->u.CCNR_T_Request.Component.Invoke.Originating, bc);
  2929. }
  2930. break;
  2931. case FacComponent_Result:
  2932. chan_misdn_log(1, bc->port, " --> Result: RetentionSupported:%d\n",
  2933. fac->u.CCNR_T_Request.Component.Result.RetentionSupported);
  2934. break;
  2935. default:
  2936. break;
  2937. }
  2938. break;
  2939. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2940. case Fac_None:
  2941. /* No facility so print nothing */
  2942. break;
  2943. default:
  2944. chan_misdn_log(1, bc->port, " --> unknown facility\n");
  2945. break;
  2946. }
  2947. }
  2948. static void print_bearer(struct misdn_bchannel *bc)
  2949. {
  2950. chan_misdn_log(2, bc->port, " --> Bearer: %s\n", bearer2str(bc->capability));
  2951. switch(bc->law) {
  2952. case INFO_CODEC_ALAW:
  2953. chan_misdn_log(2, bc->port, " --> Codec: Alaw\n");
  2954. break;
  2955. case INFO_CODEC_ULAW:
  2956. chan_misdn_log(2, bc->port, " --> Codec: Ulaw\n");
  2957. break;
  2958. }
  2959. }
  2960. /*!
  2961. * \internal
  2962. * \brief Prefix a string to another string in place.
  2963. *
  2964. * \param str_prefix String to prefix to the main string.
  2965. * \param str_main String to get the prefix added to it.
  2966. * \param size Buffer size of the main string (Includes null terminator).
  2967. *
  2968. * \note The str_main buffer size must be greater than one.
  2969. *
  2970. * \return Nothing
  2971. */
  2972. static void misdn_prefix_string(const char *str_prefix, char *str_main, size_t size)
  2973. {
  2974. size_t len_over;
  2975. size_t len_total;
  2976. size_t len_main;
  2977. size_t len_prefix;
  2978. len_prefix = strlen(str_prefix);
  2979. if (!len_prefix) {
  2980. /* There is no prefix to prepend. */
  2981. return;
  2982. }
  2983. len_main = strlen(str_main);
  2984. len_total = len_prefix + len_main;
  2985. if (size <= len_total) {
  2986. /* We need to truncate since the buffer is too small. */
  2987. len_over = len_total + 1 - size;
  2988. if (len_over <= len_main) {
  2989. len_main -= len_over;
  2990. } else {
  2991. len_over -= len_main;
  2992. len_main = 0;
  2993. len_prefix -= len_over;
  2994. }
  2995. }
  2996. if (len_main) {
  2997. memmove(str_main + len_prefix, str_main, len_main);
  2998. }
  2999. memcpy(str_main, str_prefix, len_prefix);
  3000. str_main[len_prefix + len_main] = '\0';
  3001. }
  3002. /*!
  3003. * \internal
  3004. * \brief Add a configured prefix to the given number.
  3005. *
  3006. * \param port Logical port number
  3007. * \param number_type Type-of-number passed in.
  3008. * \param number Given number string to add prefix
  3009. * \param size Buffer size number string occupies.
  3010. *
  3011. * \return Nothing
  3012. */
  3013. static void misdn_add_number_prefix(int port, enum mISDN_NUMBER_TYPE number_type, char *number, size_t size)
  3014. {
  3015. enum misdn_cfg_elements type_prefix;
  3016. char num_prefix[MISDN_MAX_NUMBER_LEN];
  3017. /* Get prefix string. */
  3018. switch (number_type) {
  3019. case NUMTYPE_UNKNOWN:
  3020. type_prefix = MISDN_CFG_TON_PREFIX_UNKNOWN;
  3021. break;
  3022. case NUMTYPE_INTERNATIONAL:
  3023. type_prefix = MISDN_CFG_TON_PREFIX_INTERNATIONAL;
  3024. break;
  3025. case NUMTYPE_NATIONAL:
  3026. type_prefix = MISDN_CFG_TON_PREFIX_NATIONAL;
  3027. break;
  3028. case NUMTYPE_NETWORK_SPECIFIC:
  3029. type_prefix = MISDN_CFG_TON_PREFIX_NETWORK_SPECIFIC;
  3030. break;
  3031. case NUMTYPE_SUBSCRIBER:
  3032. type_prefix = MISDN_CFG_TON_PREFIX_SUBSCRIBER;
  3033. break;
  3034. case NUMTYPE_ABBREVIATED:
  3035. type_prefix = MISDN_CFG_TON_PREFIX_ABBREVIATED;
  3036. break;
  3037. default:
  3038. /* Type-of-number does not have a prefix that can be added. */
  3039. return;
  3040. }
  3041. misdn_cfg_get(port, type_prefix, num_prefix, sizeof(num_prefix));
  3042. misdn_prefix_string(num_prefix, number, size);
  3043. }
  3044. static void export_aoc_vars(int originator, struct ast_channel *ast, struct misdn_bchannel *bc)
  3045. {
  3046. RAII_VAR(struct ast_channel *, chan, NULL, ast_channel_cleanup);
  3047. char buf[128];
  3048. if (!bc->AOCD_need_export || !ast) {
  3049. return;
  3050. }
  3051. if (originator == ORG_AST) {
  3052. chan = ast_channel_bridge_peer(ast);
  3053. if (!chan) {
  3054. return;
  3055. }
  3056. } else {
  3057. chan = ast_channel_ref(ast);
  3058. }
  3059. switch (bc->AOCDtype) {
  3060. case Fac_AOCDCurrency:
  3061. pbx_builtin_setvar_helper(chan, "AOCD_Type", "currency");
  3062. if (bc->AOCD.currency.chargeNotAvailable) {
  3063. pbx_builtin_setvar_helper(chan, "AOCD_ChargeAvailable", "no");
  3064. } else {
  3065. pbx_builtin_setvar_helper(chan, "AOCD_ChargeAvailable", "yes");
  3066. if (bc->AOCD.currency.freeOfCharge) {
  3067. pbx_builtin_setvar_helper(chan, "AOCD_FreeOfCharge", "yes");
  3068. } else {
  3069. pbx_builtin_setvar_helper(chan, "AOCD_FreeOfCharge", "no");
  3070. if (snprintf(buf, sizeof(buf), "%d %s", bc->AOCD.currency.currencyAmount * bc->AOCD.currency.multiplier, bc->AOCD.currency.currency) < sizeof(buf)) {
  3071. pbx_builtin_setvar_helper(chan, "AOCD_Amount", buf);
  3072. if (bc->AOCD.currency.billingId >= 0 && snprintf(buf, sizeof(buf), "%d", bc->AOCD.currency.billingId) < sizeof(buf)) {
  3073. pbx_builtin_setvar_helper(chan, "AOCD_BillingId", buf);
  3074. }
  3075. }
  3076. }
  3077. }
  3078. break;
  3079. case Fac_AOCDChargingUnit:
  3080. pbx_builtin_setvar_helper(chan, "AOCD_Type", "charging_unit");
  3081. if (bc->AOCD.chargingUnit.chargeNotAvailable) {
  3082. pbx_builtin_setvar_helper(chan, "AOCD_ChargeAvailable", "no");
  3083. } else {
  3084. pbx_builtin_setvar_helper(chan, "AOCD_ChargeAvailable", "yes");
  3085. if (bc->AOCD.chargingUnit.freeOfCharge) {
  3086. pbx_builtin_setvar_helper(chan, "AOCD_FreeOfCharge", "yes");
  3087. } else {
  3088. pbx_builtin_setvar_helper(chan, "AOCD_FreeOfCharge", "no");
  3089. if (snprintf(buf, sizeof(buf), "%d", bc->AOCD.chargingUnit.recordedUnits) < sizeof(buf)) {
  3090. pbx_builtin_setvar_helper(chan, "AOCD_RecordedUnits", buf);
  3091. if (bc->AOCD.chargingUnit.billingId >= 0 && snprintf(buf, sizeof(buf), "%d", bc->AOCD.chargingUnit.billingId) < sizeof(buf)) {
  3092. pbx_builtin_setvar_helper(chan, "AOCD_BillingId", buf);
  3093. }
  3094. }
  3095. }
  3096. }
  3097. break;
  3098. default:
  3099. break;
  3100. }
  3101. bc->AOCD_need_export = 0;
  3102. }
  3103. /*************** Helpers END *************/
  3104. static void sighandler(int sig)
  3105. {
  3106. }
  3107. static void *misdn_tasks_thread_func(void *data)
  3108. {
  3109. int wait;
  3110. struct sigaction sa;
  3111. sa.sa_handler = sighandler;
  3112. sa.sa_flags = SA_NODEFER;
  3113. sigemptyset(&sa.sa_mask);
  3114. sigaddset(&sa.sa_mask, SIGUSR1);
  3115. sigaction(SIGUSR1, &sa, NULL);
  3116. sem_post((sem_t *)data);
  3117. while (1) {
  3118. wait = ast_sched_wait(misdn_tasks);
  3119. if (wait < 0) {
  3120. wait = 8000;
  3121. }
  3122. if (poll(NULL, 0, wait) < 0) {
  3123. chan_misdn_log(4, 0, "Waking up misdn_tasks thread\n");
  3124. }
  3125. ast_sched_runq(misdn_tasks);
  3126. }
  3127. return NULL;
  3128. }
  3129. static void misdn_tasks_init(void)
  3130. {
  3131. sem_t blocker;
  3132. int i = 5;
  3133. if (sem_init(&blocker, 0, 0)) {
  3134. perror("chan_misdn: Failed to initialize semaphore!");
  3135. exit(1);
  3136. }
  3137. chan_misdn_log(4, 0, "Starting misdn_tasks thread\n");
  3138. misdn_tasks = ast_sched_context_create();
  3139. pthread_create(&misdn_tasks_thread, NULL, misdn_tasks_thread_func, &blocker);
  3140. while (sem_wait(&blocker) && --i) {
  3141. }
  3142. sem_destroy(&blocker);
  3143. }
  3144. static void misdn_tasks_destroy(void)
  3145. {
  3146. if (misdn_tasks) {
  3147. chan_misdn_log(4, 0, "Killing misdn_tasks thread\n");
  3148. if (pthread_cancel(misdn_tasks_thread) == 0) {
  3149. cb_log(4, 0, "Joining misdn_tasks thread\n");
  3150. pthread_join(misdn_tasks_thread, NULL);
  3151. }
  3152. ast_sched_context_destroy(misdn_tasks);
  3153. }
  3154. }
  3155. static inline void misdn_tasks_wakeup(void)
  3156. {
  3157. pthread_kill(misdn_tasks_thread, SIGUSR1);
  3158. }
  3159. static inline int _misdn_tasks_add_variable(int timeout, ast_sched_cb callback, const void *data, int variable)
  3160. {
  3161. int task_id;
  3162. if (!misdn_tasks) {
  3163. misdn_tasks_init();
  3164. }
  3165. task_id = ast_sched_add_variable(misdn_tasks, timeout, callback, data, variable);
  3166. misdn_tasks_wakeup();
  3167. return task_id;
  3168. }
  3169. static int misdn_tasks_add(int timeout, ast_sched_cb callback, const void *data)
  3170. {
  3171. return _misdn_tasks_add_variable(timeout, callback, data, 0);
  3172. }
  3173. static int misdn_tasks_add_variable(int timeout, ast_sched_cb callback, const void *data)
  3174. {
  3175. return _misdn_tasks_add_variable(timeout, callback, data, 1);
  3176. }
  3177. static void misdn_tasks_remove(int task_id)
  3178. {
  3179. AST_SCHED_DEL(misdn_tasks, task_id);
  3180. }
  3181. static int misdn_l1_task(const void *vdata)
  3182. {
  3183. const int *data = vdata;
  3184. misdn_lib_isdn_l1watcher(*data);
  3185. chan_misdn_log(5, *data, "L1watcher timeout\n");
  3186. return 1;
  3187. }
  3188. static int misdn_overlap_dial_task(const void *data)
  3189. {
  3190. struct timeval tv_end, tv_now;
  3191. int diff;
  3192. struct chan_list *ch = (struct chan_list *) data;
  3193. char *dad;
  3194. chan_misdn_log(4, ch->bc->port, "overlap dial task, chan_state: %d\n", ch->state);
  3195. if (ch->state != MISDN_WAITING4DIGS) {
  3196. ch->overlap_dial_task = -1;
  3197. return 0;
  3198. }
  3199. ast_mutex_lock(&ch->overlap_tv_lock);
  3200. tv_end = ch->overlap_tv;
  3201. ast_mutex_unlock(&ch->overlap_tv_lock);
  3202. tv_end.tv_sec += ch->overlap_dial;
  3203. tv_now = ast_tvnow();
  3204. diff = ast_tvdiff_ms(tv_end, tv_now);
  3205. if (100 < diff) {
  3206. return diff;
  3207. }
  3208. /* if we are 100ms near the timeout, we are satisfied.. */
  3209. stop_indicate(ch);
  3210. if (ast_strlen_zero(ch->bc->dialed.number)) {
  3211. dad = "s";
  3212. ast_channel_exten_set(ch->ast, dad);
  3213. } else {
  3214. dad = ch->bc->dialed.number;
  3215. }
  3216. if (ast_exists_extension(ch->ast, ch->context, dad, 1, ch->bc->caller.number)) {
  3217. ch->state = MISDN_DIALING;
  3218. if (pbx_start_chan(ch) < 0) {
  3219. chan_misdn_log(-1, ch->bc->port, "ast_pbx_start returned < 0 in misdn_overlap_dial_task\n");
  3220. goto misdn_overlap_dial_task_disconnect;
  3221. }
  3222. } else {
  3223. misdn_overlap_dial_task_disconnect:
  3224. hanguptone_indicate(ch);
  3225. ch->bc->out_cause = AST_CAUSE_UNALLOCATED;
  3226. ch->state = MISDN_CLEANING;
  3227. misdn_lib_send_event(ch->bc, EVENT_DISCONNECT);
  3228. }
  3229. ch->overlap_dial_task = -1;
  3230. return 0;
  3231. }
  3232. static void send_digit_to_chan(struct chan_list *cl, char digit)
  3233. {
  3234. static const char * const dtmf_tones[] = {
  3235. /* *INDENT-OFF* */
  3236. "!941+1336/100,!0/100", /* 0 */
  3237. "!697+1209/100,!0/100", /* 1 */
  3238. "!697+1336/100,!0/100", /* 2 */
  3239. "!697+1477/100,!0/100", /* 3 */
  3240. "!770+1209/100,!0/100", /* 4 */
  3241. "!770+1336/100,!0/100", /* 5 */
  3242. "!770+1477/100,!0/100", /* 6 */
  3243. "!852+1209/100,!0/100", /* 7 */
  3244. "!852+1336/100,!0/100", /* 8 */
  3245. "!852+1477/100,!0/100", /* 9 */
  3246. "!697+1633/100,!0/100", /* A */
  3247. "!770+1633/100,!0/100", /* B */
  3248. "!852+1633/100,!0/100", /* C */
  3249. "!941+1633/100,!0/100", /* D */
  3250. "!941+1209/100,!0/100", /* * */
  3251. "!941+1477/100,!0/100", /* # */
  3252. /* *INDENT-ON* */
  3253. };
  3254. struct ast_channel *chan = cl->ast;
  3255. if (digit >= '0' && digit <='9') {
  3256. ast_playtones_start(chan, 0, dtmf_tones[digit - '0'], 0);
  3257. } else if (digit >= 'A' && digit <= 'D') {
  3258. ast_playtones_start(chan, 0, dtmf_tones[digit - 'A' + 10], 0);
  3259. } else if (digit == '*') {
  3260. ast_playtones_start(chan, 0, dtmf_tones[14], 0);
  3261. } else if (digit == '#') {
  3262. ast_playtones_start(chan, 0, dtmf_tones[15], 0);
  3263. } else {
  3264. /* not handled */
  3265. ast_debug(1, "Unable to handle DTMF tone '%c' for '%s'\n", digit, ast_channel_name(chan));
  3266. }
  3267. }
  3268. /*** CLI HANDLING ***/
  3269. static char *handle_cli_misdn_set_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3270. {
  3271. int level;
  3272. switch (cmd) {
  3273. case CLI_INIT:
  3274. e->command = "misdn set debug [on|off]";
  3275. e->usage =
  3276. "Usage: misdn set debug {on|off|<level>} [only] | [port <port> [only]]\n"
  3277. " Set the debug level of the mISDN channel.\n";
  3278. return NULL;
  3279. case CLI_GENERATE:
  3280. return complete_debug_port(a);
  3281. }
  3282. if (a->argc < 4 || a->argc > 7) {
  3283. return CLI_SHOWUSAGE;
  3284. }
  3285. if (!strcasecmp(a->argv[3], "on")) {
  3286. level = 1;
  3287. } else if (!strcasecmp(a->argv[3], "off")) {
  3288. level = 0;
  3289. } else if (isdigit(a->argv[3][0])) {
  3290. level = atoi(a->argv[3]);
  3291. } else {
  3292. return CLI_SHOWUSAGE;
  3293. }
  3294. switch (a->argc) {
  3295. case 4:
  3296. case 5:
  3297. {
  3298. int i;
  3299. int only = 0;
  3300. if (a->argc == 5) {
  3301. if (strncasecmp(a->argv[4], "only", strlen(a->argv[4]))) {
  3302. return CLI_SHOWUSAGE;
  3303. } else {
  3304. only = 1;
  3305. }
  3306. }
  3307. for (i = 0; i <= max_ports; i++) {
  3308. misdn_debug[i] = level;
  3309. misdn_debug_only[i] = only;
  3310. }
  3311. ast_cli(a->fd, "changing debug level for all ports to %d%s\n", misdn_debug[0], only ? " (only)" : "");
  3312. }
  3313. break;
  3314. case 6:
  3315. case 7:
  3316. {
  3317. int port;
  3318. if (strncasecmp(a->argv[4], "port", strlen(a->argv[4])))
  3319. return CLI_SHOWUSAGE;
  3320. port = atoi(a->argv[5]);
  3321. if (port <= 0 || port > max_ports) {
  3322. switch (max_ports) {
  3323. case 0:
  3324. ast_cli(a->fd, "port number not valid! no ports available so you won't get lucky with any number here...\n");
  3325. break;
  3326. case 1:
  3327. ast_cli(a->fd, "port number not valid! only port 1 is available.\n");
  3328. break;
  3329. default:
  3330. ast_cli(a->fd, "port number not valid! only ports 1 to %d are available.\n", max_ports);
  3331. }
  3332. return 0;
  3333. }
  3334. if (a->argc == 7) {
  3335. if (strncasecmp(a->argv[6], "only", strlen(a->argv[6]))) {
  3336. return CLI_SHOWUSAGE;
  3337. } else {
  3338. misdn_debug_only[port] = 1;
  3339. }
  3340. } else {
  3341. misdn_debug_only[port] = 0;
  3342. }
  3343. misdn_debug[port] = level;
  3344. ast_cli(a->fd, "changing debug level to %d%s for port %d\n", misdn_debug[port], misdn_debug_only[port] ? " (only)" : "", port);
  3345. }
  3346. }
  3347. return CLI_SUCCESS;
  3348. }
  3349. static char *handle_cli_misdn_set_crypt_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3350. {
  3351. switch (cmd) {
  3352. case CLI_INIT:
  3353. e->command = "misdn set crypt debug";
  3354. e->usage =
  3355. "Usage: misdn set crypt debug <level>\n"
  3356. " Set the crypt debug level of the mISDN channel. Level\n"
  3357. " must be 1 or 2.\n";
  3358. return NULL;
  3359. case CLI_GENERATE:
  3360. return NULL;
  3361. }
  3362. if (a->argc != 5) {
  3363. return CLI_SHOWUSAGE;
  3364. }
  3365. /* XXX Is this supposed to not do anything? XXX */
  3366. return CLI_SUCCESS;
  3367. }
  3368. static char *handle_cli_misdn_port_block(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3369. {
  3370. switch (cmd) {
  3371. case CLI_INIT:
  3372. e->command = "misdn port block";
  3373. e->usage =
  3374. "Usage: misdn port block <port>\n"
  3375. " Block the specified port by <port>.\n";
  3376. return NULL;
  3377. case CLI_GENERATE:
  3378. return NULL;
  3379. }
  3380. if (a->argc != 4) {
  3381. return CLI_SHOWUSAGE;
  3382. }
  3383. misdn_lib_port_block(atoi(a->argv[3]));
  3384. return CLI_SUCCESS;
  3385. }
  3386. static char *handle_cli_misdn_port_unblock(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3387. {
  3388. switch (cmd) {
  3389. case CLI_INIT:
  3390. e->command = "misdn port unblock";
  3391. e->usage =
  3392. "Usage: misdn port unblock <port>\n"
  3393. " Unblock the port specified by <port>.\n";
  3394. return NULL;
  3395. case CLI_GENERATE:
  3396. return NULL;
  3397. }
  3398. if (a->argc != 4) {
  3399. return CLI_SHOWUSAGE;
  3400. }
  3401. misdn_lib_port_unblock(atoi(a->argv[3]));
  3402. return CLI_SUCCESS;
  3403. }
  3404. static char *handle_cli_misdn_restart_port(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3405. {
  3406. switch (cmd) {
  3407. case CLI_INIT:
  3408. e->command = "misdn restart port";
  3409. e->usage =
  3410. "Usage: misdn restart port <port>\n"
  3411. " Restart the given port.\n";
  3412. return NULL;
  3413. case CLI_GENERATE:
  3414. return NULL;
  3415. }
  3416. if (a->argc != 4) {
  3417. return CLI_SHOWUSAGE;
  3418. }
  3419. misdn_lib_port_restart(atoi(a->argv[3]));
  3420. return CLI_SUCCESS;
  3421. }
  3422. static char *handle_cli_misdn_restart_pid(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3423. {
  3424. switch (cmd) {
  3425. case CLI_INIT:
  3426. e->command = "misdn restart pid";
  3427. e->usage =
  3428. "Usage: misdn restart pid <pid>\n"
  3429. " Restart the given pid\n";
  3430. return NULL;
  3431. case CLI_GENERATE:
  3432. return NULL;
  3433. }
  3434. if (a->argc != 4) {
  3435. return CLI_SHOWUSAGE;
  3436. }
  3437. misdn_lib_pid_restart(atoi(a->argv[3]));
  3438. return CLI_SUCCESS;
  3439. }
  3440. static char *handle_cli_misdn_port_up(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3441. {
  3442. switch (cmd) {
  3443. case CLI_INIT:
  3444. e->command = "misdn port up";
  3445. e->usage =
  3446. "Usage: misdn port up <port>\n"
  3447. " Try to establish L1 on the given port.\n";
  3448. return NULL;
  3449. case CLI_GENERATE:
  3450. return NULL;
  3451. }
  3452. if (a->argc != 4) {
  3453. return CLI_SHOWUSAGE;
  3454. }
  3455. misdn_lib_get_port_up(atoi(a->argv[3]));
  3456. return CLI_SUCCESS;
  3457. }
  3458. static char *handle_cli_misdn_port_down(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3459. {
  3460. switch (cmd) {
  3461. case CLI_INIT:
  3462. e->command = "misdn port down";
  3463. e->usage =
  3464. "Usage: misdn port down <port>\n"
  3465. " Try to deactivate the L1 on the given port.\n";
  3466. return NULL;
  3467. case CLI_GENERATE:
  3468. return NULL;
  3469. }
  3470. if (a->argc != 4) {
  3471. return CLI_SHOWUSAGE;
  3472. }
  3473. misdn_lib_get_port_down(atoi(a->argv[3]));
  3474. return CLI_SUCCESS;
  3475. }
  3476. static inline void show_config_description(int fd, enum misdn_cfg_elements elem)
  3477. {
  3478. char section[BUFFERSIZE];
  3479. char name[BUFFERSIZE];
  3480. char desc[BUFFERSIZE];
  3481. char def[BUFFERSIZE];
  3482. char tmp[BUFFERSIZE];
  3483. misdn_cfg_get_name(elem, tmp, sizeof(tmp));
  3484. term_color(name, tmp, COLOR_BRWHITE, 0, sizeof(tmp));
  3485. misdn_cfg_get_desc(elem, desc, sizeof(desc), def, sizeof(def));
  3486. if (elem < MISDN_CFG_LAST) {
  3487. term_color(section, "PORTS SECTION", COLOR_YELLOW, 0, sizeof(section));
  3488. } else {
  3489. term_color(section, "GENERAL SECTION", COLOR_YELLOW, 0, sizeof(section));
  3490. }
  3491. if (*def) {
  3492. ast_cli(fd, "[%s] %s (Default: %s)\n\t%s\n", section, name, def, desc);
  3493. } else {
  3494. ast_cli(fd, "[%s] %s\n\t%s\n", section, name, desc);
  3495. }
  3496. }
  3497. static char *handle_cli_misdn_show_config(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3498. {
  3499. char buffer[BUFFERSIZE];
  3500. enum misdn_cfg_elements elem;
  3501. int linebreak;
  3502. int onlyport = -1;
  3503. int ok = 0;
  3504. switch (cmd) {
  3505. case CLI_INIT:
  3506. e->command = "misdn show config";
  3507. e->usage =
  3508. "Usage: misdn show config [<port> | description <config element> | descriptions [general|ports]]\n"
  3509. " Use 0 for <port> to only print the general config.\n";
  3510. return NULL;
  3511. case CLI_GENERATE:
  3512. return complete_show_config(a);
  3513. }
  3514. if (a->argc >= 4) {
  3515. if (!strcmp(a->argv[3], "description")) {
  3516. if (a->argc == 5) {
  3517. enum misdn_cfg_elements elem = misdn_cfg_get_elem(a->argv[4]);
  3518. if (elem == MISDN_CFG_FIRST) {
  3519. ast_cli(a->fd, "Unknown element: %s\n", a->argv[4]);
  3520. } else {
  3521. show_config_description(a->fd, elem);
  3522. }
  3523. return CLI_SUCCESS;
  3524. }
  3525. return CLI_SHOWUSAGE;
  3526. } else if (!strcmp(a->argv[3], "descriptions")) {
  3527. if ((a->argc == 4) || ((a->argc == 5) && !strcmp(a->argv[4], "general"))) {
  3528. for (elem = MISDN_GEN_FIRST + 1; elem < MISDN_GEN_LAST; ++elem) {
  3529. show_config_description(a->fd, elem);
  3530. ast_cli(a->fd, "\n");
  3531. }
  3532. ok = 1;
  3533. }
  3534. if ((a->argc == 4) || ((a->argc == 5) && !strcmp(a->argv[4], "ports"))) {
  3535. for (elem = MISDN_CFG_FIRST + 1; elem < MISDN_CFG_LAST - 1 /* the ptp hack, remove the -1 when ptp is gone */; ++elem) {
  3536. show_config_description(a->fd, elem);
  3537. ast_cli(a->fd, "\n");
  3538. }
  3539. ok = 1;
  3540. }
  3541. return ok ? CLI_SUCCESS : CLI_SHOWUSAGE;
  3542. } else if (!sscanf(a->argv[3], "%5d", &onlyport) || onlyport < 0) {
  3543. ast_cli(a->fd, "Unknown option: %s\n", a->argv[3]);
  3544. return CLI_SHOWUSAGE;
  3545. }
  3546. }
  3547. if (a->argc == 3 || onlyport == 0) {
  3548. ast_cli(a->fd, "mISDN General-Config:\n");
  3549. for (elem = MISDN_GEN_FIRST + 1, linebreak = 1; elem < MISDN_GEN_LAST; elem++, linebreak++) {
  3550. misdn_cfg_get_config_string(0, elem, buffer, sizeof(buffer));
  3551. ast_cli(a->fd, "%-36s%s", buffer, !(linebreak % 2) ? "\n" : "");
  3552. }
  3553. ast_cli(a->fd, "\n");
  3554. }
  3555. if (onlyport < 0) {
  3556. int port = misdn_cfg_get_next_port(0);
  3557. for (; port > 0; port = misdn_cfg_get_next_port(port)) {
  3558. ast_cli(a->fd, "\n[PORT %d]\n", port);
  3559. for (elem = MISDN_CFG_FIRST + 1, linebreak = 1; elem < MISDN_CFG_LAST; elem++, linebreak++) {
  3560. misdn_cfg_get_config_string(port, elem, buffer, sizeof(buffer));
  3561. ast_cli(a->fd, "%-36s%s", buffer, !(linebreak % 2) ? "\n" : "");
  3562. }
  3563. ast_cli(a->fd, "\n");
  3564. }
  3565. }
  3566. if (onlyport > 0) {
  3567. if (misdn_cfg_is_port_valid(onlyport)) {
  3568. ast_cli(a->fd, "[PORT %d]\n", onlyport);
  3569. for (elem = MISDN_CFG_FIRST + 1, linebreak = 1; elem < MISDN_CFG_LAST; elem++, linebreak++) {
  3570. misdn_cfg_get_config_string(onlyport, elem, buffer, sizeof(buffer));
  3571. ast_cli(a->fd, "%-36s%s", buffer, !(linebreak % 2) ? "\n" : "");
  3572. }
  3573. ast_cli(a->fd, "\n");
  3574. } else {
  3575. ast_cli(a->fd, "Port %d is not active!\n", onlyport);
  3576. }
  3577. }
  3578. return CLI_SUCCESS;
  3579. }
  3580. struct state_struct {
  3581. enum misdn_chan_state state;
  3582. char txt[255];
  3583. };
  3584. static const struct state_struct state_array[] = {
  3585. /* *INDENT-OFF* */
  3586. { MISDN_NOTHING, "NOTHING" }, /* at beginning */
  3587. { MISDN_WAITING4DIGS, "WAITING4DIGS" }, /* when waiting for infos */
  3588. { MISDN_EXTCANTMATCH, "EXTCANTMATCH" }, /* when asterisk couldn't match our ext */
  3589. { MISDN_INCOMING_SETUP, "INCOMING SETUP" }, /* when pbx_start */
  3590. { MISDN_DIALING, "DIALING" }, /* when pbx_start */
  3591. { MISDN_PROGRESS, "PROGRESS" }, /* when pbx_start */
  3592. { MISDN_PROCEEDING, "PROCEEDING" }, /* when pbx_start */
  3593. { MISDN_CALLING, "CALLING" }, /* when misdn_call is called */
  3594. { MISDN_CALLING_ACKNOWLEDGE, "CALLING_ACKNOWLEDGE" }, /* when misdn_call is called */
  3595. { MISDN_ALERTING, "ALERTING" }, /* when Alerting */
  3596. { MISDN_BUSY, "BUSY" }, /* when BUSY */
  3597. { MISDN_CONNECTED, "CONNECTED" }, /* when connected */
  3598. { MISDN_DISCONNECTED, "DISCONNECTED" }, /* when connected */
  3599. { MISDN_CLEANING, "CLEANING" }, /* when hangup from * but we were connected before */
  3600. /* *INDENT-ON* */
  3601. };
  3602. static const char *misdn_get_ch_state(struct chan_list *p)
  3603. {
  3604. int i;
  3605. static char state[8];
  3606. if (!p) {
  3607. return NULL;
  3608. }
  3609. for (i = 0; i < ARRAY_LEN(state_array); i++) {
  3610. if (state_array[i].state == p->state) {
  3611. return state_array[i].txt;
  3612. }
  3613. }
  3614. snprintf(state, sizeof(state), "%d", p->state) ;
  3615. return state;
  3616. }
  3617. static void reload_config(void)
  3618. {
  3619. int i, cfg_debug;
  3620. if (!g_config_initialized) {
  3621. ast_log(LOG_WARNING, "chan_misdn is not initialized properly, still reloading ?\n");
  3622. return ;
  3623. }
  3624. free_robin_list();
  3625. misdn_cfg_reload();
  3626. misdn_cfg_update_ptp();
  3627. misdn_cfg_get(0, MISDN_GEN_TRACEFILE, global_tracefile, sizeof(global_tracefile));
  3628. misdn_cfg_get(0, MISDN_GEN_DEBUG, &cfg_debug, sizeof(cfg_debug));
  3629. for (i = 0; i <= max_ports; i++) {
  3630. misdn_debug[i] = cfg_debug;
  3631. misdn_debug_only[i] = 0;
  3632. }
  3633. }
  3634. static char *handle_cli_misdn_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3635. {
  3636. switch (cmd) {
  3637. case CLI_INIT:
  3638. e->command = "misdn reload";
  3639. e->usage =
  3640. "Usage: misdn reload\n"
  3641. " Reload internal mISDN config, read from the config\n"
  3642. " file.\n";
  3643. return NULL;
  3644. case CLI_GENERATE:
  3645. return NULL;
  3646. }
  3647. if (a->argc != 2) {
  3648. return CLI_SHOWUSAGE;
  3649. }
  3650. ast_cli(a->fd, "Reloading mISDN configuration\n");
  3651. reload_config();
  3652. return CLI_SUCCESS;
  3653. }
  3654. static void print_bc_info(int fd, struct chan_list *help, struct misdn_bchannel *bc)
  3655. {
  3656. struct ast_channel *ast = help->ast;
  3657. ast_cli(fd,
  3658. "* Pid:%d Port:%d Ch:%d Mode:%s Orig:%s dialed:%s\n"
  3659. " --> caller:\"%s\" <%s>\n"
  3660. " --> redirecting-from:\"%s\" <%s>\n"
  3661. " --> redirecting-to:\"%s\" <%s>\n"
  3662. " --> context:%s state:%s\n",
  3663. bc->pid,
  3664. bc->port,
  3665. bc->channel,
  3666. bc->nt ? "NT" : "TE",
  3667. help->originator == ORG_AST ? "*" : "I",
  3668. ast ? ast_channel_exten(ast) : "",
  3669. (ast && ast_channel_caller(ast)->id.name.valid && ast_channel_caller(ast)->id.name.str)
  3670. ? ast_channel_caller(ast)->id.name.str : "",
  3671. (ast && ast_channel_caller(ast)->id.number.valid && ast_channel_caller(ast)->id.number.str)
  3672. ? ast_channel_caller(ast)->id.number.str : "",
  3673. bc->redirecting.from.name,
  3674. bc->redirecting.from.number,
  3675. bc->redirecting.to.name,
  3676. bc->redirecting.to.number,
  3677. ast ? ast_channel_context(ast) : "",
  3678. misdn_get_ch_state(help));
  3679. if (misdn_debug[bc->port] > 0) {
  3680. ast_cli(fd,
  3681. " --> astname: %s\n"
  3682. " --> ch_l3id: %x\n"
  3683. " --> ch_addr: %x\n"
  3684. " --> bc_addr: %x\n"
  3685. " --> bc_l3id: %x\n"
  3686. " --> display: %s\n"
  3687. " --> activated: %d\n"
  3688. " --> state: %s\n"
  3689. " --> capability: %s\n"
  3690. #ifdef MISDN_1_2
  3691. " --> pipeline: %s\n"
  3692. #else
  3693. " --> echo_cancel: %d\n"
  3694. #endif
  3695. " --> notone : rx %d tx:%d\n"
  3696. " --> bc_hold: %d\n",
  3697. ast ? ast_channel_name(ast) : "",
  3698. help->l3id,
  3699. help->addr,
  3700. bc->addr,
  3701. bc->l3_id,
  3702. bc->display,
  3703. bc->active,
  3704. bc_state2str(bc->bc_state),
  3705. bearer2str(bc->capability),
  3706. #ifdef MISDN_1_2
  3707. bc->pipeline,
  3708. #else
  3709. bc->ec_enable,
  3710. #endif
  3711. help->norxtone, help->notxtone,
  3712. bc->holded);
  3713. }
  3714. }
  3715. static char *handle_cli_misdn_show_channels(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3716. {
  3717. struct chan_list *help;
  3718. switch (cmd) {
  3719. case CLI_INIT:
  3720. e->command = "misdn show channels";
  3721. e->usage =
  3722. "Usage: misdn show channels\n"
  3723. " Show the internal mISDN channel list\n";
  3724. return NULL;
  3725. case CLI_GENERATE:
  3726. return NULL;
  3727. }
  3728. if (a->argc != 3) {
  3729. return CLI_SHOWUSAGE;
  3730. }
  3731. ast_cli(a->fd, "Channel List: %p\n", cl_te);
  3732. /*
  3733. * Walking the list and dumping the channel information could
  3734. * take awhile. With the list locked for the duration, the
  3735. * channel driver cannot process signaling messages. However,
  3736. * since this is a CLI command it should not be run very often.
  3737. */
  3738. ast_mutex_lock(&cl_te_lock);
  3739. for (help = cl_te; help; help = help->next) {
  3740. struct misdn_bchannel *bc = help->bc;
  3741. struct ast_channel *ast = help->ast;
  3742. if (!ast) {
  3743. if (!bc) {
  3744. ast_cli(a->fd, "chan_list obj. with l3id:%x has no bc and no ast Leg\n", help->l3id);
  3745. continue;
  3746. }
  3747. ast_cli(a->fd, "bc with pid:%d has no Ast Leg\n", bc->pid);
  3748. }
  3749. if (misdn_debug[0] > 2) {
  3750. ast_cli(a->fd, "Bc:%p Ast:%p\n", bc, ast);
  3751. }
  3752. if (bc) {
  3753. print_bc_info(a->fd, help, bc);
  3754. } else {
  3755. if (help->hold.state != MISDN_HOLD_IDLE) {
  3756. ast_cli(a->fd, "ITS A HELD CALL BC:\n");
  3757. ast_cli(a->fd, " --> l3_id: %x\n"
  3758. " --> dialed:%s\n"
  3759. " --> caller:\"%s\" <%s>\n"
  3760. " --> hold_port: %d\n"
  3761. " --> hold_channel: %d\n",
  3762. help->l3id,
  3763. ast_channel_exten(ast),
  3764. S_COR(ast_channel_caller(ast)->id.name.valid, ast_channel_caller(ast)->id.name.str, ""),
  3765. S_COR(ast_channel_caller(ast)->id.number.valid, ast_channel_caller(ast)->id.number.str, ""),
  3766. help->hold.port,
  3767. help->hold.channel
  3768. );
  3769. } else {
  3770. ast_cli(a->fd, "* Channel in unknown STATE !!! Exten:%s, Callerid:%s\n",
  3771. ast_channel_exten(ast),
  3772. S_COR(ast_channel_caller(ast)->id.number.valid, ast_channel_caller(ast)->id.number.str, ""));
  3773. }
  3774. }
  3775. }
  3776. ast_mutex_unlock(&cl_te_lock);
  3777. misdn_dump_chanlist();
  3778. return CLI_SUCCESS;
  3779. }
  3780. static char *handle_cli_misdn_show_channel(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3781. {
  3782. struct chan_list *help;
  3783. switch (cmd) {
  3784. case CLI_INIT:
  3785. e->command = "misdn show channel";
  3786. e->usage =
  3787. "Usage: misdn show channel <channel>\n"
  3788. " Show an internal mISDN channel\n.";
  3789. return NULL;
  3790. case CLI_GENERATE:
  3791. return complete_ch(a);
  3792. }
  3793. if (a->argc != 4) {
  3794. return CLI_SHOWUSAGE;
  3795. }
  3796. ast_mutex_lock(&cl_te_lock);
  3797. for (help = cl_te; help; help = help->next) {
  3798. struct misdn_bchannel *bc = help->bc;
  3799. struct ast_channel *ast = help->ast;
  3800. if (bc && ast) {
  3801. if (!strcasecmp(ast_channel_name(ast), a->argv[3])) {
  3802. print_bc_info(a->fd, help, bc);
  3803. break;
  3804. }
  3805. }
  3806. }
  3807. ast_mutex_unlock(&cl_te_lock);
  3808. return CLI_SUCCESS;
  3809. }
  3810. static char *handle_cli_misdn_set_tics(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3811. {
  3812. switch (cmd) {
  3813. case CLI_INIT:
  3814. e->command = "misdn set tics";
  3815. e->usage =
  3816. "Usage: misdn set tics <value>\n";
  3817. return NULL;
  3818. case CLI_GENERATE:
  3819. return NULL;
  3820. }
  3821. if (a->argc != 4) {
  3822. return CLI_SHOWUSAGE;
  3823. }
  3824. /* XXX Wow, this does... a whole lot of nothing... XXX */
  3825. MAXTICS = atoi(a->argv[3]);
  3826. return CLI_SUCCESS;
  3827. }
  3828. static char *handle_cli_misdn_show_stacks(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3829. {
  3830. int port;
  3831. switch (cmd) {
  3832. case CLI_INIT:
  3833. e->command = "misdn show stacks";
  3834. e->usage =
  3835. "Usage: misdn show stacks\n"
  3836. " Show internal mISDN stack_list.\n";
  3837. return NULL;
  3838. case CLI_GENERATE:
  3839. return NULL;
  3840. }
  3841. if (a->argc != 3) {
  3842. return CLI_SHOWUSAGE;
  3843. }
  3844. ast_cli(a->fd, "BEGIN STACK_LIST:\n");
  3845. for (port = misdn_cfg_get_next_port(0); port > 0;
  3846. port = misdn_cfg_get_next_port(port)) {
  3847. char buf[128];
  3848. get_show_stack_details(port, buf);
  3849. ast_cli(a->fd, " %s Debug:%d%s\n", buf, misdn_debug[port], misdn_debug_only[port] ? "(only)" : "");
  3850. }
  3851. return CLI_SUCCESS;
  3852. }
  3853. static char *handle_cli_misdn_show_ports_stats(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3854. {
  3855. int port;
  3856. switch (cmd) {
  3857. case CLI_INIT:
  3858. e->command = "misdn show ports stats";
  3859. e->usage =
  3860. "Usage: misdn show ports stats\n"
  3861. " Show mISDNs channel's call statistics per port.\n";
  3862. return NULL;
  3863. case CLI_GENERATE:
  3864. return NULL;
  3865. }
  3866. if (a->argc != 4) {
  3867. return CLI_SHOWUSAGE;
  3868. }
  3869. ast_cli(a->fd, "Port\tin_calls\tout_calls\n");
  3870. for (port = misdn_cfg_get_next_port(0); port > 0;
  3871. port = misdn_cfg_get_next_port(port)) {
  3872. ast_cli(a->fd, "%d\t%d\t\t%d\n", port, misdn_in_calls[port], misdn_out_calls[port]);
  3873. }
  3874. ast_cli(a->fd, "\n");
  3875. return CLI_SUCCESS;
  3876. }
  3877. static char *handle_cli_misdn_show_port(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3878. {
  3879. int port;
  3880. char buf[128];
  3881. switch (cmd) {
  3882. case CLI_INIT:
  3883. e->command = "misdn show port";
  3884. e->usage =
  3885. "Usage: misdn show port <port>\n"
  3886. " Show detailed information for given port.\n";
  3887. return NULL;
  3888. case CLI_GENERATE:
  3889. return NULL;
  3890. }
  3891. if (a->argc != 4) {
  3892. return CLI_SHOWUSAGE;
  3893. }
  3894. port = atoi(a->argv[3]);
  3895. ast_cli(a->fd, "BEGIN STACK_LIST:\n");
  3896. get_show_stack_details(port, buf);
  3897. ast_cli(a->fd, " %s Debug:%d%s\n", buf, misdn_debug[port], misdn_debug_only[port] ? "(only)" : "");
  3898. return CLI_SUCCESS;
  3899. }
  3900. #if defined(AST_MISDN_ENHANCEMENTS) && defined(CCBS_TEST_MESSAGES)
  3901. static const struct FacParm Fac_Msgs[] = {
  3902. /* *INDENT-OFF* */
  3903. [0].Function = Fac_ERROR,
  3904. [0].u.ERROR.invokeId = 8,
  3905. [0].u.ERROR.errorValue = FacError_CCBS_AlreadyAccepted,
  3906. [1].Function = Fac_RESULT,
  3907. [1].u.RESULT.InvokeID = 9,
  3908. [2].Function = Fac_REJECT,
  3909. [2].u.REJECT.Code = FacReject_Gen_BadlyStructuredComponent,
  3910. [3].Function = Fac_REJECT,
  3911. [3].u.REJECT.InvokeIDPresent = 1,
  3912. [3].u.REJECT.InvokeID = 10,
  3913. [3].u.REJECT.Code = FacReject_Inv_InitiatorReleasing,
  3914. [4].Function = Fac_REJECT,
  3915. [4].u.REJECT.InvokeIDPresent = 1,
  3916. [4].u.REJECT.InvokeID = 11,
  3917. [4].u.REJECT.Code = FacReject_Res_MistypedResult,
  3918. [5].Function = Fac_REJECT,
  3919. [5].u.REJECT.InvokeIDPresent = 1,
  3920. [5].u.REJECT.InvokeID = 12,
  3921. [5].u.REJECT.Code = FacReject_Err_ErrorResponseUnexpected,
  3922. [6].Function = Fac_StatusRequest,
  3923. [6].u.StatusRequest.InvokeID = 13,
  3924. [6].u.StatusRequest.ComponentType = FacComponent_Invoke,
  3925. [6].u.StatusRequest.Component.Invoke.Q931ie.Bc.Length = 2,
  3926. [6].u.StatusRequest.Component.Invoke.Q931ie.Bc.Contents = "AB",
  3927. [6].u.StatusRequest.Component.Invoke.Q931ie.Llc.Length = 3,
  3928. [6].u.StatusRequest.Component.Invoke.Q931ie.Llc.Contents = "CDE",
  3929. [6].u.StatusRequest.Component.Invoke.Q931ie.Hlc.Length = 4,
  3930. [6].u.StatusRequest.Component.Invoke.Q931ie.Hlc.Contents = "FGHI",
  3931. [6].u.StatusRequest.Component.Invoke.CompatibilityMode = 1,
  3932. [7].Function = Fac_StatusRequest,
  3933. [7].u.StatusRequest.InvokeID = 14,
  3934. [7].u.StatusRequest.ComponentType = FacComponent_Result,
  3935. [7].u.StatusRequest.Component.Result.Status = 2,
  3936. [8].Function = Fac_CallInfoRetain,
  3937. [8].u.CallInfoRetain.InvokeID = 15,
  3938. [8].u.CallInfoRetain.CallLinkageID = 115,
  3939. [9].Function = Fac_EraseCallLinkageID,
  3940. [9].u.EraseCallLinkageID.InvokeID = 16,
  3941. [9].u.EraseCallLinkageID.CallLinkageID = 105,
  3942. [10].Function = Fac_CCBSDeactivate,
  3943. [10].u.CCBSDeactivate.InvokeID = 17,
  3944. [10].u.CCBSDeactivate.ComponentType = FacComponent_Invoke,
  3945. [10].u.CCBSDeactivate.Component.Invoke.CCBSReference = 2,
  3946. [11].Function = Fac_CCBSDeactivate,
  3947. [11].u.CCBSDeactivate.InvokeID = 18,
  3948. [11].u.CCBSDeactivate.ComponentType = FacComponent_Result,
  3949. [12].Function = Fac_CCBSErase,
  3950. [12].u.CCBSErase.InvokeID = 19,
  3951. [12].u.CCBSErase.Q931ie.Bc.Length = 2,
  3952. [12].u.CCBSErase.Q931ie.Bc.Contents = "JK",
  3953. [12].u.CCBSErase.AddressOfB.Party.Type = 0,
  3954. [12].u.CCBSErase.AddressOfB.Party.LengthOfNumber = 5,
  3955. [12].u.CCBSErase.AddressOfB.Party.Number = "33403",
  3956. [12].u.CCBSErase.AddressOfB.Subaddress.Type = 0,
  3957. [12].u.CCBSErase.AddressOfB.Subaddress.Length = 4,
  3958. [12].u.CCBSErase.AddressOfB.Subaddress.u.UserSpecified.Information = "3748",
  3959. [12].u.CCBSErase.RecallMode = 1,
  3960. [12].u.CCBSErase.CCBSReference = 102,
  3961. [12].u.CCBSErase.Reason = 3,
  3962. [13].Function = Fac_CCBSErase,
  3963. [13].u.CCBSErase.InvokeID = 20,
  3964. [13].u.CCBSErase.Q931ie.Bc.Length = 2,
  3965. [13].u.CCBSErase.Q931ie.Bc.Contents = "JK",
  3966. [13].u.CCBSErase.AddressOfB.Party.Type = 1,
  3967. [13].u.CCBSErase.AddressOfB.Party.LengthOfNumber = 11,
  3968. [13].u.CCBSErase.AddressOfB.Party.TypeOfNumber = 1,
  3969. [13].u.CCBSErase.AddressOfB.Party.Number = "18003020102",
  3970. [13].u.CCBSErase.AddressOfB.Subaddress.Type = 0,
  3971. [13].u.CCBSErase.AddressOfB.Subaddress.Length = 4,
  3972. [13].u.CCBSErase.AddressOfB.Subaddress.u.UserSpecified.OddCountPresent = 1,
  3973. [13].u.CCBSErase.AddressOfB.Subaddress.u.UserSpecified.OddCount = 1,
  3974. [13].u.CCBSErase.AddressOfB.Subaddress.u.UserSpecified.Information = "3748",
  3975. [13].u.CCBSErase.RecallMode = 1,
  3976. [13].u.CCBSErase.CCBSReference = 102,
  3977. [13].u.CCBSErase.Reason = 3,
  3978. [14].Function = Fac_CCBSErase,
  3979. [14].u.CCBSErase.InvokeID = 21,
  3980. [14].u.CCBSErase.Q931ie.Bc.Length = 2,
  3981. [14].u.CCBSErase.Q931ie.Bc.Contents = "JK",
  3982. [14].u.CCBSErase.AddressOfB.Party.Type = 2,
  3983. [14].u.CCBSErase.AddressOfB.Party.LengthOfNumber = 4,
  3984. [14].u.CCBSErase.AddressOfB.Party.Number = "1803",
  3985. [14].u.CCBSErase.AddressOfB.Subaddress.Type = 1,
  3986. [14].u.CCBSErase.AddressOfB.Subaddress.Length = 4,
  3987. [14].u.CCBSErase.AddressOfB.Subaddress.u.Nsap = "6492",
  3988. [14].u.CCBSErase.RecallMode = 1,
  3989. [14].u.CCBSErase.CCBSReference = 102,
  3990. [14].u.CCBSErase.Reason = 3,
  3991. [15].Function = Fac_CCBSErase,
  3992. [15].u.CCBSErase.InvokeID = 22,
  3993. [15].u.CCBSErase.Q931ie.Bc.Length = 2,
  3994. [15].u.CCBSErase.Q931ie.Bc.Contents = "JK",
  3995. [15].u.CCBSErase.AddressOfB.Party.Type = 3,
  3996. [15].u.CCBSErase.AddressOfB.Party.LengthOfNumber = 4,
  3997. [15].u.CCBSErase.AddressOfB.Party.Number = "1803",
  3998. [15].u.CCBSErase.RecallMode = 1,
  3999. [15].u.CCBSErase.CCBSReference = 102,
  4000. [15].u.CCBSErase.Reason = 3,
  4001. [16].Function = Fac_CCBSErase,
  4002. [16].u.CCBSErase.InvokeID = 23,
  4003. [16].u.CCBSErase.Q931ie.Bc.Length = 2,
  4004. [16].u.CCBSErase.Q931ie.Bc.Contents = "JK",
  4005. [16].u.CCBSErase.AddressOfB.Party.Type = 4,
  4006. [16].u.CCBSErase.AddressOfB.Party.LengthOfNumber = 4,
  4007. [16].u.CCBSErase.AddressOfB.Party.Number = "1803",
  4008. [16].u.CCBSErase.RecallMode = 1,
  4009. [16].u.CCBSErase.CCBSReference = 102,
  4010. [16].u.CCBSErase.Reason = 3,
  4011. [17].Function = Fac_CCBSErase,
  4012. [17].u.CCBSErase.InvokeID = 24,
  4013. [17].u.CCBSErase.Q931ie.Bc.Length = 2,
  4014. [17].u.CCBSErase.Q931ie.Bc.Contents = "JK",
  4015. [17].u.CCBSErase.AddressOfB.Party.Type = 5,
  4016. [17].u.CCBSErase.AddressOfB.Party.LengthOfNumber = 11,
  4017. [17].u.CCBSErase.AddressOfB.Party.TypeOfNumber = 4,
  4018. [17].u.CCBSErase.AddressOfB.Party.Number = "18003020102",
  4019. [17].u.CCBSErase.RecallMode = 1,
  4020. [17].u.CCBSErase.CCBSReference = 102,
  4021. [17].u.CCBSErase.Reason = 3,
  4022. [18].Function = Fac_CCBSErase,
  4023. [18].u.CCBSErase.InvokeID = 25,
  4024. [18].u.CCBSErase.Q931ie.Bc.Length = 2,
  4025. [18].u.CCBSErase.Q931ie.Bc.Contents = "JK",
  4026. [18].u.CCBSErase.AddressOfB.Party.Type = 8,
  4027. [18].u.CCBSErase.AddressOfB.Party.LengthOfNumber = 4,
  4028. [18].u.CCBSErase.AddressOfB.Party.Number = "1803",
  4029. [18].u.CCBSErase.RecallMode = 1,
  4030. [18].u.CCBSErase.CCBSReference = 102,
  4031. [18].u.CCBSErase.Reason = 3,
  4032. [19].Function = Fac_CCBSRemoteUserFree,
  4033. [19].u.CCBSRemoteUserFree.InvokeID = 26,
  4034. [19].u.CCBSRemoteUserFree.Q931ie.Bc.Length = 2,
  4035. [19].u.CCBSRemoteUserFree.Q931ie.Bc.Contents = "JK",
  4036. [19].u.CCBSRemoteUserFree.AddressOfB.Party.Type = 8,
  4037. [19].u.CCBSRemoteUserFree.AddressOfB.Party.LengthOfNumber = 4,
  4038. [19].u.CCBSRemoteUserFree.AddressOfB.Party.Number = "1803",
  4039. [19].u.CCBSRemoteUserFree.RecallMode = 1,
  4040. [19].u.CCBSRemoteUserFree.CCBSReference = 102,
  4041. [20].Function = Fac_CCBSCall,
  4042. [20].u.CCBSCall.InvokeID = 27,
  4043. [20].u.CCBSCall.CCBSReference = 115,
  4044. [21].Function = Fac_CCBSStatusRequest,
  4045. [21].u.CCBSStatusRequest.InvokeID = 28,
  4046. [21].u.CCBSStatusRequest.ComponentType = FacComponent_Invoke,
  4047. [21].u.CCBSStatusRequest.Component.Invoke.Q931ie.Bc.Length = 2,
  4048. [21].u.CCBSStatusRequest.Component.Invoke.Q931ie.Bc.Contents = "JK",
  4049. [21].u.CCBSStatusRequest.Component.Invoke.RecallMode = 1,
  4050. [21].u.CCBSStatusRequest.Component.Invoke.CCBSReference = 102,
  4051. [22].Function = Fac_CCBSStatusRequest,
  4052. [22].u.CCBSStatusRequest.InvokeID = 29,
  4053. [22].u.CCBSStatusRequest.ComponentType = FacComponent_Result,
  4054. [22].u.CCBSStatusRequest.Component.Result.Free = 1,
  4055. [23].Function = Fac_CCBSBFree,
  4056. [23].u.CCBSBFree.InvokeID = 30,
  4057. [23].u.CCBSBFree.Q931ie.Bc.Length = 2,
  4058. [23].u.CCBSBFree.Q931ie.Bc.Contents = "JK",
  4059. [23].u.CCBSBFree.AddressOfB.Party.Type = 8,
  4060. [23].u.CCBSBFree.AddressOfB.Party.LengthOfNumber = 4,
  4061. [23].u.CCBSBFree.AddressOfB.Party.Number = "1803",
  4062. [23].u.CCBSBFree.RecallMode = 1,
  4063. [23].u.CCBSBFree.CCBSReference = 14,
  4064. [24].Function = Fac_CCBSStopAlerting,
  4065. [24].u.CCBSStopAlerting.InvokeID = 31,
  4066. [24].u.CCBSStopAlerting.CCBSReference = 37,
  4067. [25].Function = Fac_CCBSRequest,
  4068. [25].u.CCBSRequest.InvokeID = 32,
  4069. [25].u.CCBSRequest.ComponentType = FacComponent_Invoke,
  4070. [25].u.CCBSRequest.Component.Invoke.CallLinkageID = 57,
  4071. [26].Function = Fac_CCBSRequest,
  4072. [26].u.CCBSRequest.InvokeID = 33,
  4073. [26].u.CCBSRequest.ComponentType = FacComponent_Result,
  4074. [26].u.CCBSRequest.Component.Result.RecallMode = 1,
  4075. [26].u.CCBSRequest.Component.Result.CCBSReference = 102,
  4076. [27].Function = Fac_CCBSInterrogate,
  4077. [27].u.CCBSInterrogate.InvokeID = 34,
  4078. [27].u.CCBSInterrogate.ComponentType = FacComponent_Invoke,
  4079. [27].u.CCBSInterrogate.Component.Invoke.AParty.Type = 8,
  4080. [27].u.CCBSInterrogate.Component.Invoke.AParty.LengthOfNumber = 4,
  4081. [27].u.CCBSInterrogate.Component.Invoke.AParty.Number = "1803",
  4082. [27].u.CCBSInterrogate.Component.Invoke.CCBSReferencePresent = 1,
  4083. [27].u.CCBSInterrogate.Component.Invoke.CCBSReference = 76,
  4084. [28].Function = Fac_CCBSInterrogate,
  4085. [28].u.CCBSInterrogate.InvokeID = 35,
  4086. [28].u.CCBSInterrogate.ComponentType = FacComponent_Invoke,
  4087. [28].u.CCBSInterrogate.Component.Invoke.AParty.Type = 8,
  4088. [28].u.CCBSInterrogate.Component.Invoke.AParty.LengthOfNumber = 4,
  4089. [28].u.CCBSInterrogate.Component.Invoke.AParty.Number = "1803",
  4090. [29].Function = Fac_CCBSInterrogate,
  4091. [29].u.CCBSInterrogate.InvokeID = 36,
  4092. [29].u.CCBSInterrogate.ComponentType = FacComponent_Invoke,
  4093. [29].u.CCBSInterrogate.Component.Invoke.CCBSReferencePresent = 1,
  4094. [29].u.CCBSInterrogate.Component.Invoke.CCBSReference = 76,
  4095. [30].Function = Fac_CCBSInterrogate,
  4096. [30].u.CCBSInterrogate.InvokeID = 37,
  4097. [30].u.CCBSInterrogate.ComponentType = FacComponent_Invoke,
  4098. [31].Function = Fac_CCBSInterrogate,
  4099. [31].u.CCBSInterrogate.InvokeID = 38,
  4100. [31].u.CCBSInterrogate.ComponentType = FacComponent_Result,
  4101. [31].u.CCBSInterrogate.Component.Result.RecallMode = 1,
  4102. [32].Function = Fac_CCBSInterrogate,
  4103. [32].u.CCBSInterrogate.InvokeID = 39,
  4104. [32].u.CCBSInterrogate.ComponentType = FacComponent_Result,
  4105. [32].u.CCBSInterrogate.Component.Result.RecallMode = 1,
  4106. [32].u.CCBSInterrogate.Component.Result.NumRecords = 1,
  4107. [32].u.CCBSInterrogate.Component.Result.CallDetails[0].CCBSReference = 12,
  4108. [32].u.CCBSInterrogate.Component.Result.CallDetails[0].Q931ie.Bc.Length = 2,
  4109. [32].u.CCBSInterrogate.Component.Result.CallDetails[0].Q931ie.Bc.Contents = "JK",
  4110. [32].u.CCBSInterrogate.Component.Result.CallDetails[0].AddressOfB.Party.Type = 8,
  4111. [32].u.CCBSInterrogate.Component.Result.CallDetails[0].AddressOfB.Party.LengthOfNumber = 4,
  4112. [32].u.CCBSInterrogate.Component.Result.CallDetails[0].AddressOfB.Party.Number = "1803",
  4113. [32].u.CCBSInterrogate.Component.Result.CallDetails[0].SubaddressOfA.Type = 1,
  4114. [32].u.CCBSInterrogate.Component.Result.CallDetails[0].SubaddressOfA.Length = 4,
  4115. [32].u.CCBSInterrogate.Component.Result.CallDetails[0].SubaddressOfA.u.Nsap = "6492",
  4116. [33].Function = Fac_CCBSInterrogate,
  4117. [33].u.CCBSInterrogate.InvokeID = 40,
  4118. [33].u.CCBSInterrogate.ComponentType = FacComponent_Result,
  4119. [33].u.CCBSInterrogate.Component.Result.RecallMode = 1,
  4120. [33].u.CCBSInterrogate.Component.Result.NumRecords = 2,
  4121. [33].u.CCBSInterrogate.Component.Result.CallDetails[0].CCBSReference = 12,
  4122. [33].u.CCBSInterrogate.Component.Result.CallDetails[0].Q931ie.Bc.Length = 2,
  4123. [33].u.CCBSInterrogate.Component.Result.CallDetails[0].Q931ie.Bc.Contents = "JK",
  4124. [33].u.CCBSInterrogate.Component.Result.CallDetails[0].AddressOfB.Party.Type = 8,
  4125. [33].u.CCBSInterrogate.Component.Result.CallDetails[0].AddressOfB.Party.LengthOfNumber = 4,
  4126. [33].u.CCBSInterrogate.Component.Result.CallDetails[0].AddressOfB.Party.Number = "1803",
  4127. [33].u.CCBSInterrogate.Component.Result.CallDetails[1].CCBSReference = 102,
  4128. [33].u.CCBSInterrogate.Component.Result.CallDetails[1].Q931ie.Bc.Length = 2,
  4129. [33].u.CCBSInterrogate.Component.Result.CallDetails[1].Q931ie.Bc.Contents = "LM",
  4130. [33].u.CCBSInterrogate.Component.Result.CallDetails[1].AddressOfB.Party.Type = 8,
  4131. [33].u.CCBSInterrogate.Component.Result.CallDetails[1].AddressOfB.Party.LengthOfNumber = 4,
  4132. [33].u.CCBSInterrogate.Component.Result.CallDetails[1].AddressOfB.Party.Number = "6229",
  4133. [33].u.CCBSInterrogate.Component.Result.CallDetails[1].AddressOfB.Subaddress.Type = 1,
  4134. [33].u.CCBSInterrogate.Component.Result.CallDetails[1].AddressOfB.Subaddress.Length = 4,
  4135. [33].u.CCBSInterrogate.Component.Result.CallDetails[1].AddressOfB.Subaddress.u.Nsap = "8592",
  4136. [33].u.CCBSInterrogate.Component.Result.CallDetails[1].SubaddressOfA.Type = 1,
  4137. [33].u.CCBSInterrogate.Component.Result.CallDetails[1].SubaddressOfA.Length = 4,
  4138. [33].u.CCBSInterrogate.Component.Result.CallDetails[1].SubaddressOfA.u.Nsap = "6492",
  4139. [34].Function = Fac_CCNRRequest,
  4140. [34].u.CCNRRequest.InvokeID = 512,
  4141. [34].u.CCNRRequest.ComponentType = FacComponent_Invoke,
  4142. [34].u.CCNRRequest.Component.Invoke.CallLinkageID = 57,
  4143. [35].Function = Fac_CCNRRequest,
  4144. [35].u.CCNRRequest.InvokeID = 150,
  4145. [35].u.CCNRRequest.ComponentType = FacComponent_Result,
  4146. [35].u.CCNRRequest.Component.Result.RecallMode = 1,
  4147. [35].u.CCNRRequest.Component.Result.CCBSReference = 102,
  4148. [36].Function = Fac_CCNRInterrogate,
  4149. [36].u.CCNRInterrogate.InvokeID = -129,
  4150. [36].u.CCNRInterrogate.ComponentType = FacComponent_Invoke,
  4151. [37].Function = Fac_CCNRInterrogate,
  4152. [37].u.CCNRInterrogate.InvokeID = -3,
  4153. [37].u.CCNRInterrogate.ComponentType = FacComponent_Result,
  4154. [37].u.CCNRInterrogate.Component.Result.RecallMode = 1,
  4155. [38].Function = Fac_CCBS_T_Call,
  4156. [38].u.EctExecute.InvokeID = 41,
  4157. [39].Function = Fac_CCBS_T_Suspend,
  4158. [39].u.EctExecute.InvokeID = 42,
  4159. [40].Function = Fac_CCBS_T_Resume,
  4160. [40].u.EctExecute.InvokeID = 43,
  4161. [41].Function = Fac_CCBS_T_RemoteUserFree,
  4162. [41].u.EctExecute.InvokeID = 44,
  4163. [42].Function = Fac_CCBS_T_Available,
  4164. [42].u.EctExecute.InvokeID = 45,
  4165. [43].Function = Fac_CCBS_T_Request,
  4166. [43].u.CCBS_T_Request.InvokeID = 46,
  4167. [43].u.CCBS_T_Request.ComponentType = FacComponent_Invoke,
  4168. [43].u.CCBS_T_Request.Component.Invoke.Destination.Party.Type = 8,
  4169. [43].u.CCBS_T_Request.Component.Invoke.Destination.Party.LengthOfNumber = 4,
  4170. [43].u.CCBS_T_Request.Component.Invoke.Destination.Party.Number = "6229",
  4171. [43].u.CCBS_T_Request.Component.Invoke.Q931ie.Bc.Length = 2,
  4172. [43].u.CCBS_T_Request.Component.Invoke.Q931ie.Bc.Contents = "LM",
  4173. [43].u.CCBS_T_Request.Component.Invoke.RetentionSupported = 1,
  4174. [43].u.CCBS_T_Request.Component.Invoke.PresentationAllowedIndicatorPresent = 1,
  4175. [43].u.CCBS_T_Request.Component.Invoke.PresentationAllowedIndicator = 1,
  4176. [43].u.CCBS_T_Request.Component.Invoke.Originating.Party.Type = 8,
  4177. [43].u.CCBS_T_Request.Component.Invoke.Originating.Party.LengthOfNumber = 4,
  4178. [43].u.CCBS_T_Request.Component.Invoke.Originating.Party.Number = "9864",
  4179. [44].Function = Fac_CCBS_T_Request,
  4180. [44].u.CCBS_T_Request.InvokeID = 47,
  4181. [44].u.CCBS_T_Request.ComponentType = FacComponent_Invoke,
  4182. [44].u.CCBS_T_Request.Component.Invoke.Destination.Party.Type = 8,
  4183. [44].u.CCBS_T_Request.Component.Invoke.Destination.Party.LengthOfNumber = 4,
  4184. [44].u.CCBS_T_Request.Component.Invoke.Destination.Party.Number = "6229",
  4185. [44].u.CCBS_T_Request.Component.Invoke.Q931ie.Bc.Length = 2,
  4186. [44].u.CCBS_T_Request.Component.Invoke.Q931ie.Bc.Contents = "LM",
  4187. [44].u.CCBS_T_Request.Component.Invoke.PresentationAllowedIndicatorPresent = 1,
  4188. [44].u.CCBS_T_Request.Component.Invoke.PresentationAllowedIndicator = 1,
  4189. [44].u.CCBS_T_Request.Component.Invoke.Originating.Party.Type = 8,
  4190. [44].u.CCBS_T_Request.Component.Invoke.Originating.Party.LengthOfNumber = 4,
  4191. [44].u.CCBS_T_Request.Component.Invoke.Originating.Party.Number = "9864",
  4192. [45].Function = Fac_CCBS_T_Request,
  4193. [45].u.CCBS_T_Request.InvokeID = 48,
  4194. [45].u.CCBS_T_Request.ComponentType = FacComponent_Invoke,
  4195. [45].u.CCBS_T_Request.Component.Invoke.Destination.Party.Type = 8,
  4196. [45].u.CCBS_T_Request.Component.Invoke.Destination.Party.LengthOfNumber = 4,
  4197. [45].u.CCBS_T_Request.Component.Invoke.Destination.Party.Number = "6229",
  4198. [45].u.CCBS_T_Request.Component.Invoke.Q931ie.Bc.Length = 2,
  4199. [45].u.CCBS_T_Request.Component.Invoke.Q931ie.Bc.Contents = "LM",
  4200. [45].u.CCBS_T_Request.Component.Invoke.Originating.Party.Type = 8,
  4201. [45].u.CCBS_T_Request.Component.Invoke.Originating.Party.LengthOfNumber = 4,
  4202. [45].u.CCBS_T_Request.Component.Invoke.Originating.Party.Number = "9864",
  4203. [46].Function = Fac_CCBS_T_Request,
  4204. [46].u.CCBS_T_Request.InvokeID = 49,
  4205. [46].u.CCBS_T_Request.ComponentType = FacComponent_Invoke,
  4206. [46].u.CCBS_T_Request.Component.Invoke.Destination.Party.Type = 8,
  4207. [46].u.CCBS_T_Request.Component.Invoke.Destination.Party.LengthOfNumber = 4,
  4208. [46].u.CCBS_T_Request.Component.Invoke.Destination.Party.Number = "6229",
  4209. [46].u.CCBS_T_Request.Component.Invoke.Q931ie.Bc.Length = 2,
  4210. [46].u.CCBS_T_Request.Component.Invoke.Q931ie.Bc.Contents = "LM",
  4211. [46].u.CCBS_T_Request.Component.Invoke.PresentationAllowedIndicatorPresent = 1,
  4212. [46].u.CCBS_T_Request.Component.Invoke.PresentationAllowedIndicator = 1,
  4213. [47].Function = Fac_CCBS_T_Request,
  4214. [47].u.CCBS_T_Request.InvokeID = 50,
  4215. [47].u.CCBS_T_Request.ComponentType = FacComponent_Invoke,
  4216. [47].u.CCBS_T_Request.Component.Invoke.Destination.Party.Type = 8,
  4217. [47].u.CCBS_T_Request.Component.Invoke.Destination.Party.LengthOfNumber = 4,
  4218. [47].u.CCBS_T_Request.Component.Invoke.Destination.Party.Number = "6229",
  4219. [47].u.CCBS_T_Request.Component.Invoke.Q931ie.Bc.Length = 2,
  4220. [47].u.CCBS_T_Request.Component.Invoke.Q931ie.Bc.Contents = "LM",
  4221. [48].Function = Fac_CCBS_T_Request,
  4222. [48].u.CCBS_T_Request.InvokeID = 51,
  4223. [48].u.CCBS_T_Request.ComponentType = FacComponent_Result,
  4224. [48].u.CCBS_T_Request.Component.Result.RetentionSupported = 1,
  4225. [49].Function = Fac_CCNR_T_Request,
  4226. [49].u.CCNR_T_Request.InvokeID = 52,
  4227. [49].u.CCNR_T_Request.ComponentType = FacComponent_Invoke,
  4228. [49].u.CCNR_T_Request.Component.Invoke.Destination.Party.Type = 8,
  4229. [49].u.CCNR_T_Request.Component.Invoke.Destination.Party.LengthOfNumber = 4,
  4230. [49].u.CCNR_T_Request.Component.Invoke.Destination.Party.Number = "6229",
  4231. [49].u.CCNR_T_Request.Component.Invoke.Q931ie.Bc.Length = 2,
  4232. [49].u.CCNR_T_Request.Component.Invoke.Q931ie.Bc.Contents = "LM",
  4233. [50].Function = Fac_CCNR_T_Request,
  4234. [50].u.CCNR_T_Request.InvokeID = 53,
  4235. [50].u.CCNR_T_Request.ComponentType = FacComponent_Result,
  4236. [50].u.CCNR_T_Request.Component.Result.RetentionSupported = 1,
  4237. [51].Function = Fac_EctExecute,
  4238. [51].u.EctExecute.InvokeID = 54,
  4239. [52].Function = Fac_ExplicitEctExecute,
  4240. [52].u.ExplicitEctExecute.InvokeID = 55,
  4241. [52].u.ExplicitEctExecute.LinkID = 23,
  4242. [53].Function = Fac_RequestSubaddress,
  4243. [53].u.RequestSubaddress.InvokeID = 56,
  4244. [54].Function = Fac_SubaddressTransfer,
  4245. [54].u.SubaddressTransfer.InvokeID = 57,
  4246. [54].u.SubaddressTransfer.Subaddress.Type = 1,
  4247. [54].u.SubaddressTransfer.Subaddress.Length = 4,
  4248. [54].u.SubaddressTransfer.Subaddress.u.Nsap = "6492",
  4249. [55].Function = Fac_EctLinkIdRequest,
  4250. [55].u.EctLinkIdRequest.InvokeID = 58,
  4251. [55].u.EctLinkIdRequest.ComponentType = FacComponent_Invoke,
  4252. [56].Function = Fac_EctLinkIdRequest,
  4253. [56].u.EctLinkIdRequest.InvokeID = 59,
  4254. [56].u.EctLinkIdRequest.ComponentType = FacComponent_Result,
  4255. [56].u.EctLinkIdRequest.Component.Result.LinkID = 76,
  4256. [57].Function = Fac_EctInform,
  4257. [57].u.EctInform.InvokeID = 60,
  4258. [57].u.EctInform.Status = 1,
  4259. [57].u.EctInform.RedirectionPresent = 1,
  4260. [57].u.EctInform.Redirection.Type = 0,
  4261. [57].u.EctInform.Redirection.Unscreened.Type = 8,
  4262. [57].u.EctInform.Redirection.Unscreened.LengthOfNumber = 4,
  4263. [57].u.EctInform.Redirection.Unscreened.Number = "6229",
  4264. [58].Function = Fac_EctInform,
  4265. [58].u.EctInform.InvokeID = 61,
  4266. [58].u.EctInform.Status = 1,
  4267. [58].u.EctInform.RedirectionPresent = 1,
  4268. [58].u.EctInform.Redirection.Type = 1,
  4269. [59].Function = Fac_EctInform,
  4270. [59].u.EctInform.InvokeID = 62,
  4271. [59].u.EctInform.Status = 1,
  4272. [59].u.EctInform.RedirectionPresent = 1,
  4273. [59].u.EctInform.Redirection.Type = 2,
  4274. [60].Function = Fac_EctInform,
  4275. [60].u.EctInform.InvokeID = 63,
  4276. [60].u.EctInform.Status = 1,
  4277. [60].u.EctInform.RedirectionPresent = 1,
  4278. [60].u.EctInform.Redirection.Type = 3,
  4279. [60].u.EctInform.Redirection.Unscreened.Type = 8,
  4280. [60].u.EctInform.Redirection.Unscreened.LengthOfNumber = 4,
  4281. [60].u.EctInform.Redirection.Unscreened.Number = "3340",
  4282. [61].Function = Fac_EctInform,
  4283. [61].u.EctInform.InvokeID = 64,
  4284. [61].u.EctInform.Status = 1,
  4285. [61].u.EctInform.RedirectionPresent = 0,
  4286. [62].Function = Fac_EctLoopTest,
  4287. [62].u.EctLoopTest.InvokeID = 65,
  4288. [62].u.EctLoopTest.ComponentType = FacComponent_Invoke,
  4289. [62].u.EctLoopTest.Component.Invoke.CallTransferID = 7,
  4290. [63].Function = Fac_EctLoopTest,
  4291. [63].u.EctLoopTest.InvokeID = 66,
  4292. [63].u.EctLoopTest.ComponentType = FacComponent_Result,
  4293. [63].u.EctLoopTest.Component.Result.LoopResult = 2,
  4294. [64].Function = Fac_ActivationDiversion,
  4295. [64].u.ActivationDiversion.InvokeID = 67,
  4296. [64].u.ActivationDiversion.ComponentType = FacComponent_Invoke,
  4297. [64].u.ActivationDiversion.Component.Invoke.Procedure = 2,
  4298. [64].u.ActivationDiversion.Component.Invoke.BasicService = 3,
  4299. [64].u.ActivationDiversion.Component.Invoke.ForwardedTo.Party.Type = 4,
  4300. [64].u.ActivationDiversion.Component.Invoke.ForwardedTo.Party.LengthOfNumber = 4,
  4301. [64].u.ActivationDiversion.Component.Invoke.ForwardedTo.Party.Number = "1803",
  4302. [64].u.ActivationDiversion.Component.Invoke.ServedUser.Type = 4,
  4303. [64].u.ActivationDiversion.Component.Invoke.ServedUser.LengthOfNumber = 4,
  4304. [64].u.ActivationDiversion.Component.Invoke.ServedUser.Number = "5398",
  4305. [65].Function = Fac_ActivationDiversion,
  4306. [65].u.ActivationDiversion.InvokeID = 68,
  4307. [65].u.ActivationDiversion.ComponentType = FacComponent_Invoke,
  4308. [65].u.ActivationDiversion.Component.Invoke.Procedure = 1,
  4309. [65].u.ActivationDiversion.Component.Invoke.BasicService = 5,
  4310. [65].u.ActivationDiversion.Component.Invoke.ForwardedTo.Party.Type = 4,
  4311. [65].u.ActivationDiversion.Component.Invoke.ForwardedTo.Party.LengthOfNumber = 4,
  4312. [65].u.ActivationDiversion.Component.Invoke.ForwardedTo.Party.Number = "1803",
  4313. [66].Function = Fac_ActivationDiversion,
  4314. [66].u.ActivationDiversion.InvokeID = 69,
  4315. [66].u.ActivationDiversion.ComponentType = FacComponent_Result,
  4316. [67].Function = Fac_DeactivationDiversion,
  4317. [67].u.DeactivationDiversion.InvokeID = 70,
  4318. [67].u.DeactivationDiversion.ComponentType = FacComponent_Invoke,
  4319. [67].u.DeactivationDiversion.Component.Invoke.Procedure = 1,
  4320. [67].u.DeactivationDiversion.Component.Invoke.BasicService = 5,
  4321. [68].Function = Fac_DeactivationDiversion,
  4322. [68].u.DeactivationDiversion.InvokeID = 71,
  4323. [68].u.DeactivationDiversion.ComponentType = FacComponent_Result,
  4324. [69].Function = Fac_ActivationStatusNotificationDiv,
  4325. [69].u.ActivationStatusNotificationDiv.InvokeID = 72,
  4326. [69].u.ActivationStatusNotificationDiv.Procedure = 1,
  4327. [69].u.ActivationStatusNotificationDiv.BasicService = 5,
  4328. [69].u.ActivationStatusNotificationDiv.ForwardedTo.Party.Type = 4,
  4329. [69].u.ActivationStatusNotificationDiv.ForwardedTo.Party.LengthOfNumber = 4,
  4330. [69].u.ActivationStatusNotificationDiv.ForwardedTo.Party.Number = "1803",
  4331. [70].Function = Fac_DeactivationStatusNotificationDiv,
  4332. [70].u.DeactivationStatusNotificationDiv.InvokeID = 73,
  4333. [70].u.DeactivationStatusNotificationDiv.Procedure = 1,
  4334. [70].u.DeactivationStatusNotificationDiv.BasicService = 5,
  4335. [71].Function = Fac_InterrogationDiversion,
  4336. [71].u.InterrogationDiversion.InvokeID = 74,
  4337. [71].u.InterrogationDiversion.ComponentType = FacComponent_Invoke,
  4338. [71].u.InterrogationDiversion.Component.Invoke.Procedure = 1,
  4339. [71].u.InterrogationDiversion.Component.Invoke.BasicService = 5,
  4340. [72].Function = Fac_InterrogationDiversion,
  4341. [72].u.InterrogationDiversion.InvokeID = 75,
  4342. [72].u.InterrogationDiversion.ComponentType = FacComponent_Invoke,
  4343. [72].u.InterrogationDiversion.Component.Invoke.Procedure = 1,
  4344. [73].Function = Fac_InterrogationDiversion,
  4345. [73].u.InterrogationDiversion.InvokeID = 76,
  4346. [73].u.InterrogationDiversion.ComponentType = FacComponent_Result,
  4347. [73].u.InterrogationDiversion.Component.Result.NumRecords = 2,
  4348. [73].u.InterrogationDiversion.Component.Result.List[0].Procedure = 2,
  4349. [73].u.InterrogationDiversion.Component.Result.List[0].BasicService = 5,
  4350. [73].u.InterrogationDiversion.Component.Result.List[0].ForwardedTo.Party.Type = 4,
  4351. [73].u.InterrogationDiversion.Component.Result.List[0].ForwardedTo.Party.LengthOfNumber = 4,
  4352. [73].u.InterrogationDiversion.Component.Result.List[0].ForwardedTo.Party.Number = "1803",
  4353. [73].u.InterrogationDiversion.Component.Result.List[1].Procedure = 1,
  4354. [73].u.InterrogationDiversion.Component.Result.List[1].BasicService = 3,
  4355. [73].u.InterrogationDiversion.Component.Result.List[1].ForwardedTo.Party.Type = 4,
  4356. [73].u.InterrogationDiversion.Component.Result.List[1].ForwardedTo.Party.LengthOfNumber = 4,
  4357. [73].u.InterrogationDiversion.Component.Result.List[1].ForwardedTo.Party.Number = "1903",
  4358. [73].u.InterrogationDiversion.Component.Result.List[1].ServedUser.Type = 4,
  4359. [73].u.InterrogationDiversion.Component.Result.List[1].ServedUser.LengthOfNumber = 4,
  4360. [73].u.InterrogationDiversion.Component.Result.List[1].ServedUser.Number = "5398",
  4361. [74].Function = Fac_DiversionInformation,
  4362. [74].u.DiversionInformation.InvokeID = 77,
  4363. [74].u.DiversionInformation.DiversionReason = 3,
  4364. [74].u.DiversionInformation.BasicService = 5,
  4365. [74].u.DiversionInformation.ServedUserSubaddress.Type = 1,
  4366. [74].u.DiversionInformation.ServedUserSubaddress.Length = 4,
  4367. [74].u.DiversionInformation.ServedUserSubaddress.u.Nsap = "6492",
  4368. [74].u.DiversionInformation.CallingAddressPresent = 1,
  4369. [74].u.DiversionInformation.CallingAddress.Type = 0,
  4370. [74].u.DiversionInformation.CallingAddress.Address.ScreeningIndicator = 3,
  4371. [74].u.DiversionInformation.CallingAddress.Address.Party.Type = 4,
  4372. [74].u.DiversionInformation.CallingAddress.Address.Party.LengthOfNumber = 4,
  4373. [74].u.DiversionInformation.CallingAddress.Address.Party.Number = "1803",
  4374. [74].u.DiversionInformation.OriginalCalledPresent = 1,
  4375. [74].u.DiversionInformation.OriginalCalled.Type = 1,
  4376. [74].u.DiversionInformation.LastDivertingPresent = 1,
  4377. [74].u.DiversionInformation.LastDiverting.Type = 2,
  4378. [74].u.DiversionInformation.LastDivertingReasonPresent = 1,
  4379. [74].u.DiversionInformation.LastDivertingReason = 3,
  4380. [74].u.DiversionInformation.UserInfo.Length = 5,
  4381. [74].u.DiversionInformation.UserInfo.Contents = "79828",
  4382. [75].Function = Fac_DiversionInformation,
  4383. [75].u.DiversionInformation.InvokeID = 78,
  4384. [75].u.DiversionInformation.DiversionReason = 3,
  4385. [75].u.DiversionInformation.BasicService = 5,
  4386. [75].u.DiversionInformation.CallingAddressPresent = 1,
  4387. [75].u.DiversionInformation.CallingAddress.Type = 1,
  4388. [75].u.DiversionInformation.OriginalCalledPresent = 1,
  4389. [75].u.DiversionInformation.OriginalCalled.Type = 2,
  4390. [75].u.DiversionInformation.LastDivertingPresent = 1,
  4391. [75].u.DiversionInformation.LastDiverting.Type = 1,
  4392. [76].Function = Fac_DiversionInformation,
  4393. [76].u.DiversionInformation.InvokeID = 79,
  4394. [76].u.DiversionInformation.DiversionReason = 2,
  4395. [76].u.DiversionInformation.BasicService = 3,
  4396. [76].u.DiversionInformation.CallingAddressPresent = 1,
  4397. [76].u.DiversionInformation.CallingAddress.Type = 2,
  4398. [77].Function = Fac_DiversionInformation,
  4399. [77].u.DiversionInformation.InvokeID = 80,
  4400. [77].u.DiversionInformation.DiversionReason = 3,
  4401. [77].u.DiversionInformation.BasicService = 5,
  4402. [77].u.DiversionInformation.CallingAddressPresent = 1,
  4403. [77].u.DiversionInformation.CallingAddress.Type = 3,
  4404. [77].u.DiversionInformation.CallingAddress.Address.ScreeningIndicator = 2,
  4405. [77].u.DiversionInformation.CallingAddress.Address.Party.Type = 4,
  4406. [77].u.DiversionInformation.CallingAddress.Address.Party.LengthOfNumber = 4,
  4407. [77].u.DiversionInformation.CallingAddress.Address.Party.Number = "1803",
  4408. [78].Function = Fac_DiversionInformation,
  4409. [78].u.DiversionInformation.InvokeID = 81,
  4410. [78].u.DiversionInformation.DiversionReason = 2,
  4411. [78].u.DiversionInformation.BasicService = 4,
  4412. [78].u.DiversionInformation.UserInfo.Length = 5,
  4413. [78].u.DiversionInformation.UserInfo.Contents = "79828",
  4414. [79].Function = Fac_DiversionInformation,
  4415. [79].u.DiversionInformation.InvokeID = 82,
  4416. [79].u.DiversionInformation.DiversionReason = 2,
  4417. [79].u.DiversionInformation.BasicService = 4,
  4418. [80].Function = Fac_CallDeflection,
  4419. [80].u.CallDeflection.InvokeID = 83,
  4420. [80].u.CallDeflection.ComponentType = FacComponent_Invoke,
  4421. [80].u.CallDeflection.Component.Invoke.Deflection.Party.Type = 4,
  4422. [80].u.CallDeflection.Component.Invoke.Deflection.Party.LengthOfNumber = 4,
  4423. [80].u.CallDeflection.Component.Invoke.Deflection.Party.Number = "1803",
  4424. [80].u.CallDeflection.Component.Invoke.PresentationAllowedToDivertedToUserPresent = 1,
  4425. [80].u.CallDeflection.Component.Invoke.PresentationAllowedToDivertedToUser = 1,
  4426. [81].Function = Fac_CallDeflection,
  4427. [81].u.CallDeflection.InvokeID = 84,
  4428. [81].u.CallDeflection.ComponentType = FacComponent_Invoke,
  4429. [81].u.CallDeflection.Component.Invoke.Deflection.Party.Type = 4,
  4430. [81].u.CallDeflection.Component.Invoke.Deflection.Party.LengthOfNumber = 4,
  4431. [81].u.CallDeflection.Component.Invoke.Deflection.Party.Number = "1803",
  4432. [81].u.CallDeflection.Component.Invoke.PresentationAllowedToDivertedToUserPresent = 1,
  4433. [81].u.CallDeflection.Component.Invoke.PresentationAllowedToDivertedToUser = 0,
  4434. [82].Function = Fac_CallDeflection,
  4435. [82].u.CallDeflection.InvokeID = 85,
  4436. [82].u.CallDeflection.ComponentType = FacComponent_Invoke,
  4437. [82].u.CallDeflection.Component.Invoke.Deflection.Party.Type = 4,
  4438. [82].u.CallDeflection.Component.Invoke.Deflection.Party.LengthOfNumber = 4,
  4439. [82].u.CallDeflection.Component.Invoke.Deflection.Party.Number = "1803",
  4440. [83].Function = Fac_CallDeflection,
  4441. [83].u.CallDeflection.InvokeID = 86,
  4442. [83].u.CallDeflection.ComponentType = FacComponent_Result,
  4443. [84].Function = Fac_CallRerouteing,
  4444. [84].u.CallRerouteing.InvokeID = 87,
  4445. [84].u.CallRerouteing.ComponentType = FacComponent_Invoke,
  4446. [84].u.CallRerouteing.Component.Invoke.ReroutingReason = 3,
  4447. [84].u.CallRerouteing.Component.Invoke.ReroutingCounter = 2,
  4448. [84].u.CallRerouteing.Component.Invoke.CalledAddress.Party.Type = 4,
  4449. [84].u.CallRerouteing.Component.Invoke.CalledAddress.Party.LengthOfNumber = 4,
  4450. [84].u.CallRerouteing.Component.Invoke.CalledAddress.Party.Number = "1803",
  4451. [84].u.CallRerouteing.Component.Invoke.Q931ie.Bc.Length = 2,
  4452. [84].u.CallRerouteing.Component.Invoke.Q931ie.Bc.Contents = "RT",
  4453. [84].u.CallRerouteing.Component.Invoke.Q931ie.Hlc.Length = 3,
  4454. [84].u.CallRerouteing.Component.Invoke.Q931ie.Hlc.Contents = "RTG",
  4455. [84].u.CallRerouteing.Component.Invoke.Q931ie.Llc.Length = 2,
  4456. [84].u.CallRerouteing.Component.Invoke.Q931ie.Llc.Contents = "MY",
  4457. [84].u.CallRerouteing.Component.Invoke.Q931ie.UserInfo.Length = 5,
  4458. [84].u.CallRerouteing.Component.Invoke.Q931ie.UserInfo.Contents = "YEHAW",
  4459. [84].u.CallRerouteing.Component.Invoke.LastRerouting.Type = 1,
  4460. [84].u.CallRerouteing.Component.Invoke.SubscriptionOption = 2,
  4461. [84].u.CallRerouteing.Component.Invoke.CallingPartySubaddress.Type = 1,
  4462. [84].u.CallRerouteing.Component.Invoke.CallingPartySubaddress.Length = 4,
  4463. [84].u.CallRerouteing.Component.Invoke.CallingPartySubaddress.u.Nsap = "6492",
  4464. [85].Function = Fac_CallRerouteing,
  4465. [85].u.CallRerouteing.InvokeID = 88,
  4466. [85].u.CallRerouteing.ComponentType = FacComponent_Invoke,
  4467. [85].u.CallRerouteing.Component.Invoke.ReroutingReason = 3,
  4468. [85].u.CallRerouteing.Component.Invoke.ReroutingCounter = 2,
  4469. [85].u.CallRerouteing.Component.Invoke.CalledAddress.Party.Type = 4,
  4470. [85].u.CallRerouteing.Component.Invoke.CalledAddress.Party.LengthOfNumber = 4,
  4471. [85].u.CallRerouteing.Component.Invoke.CalledAddress.Party.Number = "1803",
  4472. [85].u.CallRerouteing.Component.Invoke.Q931ie.Bc.Length = 2,
  4473. [85].u.CallRerouteing.Component.Invoke.Q931ie.Bc.Contents = "RT",
  4474. [85].u.CallRerouteing.Component.Invoke.LastRerouting.Type = 1,
  4475. [85].u.CallRerouteing.Component.Invoke.SubscriptionOption = 2,
  4476. [86].Function = Fac_CallRerouteing,
  4477. [86].u.CallRerouteing.InvokeID = 89,
  4478. [86].u.CallRerouteing.ComponentType = FacComponent_Invoke,
  4479. [86].u.CallRerouteing.Component.Invoke.ReroutingReason = 3,
  4480. [86].u.CallRerouteing.Component.Invoke.ReroutingCounter = 2,
  4481. [86].u.CallRerouteing.Component.Invoke.CalledAddress.Party.Type = 4,
  4482. [86].u.CallRerouteing.Component.Invoke.CalledAddress.Party.LengthOfNumber = 4,
  4483. [86].u.CallRerouteing.Component.Invoke.CalledAddress.Party.Number = "1803",
  4484. [86].u.CallRerouteing.Component.Invoke.Q931ie.Bc.Length = 2,
  4485. [86].u.CallRerouteing.Component.Invoke.Q931ie.Bc.Contents = "RT",
  4486. [86].u.CallRerouteing.Component.Invoke.LastRerouting.Type = 2,
  4487. [87].Function = Fac_CallRerouteing,
  4488. [87].u.CallRerouteing.InvokeID = 90,
  4489. [87].u.CallRerouteing.ComponentType = FacComponent_Result,
  4490. [88].Function = Fac_InterrogateServedUserNumbers,
  4491. [88].u.InterrogateServedUserNumbers.InvokeID = 91,
  4492. [88].u.InterrogateServedUserNumbers.ComponentType = FacComponent_Invoke,
  4493. [89].Function = Fac_InterrogateServedUserNumbers,
  4494. [89].u.InterrogateServedUserNumbers.InvokeID = 92,
  4495. [89].u.InterrogateServedUserNumbers.ComponentType = FacComponent_Result,
  4496. [89].u.InterrogateServedUserNumbers.Component.Result.NumRecords = 2,
  4497. [89].u.InterrogateServedUserNumbers.Component.Result.List[0].Type = 4,
  4498. [89].u.InterrogateServedUserNumbers.Component.Result.List[0].LengthOfNumber = 4,
  4499. [89].u.InterrogateServedUserNumbers.Component.Result.List[0].Number = "1803",
  4500. [89].u.InterrogateServedUserNumbers.Component.Result.List[1].Type = 4,
  4501. [89].u.InterrogateServedUserNumbers.Component.Result.List[1].LengthOfNumber = 4,
  4502. [89].u.InterrogateServedUserNumbers.Component.Result.List[1].Number = "5786",
  4503. [90].Function = Fac_DivertingLegInformation1,
  4504. [90].u.DivertingLegInformation1.InvokeID = 93,
  4505. [90].u.DivertingLegInformation1.DiversionReason = 4,
  4506. [90].u.DivertingLegInformation1.SubscriptionOption = 1,
  4507. [90].u.DivertingLegInformation1.DivertedToPresent = 1,
  4508. [90].u.DivertingLegInformation1.DivertedTo.Type = 2,
  4509. [91].Function = Fac_DivertingLegInformation1,
  4510. [91].u.DivertingLegInformation1.InvokeID = 94,
  4511. [91].u.DivertingLegInformation1.DiversionReason = 4,
  4512. [91].u.DivertingLegInformation1.SubscriptionOption = 1,
  4513. [92].Function = Fac_DivertingLegInformation2,
  4514. [92].u.DivertingLegInformation2.InvokeID = 95,
  4515. [92].u.DivertingLegInformation2.DiversionCounter = 3,
  4516. [92].u.DivertingLegInformation2.DiversionReason = 2,
  4517. [92].u.DivertingLegInformation2.DivertingPresent = 1,
  4518. [92].u.DivertingLegInformation2.Diverting.Type = 2,
  4519. [92].u.DivertingLegInformation2.OriginalCalledPresent = 1,
  4520. [92].u.DivertingLegInformation2.OriginalCalled.Type = 1,
  4521. [93].Function = Fac_DivertingLegInformation2,
  4522. [93].u.DivertingLegInformation2.InvokeID = 96,
  4523. [93].u.DivertingLegInformation2.DiversionCounter = 3,
  4524. [93].u.DivertingLegInformation2.DiversionReason = 2,
  4525. [93].u.DivertingLegInformation2.OriginalCalledPresent = 1,
  4526. [93].u.DivertingLegInformation2.OriginalCalled.Type = 1,
  4527. [94].Function = Fac_DivertingLegInformation2,
  4528. [94].u.DivertingLegInformation2.InvokeID = 97,
  4529. [94].u.DivertingLegInformation2.DiversionCounter = 1,
  4530. [94].u.DivertingLegInformation2.DiversionReason = 2,
  4531. [95].Function = Fac_DivertingLegInformation3,
  4532. [95].u.DivertingLegInformation3.InvokeID = 98,
  4533. [95].u.DivertingLegInformation3.PresentationAllowedIndicator = 1,
  4534. /* *INDENT-ON* */
  4535. };
  4536. #endif /* defined(AST_MISDN_ENHANCEMENTS) && defined(CCBS_TEST_MESSAGES) */
  4537. static char *handle_cli_misdn_send_facility(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  4538. {
  4539. const char *channame;
  4540. const char *nr;
  4541. struct chan_list *tmp;
  4542. int port;
  4543. const char *served_nr;
  4544. struct misdn_bchannel dummy, *bc=&dummy;
  4545. unsigned max_len;
  4546. switch (cmd) {
  4547. case CLI_INIT:
  4548. e->command = "misdn send facility";
  4549. e->usage = "Usage: misdn send facility <type> <channel|port> \"<args>\" \n"
  4550. "\t type is one of:\n"
  4551. "\t - calldeflect\n"
  4552. #if defined(AST_MISDN_ENHANCEMENTS)
  4553. "\t - callrerouting\n"
  4554. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  4555. "\t - CFActivate\n"
  4556. "\t - CFDeactivate\n";
  4557. return NULL;
  4558. case CLI_GENERATE:
  4559. return complete_ch(a);
  4560. }
  4561. if (a->argc < 5) {
  4562. return CLI_SHOWUSAGE;
  4563. }
  4564. if (strstr(a->argv[3], "calldeflect")) {
  4565. if (a->argc < 6) {
  4566. ast_verbose("calldeflect requires 1 arg: ToNumber\n\n");
  4567. return 0;
  4568. }
  4569. channame = a->argv[4];
  4570. nr = a->argv[5];
  4571. ast_verbose("Sending Calldeflection (%s) to %s\n", nr, channame);
  4572. tmp = get_chan_by_ast_name(channame);
  4573. if (!tmp) {
  4574. ast_verbose("Sending CD with nr %s to %s failed: Channel does not exist.\n", nr, channame);
  4575. return 0;
  4576. }
  4577. ao2_lock(tmp);
  4578. #if defined(AST_MISDN_ENHANCEMENTS)
  4579. max_len = sizeof(tmp->bc->fac_out.u.CallDeflection.Component.Invoke.Deflection.Party.Number) - 1;
  4580. if (max_len < strlen(nr)) {
  4581. ast_verbose("Sending CD with nr %s to %s failed: Number too long (up to %u digits are allowed).\n",
  4582. nr, channame, max_len);
  4583. ao2_unlock(tmp);
  4584. chan_list_unref(tmp, "Number too long");
  4585. return 0;
  4586. }
  4587. tmp->bc->fac_out.Function = Fac_CallDeflection;
  4588. tmp->bc->fac_out.u.CallDeflection.InvokeID = ++misdn_invoke_id;
  4589. tmp->bc->fac_out.u.CallDeflection.ComponentType = FacComponent_Invoke;
  4590. tmp->bc->fac_out.u.CallDeflection.Component.Invoke.PresentationAllowedToDivertedToUserPresent = 1;
  4591. tmp->bc->fac_out.u.CallDeflection.Component.Invoke.PresentationAllowedToDivertedToUser = 0;
  4592. tmp->bc->fac_out.u.CallDeflection.Component.Invoke.Deflection.Party.Type = 0;/* unknown */
  4593. tmp->bc->fac_out.u.CallDeflection.Component.Invoke.Deflection.Party.LengthOfNumber = strlen(nr);
  4594. strcpy((char *) tmp->bc->fac_out.u.CallDeflection.Component.Invoke.Deflection.Party.Number, nr);
  4595. tmp->bc->fac_out.u.CallDeflection.Component.Invoke.Deflection.Subaddress.Length = 0;
  4596. #else /* !defined(AST_MISDN_ENHANCEMENTS) */
  4597. max_len = sizeof(tmp->bc->fac_out.u.CDeflection.DeflectedToNumber) - 1;
  4598. if (max_len < strlen(nr)) {
  4599. ast_verbose("Sending CD with nr %s to %s failed: Number too long (up to %u digits are allowed).\n",
  4600. nr, channame, max_len);
  4601. ao2_unlock(tmp);
  4602. chan_list_unref(tmp, "Number too long");
  4603. return 0;
  4604. }
  4605. tmp->bc->fac_out.Function = Fac_CD;
  4606. tmp->bc->fac_out.u.CDeflection.PresentationAllowed = 0;
  4607. //tmp->bc->fac_out.u.CDeflection.DeflectedToSubaddress[0] = 0;
  4608. strcpy((char *) tmp->bc->fac_out.u.CDeflection.DeflectedToNumber, nr);
  4609. #endif /* !defined(AST_MISDN_ENHANCEMENTS) */
  4610. /* Send message */
  4611. print_facility(&tmp->bc->fac_out, tmp->bc);
  4612. ao2_unlock(tmp);
  4613. misdn_lib_send_event(tmp->bc, EVENT_FACILITY);
  4614. chan_list_unref(tmp, "Send facility complete");
  4615. #if defined(AST_MISDN_ENHANCEMENTS)
  4616. } else if (strstr(a->argv[3], "callrerouteing") || strstr(a->argv[3], "callrerouting")) {
  4617. if (a->argc < 6) {
  4618. ast_verbose("callrerouting requires 1 arg: ToNumber\n\n");
  4619. return 0;
  4620. }
  4621. channame = a->argv[4];
  4622. nr = a->argv[5];
  4623. ast_verbose("Sending Callrerouting (%s) to %s\n", nr, channame);
  4624. tmp = get_chan_by_ast_name(channame);
  4625. if (!tmp) {
  4626. ast_verbose("Sending Call Rerouting with nr %s to %s failed: Channel does not exist.\n", nr, channame);
  4627. return 0;
  4628. }
  4629. ao2_lock(tmp);
  4630. max_len = sizeof(tmp->bc->fac_out.u.CallRerouteing.Component.Invoke.CalledAddress.Party.Number) - 1;
  4631. if (max_len < strlen(nr)) {
  4632. ast_verbose("Sending Call Rerouting with nr %s to %s failed: Number too long (up to %u digits are allowed).\n",
  4633. nr, channame, max_len);
  4634. ao2_unlock(tmp);
  4635. chan_list_unref(tmp, "Number too long");
  4636. return 0;
  4637. }
  4638. tmp->bc->fac_out.Function = Fac_CallRerouteing;
  4639. tmp->bc->fac_out.u.CallRerouteing.InvokeID = ++misdn_invoke_id;
  4640. tmp->bc->fac_out.u.CallRerouteing.ComponentType = FacComponent_Invoke;
  4641. tmp->bc->fac_out.u.CallRerouteing.Component.Invoke.ReroutingReason = 0;/* unknown */
  4642. tmp->bc->fac_out.u.CallRerouteing.Component.Invoke.ReroutingCounter = 1;
  4643. tmp->bc->fac_out.u.CallRerouteing.Component.Invoke.CalledAddress.Party.Type = 0;/* unknown */
  4644. tmp->bc->fac_out.u.CallRerouteing.Component.Invoke.CalledAddress.Party.LengthOfNumber = strlen(nr);
  4645. strcpy((char *) tmp->bc->fac_out.u.CallRerouteing.Component.Invoke.CalledAddress.Party.Number, nr);
  4646. tmp->bc->fac_out.u.CallRerouteing.Component.Invoke.CalledAddress.Subaddress.Length = 0;
  4647. tmp->bc->fac_out.u.CallRerouteing.Component.Invoke.CallingPartySubaddress.Length = 0;
  4648. /* 0x90 0x90 0xa3 3.1 kHz audio, circuit mode, 64kbit/sec, level1/a-Law */
  4649. tmp->bc->fac_out.u.CallRerouteing.Component.Invoke.Q931ie.Bc.Length = 3;
  4650. tmp->bc->fac_out.u.CallRerouteing.Component.Invoke.Q931ie.Bc.Contents[0] = 0x90;
  4651. tmp->bc->fac_out.u.CallRerouteing.Component.Invoke.Q931ie.Bc.Contents[1] = 0x90;
  4652. tmp->bc->fac_out.u.CallRerouteing.Component.Invoke.Q931ie.Bc.Contents[2] = 0xa3;
  4653. tmp->bc->fac_out.u.CallRerouteing.Component.Invoke.Q931ie.Hlc.Length = 0;
  4654. tmp->bc->fac_out.u.CallRerouteing.Component.Invoke.Q931ie.Llc.Length = 0;
  4655. tmp->bc->fac_out.u.CallRerouteing.Component.Invoke.Q931ie.UserInfo.Length = 0;
  4656. tmp->bc->fac_out.u.CallRerouteing.Component.Invoke.LastRerouting.Type = 1;/* presentationRestricted */
  4657. tmp->bc->fac_out.u.CallRerouteing.Component.Invoke.SubscriptionOption = 0;/* no notification to caller */
  4658. /* Send message */
  4659. print_facility(&tmp->bc->fac_out, tmp->bc);
  4660. ao2_unlock(tmp);
  4661. misdn_lib_send_event(tmp->bc, EVENT_FACILITY);
  4662. chan_list_unref(tmp, "Send facility complete");
  4663. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  4664. } else if (strstr(a->argv[3], "CFActivate")) {
  4665. if (a->argc < 7) {
  4666. ast_verbose("CFActivate requires 2 args: 1.FromNumber, 2.ToNumber\n\n");
  4667. return 0;
  4668. }
  4669. port = atoi(a->argv[4]);
  4670. served_nr = a->argv[5];
  4671. nr = a->argv[6];
  4672. misdn_make_dummy(bc, port, 0, misdn_lib_port_is_nt(port), 0);
  4673. ast_verbose("Sending CFActivate Port:(%d) FromNr. (%s) to Nr. (%s)\n", port, served_nr, nr);
  4674. #if defined(AST_MISDN_ENHANCEMENTS)
  4675. bc->fac_out.Function = Fac_ActivationDiversion;
  4676. bc->fac_out.u.ActivationDiversion.InvokeID = ++misdn_invoke_id;
  4677. bc->fac_out.u.ActivationDiversion.ComponentType = FacComponent_Invoke;
  4678. bc->fac_out.u.ActivationDiversion.Component.Invoke.BasicService = 0;/* allServices */
  4679. bc->fac_out.u.ActivationDiversion.Component.Invoke.Procedure = 0;/* cfu (Call Forward Unconditional) */
  4680. ast_copy_string((char *) bc->fac_out.u.ActivationDiversion.Component.Invoke.ServedUser.Number,
  4681. served_nr, sizeof(bc->fac_out.u.ActivationDiversion.Component.Invoke.ServedUser.Number));
  4682. bc->fac_out.u.ActivationDiversion.Component.Invoke.ServedUser.LengthOfNumber =
  4683. strlen((char *) bc->fac_out.u.ActivationDiversion.Component.Invoke.ServedUser.Number);
  4684. bc->fac_out.u.ActivationDiversion.Component.Invoke.ServedUser.Type = 0;/* unknown */
  4685. ast_copy_string((char *) bc->fac_out.u.ActivationDiversion.Component.Invoke.ForwardedTo.Party.Number,
  4686. nr, sizeof(bc->fac_out.u.ActivationDiversion.Component.Invoke.ForwardedTo.Party.Number));
  4687. bc->fac_out.u.ActivationDiversion.Component.Invoke.ForwardedTo.Party.LengthOfNumber =
  4688. strlen((char *) bc->fac_out.u.ActivationDiversion.Component.Invoke.ForwardedTo.Party.Number);
  4689. bc->fac_out.u.ActivationDiversion.Component.Invoke.ForwardedTo.Party.Type = 0;/* unknown */
  4690. bc->fac_out.u.ActivationDiversion.Component.Invoke.ForwardedTo.Subaddress.Length = 0;
  4691. #else /* !defined(AST_MISDN_ENHANCEMENTS) */
  4692. bc->fac_out.Function = Fac_CFActivate;
  4693. bc->fac_out.u.CFActivate.BasicService = 0; /* All Services */
  4694. bc->fac_out.u.CFActivate.Procedure = 0; /* Unconditional */
  4695. ast_copy_string((char *) bc->fac_out.u.CFActivate.ServedUserNumber, served_nr, sizeof(bc->fac_out.u.CFActivate.ServedUserNumber));
  4696. ast_copy_string((char *) bc->fac_out.u.CFActivate.ForwardedToNumber, nr, sizeof(bc->fac_out.u.CFActivate.ForwardedToNumber));
  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. } else if (strstr(a->argv[3], "CFDeactivate")) {
  4702. if (a->argc < 6) {
  4703. ast_verbose("CFDeactivate requires 1 arg: FromNumber\n\n");
  4704. return 0;
  4705. }
  4706. port = atoi(a->argv[4]);
  4707. served_nr = a->argv[5];
  4708. misdn_make_dummy(bc, port, 0, misdn_lib_port_is_nt(port), 0);
  4709. ast_verbose("Sending CFDeactivate Port:(%d) FromNr. (%s)\n", port, served_nr);
  4710. #if defined(AST_MISDN_ENHANCEMENTS)
  4711. bc->fac_out.Function = Fac_DeactivationDiversion;
  4712. bc->fac_out.u.DeactivationDiversion.InvokeID = ++misdn_invoke_id;
  4713. bc->fac_out.u.DeactivationDiversion.ComponentType = FacComponent_Invoke;
  4714. bc->fac_out.u.DeactivationDiversion.Component.Invoke.BasicService = 0;/* allServices */
  4715. bc->fac_out.u.DeactivationDiversion.Component.Invoke.Procedure = 0;/* cfu (Call Forward Unconditional) */
  4716. ast_copy_string((char *) bc->fac_out.u.DeactivationDiversion.Component.Invoke.ServedUser.Number,
  4717. served_nr, sizeof(bc->fac_out.u.DeactivationDiversion.Component.Invoke.ServedUser.Number));
  4718. bc->fac_out.u.DeactivationDiversion.Component.Invoke.ServedUser.LengthOfNumber =
  4719. strlen((char *) bc->fac_out.u.DeactivationDiversion.Component.Invoke.ServedUser.Number);
  4720. bc->fac_out.u.DeactivationDiversion.Component.Invoke.ServedUser.Type = 0;/* unknown */
  4721. #else /* !defined(AST_MISDN_ENHANCEMENTS) */
  4722. bc->fac_out.Function = Fac_CFDeactivate;
  4723. bc->fac_out.u.CFDeactivate.BasicService = 0; /* All Services */
  4724. bc->fac_out.u.CFDeactivate.Procedure = 0; /* Unconditional */
  4725. ast_copy_string((char *) bc->fac_out.u.CFActivate.ServedUserNumber, served_nr, sizeof(bc->fac_out.u.CFActivate.ServedUserNumber));
  4726. #endif /* !defined(AST_MISDN_ENHANCEMENTS) */
  4727. /* Send message */
  4728. print_facility(&bc->fac_out, bc);
  4729. misdn_lib_send_event(bc, EVENT_FACILITY);
  4730. #if defined(AST_MISDN_ENHANCEMENTS) && defined(CCBS_TEST_MESSAGES)
  4731. } else if (strstr(a->argv[3], "test")) {
  4732. int msg_number;
  4733. if (a->argc < 5) {
  4734. ast_verbose("test (<port> [<msg#>]) | (<channel-name> <msg#>)\n\n");
  4735. return 0;
  4736. }
  4737. port = atoi(a->argv[4]);
  4738. channame = a->argv[4];
  4739. tmp = get_chan_by_ast_name(channame);
  4740. if (tmp) {
  4741. /* We are going to send this FACILITY message out on an existing connection */
  4742. msg_number = atoi(a->argv[5]);
  4743. if (msg_number < ARRAY_LEN(Fac_Msgs)) {
  4744. ao2_lock(tmp);
  4745. tmp->bc->fac_out = Fac_Msgs[msg_number];
  4746. /* Send message */
  4747. print_facility(&tmp->bc->fac_out, tmp->bc);
  4748. ao2_unlock(tmp);
  4749. misdn_lib_send_event(tmp->bc, EVENT_FACILITY);
  4750. } else {
  4751. ast_verbose("test <channel-name> <msg#>\n\n");
  4752. }
  4753. chan_list_unref(tmp, "Facility test done");
  4754. } else if (a->argc < 6) {
  4755. for (msg_number = 0; msg_number < ARRAY_LEN(Fac_Msgs); ++msg_number) {
  4756. misdn_make_dummy(bc, port, 0, misdn_lib_port_is_nt(port), 0);
  4757. bc->fac_out = Fac_Msgs[msg_number];
  4758. /* Send message */
  4759. print_facility(&bc->fac_out, bc);
  4760. misdn_lib_send_event(bc, EVENT_FACILITY);
  4761. sleep(1);
  4762. }
  4763. } else {
  4764. msg_number = atoi(a->argv[5]);
  4765. if (msg_number < ARRAY_LEN(Fac_Msgs)) {
  4766. misdn_make_dummy(bc, port, 0, misdn_lib_port_is_nt(port), 0);
  4767. bc->fac_out = Fac_Msgs[msg_number];
  4768. /* Send message */
  4769. print_facility(&bc->fac_out, bc);
  4770. misdn_lib_send_event(bc, EVENT_FACILITY);
  4771. } else {
  4772. ast_verbose("test <port> [<msg#>]\n\n");
  4773. }
  4774. }
  4775. } else if (strstr(a->argv[3], "register")) {
  4776. if (a->argc < 5) {
  4777. ast_verbose("register <port>\n\n");
  4778. return 0;
  4779. }
  4780. port = atoi(a->argv[4]);
  4781. bc = misdn_lib_get_register_bc(port);
  4782. if (!bc) {
  4783. ast_verbose("Could not allocate REGISTER bc struct\n\n");
  4784. return 0;
  4785. }
  4786. bc->fac_out = Fac_Msgs[45];
  4787. /* Send message */
  4788. print_facility(&bc->fac_out, bc);
  4789. misdn_lib_send_event(bc, EVENT_REGISTER);
  4790. #endif /* defined(AST_MISDN_ENHANCEMENTS) && defined(CCBS_TEST_MESSAGES) */
  4791. }
  4792. return CLI_SUCCESS;
  4793. }
  4794. static char *handle_cli_misdn_send_restart(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  4795. {
  4796. int port;
  4797. int channel;
  4798. switch (cmd) {
  4799. case CLI_INIT:
  4800. e->command = "misdn send restart";
  4801. e->usage =
  4802. "Usage: misdn send restart [port [channel]]\n"
  4803. " Send a restart for every bchannel on the given port.\n";
  4804. return NULL;
  4805. case CLI_GENERATE:
  4806. return NULL;
  4807. }
  4808. if (a->argc < 4 || a->argc > 5) {
  4809. return CLI_SHOWUSAGE;
  4810. }
  4811. port = atoi(a->argv[3]);
  4812. if (a->argc == 5) {
  4813. channel = atoi(a->argv[4]);
  4814. misdn_lib_send_restart(port, channel);
  4815. } else {
  4816. misdn_lib_send_restart(port, -1);
  4817. }
  4818. return CLI_SUCCESS;
  4819. }
  4820. static char *handle_cli_misdn_send_digit(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  4821. {
  4822. const char *channame;
  4823. const char *msg;
  4824. struct chan_list *tmp;
  4825. int i, msglen;
  4826. switch (cmd) {
  4827. case CLI_INIT:
  4828. e->command = "misdn send digit";
  4829. e->usage =
  4830. "Usage: misdn send digit <channel> \"<msg>\" \n"
  4831. " Send <digit> to <channel> as DTMF Tone\n"
  4832. " when channel is a mISDN channel\n";
  4833. return NULL;
  4834. case CLI_GENERATE:
  4835. return complete_ch(a);
  4836. }
  4837. if (a->argc != 5) {
  4838. return CLI_SHOWUSAGE;
  4839. }
  4840. channame = a->argv[3];
  4841. msg = a->argv[4];
  4842. msglen = strlen(msg);
  4843. ast_cli(a->fd, "Sending %s to %s\n", msg, channame);
  4844. tmp = get_chan_by_ast_name(channame);
  4845. if (!tmp) {
  4846. ast_cli(a->fd, "Sending %s to %s failed Channel does not exist\n", msg, channame);
  4847. return CLI_SUCCESS;
  4848. }
  4849. #if 1
  4850. for (i = 0; i < msglen; i++) {
  4851. if (!tmp->ast) {
  4852. break;
  4853. }
  4854. ast_cli(a->fd, "Sending: %c\n", msg[i]);
  4855. send_digit_to_chan(tmp, msg[i]);
  4856. /* res = ast_safe_sleep(tmp->ast, 250); */
  4857. usleep(250000);
  4858. /* res = ast_waitfor(tmp->ast,100); */
  4859. }
  4860. #else
  4861. if (tmp->ast) {
  4862. ast_dtmf_stream(tmp->ast, NULL, msg, 250);
  4863. }
  4864. #endif
  4865. chan_list_unref(tmp, "Digit(s) sent");
  4866. return CLI_SUCCESS;
  4867. }
  4868. static char *handle_cli_misdn_toggle_echocancel(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  4869. {
  4870. const char *channame;
  4871. struct chan_list *tmp;
  4872. switch (cmd) {
  4873. case CLI_INIT:
  4874. e->command = "misdn toggle echocancel";
  4875. e->usage =
  4876. "Usage: misdn toggle echocancel <channel>\n"
  4877. " Toggle EchoCancel on mISDN Channel.\n";
  4878. return NULL;
  4879. case CLI_GENERATE:
  4880. return complete_ch(a);
  4881. }
  4882. if (a->argc != 4) {
  4883. return CLI_SHOWUSAGE;
  4884. }
  4885. channame = a->argv[3];
  4886. ast_cli(a->fd, "Toggling EchoCancel on %s\n", channame);
  4887. tmp = get_chan_by_ast_name(channame);
  4888. if (!tmp) {
  4889. ast_cli(a->fd, "Toggling EchoCancel %s failed Channel does not exist\n", channame);
  4890. return CLI_SUCCESS;
  4891. }
  4892. tmp->toggle_ec = tmp->toggle_ec ? 0 : 1;
  4893. if (tmp->toggle_ec) {
  4894. #ifdef MISDN_1_2
  4895. update_pipeline_config(tmp->bc);
  4896. #else
  4897. update_ec_config(tmp->bc);
  4898. #endif
  4899. manager_ec_enable(tmp->bc);
  4900. } else {
  4901. manager_ec_disable(tmp->bc);
  4902. }
  4903. chan_list_unref(tmp, "Done toggling echo cancel");
  4904. return CLI_SUCCESS;
  4905. }
  4906. static char *handle_cli_misdn_send_display(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  4907. {
  4908. const char *channame;
  4909. const char *msg;
  4910. struct chan_list *tmp;
  4911. switch (cmd) {
  4912. case CLI_INIT:
  4913. e->command = "misdn send display";
  4914. e->usage =
  4915. "Usage: misdn send display <channel> \"<msg>\" \n"
  4916. " Send <msg> to <channel> as Display Message\n"
  4917. " when channel is a mISDN channel\n";
  4918. return NULL;
  4919. case CLI_GENERATE:
  4920. return complete_ch(a);
  4921. }
  4922. if (a->argc != 5) {
  4923. return CLI_SHOWUSAGE;
  4924. }
  4925. channame = a->argv[3];
  4926. msg = a->argv[4];
  4927. ast_cli(a->fd, "Sending %s to %s\n", msg, channame);
  4928. tmp = get_chan_by_ast_name(channame);
  4929. if (tmp && tmp->bc) {
  4930. ast_copy_string(tmp->bc->display, msg, sizeof(tmp->bc->display));
  4931. misdn_lib_send_event(tmp->bc, EVENT_INFORMATION);
  4932. chan_list_unref(tmp, "Done sending display");
  4933. } else {
  4934. if (tmp) {
  4935. chan_list_unref(tmp, "Display failed");
  4936. }
  4937. ast_cli(a->fd, "No such channel %s\n", channame);
  4938. return CLI_SUCCESS;
  4939. }
  4940. return CLI_SUCCESS;
  4941. }
  4942. static char *complete_ch(struct ast_cli_args *a)
  4943. {
  4944. return ast_complete_channels(a->line, a->word, a->pos, a->n, 3);
  4945. }
  4946. static char *complete_debug_port(struct ast_cli_args *a)
  4947. {
  4948. if (a->n) {
  4949. return NULL;
  4950. }
  4951. switch (a->pos) {
  4952. case 4:
  4953. if (a->word[0] == 'p') {
  4954. return ast_strdup("port");
  4955. } else if (a->word[0] == 'o') {
  4956. return ast_strdup("only");
  4957. }
  4958. break;
  4959. case 6:
  4960. if (a->word[0] == 'o') {
  4961. return ast_strdup("only");
  4962. }
  4963. break;
  4964. }
  4965. return NULL;
  4966. }
  4967. static char *complete_show_config(struct ast_cli_args *a)
  4968. {
  4969. char buffer[BUFFERSIZE];
  4970. enum misdn_cfg_elements elem;
  4971. int wordlen = strlen(a->word);
  4972. int which = 0;
  4973. int port = 0;
  4974. switch (a->pos) {
  4975. case 3:
  4976. if ((!strncmp(a->word, "description", wordlen)) && (++which > a->n)) {
  4977. return ast_strdup("description");
  4978. }
  4979. if ((!strncmp(a->word, "descriptions", wordlen)) && (++which > a->n)) {
  4980. return ast_strdup("descriptions");
  4981. }
  4982. if ((!strncmp(a->word, "0", wordlen)) && (++which > a->n)) {
  4983. return ast_strdup("0");
  4984. }
  4985. while ((port = misdn_cfg_get_next_port(port)) != -1) {
  4986. snprintf(buffer, sizeof(buffer), "%d", port);
  4987. if ((!strncmp(a->word, buffer, wordlen)) && (++which > a->n)) {
  4988. return ast_strdup(buffer);
  4989. }
  4990. }
  4991. break;
  4992. case 4:
  4993. if (strstr(a->line, "description ")) {
  4994. for (elem = MISDN_CFG_FIRST + 1; elem < MISDN_GEN_LAST; ++elem) {
  4995. if ((elem == MISDN_CFG_LAST) || (elem == MISDN_GEN_FIRST)) {
  4996. continue;
  4997. }
  4998. misdn_cfg_get_name(elem, buffer, sizeof(buffer));
  4999. if (!wordlen || !strncmp(a->word, buffer, wordlen)) {
  5000. if (++which > a->n) {
  5001. return ast_strdup(buffer);
  5002. }
  5003. }
  5004. }
  5005. } else if (strstr(a->line, "descriptions ")) {
  5006. if ((!wordlen || !strncmp(a->word, "general", wordlen)) && (++which > a->n)) {
  5007. return ast_strdup("general");
  5008. }
  5009. if ((!wordlen || !strncmp(a->word, "ports", wordlen)) && (++which > a->n)) {
  5010. return ast_strdup("ports");
  5011. }
  5012. }
  5013. break;
  5014. }
  5015. return NULL;
  5016. }
  5017. static struct ast_cli_entry chan_misdn_clis[] = {
  5018. /* *INDENT-OFF* */
  5019. AST_CLI_DEFINE(handle_cli_misdn_port_block, "Block the given port"),
  5020. AST_CLI_DEFINE(handle_cli_misdn_port_down, "Try to deactivate the L1 on the given port"),
  5021. AST_CLI_DEFINE(handle_cli_misdn_port_unblock, "Unblock the given port"),
  5022. AST_CLI_DEFINE(handle_cli_misdn_port_up, "Try to establish L1 on the given port"),
  5023. AST_CLI_DEFINE(handle_cli_misdn_reload, "Reload internal mISDN config, read from the config file"),
  5024. AST_CLI_DEFINE(handle_cli_misdn_restart_pid, "Restart the given pid"),
  5025. AST_CLI_DEFINE(handle_cli_misdn_restart_port, "Restart the given port"),
  5026. AST_CLI_DEFINE(handle_cli_misdn_show_channel, "Show an internal mISDN channel"),
  5027. AST_CLI_DEFINE(handle_cli_misdn_show_channels, "Show the internal mISDN channel list"),
  5028. AST_CLI_DEFINE(handle_cli_misdn_show_config, "Show internal mISDN config, read from the config file"),
  5029. AST_CLI_DEFINE(handle_cli_misdn_show_port, "Show detailed information for given port"),
  5030. AST_CLI_DEFINE(handle_cli_misdn_show_ports_stats, "Show mISDNs channel's call statistics per port"),
  5031. AST_CLI_DEFINE(handle_cli_misdn_show_stacks, "Show internal mISDN stack_list"),
  5032. AST_CLI_DEFINE(handle_cli_misdn_send_facility, "Sends a Facility Message to the mISDN Channel"),
  5033. AST_CLI_DEFINE(handle_cli_misdn_send_digit, "Send DTMF digit to mISDN Channel"),
  5034. AST_CLI_DEFINE(handle_cli_misdn_send_display, "Send Text to mISDN Channel"),
  5035. AST_CLI_DEFINE(handle_cli_misdn_send_restart, "Send a restart for every bchannel on the given port"),
  5036. AST_CLI_DEFINE(handle_cli_misdn_set_crypt_debug, "Set CryptDebuglevel of chan_misdn, at the moment, level={1,2}"),
  5037. AST_CLI_DEFINE(handle_cli_misdn_set_debug, "Set Debuglevel of chan_misdn"),
  5038. AST_CLI_DEFINE(handle_cli_misdn_set_tics, "???"),
  5039. AST_CLI_DEFINE(handle_cli_misdn_toggle_echocancel, "Toggle EchoCancel on mISDN Channel"),
  5040. /* *INDENT-ON* */
  5041. };
  5042. /*! \brief Updates caller ID information from config */
  5043. static void update_config(struct chan_list *ch)
  5044. {
  5045. struct ast_channel *ast;
  5046. struct misdn_bchannel *bc;
  5047. int port;
  5048. int hdlc = 0;
  5049. int pres;
  5050. int screen;
  5051. if (!ch) {
  5052. ast_log(LOG_WARNING, "Cannot configure without chanlist\n");
  5053. return;
  5054. }
  5055. ast = ch->ast;
  5056. bc = ch->bc;
  5057. if (! ast || ! bc) {
  5058. ast_log(LOG_WARNING, "Cannot configure without ast || bc\n");
  5059. return;
  5060. }
  5061. port = bc->port;
  5062. chan_misdn_log(7, port, "update_config: Getting Config\n");
  5063. misdn_cfg_get(port, MISDN_CFG_HDLC, &hdlc, sizeof(int));
  5064. if (hdlc) {
  5065. switch (bc->capability) {
  5066. case INFO_CAPABILITY_DIGITAL_UNRESTRICTED:
  5067. case INFO_CAPABILITY_DIGITAL_RESTRICTED:
  5068. chan_misdn_log(1, bc->port, " --> CONF HDLC\n");
  5069. bc->hdlc = 1;
  5070. break;
  5071. }
  5072. }
  5073. misdn_cfg_get(port, MISDN_CFG_PRES, &pres, sizeof(pres));
  5074. misdn_cfg_get(port, MISDN_CFG_SCREEN, &screen, sizeof(screen));
  5075. chan_misdn_log(2, port, " --> pres: %d screen: %d\n", pres, screen);
  5076. if (pres < 0 || screen < 0) {
  5077. chan_misdn_log(2, port, " --> pres: %x\n", ast_channel_connected(ast)->id.number.presentation);
  5078. bc->caller.presentation = ast_to_misdn_pres(ast_channel_connected(ast)->id.number.presentation);
  5079. chan_misdn_log(2, port, " --> PRES: %s(%d)\n", misdn_to_str_pres(bc->caller.presentation), bc->caller.presentation);
  5080. bc->caller.screening = ast_to_misdn_screen(ast_channel_connected(ast)->id.number.presentation);
  5081. chan_misdn_log(2, port, " --> SCREEN: %s(%d)\n", misdn_to_str_screen(bc->caller.screening), bc->caller.screening);
  5082. } else {
  5083. bc->caller.screening = screen;
  5084. bc->caller.presentation = pres;
  5085. }
  5086. }
  5087. static void config_jitterbuffer(struct chan_list *ch)
  5088. {
  5089. struct misdn_bchannel *bc = ch->bc;
  5090. int len = ch->jb_len;
  5091. int threshold = ch->jb_upper_threshold;
  5092. chan_misdn_log(5, bc->port, "config_jb: Called\n");
  5093. if (!len) {
  5094. chan_misdn_log(1, bc->port, "config_jb: Deactivating Jitterbuffer\n");
  5095. bc->nojitter = 1;
  5096. } else {
  5097. if (len <= 100 || len > 8000) {
  5098. chan_misdn_log(0, bc->port, "config_jb: Jitterbuffer out of Bounds, setting to 1000\n");
  5099. len = 1000;
  5100. }
  5101. if (threshold > len) {
  5102. chan_misdn_log(0, bc->port, "config_jb: Jitterbuffer Threshold > Jitterbuffer setting to Jitterbuffer -1\n");
  5103. }
  5104. if (ch->jb) {
  5105. cb_log(0, bc->port, "config_jb: We've got a Jitterbuffer Already on this port.\n");
  5106. misdn_jb_destroy(ch->jb);
  5107. ch->jb = NULL;
  5108. }
  5109. ch->jb = misdn_jb_init(len, threshold);
  5110. if (!ch->jb) {
  5111. bc->nojitter = 1;
  5112. }
  5113. }
  5114. }
  5115. void debug_numtype(int port, int numtype, char *type)
  5116. {
  5117. switch (numtype) {
  5118. case NUMTYPE_UNKNOWN:
  5119. chan_misdn_log(2, port, " --> %s: Unknown\n", type);
  5120. break;
  5121. case NUMTYPE_INTERNATIONAL:
  5122. chan_misdn_log(2, port, " --> %s: International\n", type);
  5123. break;
  5124. case NUMTYPE_NATIONAL:
  5125. chan_misdn_log(2, port, " --> %s: National\n", type);
  5126. break;
  5127. case NUMTYPE_NETWORK_SPECIFIC:
  5128. chan_misdn_log(2, port, " --> %s: Network Specific\n", type);
  5129. break;
  5130. case NUMTYPE_SUBSCRIBER:
  5131. chan_misdn_log(2, port, " --> %s: Subscriber\n", type);
  5132. break;
  5133. case NUMTYPE_ABBREVIATED:
  5134. chan_misdn_log(2, port, " --> %s: Abbreviated\n", type);
  5135. break;
  5136. /* Maybe we should cut off the prefix if present ? */
  5137. default:
  5138. chan_misdn_log(0, port, " --> !!!! Wrong dialplan setting, please see the misdn.conf sample file\n ");
  5139. break;
  5140. }
  5141. }
  5142. #ifdef MISDN_1_2
  5143. static int update_pipeline_config(struct misdn_bchannel *bc)
  5144. {
  5145. int ec;
  5146. misdn_cfg_get(bc->port, MISDN_CFG_PIPELINE, bc->pipeline, sizeof(bc->pipeline));
  5147. if (*bc->pipeline) {
  5148. return 0;
  5149. }
  5150. misdn_cfg_get(bc->port, MISDN_CFG_ECHOCANCEL, &ec, sizeof(ec));
  5151. if (ec == 1) {
  5152. ast_copy_string(bc->pipeline, "mg2ec", sizeof(bc->pipeline));
  5153. } else if (ec > 1) {
  5154. snprintf(bc->pipeline, sizeof(bc->pipeline), "mg2ec(deftaps=%d)", ec);
  5155. }
  5156. return 0;
  5157. }
  5158. #else
  5159. static int update_ec_config(struct misdn_bchannel *bc)
  5160. {
  5161. int ec;
  5162. int port = bc->port;
  5163. misdn_cfg_get(port, MISDN_CFG_ECHOCANCEL, &ec, sizeof(ec));
  5164. if (ec == 1) {
  5165. bc->ec_enable = 1;
  5166. } else if (ec > 1) {
  5167. bc->ec_enable = 1;
  5168. bc->ec_deftaps = ec;
  5169. }
  5170. return 0;
  5171. }
  5172. #endif
  5173. static int read_config(struct chan_list *ch)
  5174. {
  5175. struct ast_channel *ast;
  5176. struct misdn_bchannel *bc;
  5177. int port;
  5178. int hdlc = 0;
  5179. char lang[BUFFERSIZE + 1];
  5180. char faxdetect[BUFFERSIZE + 1];
  5181. char buf[256];
  5182. char buf2[256];
  5183. ast_group_t pg;
  5184. ast_group_t cg;
  5185. struct ast_namedgroups *npg;
  5186. struct ast_namedgroups *ncg;
  5187. struct ast_str *tmp_str;
  5188. if (!ch) {
  5189. ast_log(LOG_WARNING, "Cannot configure without chanlist\n");
  5190. return -1;
  5191. }
  5192. ast = ch->ast;
  5193. bc = ch->bc;
  5194. if (! ast || ! bc) {
  5195. ast_log(LOG_WARNING, "Cannot configure without ast || bc\n");
  5196. return -1;
  5197. }
  5198. port = bc->port;
  5199. chan_misdn_log(1, port, "read_config: Getting Config\n");
  5200. misdn_cfg_get(port, MISDN_CFG_LANGUAGE, lang, sizeof(lang));
  5201. ast_channel_lock(ast);
  5202. ast_channel_language_set(ast, lang);
  5203. ast_channel_unlock(ast);
  5204. misdn_cfg_get(port, MISDN_CFG_MUSICCLASS, ch->mohinterpret, sizeof(ch->mohinterpret));
  5205. misdn_cfg_get(port, MISDN_CFG_TXGAIN, &bc->txgain, sizeof(bc->txgain));
  5206. misdn_cfg_get(port, MISDN_CFG_RXGAIN, &bc->rxgain, sizeof(bc->rxgain));
  5207. misdn_cfg_get(port, MISDN_CFG_INCOMING_EARLY_AUDIO, &ch->incoming_early_audio, sizeof(ch->incoming_early_audio));
  5208. misdn_cfg_get(port, MISDN_CFG_SENDDTMF, &bc->send_dtmf, sizeof(bc->send_dtmf));
  5209. misdn_cfg_get(port, MISDN_CFG_ASTDTMF, &ch->ast_dsp, sizeof(int));
  5210. if (ch->ast_dsp) {
  5211. ch->ignore_dtmf = 1;
  5212. }
  5213. misdn_cfg_get(port, MISDN_CFG_NEED_MORE_INFOS, &bc->need_more_infos, sizeof(bc->need_more_infos));
  5214. misdn_cfg_get(port, MISDN_CFG_NTTIMEOUT, &ch->nttimeout, sizeof(ch->nttimeout));
  5215. misdn_cfg_get(port, MISDN_CFG_NOAUTORESPOND_ON_SETUP, &ch->noautorespond_on_setup, sizeof(ch->noautorespond_on_setup));
  5216. misdn_cfg_get(port, MISDN_CFG_FAR_ALERTING, &ch->far_alerting, sizeof(ch->far_alerting));
  5217. misdn_cfg_get(port, MISDN_CFG_ALLOWED_BEARERS, &ch->allowed_bearers, sizeof(ch->allowed_bearers));
  5218. misdn_cfg_get(port, MISDN_CFG_FAXDETECT, faxdetect, sizeof(faxdetect));
  5219. misdn_cfg_get(port, MISDN_CFG_HDLC, &hdlc, sizeof(hdlc));
  5220. if (hdlc) {
  5221. switch (bc->capability) {
  5222. case INFO_CAPABILITY_DIGITAL_UNRESTRICTED:
  5223. case INFO_CAPABILITY_DIGITAL_RESTRICTED:
  5224. chan_misdn_log(1, bc->port, " --> CONF HDLC\n");
  5225. bc->hdlc = 1;
  5226. break;
  5227. }
  5228. }
  5229. /*Initialize new Jitterbuffer*/
  5230. misdn_cfg_get(port, MISDN_CFG_JITTERBUFFER, &ch->jb_len, sizeof(ch->jb_len));
  5231. misdn_cfg_get(port, MISDN_CFG_JITTERBUFFER_UPPER_THRESHOLD, &ch->jb_upper_threshold, sizeof(ch->jb_upper_threshold));
  5232. config_jitterbuffer(ch);
  5233. misdn_cfg_get(bc->port, MISDN_CFG_CONTEXT, ch->context, sizeof(ch->context));
  5234. ast_channel_lock(ast);
  5235. ast_channel_context_set(ast, ch->context);
  5236. ast_channel_unlock(ast);
  5237. #ifdef MISDN_1_2
  5238. update_pipeline_config(bc);
  5239. #else
  5240. update_ec_config(bc);
  5241. #endif
  5242. misdn_cfg_get(bc->port, MISDN_CFG_EARLY_BCONNECT, &bc->early_bconnect, sizeof(bc->early_bconnect));
  5243. misdn_cfg_get(port, MISDN_CFG_DISPLAY_CONNECTED, &bc->display_connected, sizeof(bc->display_connected));
  5244. misdn_cfg_get(port, MISDN_CFG_DISPLAY_SETUP, &bc->display_setup, sizeof(bc->display_setup));
  5245. misdn_cfg_get(port, MISDN_CFG_OUTGOING_COLP, &bc->outgoing_colp, sizeof(bc->outgoing_colp));
  5246. misdn_cfg_get(port, MISDN_CFG_PICKUPGROUP, &pg, sizeof(pg));
  5247. misdn_cfg_get(port, MISDN_CFG_CALLGROUP, &cg, sizeof(cg));
  5248. chan_misdn_log(5, port, " --> * CallGrp:%s PickupGrp:%s\n", ast_print_group(buf, sizeof(buf), cg), ast_print_group(buf2, sizeof(buf2), pg));
  5249. ast_channel_lock(ast);
  5250. ast_channel_pickupgroup_set(ast, pg);
  5251. ast_channel_callgroup_set(ast, cg);
  5252. ast_channel_unlock(ast);
  5253. misdn_cfg_get(port, MISDN_CFG_NAMEDPICKUPGROUP, &npg, sizeof(npg));
  5254. misdn_cfg_get(port, MISDN_CFG_NAMEDCALLGROUP, &ncg, sizeof(ncg));
  5255. tmp_str = ast_str_create(1024);
  5256. if (tmp_str) {
  5257. chan_misdn_log(5, port, " --> * NamedCallGrp:%s\n", ast_print_namedgroups(&tmp_str, ncg));
  5258. ast_str_reset(tmp_str);
  5259. chan_misdn_log(5, port, " --> * NamedPickupGrp:%s\n", ast_print_namedgroups(&tmp_str, npg));
  5260. ast_free(tmp_str);
  5261. }
  5262. ast_channel_lock(ast);
  5263. ast_channel_named_pickupgroups_set(ast, npg);
  5264. ast_channel_named_callgroups_set(ast, ncg);
  5265. ast_channel_unlock(ast);
  5266. if (ch->originator == ORG_AST) {
  5267. char callerid[BUFFERSIZE + 1];
  5268. /* ORIGINATOR Asterisk (outgoing call) */
  5269. misdn_cfg_get(port, MISDN_CFG_TE_CHOOSE_CHANNEL, &(bc->te_choose_channel), sizeof(bc->te_choose_channel));
  5270. if (strstr(faxdetect, "outgoing") || strstr(faxdetect, "both")) {
  5271. ch->faxdetect = strstr(faxdetect, "nojump") ? 2 : 1;
  5272. }
  5273. misdn_cfg_get(port, MISDN_CFG_CALLERID, callerid, sizeof(callerid));
  5274. if (!ast_strlen_zero(callerid)) {
  5275. char *cid_name = NULL;
  5276. char *cid_num = NULL;
  5277. ast_callerid_parse(callerid, &cid_name, &cid_num);
  5278. if (cid_name) {
  5279. ast_copy_string(bc->caller.name, cid_name, sizeof(bc->caller.name));
  5280. } else {
  5281. bc->caller.name[0] = '\0';
  5282. }
  5283. if (cid_num) {
  5284. ast_copy_string(bc->caller.number, cid_num, sizeof(bc->caller.number));
  5285. } else {
  5286. bc->caller.number[0] = '\0';
  5287. }
  5288. chan_misdn_log(1, port, " --> * Setting caller to \"%s\" <%s>\n", bc->caller.name, bc->caller.number);
  5289. }
  5290. misdn_cfg_get(port, MISDN_CFG_DIALPLAN, &bc->dialed.number_type, sizeof(bc->dialed.number_type));
  5291. bc->dialed.number_plan = NUMPLAN_ISDN;
  5292. debug_numtype(port, bc->dialed.number_type, "TON");
  5293. ch->overlap_dial = 0;
  5294. } else {
  5295. /* ORIGINATOR MISDN (incoming call) */
  5296. if (strstr(faxdetect, "incoming") || strstr(faxdetect, "both")) {
  5297. ch->faxdetect = (strstr(faxdetect, "nojump")) ? 2 : 1;
  5298. }
  5299. /* Add configured prefix to caller.number */
  5300. misdn_add_number_prefix(bc->port, bc->caller.number_type, bc->caller.number, sizeof(bc->caller.number));
  5301. if (ast_strlen_zero(bc->dialed.number) && !ast_strlen_zero(bc->keypad)) {
  5302. ast_copy_string(bc->dialed.number, bc->keypad, sizeof(bc->dialed.number));
  5303. }
  5304. /* Add configured prefix to dialed.number */
  5305. misdn_add_number_prefix(bc->port, bc->dialed.number_type, bc->dialed.number, sizeof(bc->dialed.number));
  5306. ast_channel_lock(ast);
  5307. ast_channel_exten_set(ast, bc->dialed.number);
  5308. ast_channel_unlock(ast);
  5309. misdn_cfg_get(bc->port, MISDN_CFG_OVERLAP_DIAL, &ch->overlap_dial, sizeof(ch->overlap_dial));
  5310. ast_mutex_init(&ch->overlap_tv_lock);
  5311. } /* ORIG MISDN END */
  5312. misdn_cfg_get(port, MISDN_CFG_INCOMING_CALLERID_TAG, bc->incoming_cid_tag, sizeof(bc->incoming_cid_tag));
  5313. if (!ast_strlen_zero(bc->incoming_cid_tag)) {
  5314. chan_misdn_log(1, port, " --> * Setting incoming caller id tag to \"%s\"\n", bc->incoming_cid_tag);
  5315. }
  5316. ch->overlap_dial_task = -1;
  5317. if (ch->faxdetect || ch->ast_dsp) {
  5318. misdn_cfg_get(port, MISDN_CFG_FAXDETECT_TIMEOUT, &ch->faxdetect_timeout, sizeof(ch->faxdetect_timeout));
  5319. if (!ch->dsp) {
  5320. ch->dsp = ast_dsp_new();
  5321. }
  5322. if (ch->dsp) {
  5323. ast_dsp_set_features(ch->dsp, DSP_FEATURE_DIGIT_DETECT | (ch->faxdetect ? DSP_FEATURE_FAX_DETECT : 0));
  5324. }
  5325. }
  5326. /* AOCD initialization */
  5327. bc->AOCDtype = Fac_None;
  5328. return 0;
  5329. }
  5330. /*!
  5331. * \internal
  5332. * \brief Send a connected line update to the other channel
  5333. *
  5334. * \param ast Current Asterisk channel
  5335. * \param id Party id information to send to the other side
  5336. * \param source Why are we sending this update
  5337. * \param cid_tag User tag to apply to the party id.
  5338. *
  5339. * \return Nothing
  5340. */
  5341. 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)
  5342. {
  5343. struct ast_party_connected_line connected;
  5344. struct ast_set_party_connected_line update_connected;
  5345. ast_party_connected_line_init(&connected);
  5346. memset(&update_connected, 0, sizeof(update_connected));
  5347. update_connected.id.number = 1;
  5348. connected.id.number.valid = 1;
  5349. connected.id.number.str = (char *) id->number;
  5350. connected.id.number.plan = misdn_to_ast_ton(id->number_type)
  5351. | misdn_to_ast_plan(id->number_plan);
  5352. connected.id.number.presentation = misdn_to_ast_pres(id->presentation)
  5353. | misdn_to_ast_screen(id->screening);
  5354. /*
  5355. * Make sure that any earlier private connected id
  5356. * representation at the remote end is invalidated
  5357. */
  5358. ast_set_party_id_all(&update_connected.priv);
  5359. connected.id.tag = cid_tag;
  5360. connected.source = source;
  5361. ast_channel_queue_connected_line_update(ast, &connected, &update_connected);
  5362. }
  5363. /*!
  5364. * \internal
  5365. * \brief Update the caller id party on this channel.
  5366. *
  5367. * \param ast Current Asterisk channel
  5368. * \param id Remote party id information to update.
  5369. * \param cid_tag User tag to apply to the party id.
  5370. *
  5371. * \return Nothing
  5372. */
  5373. static void misdn_update_caller_id(struct ast_channel *ast, const struct misdn_party_id *id, char *cid_tag)
  5374. {
  5375. struct ast_party_caller caller;
  5376. struct ast_set_party_caller update_caller;
  5377. memset(&update_caller, 0, sizeof(update_caller));
  5378. update_caller.id.number = 1;
  5379. update_caller.ani.number = 1;
  5380. ast_channel_lock(ast);
  5381. ast_party_caller_set_init(&caller, ast_channel_caller(ast));
  5382. caller.id.number.valid = 1;
  5383. caller.id.number.str = (char *) id->number;
  5384. caller.id.number.plan = misdn_to_ast_ton(id->number_type)
  5385. | misdn_to_ast_plan(id->number_plan);
  5386. caller.id.number.presentation = misdn_to_ast_pres(id->presentation)
  5387. | misdn_to_ast_screen(id->screening);
  5388. caller.ani.number = caller.id.number;
  5389. caller.id.tag = cid_tag;
  5390. caller.ani.tag = cid_tag;
  5391. ast_channel_set_caller_event(ast, &caller, &update_caller);
  5392. ast_channel_unlock(ast);
  5393. }
  5394. /*!
  5395. * \internal
  5396. * \brief Update the remote party id information.
  5397. *
  5398. * \param ast Current Asterisk channel
  5399. * \param id Remote party id information to update.
  5400. * \param source Why are we sending this update
  5401. * \param cid_tag User tag to apply to the party id.
  5402. *
  5403. * \return Nothing
  5404. */
  5405. 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)
  5406. {
  5407. misdn_update_caller_id(ast, id, cid_tag);
  5408. misdn_queue_connected_line_update(ast, id, source, cid_tag);
  5409. }
  5410. /*!
  5411. * \internal
  5412. * \brief Get the connected line information out of the Asterisk channel.
  5413. *
  5414. * \param ast Current Asterisk channel
  5415. * \param bc Associated B channel
  5416. * \param originator Who originally created this channel. ORG_AST or ORG_MISDN
  5417. *
  5418. * \return Nothing
  5419. */
  5420. static void misdn_get_connected_line(struct ast_channel *ast, struct misdn_bchannel *bc, int originator)
  5421. {
  5422. int number_type;
  5423. struct ast_party_id connected_id = ast_channel_connected_effective_id(ast);
  5424. if (originator == ORG_MISDN) {
  5425. /* ORIGINATOR MISDN (incoming call) */
  5426. ast_copy_string(bc->connected.name,
  5427. S_COR(connected_id.name.valid, connected_id.name.str, ""),
  5428. sizeof(bc->connected.name));
  5429. if (connected_id.number.valid) {
  5430. ast_copy_string(bc->connected.number, S_OR(connected_id.number.str, ""),
  5431. sizeof(bc->connected.number));
  5432. bc->connected.presentation = ast_to_misdn_pres(connected_id.number.presentation);
  5433. bc->connected.screening = ast_to_misdn_screen(connected_id.number.presentation);
  5434. bc->connected.number_type = ast_to_misdn_ton(connected_id.number.plan);
  5435. bc->connected.number_plan = ast_to_misdn_plan(connected_id.number.plan);
  5436. } else {
  5437. bc->connected.number[0] = '\0';
  5438. bc->connected.presentation = 0;/* Allowed */
  5439. bc->connected.screening = 0;/* Unscreened */
  5440. bc->connected.number_type = NUMTYPE_UNKNOWN;
  5441. bc->connected.number_plan = NUMPLAN_UNKNOWN;
  5442. }
  5443. misdn_cfg_get(bc->port, MISDN_CFG_CPNDIALPLAN, &number_type, sizeof(number_type));
  5444. if (0 <= number_type) {
  5445. /* Force us to send in CONNECT message */
  5446. bc->connected.number_type = number_type;
  5447. bc->connected.number_plan = NUMPLAN_ISDN;
  5448. }
  5449. debug_numtype(bc->port, bc->connected.number_type, "CTON");
  5450. } else {
  5451. /* ORIGINATOR Asterisk (outgoing call) */
  5452. ast_copy_string(bc->caller.name,
  5453. S_COR(connected_id.name.valid, connected_id.name.str, ""),
  5454. sizeof(bc->caller.name));
  5455. if (connected_id.number.valid) {
  5456. ast_copy_string(bc->caller.number, S_OR(connected_id.number.str, ""),
  5457. sizeof(bc->caller.number));
  5458. bc->caller.presentation = ast_to_misdn_pres(connected_id.number.presentation);
  5459. bc->caller.screening = ast_to_misdn_screen(connected_id.number.presentation);
  5460. bc->caller.number_type = ast_to_misdn_ton(connected_id.number.plan);
  5461. bc->caller.number_plan = ast_to_misdn_plan(connected_id.number.plan);
  5462. } else {
  5463. bc->caller.number[0] = '\0';
  5464. bc->caller.presentation = 0;/* Allowed */
  5465. bc->caller.screening = 0;/* Unscreened */
  5466. bc->caller.number_type = NUMTYPE_UNKNOWN;
  5467. bc->caller.number_plan = NUMPLAN_UNKNOWN;
  5468. }
  5469. misdn_cfg_get(bc->port, MISDN_CFG_LOCALDIALPLAN, &number_type, sizeof(number_type));
  5470. if (0 <= number_type) {
  5471. /* Force us to send in SETUP message */
  5472. bc->caller.number_type = number_type;
  5473. bc->caller.number_plan = NUMPLAN_ISDN;
  5474. }
  5475. debug_numtype(bc->port, bc->caller.number_type, "LTON");
  5476. }
  5477. }
  5478. /*!
  5479. * \internal
  5480. * \brief Notify peer that the connected line has changed.
  5481. *
  5482. * \param ast Current Asterisk channel
  5483. * \param bc Associated B channel
  5484. * \param originator Who originally created this channel. ORG_AST or ORG_MISDN
  5485. *
  5486. * \return Nothing
  5487. */
  5488. static void misdn_update_connected_line(struct ast_channel *ast, struct misdn_bchannel *bc, int originator)
  5489. {
  5490. struct chan_list *ch;
  5491. misdn_get_connected_line(ast, bc, originator);
  5492. if (originator == ORG_MISDN) {
  5493. bc->redirecting.to = bc->connected;
  5494. } else {
  5495. bc->redirecting.to = bc->caller;
  5496. }
  5497. switch (bc->outgoing_colp) {
  5498. case 1:/* restricted */
  5499. bc->redirecting.to.presentation = 1;/* restricted */
  5500. break;
  5501. case 2:/* blocked */
  5502. /* Don't tell the remote party that the call was transferred. */
  5503. return;
  5504. default:
  5505. break;
  5506. }
  5507. ch = MISDN_ASTERISK_TECH_PVT(ast);
  5508. if (ch->state == MISDN_CONNECTED
  5509. || originator != ORG_MISDN) {
  5510. int is_ptmp;
  5511. is_ptmp = !misdn_lib_is_ptp(bc->port);
  5512. if (is_ptmp) {
  5513. /*
  5514. * We should not send these messages to the network if we are
  5515. * the CPE side since phones do not transfer calls within
  5516. * themselves. Well... If you consider handing the handset to
  5517. * someone else a transfer then how is the network to know?
  5518. */
  5519. if (!misdn_lib_port_is_nt(bc->port)) {
  5520. return;
  5521. }
  5522. if (ch->state != MISDN_CONNECTED) {
  5523. /* Send NOTIFY(Nie(transfer-active), RDNie(redirecting.to data)) */
  5524. bc->redirecting.to_changed = 1;
  5525. bc->notify_description_code = mISDN_NOTIFY_CODE_CALL_TRANSFER_ACTIVE;
  5526. misdn_lib_send_event(bc, EVENT_NOTIFY);
  5527. #if defined(AST_MISDN_ENHANCEMENTS)
  5528. } else {
  5529. /* Send FACILITY(Fie(RequestSubaddress), Nie(transfer-active), RDNie(redirecting.to data)) */
  5530. bc->redirecting.to_changed = 1;
  5531. bc->notify_description_code = mISDN_NOTIFY_CODE_CALL_TRANSFER_ACTIVE;
  5532. bc->fac_out.Function = Fac_RequestSubaddress;
  5533. bc->fac_out.u.RequestSubaddress.InvokeID = ++misdn_invoke_id;
  5534. /* Send message */
  5535. print_facility(&bc->fac_out, bc);
  5536. misdn_lib_send_event(bc, EVENT_FACILITY);
  5537. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  5538. }
  5539. #if defined(AST_MISDN_ENHANCEMENTS)
  5540. } else {
  5541. /* Send FACILITY(Fie(EctInform(transfer-active, redirecting.to data))) */
  5542. bc->fac_out.Function = Fac_EctInform;
  5543. bc->fac_out.u.EctInform.InvokeID = ++misdn_invoke_id;
  5544. bc->fac_out.u.EctInform.Status = 1;/* active */
  5545. bc->fac_out.u.EctInform.RedirectionPresent = 1;/* Must be present when status is active */
  5546. misdn_PresentedNumberUnscreened_fill(&bc->fac_out.u.EctInform.Redirection,
  5547. &bc->redirecting.to);
  5548. /* Send message */
  5549. print_facility(&bc->fac_out, bc);
  5550. misdn_lib_send_event(bc, EVENT_FACILITY);
  5551. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  5552. }
  5553. }
  5554. }
  5555. /*!
  5556. * \internal
  5557. * \brief Copy the redirecting information out of the Asterisk channel
  5558. *
  5559. * \param bc Associated B channel
  5560. * \param ast Current Asterisk channel
  5561. *
  5562. * \return Nothing
  5563. */
  5564. static void misdn_copy_redirecting_from_ast(struct misdn_bchannel *bc, struct ast_channel *ast)
  5565. {
  5566. struct ast_party_id from_id = ast_channel_redirecting_effective_from(ast);
  5567. struct ast_party_id to_id = ast_channel_redirecting_effective_to(ast);
  5568. ast_copy_string(bc->redirecting.from.name,
  5569. S_COR(from_id.name.valid, from_id.name.str, ""),
  5570. sizeof(bc->redirecting.from.name));
  5571. if (from_id.number.valid) {
  5572. ast_copy_string(bc->redirecting.from.number, S_OR(from_id.number.str, ""),
  5573. sizeof(bc->redirecting.from.number));
  5574. bc->redirecting.from.presentation = ast_to_misdn_pres(from_id.number.presentation);
  5575. bc->redirecting.from.screening = ast_to_misdn_screen(from_id.number.presentation);
  5576. bc->redirecting.from.number_type = ast_to_misdn_ton(from_id.number.plan);
  5577. bc->redirecting.from.number_plan = ast_to_misdn_plan(from_id.number.plan);
  5578. } else {
  5579. bc->redirecting.from.number[0] = '\0';
  5580. bc->redirecting.from.presentation = 0;/* Allowed */
  5581. bc->redirecting.from.screening = 0;/* Unscreened */
  5582. bc->redirecting.from.number_type = NUMTYPE_UNKNOWN;
  5583. bc->redirecting.from.number_plan = NUMPLAN_UNKNOWN;
  5584. }
  5585. ast_copy_string(bc->redirecting.to.name,
  5586. S_COR(to_id.name.valid, to_id.name.str, ""),
  5587. sizeof(bc->redirecting.to.name));
  5588. if (to_id.number.valid) {
  5589. ast_copy_string(bc->redirecting.to.number, S_OR(to_id.number.str, ""),
  5590. sizeof(bc->redirecting.to.number));
  5591. bc->redirecting.to.presentation = ast_to_misdn_pres(to_id.number.presentation);
  5592. bc->redirecting.to.screening = ast_to_misdn_screen(to_id.number.presentation);
  5593. bc->redirecting.to.number_type = ast_to_misdn_ton(to_id.number.plan);
  5594. bc->redirecting.to.number_plan = ast_to_misdn_plan(to_id.number.plan);
  5595. } else {
  5596. bc->redirecting.to.number[0] = '\0';
  5597. bc->redirecting.to.presentation = 0;/* Allowed */
  5598. bc->redirecting.to.screening = 0;/* Unscreened */
  5599. bc->redirecting.to.number_type = NUMTYPE_UNKNOWN;
  5600. bc->redirecting.to.number_plan = NUMPLAN_UNKNOWN;
  5601. }
  5602. bc->redirecting.reason = ast_to_misdn_reason(ast_channel_redirecting(ast)->reason.code);
  5603. bc->redirecting.count = ast_channel_redirecting(ast)->count;
  5604. }
  5605. /*!
  5606. * \internal
  5607. * \brief Copy the redirecting info into the Asterisk channel
  5608. *
  5609. * \param ast Current Asterisk channel
  5610. * \param redirect Associated B channel redirecting info
  5611. * \param tag Caller ID tag to set in the redirecting party fields
  5612. *
  5613. * \return Nothing
  5614. */
  5615. static void misdn_copy_redirecting_to_ast(struct ast_channel *ast, const struct misdn_party_redirecting *redirect, char *tag)
  5616. {
  5617. struct ast_party_redirecting redirecting;
  5618. struct ast_set_party_redirecting update_redirecting;
  5619. ast_party_redirecting_set_init(&redirecting, ast_channel_redirecting(ast));
  5620. memset(&update_redirecting, 0, sizeof(update_redirecting));
  5621. update_redirecting.from.number = 1;
  5622. redirecting.from.number.valid = 1;
  5623. redirecting.from.number.str = (char *) redirect->from.number;
  5624. redirecting.from.number.plan =
  5625. misdn_to_ast_ton(redirect->from.number_type)
  5626. | misdn_to_ast_plan(redirect->from.number_plan);
  5627. redirecting.from.number.presentation =
  5628. misdn_to_ast_pres(redirect->from.presentation)
  5629. | misdn_to_ast_screen(redirect->from.screening);
  5630. redirecting.from.tag = tag;
  5631. update_redirecting.to.number = 1;
  5632. redirecting.to.number.valid = 1;
  5633. redirecting.to.number.str = (char *) redirect->to.number;
  5634. redirecting.to.number.plan =
  5635. misdn_to_ast_ton(redirect->to.number_type)
  5636. | misdn_to_ast_plan(redirect->to.number_plan);
  5637. redirecting.to.number.presentation =
  5638. misdn_to_ast_pres(redirect->to.presentation)
  5639. | misdn_to_ast_screen(redirect->to.screening);
  5640. redirecting.to.tag = tag;
  5641. redirecting.reason.code = misdn_to_ast_reason(redirect->reason);
  5642. redirecting.count = redirect->count;
  5643. ast_channel_set_redirecting(ast, &redirecting, &update_redirecting);
  5644. }
  5645. /*!
  5646. * \internal
  5647. * \brief Notify peer that the redirecting information has changed.
  5648. *
  5649. * \param ast Current Asterisk channel
  5650. * \param bc Associated B channel
  5651. * \param originator Who originally created this channel. ORG_AST or ORG_MISDN
  5652. *
  5653. * \return Nothing
  5654. */
  5655. static void misdn_update_redirecting(struct ast_channel *ast, struct misdn_bchannel *bc, int originator)
  5656. {
  5657. int is_ptmp;
  5658. misdn_copy_redirecting_from_ast(bc, ast);
  5659. switch (bc->outgoing_colp) {
  5660. case 1:/* restricted */
  5661. bc->redirecting.to.presentation = 1;/* restricted */
  5662. break;
  5663. case 2:/* blocked */
  5664. /* Don't tell the remote party that the call was redirected. */
  5665. return;
  5666. default:
  5667. break;
  5668. }
  5669. if (originator != ORG_MISDN) {
  5670. return;
  5671. }
  5672. is_ptmp = !misdn_lib_is_ptp(bc->port);
  5673. if (is_ptmp) {
  5674. /*
  5675. * We should not send these messages to the network if we are
  5676. * the CPE side since phones do not redirect calls within
  5677. * themselves. Well... If you consider someone else picking up
  5678. * the handset a redirection then how is the network to know?
  5679. */
  5680. if (!misdn_lib_port_is_nt(bc->port)) {
  5681. return;
  5682. }
  5683. /* Send NOTIFY(call-is-diverting, redirecting.to data) */
  5684. bc->redirecting.to_changed = 1;
  5685. bc->notify_description_code = mISDN_NOTIFY_CODE_CALL_IS_DIVERTING;
  5686. misdn_lib_send_event(bc, EVENT_NOTIFY);
  5687. #if defined(AST_MISDN_ENHANCEMENTS)
  5688. } else {
  5689. int match; /* TRUE if the dialed number matches the redirecting to number */
  5690. match = (strcmp(ast_channel_exten(ast), bc->redirecting.to.number) == 0) ? 1 : 0;
  5691. if (!bc->div_leg_3_tx_pending
  5692. || !match) {
  5693. /* Send DivertingLegInformation1 */
  5694. bc->fac_out.Function = Fac_DivertingLegInformation1;
  5695. bc->fac_out.u.DivertingLegInformation1.InvokeID = ++misdn_invoke_id;
  5696. bc->fac_out.u.DivertingLegInformation1.DiversionReason =
  5697. misdn_to_diversion_reason(bc->redirecting.reason);
  5698. bc->fac_out.u.DivertingLegInformation1.SubscriptionOption = 2;/* notificationWithDivertedToNr */
  5699. bc->fac_out.u.DivertingLegInformation1.DivertedToPresent = 1;
  5700. misdn_PresentedNumberUnscreened_fill(&bc->fac_out.u.DivertingLegInformation1.DivertedTo, &bc->redirecting.to);
  5701. print_facility(&bc->fac_out, bc);
  5702. misdn_lib_send_event(bc, EVENT_FACILITY);
  5703. }
  5704. bc->div_leg_3_tx_pending = 0;
  5705. /* Send DivertingLegInformation3 */
  5706. bc->fac_out.Function = Fac_DivertingLegInformation3;
  5707. bc->fac_out.u.DivertingLegInformation3.InvokeID = ++misdn_invoke_id;
  5708. bc->fac_out.u.DivertingLegInformation3.PresentationAllowedIndicator =
  5709. bc->redirecting.to.presentation == 0 ? 1 : 0;
  5710. print_facility(&bc->fac_out, bc);
  5711. misdn_lib_send_event(bc, EVENT_FACILITY);
  5712. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  5713. }
  5714. }
  5715. /*****************************/
  5716. /*** AST Indications Start ***/
  5717. /*****************************/
  5718. static int misdn_call(struct ast_channel *ast, const char *dest, int timeout)
  5719. {
  5720. int port = 0;
  5721. int r;
  5722. int exceed;
  5723. int number_type;
  5724. struct chan_list *ch;
  5725. struct misdn_bchannel *newbc;
  5726. char *dest_cp;
  5727. int append_msn = 0;
  5728. AST_DECLARE_APP_ARGS(args,
  5729. AST_APP_ARG(intf); /* The interface token is discarded. */
  5730. AST_APP_ARG(ext); /* extension token */
  5731. AST_APP_ARG(opts); /* options token */
  5732. );
  5733. if (!ast) {
  5734. ast_log(LOG_WARNING, " --> ! misdn_call called on ast_channel *ast where ast == NULL\n");
  5735. return -1;
  5736. }
  5737. if (((ast_channel_state(ast) != AST_STATE_DOWN) && (ast_channel_state(ast) != AST_STATE_RESERVED)) || !dest) {
  5738. ast_log(LOG_WARNING, " --> ! misdn_call called on %s, neither down nor reserved (or dest==NULL)\n", ast_channel_name(ast));
  5739. ast_channel_hangupcause_set(ast, AST_CAUSE_NORMAL_TEMPORARY_FAILURE);
  5740. ast_setstate(ast, AST_STATE_DOWN);
  5741. return -1;
  5742. }
  5743. ch = MISDN_ASTERISK_TECH_PVT(ast);
  5744. if (!ch) {
  5745. ast_log(LOG_WARNING, " --> ! misdn_call called on %s, chan_list *ch==NULL\n", ast_channel_name(ast));
  5746. ast_channel_hangupcause_set(ast, AST_CAUSE_NORMAL_TEMPORARY_FAILURE);
  5747. ast_setstate(ast, AST_STATE_DOWN);
  5748. return -1;
  5749. }
  5750. newbc = ch->bc;
  5751. if (!newbc) {
  5752. ast_log(LOG_WARNING, " --> ! misdn_call called on %s, newbc==NULL\n", ast_channel_name(ast));
  5753. ast_channel_hangupcause_set(ast, AST_CAUSE_NORMAL_TEMPORARY_FAILURE);
  5754. ast_setstate(ast, AST_STATE_DOWN);
  5755. return -1;
  5756. }
  5757. port = newbc->port;
  5758. #if defined(AST_MISDN_ENHANCEMENTS)
  5759. if ((ch->peer = misdn_cc_caller_get(ast))) {
  5760. chan_misdn_log(3, port, " --> Found CC caller data, peer:%s\n",
  5761. ch->peer->chan ? "available" : "NULL");
  5762. }
  5763. if (ch->record_id != -1) {
  5764. struct misdn_cc_record *cc_record;
  5765. /* This is a call completion retry call */
  5766. AST_LIST_LOCK(&misdn_cc_records_db);
  5767. cc_record = misdn_cc_find_by_id(ch->record_id);
  5768. if (!cc_record) {
  5769. AST_LIST_UNLOCK(&misdn_cc_records_db);
  5770. ast_log(LOG_WARNING, " --> ! misdn_call called on %s, cc_record==NULL\n", ast_channel_name(ast));
  5771. ast_channel_hangupcause_set(ast, AST_CAUSE_NORMAL_TEMPORARY_FAILURE);
  5772. ast_setstate(ast, AST_STATE_DOWN);
  5773. return -1;
  5774. }
  5775. /* Setup calling parameters to retry the call. */
  5776. newbc->dialed = cc_record->redial.dialed;
  5777. newbc->caller = cc_record->redial.caller;
  5778. memset(&newbc->redirecting, 0, sizeof(newbc->redirecting));
  5779. newbc->capability = cc_record->redial.capability;
  5780. newbc->hdlc = cc_record->redial.hdlc;
  5781. newbc->sending_complete = 1;
  5782. if (cc_record->ptp) {
  5783. newbc->fac_out.Function = Fac_CCBS_T_Call;
  5784. newbc->fac_out.u.CCBS_T_Call.InvokeID = ++misdn_invoke_id;
  5785. } else {
  5786. newbc->fac_out.Function = Fac_CCBSCall;
  5787. newbc->fac_out.u.CCBSCall.InvokeID = ++misdn_invoke_id;
  5788. newbc->fac_out.u.CCBSCall.CCBSReference = cc_record->mode.ptmp.reference_id;
  5789. }
  5790. AST_LIST_UNLOCK(&misdn_cc_records_db);
  5791. ast_channel_exten_set(ast, newbc->dialed.number);
  5792. chan_misdn_log(1, port, "* Call completion to: %s\n", newbc->dialed.number);
  5793. chan_misdn_log(2, port, " --> * tech:%s context:%s\n", ast_channel_name(ast), ast_channel_context(ast));
  5794. } else
  5795. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  5796. {
  5797. struct ast_party_id connected_id = ast_channel_connected_effective_id(ast);
  5798. /*
  5799. * dest is ---v
  5800. * Dial(mISDN/g:group_name[/extension[/options]])
  5801. * Dial(mISDN/port[:preselected_channel][/extension[/options]])
  5802. *
  5803. * The dial extension could be empty if you are using MISDN_KEYPAD
  5804. * to control ISDN provider features.
  5805. */
  5806. dest_cp = ast_strdupa(dest);
  5807. AST_NONSTANDARD_APP_ARGS(args, dest_cp, '/');
  5808. if (!args.ext) {
  5809. args.ext = "";
  5810. }
  5811. chan_misdn_log(1, port, "* CALL: %s\n", dest);
  5812. chan_misdn_log(2, port, " --> * dialed:%s tech:%s context:%s\n", args.ext, ast_channel_name(ast), ast_channel_context(ast));
  5813. ast_channel_exten_set(ast, args.ext);
  5814. ast_copy_string(newbc->dialed.number, args.ext, sizeof(newbc->dialed.number));
  5815. if (ast_strlen_zero(newbc->caller.name)
  5816. && connected_id.name.valid
  5817. && !ast_strlen_zero(connected_id.name.str)) {
  5818. ast_copy_string(newbc->caller.name, connected_id.name.str, sizeof(newbc->caller.name));
  5819. chan_misdn_log(3, port, " --> * set caller:\"%s\" <%s>\n", newbc->caller.name, newbc->caller.number);
  5820. }
  5821. if (ast_strlen_zero(newbc->caller.number)
  5822. && connected_id.number.valid
  5823. && !ast_strlen_zero(connected_id.number.str)) {
  5824. ast_copy_string(newbc->caller.number, connected_id.number.str, sizeof(newbc->caller.number));
  5825. chan_misdn_log(3, port, " --> * set caller:\"%s\" <%s>\n", newbc->caller.name, newbc->caller.number);
  5826. }
  5827. misdn_cfg_get(port, MISDN_CFG_APPEND_MSN_TO_CALLERID_TAG, &append_msn, sizeof(append_msn));
  5828. if (append_msn) {
  5829. strncat(newbc->incoming_cid_tag, "_", sizeof(newbc->incoming_cid_tag) - strlen(newbc->incoming_cid_tag) - 1);
  5830. strncat(newbc->incoming_cid_tag, newbc->caller.number, sizeof(newbc->incoming_cid_tag) - strlen(newbc->incoming_cid_tag) - 1);
  5831. }
  5832. ast_channel_caller(ast)->id.tag = ast_strdup(newbc->incoming_cid_tag);
  5833. misdn_cfg_get(port, MISDN_CFG_LOCALDIALPLAN, &number_type, sizeof(number_type));
  5834. if (number_type < 0) {
  5835. if (connected_id.number.valid) {
  5836. newbc->caller.number_type = ast_to_misdn_ton(connected_id.number.plan);
  5837. newbc->caller.number_plan = ast_to_misdn_plan(connected_id.number.plan);
  5838. } else {
  5839. newbc->caller.number_type = NUMTYPE_UNKNOWN;
  5840. newbc->caller.number_plan = NUMPLAN_ISDN;
  5841. }
  5842. } else {
  5843. /* Force us to send in SETUP message */
  5844. newbc->caller.number_type = number_type;
  5845. newbc->caller.number_plan = NUMPLAN_ISDN;
  5846. }
  5847. debug_numtype(port, newbc->caller.number_type, "LTON");
  5848. newbc->capability = ast_channel_transfercapability(ast);
  5849. pbx_builtin_setvar_helper(ast, "TRANSFERCAPABILITY", ast_transfercapability2str(newbc->capability));
  5850. if (ast_channel_transfercapability(ast) == INFO_CAPABILITY_DIGITAL_UNRESTRICTED) {
  5851. chan_misdn_log(2, port, " --> * Call with flag Digital\n");
  5852. }
  5853. /* update caller screening and presentation */
  5854. update_config(ch);
  5855. /* fill in some ies from channel dialplan variables */
  5856. import_ch(ast, newbc, ch);
  5857. /* Finally The Options Override Everything */
  5858. if (!ast_strlen_zero(args.opts)) {
  5859. misdn_set_opt_exec(ast, args.opts);
  5860. } else {
  5861. chan_misdn_log(2, port, "NO OPTS GIVEN\n");
  5862. }
  5863. if (newbc->set_presentation) {
  5864. newbc->caller.presentation = newbc->presentation;
  5865. }
  5866. misdn_copy_redirecting_from_ast(newbc, ast);
  5867. switch (newbc->outgoing_colp) {
  5868. case 1:/* restricted */
  5869. case 2:/* blocked */
  5870. newbc->redirecting.from.presentation = 1;/* restricted */
  5871. break;
  5872. default:
  5873. break;
  5874. }
  5875. #if defined(AST_MISDN_ENHANCEMENTS)
  5876. if (newbc->redirecting.from.number[0] && misdn_lib_is_ptp(port)) {
  5877. if (newbc->redirecting.count < 1) {
  5878. newbc->redirecting.count = 1;
  5879. }
  5880. /* Create DivertingLegInformation2 facility */
  5881. newbc->fac_out.Function = Fac_DivertingLegInformation2;
  5882. newbc->fac_out.u.DivertingLegInformation2.InvokeID = ++misdn_invoke_id;
  5883. newbc->fac_out.u.DivertingLegInformation2.DivertingPresent = 1;
  5884. misdn_PresentedNumberUnscreened_fill(
  5885. &newbc->fac_out.u.DivertingLegInformation2.Diverting,
  5886. &newbc->redirecting.from);
  5887. switch (newbc->outgoing_colp) {
  5888. case 2:/* blocked */
  5889. /* Block the number going out */
  5890. newbc->fac_out.u.DivertingLegInformation2.Diverting.Type = 1;/* presentationRestricted */
  5891. /* Don't tell about any previous diversions or why for that matter. */
  5892. newbc->fac_out.u.DivertingLegInformation2.DiversionCounter = 1;
  5893. newbc->fac_out.u.DivertingLegInformation2.DiversionReason = 0;/* unknown */
  5894. break;
  5895. default:
  5896. newbc->fac_out.u.DivertingLegInformation2.DiversionCounter =
  5897. newbc->redirecting.count;
  5898. newbc->fac_out.u.DivertingLegInformation2.DiversionReason =
  5899. misdn_to_diversion_reason(newbc->redirecting.reason);
  5900. break;
  5901. }
  5902. newbc->fac_out.u.DivertingLegInformation2.OriginalCalledPresent = 0;
  5903. if (1 < newbc->fac_out.u.DivertingLegInformation2.DiversionCounter) {
  5904. newbc->fac_out.u.DivertingLegInformation2.OriginalCalledPresent = 1;
  5905. newbc->fac_out.u.DivertingLegInformation2.OriginalCalled.Type = 2;/* numberNotAvailableDueToInterworking */
  5906. }
  5907. /*
  5908. * Expect a DivertingLegInformation3 to update the COLR of the
  5909. * redirecting-to party we are attempting to call now.
  5910. */
  5911. newbc->div_leg_3_rx_wanted = 1;
  5912. }
  5913. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  5914. }
  5915. exceed = add_out_calls(port);
  5916. if (exceed != 0) {
  5917. char tmp[16];
  5918. snprintf(tmp, sizeof(tmp), "%d", exceed);
  5919. pbx_builtin_setvar_helper(ast, "MAX_OVERFLOW", tmp);
  5920. ast_channel_hangupcause_set(ast, AST_CAUSE_NORMAL_TEMPORARY_FAILURE);
  5921. ast_setstate(ast, AST_STATE_DOWN);
  5922. return -1;
  5923. }
  5924. #if defined(AST_MISDN_ENHANCEMENTS)
  5925. if (newbc->fac_out.Function != Fac_None) {
  5926. print_facility(&newbc->fac_out, newbc);
  5927. }
  5928. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  5929. r = misdn_lib_send_event(newbc, EVENT_SETUP);
  5930. /** we should have l3id after sending setup **/
  5931. ch->l3id = newbc->l3_id;
  5932. if (r == -ENOCHAN) {
  5933. chan_misdn_log(0, port, " --> * Theres no Channel at the moment .. !\n");
  5934. chan_misdn_log(1, port, " --> * SEND: State Down pid:%d\n", newbc ? newbc->pid : -1);
  5935. ast_channel_hangupcause_set(ast, AST_CAUSE_NORMAL_CIRCUIT_CONGESTION);
  5936. ast_setstate(ast, AST_STATE_DOWN);
  5937. return -1;
  5938. }
  5939. chan_misdn_log(2, port, " --> * SEND: State Dialing pid:%d\n", newbc ? newbc->pid : 1);
  5940. ast_setstate(ast, AST_STATE_DIALING);
  5941. ast_channel_hangupcause_set(ast, AST_CAUSE_NORMAL_CLEARING);
  5942. if (newbc->nt) {
  5943. stop_bc_tones(ch);
  5944. }
  5945. ch->state = MISDN_CALLING;
  5946. return 0;
  5947. }
  5948. static int misdn_answer(struct ast_channel *ast)
  5949. {
  5950. struct chan_list *p;
  5951. const char *tmp;
  5952. if (!ast || !(p = MISDN_ASTERISK_TECH_PVT(ast))) {
  5953. return -1;
  5954. }
  5955. chan_misdn_log(1, p ? (p->bc ? p->bc->port : 0) : 0, "* ANSWER:\n");
  5956. if (!p) {
  5957. ast_log(LOG_WARNING, " --> Channel not connected ??\n");
  5958. ast_queue_hangup_with_cause(ast, AST_CAUSE_NETWORK_OUT_OF_ORDER);
  5959. }
  5960. if (!p->bc) {
  5961. chan_misdn_log(1, 0, " --> Got Answer, but there is no bc obj ??\n");
  5962. ast_queue_hangup_with_cause(ast, AST_CAUSE_PROTOCOL_ERROR);
  5963. }
  5964. ast_channel_lock(ast);
  5965. tmp = pbx_builtin_getvar_helper(ast, "CRYPT_KEY");
  5966. if (!ast_strlen_zero(tmp)) {
  5967. chan_misdn_log(1, p->bc->port, " --> Connection will be BF crypted\n");
  5968. ast_copy_string(p->bc->crypt_key, tmp, sizeof(p->bc->crypt_key));
  5969. } else {
  5970. chan_misdn_log(3, p->bc->port, " --> Connection is without BF encryption\n");
  5971. }
  5972. tmp = pbx_builtin_getvar_helper(ast, "MISDN_DIGITAL_TRANS");
  5973. if (!ast_strlen_zero(tmp) && ast_true(tmp)) {
  5974. chan_misdn_log(1, p->bc->port, " --> Connection is transparent digital\n");
  5975. p->bc->nodsp = 1;
  5976. p->bc->hdlc = 0;
  5977. p->bc->nojitter = 1;
  5978. }
  5979. ast_channel_unlock(ast);
  5980. p->state = MISDN_CONNECTED;
  5981. stop_indicate(p);
  5982. if (ast_strlen_zero(p->bc->connected.number)) {
  5983. chan_misdn_log(2,p->bc->port," --> empty connected number using dialed number\n");
  5984. ast_copy_string(p->bc->connected.number, p->bc->dialed.number, sizeof(p->bc->connected.number));
  5985. /*
  5986. * Use the misdn_set_opt() application to set the presentation
  5987. * before we answer or you can use the CONECTEDLINE() function
  5988. * to set everything before using the Answer() application.
  5989. */
  5990. p->bc->connected.presentation = p->bc->presentation;
  5991. p->bc->connected.screening = 0; /* unscreened */
  5992. p->bc->connected.number_type = p->bc->dialed.number_type;
  5993. p->bc->connected.number_plan = p->bc->dialed.number_plan;
  5994. }
  5995. switch (p->bc->outgoing_colp) {
  5996. case 1:/* restricted */
  5997. case 2:/* blocked */
  5998. p->bc->connected.presentation = 1;/* restricted */
  5999. break;
  6000. default:
  6001. break;
  6002. }
  6003. #if defined(AST_MISDN_ENHANCEMENTS)
  6004. if (p->bc->div_leg_3_tx_pending) {
  6005. p->bc->div_leg_3_tx_pending = 0;
  6006. /* Send DivertingLegInformation3 */
  6007. p->bc->fac_out.Function = Fac_DivertingLegInformation3;
  6008. p->bc->fac_out.u.DivertingLegInformation3.InvokeID = ++misdn_invoke_id;
  6009. p->bc->fac_out.u.DivertingLegInformation3.PresentationAllowedIndicator =
  6010. (p->bc->connected.presentation == 0) ? 1 : 0;
  6011. print_facility(&p->bc->fac_out, p->bc);
  6012. }
  6013. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  6014. misdn_lib_send_event(p->bc, EVENT_CONNECT);
  6015. start_bc_tones(p);
  6016. return 0;
  6017. }
  6018. static int misdn_digit_begin(struct ast_channel *chan, char digit)
  6019. {
  6020. /* XXX Modify this callback to support Asterisk controlling the length of DTMF */
  6021. return 0;
  6022. }
  6023. static int misdn_digit_end(struct ast_channel *ast, char digit, unsigned int duration)
  6024. {
  6025. struct chan_list *p;
  6026. struct misdn_bchannel *bc;
  6027. char buf[2] = { digit, 0 };
  6028. if (!ast || !(p = MISDN_ASTERISK_TECH_PVT(ast))) {
  6029. return -1;
  6030. }
  6031. bc = p->bc;
  6032. chan_misdn_log(1, bc ? bc->port : 0, "* IND : Digit %c\n", digit);
  6033. if (!bc) {
  6034. ast_log(LOG_WARNING, " --> !! Got Digit Event without having bchannel Object\n");
  6035. return -1;
  6036. }
  6037. switch (p->state) {
  6038. case MISDN_CALLING:
  6039. if (strlen(bc->infos_pending) < sizeof(bc->infos_pending) - 1) {
  6040. strncat(bc->infos_pending, buf, sizeof(bc->infos_pending) - strlen(bc->infos_pending) - 1);
  6041. }
  6042. break;
  6043. case MISDN_CALLING_ACKNOWLEDGE:
  6044. ast_copy_string(bc->info_dad, buf, sizeof(bc->info_dad));
  6045. if (strlen(bc->dialed.number) < sizeof(bc->dialed.number) - 1) {
  6046. strncat(bc->dialed.number, buf, sizeof(bc->dialed.number) - strlen(bc->dialed.number) - 1);
  6047. }
  6048. ast_channel_exten_set(p->ast, bc->dialed.number);
  6049. misdn_lib_send_event(bc, EVENT_INFORMATION);
  6050. break;
  6051. default:
  6052. if (bc->send_dtmf) {
  6053. send_digit_to_chan(p, digit);
  6054. }
  6055. break;
  6056. }
  6057. return 0;
  6058. }
  6059. static int misdn_fixup(struct ast_channel *oldast, struct ast_channel *ast)
  6060. {
  6061. struct chan_list *p;
  6062. if (!ast || !(p = MISDN_ASTERISK_TECH_PVT(ast))) {
  6063. return -1;
  6064. }
  6065. 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);
  6066. p->ast = ast;
  6067. return 0;
  6068. }
  6069. static int misdn_indication(struct ast_channel *ast, int cond, const void *data, size_t datalen)
  6070. {
  6071. struct chan_list *p;
  6072. if (!ast || !(p = MISDN_ASTERISK_TECH_PVT(ast))) {
  6073. ast_log(LOG_WARNING, "Returned -1 in misdn_indication\n");
  6074. return -1;
  6075. }
  6076. if (!p->bc) {
  6077. if (p->hold.state == MISDN_HOLD_IDLE) {
  6078. chan_misdn_log(1, 0, "* IND : Indication [%d] ignored on %s\n", cond,
  6079. ast_channel_name(ast));
  6080. ast_log(LOG_WARNING, "Private Pointer but no bc ?\n");
  6081. } else {
  6082. chan_misdn_log(1, 0, "* IND : Indication [%d] ignored on hold %s\n",
  6083. cond, ast_channel_name(ast));
  6084. }
  6085. return -1;
  6086. }
  6087. chan_misdn_log(5, p->bc->port, "* IND : Indication [%d] on %s\n", cond, ast_channel_name(ast));
  6088. switch (cond) {
  6089. case AST_CONTROL_BUSY:
  6090. chan_misdn_log(1, p->bc->port, "* IND :\tbusy pid:%d\n", p->bc->pid);
  6091. ast_setstate(ast, AST_STATE_BUSY);
  6092. p->bc->out_cause = AST_CAUSE_USER_BUSY;
  6093. if (p->state != MISDN_CONNECTED) {
  6094. start_bc_tones(p);
  6095. misdn_lib_send_event(p->bc, EVENT_DISCONNECT);
  6096. }
  6097. return -1;
  6098. case AST_CONTROL_RING:
  6099. chan_misdn_log(1, p->bc->port, "* IND :\tring pid:%d\n", p->bc->pid);
  6100. return -1;
  6101. case AST_CONTROL_RINGING:
  6102. chan_misdn_log(1, p->bc->port, "* IND :\tringing pid:%d\n", p->bc->pid);
  6103. switch (p->state) {
  6104. case MISDN_ALERTING:
  6105. chan_misdn_log(2, p->bc->port, " --> * IND :\tringing pid:%d but I was Ringing before, so ignoring it\n", p->bc->pid);
  6106. break;
  6107. case MISDN_CONNECTED:
  6108. 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);
  6109. return -1;
  6110. default:
  6111. p->state = MISDN_ALERTING;
  6112. chan_misdn_log(2, p->bc->port, " --> * IND :\tringing pid:%d\n", p->bc->pid);
  6113. misdn_lib_send_event(p->bc, EVENT_ALERTING);
  6114. chan_misdn_log(3, p->bc->port, " --> * SEND: State Ring pid:%d\n", p->bc->pid);
  6115. ast_setstate(ast, AST_STATE_RING);
  6116. if (!p->bc->nt && (p->originator == ORG_MISDN) && !p->incoming_early_audio) {
  6117. chan_misdn_log(2, p->bc->port, " --> incoming_early_audio off\n");
  6118. } else {
  6119. return -1;
  6120. }
  6121. }
  6122. break;
  6123. case AST_CONTROL_ANSWER:
  6124. chan_misdn_log(1, p->bc->port, " --> * IND :\tanswer pid:%d\n", p->bc->pid);
  6125. start_bc_tones(p);
  6126. break;
  6127. case AST_CONTROL_TAKEOFFHOOK:
  6128. chan_misdn_log(1, p->bc->port, " --> *\ttakeoffhook pid:%d\n", p->bc->pid);
  6129. return -1;
  6130. case AST_CONTROL_OFFHOOK:
  6131. chan_misdn_log(1, p->bc->port, " --> *\toffhook pid:%d\n", p->bc->pid);
  6132. return -1;
  6133. case AST_CONTROL_FLASH:
  6134. chan_misdn_log(1, p->bc->port, " --> *\tflash pid:%d\n", p->bc->pid);
  6135. break;
  6136. case AST_CONTROL_PROGRESS:
  6137. chan_misdn_log(1, p->bc->port, " --> * IND :\tprogress pid:%d\n", p->bc->pid);
  6138. misdn_lib_send_event(p->bc, EVENT_PROGRESS);
  6139. break;
  6140. case AST_CONTROL_PROCEEDING:
  6141. chan_misdn_log(1, p->bc->port, " --> * IND :\tproceeding pid:%d\n", p->bc->pid);
  6142. misdn_lib_send_event(p->bc, EVENT_PROCEEDING);
  6143. break;
  6144. case AST_CONTROL_INCOMPLETE:
  6145. chan_misdn_log(1, p->bc->port, " --> *\tincomplete pid:%d\n", p->bc->pid);
  6146. if (!p->overlap_dial) {
  6147. /* Overlapped dialing not enabled - send hangup */
  6148. p->bc->out_cause = AST_CAUSE_INVALID_NUMBER_FORMAT;
  6149. start_bc_tones(p);
  6150. misdn_lib_send_event(p->bc, EVENT_DISCONNECT);
  6151. if (p->bc->nt) {
  6152. hanguptone_indicate(p);
  6153. }
  6154. }
  6155. break;
  6156. case AST_CONTROL_CONGESTION:
  6157. chan_misdn_log(1, p->bc->port, " --> * IND :\tcongestion pid:%d\n", p->bc->pid);
  6158. p->bc->out_cause = AST_CAUSE_SWITCH_CONGESTION;
  6159. start_bc_tones(p);
  6160. misdn_lib_send_event(p->bc, EVENT_DISCONNECT);
  6161. if (p->bc->nt) {
  6162. hanguptone_indicate(p);
  6163. }
  6164. break;
  6165. case -1 :
  6166. chan_misdn_log(1, p->bc->port, " --> * IND :\t-1! (stop indication) pid:%d\n", p->bc->pid);
  6167. stop_indicate(p);
  6168. if (p->state == MISDN_CONNECTED) {
  6169. start_bc_tones(p);
  6170. }
  6171. break;
  6172. case AST_CONTROL_HOLD:
  6173. ast_moh_start(ast, data, p->mohinterpret);
  6174. chan_misdn_log(1, p->bc->port, " --> *\tHOLD pid:%d\n", p->bc->pid);
  6175. break;
  6176. case AST_CONTROL_UNHOLD:
  6177. ast_moh_stop(ast);
  6178. chan_misdn_log(1, p->bc->port, " --> *\tUNHOLD pid:%d\n", p->bc->pid);
  6179. break;
  6180. case AST_CONTROL_CONNECTED_LINE:
  6181. chan_misdn_log(1, p->bc->port, "* IND :\tconnected line update pid:%d\n", p->bc->pid);
  6182. misdn_update_connected_line(ast, p->bc, p->originator);
  6183. break;
  6184. case AST_CONTROL_REDIRECTING:
  6185. chan_misdn_log(1, p->bc->port, "* IND :\tredirecting info update pid:%d\n", p->bc->pid);
  6186. misdn_update_redirecting(ast, p->bc, p->originator);
  6187. break;
  6188. default:
  6189. chan_misdn_log(1, p->bc->port, " --> * Unknown Indication:%d pid:%d\n", cond, p->bc->pid);
  6190. /* fallthrough */
  6191. case AST_CONTROL_PVT_CAUSE_CODE:
  6192. case AST_CONTROL_MASQUERADE_NOTIFY:
  6193. return -1;
  6194. }
  6195. return 0;
  6196. }
  6197. static int misdn_hangup(struct ast_channel *ast)
  6198. {
  6199. struct chan_list *p;
  6200. struct misdn_bchannel *bc;
  6201. const char *var;
  6202. if (!ast) {
  6203. return -1;
  6204. }
  6205. ast_debug(1, "misdn_hangup(%s)\n", ast_channel_name(ast));
  6206. /* Take the ast_channel's tech_pvt reference. */
  6207. ast_mutex_lock(&release_lock);
  6208. p = MISDN_ASTERISK_TECH_PVT(ast);
  6209. if (!p) {
  6210. ast_mutex_unlock(&release_lock);
  6211. return -1;
  6212. }
  6213. MISDN_ASTERISK_TECH_PVT_SET(ast, NULL);
  6214. if (!misdn_chan_is_valid(p)) {
  6215. ast_mutex_unlock(&release_lock);
  6216. chan_list_unref(p, "Release ast_channel reference. Was not active?");
  6217. return 0;
  6218. }
  6219. if (p->hold.state == MISDN_HOLD_IDLE) {
  6220. bc = p->bc;
  6221. } else {
  6222. p->hold.state = MISDN_HOLD_DISCONNECT;
  6223. bc = misdn_lib_find_held_bc(p->hold.port, p->l3id);
  6224. if (!bc) {
  6225. chan_misdn_log(4, p->hold.port,
  6226. "misdn_hangup: Could not find held bc for (%s)\n", ast_channel_name(ast));
  6227. release_chan_early(p);
  6228. ast_mutex_unlock(&release_lock);
  6229. chan_list_unref(p, "Release ast_channel reference");
  6230. return 0;
  6231. }
  6232. }
  6233. if (ast_channel_state(ast) == AST_STATE_RESERVED || p->state == MISDN_NOTHING) {
  6234. /* between request and call */
  6235. ast_debug(1, "State Reserved (or nothing) => chanIsAvail\n");
  6236. release_chan_early(p);
  6237. if (bc) {
  6238. misdn_lib_release(bc);
  6239. }
  6240. ast_mutex_unlock(&release_lock);
  6241. chan_list_unref(p, "Release ast_channel reference");
  6242. return 0;
  6243. }
  6244. if (!bc) {
  6245. ast_log(LOG_WARNING, "Hangup with private but no bc ? state:%s l3id:%x\n",
  6246. misdn_get_ch_state(p), p->l3id);
  6247. release_chan_early(p);
  6248. ast_mutex_unlock(&release_lock);
  6249. chan_list_unref(p, "Release ast_channel reference");
  6250. return 0;
  6251. }
  6252. p->ast = NULL;
  6253. p->need_hangup = 0;
  6254. p->need_queue_hangup = 0;
  6255. p->need_busy = 0;
  6256. if (!bc->nt) {
  6257. stop_bc_tones(p);
  6258. }
  6259. bc->out_cause = ast_channel_hangupcause(ast) ? ast_channel_hangupcause(ast) : AST_CAUSE_NORMAL_CLEARING;
  6260. /* Channel lock is already held when we are called. */
  6261. //ast_channel_lock(ast);
  6262. var = pbx_builtin_getvar_helper(ast, "HANGUPCAUSE");
  6263. if (!var) {
  6264. var = pbx_builtin_getvar_helper(ast, "PRI_CAUSE");
  6265. }
  6266. if (var) {
  6267. int tmpcause;
  6268. tmpcause = atoi(var);
  6269. bc->out_cause = tmpcause ? tmpcause : AST_CAUSE_NORMAL_CLEARING;
  6270. }
  6271. var = pbx_builtin_getvar_helper(ast, "MISDN_USERUSER");
  6272. if (var) {
  6273. ast_log(LOG_NOTICE, "MISDN_USERUSER: %s\n", var);
  6274. ast_copy_string(bc->uu, var, sizeof(bc->uu));
  6275. bc->uulen = strlen(bc->uu);
  6276. }
  6277. //ast_channel_unlock(ast);
  6278. chan_misdn_log(1, bc->port,
  6279. "* IND : HANGUP\tpid:%d context:%s dialed:%s caller:\"%s\" <%s> State:%s\n",
  6280. bc->pid,
  6281. ast_channel_context(ast),
  6282. ast_channel_exten(ast),
  6283. (ast_channel_caller(ast)->id.name.valid && ast_channel_caller(ast)->id.name.str)
  6284. ? ast_channel_caller(ast)->id.name.str : "",
  6285. (ast_channel_caller(ast)->id.number.valid && ast_channel_caller(ast)->id.number.str)
  6286. ? ast_channel_caller(ast)->id.number.str : "",
  6287. misdn_get_ch_state(p));
  6288. chan_misdn_log(3, bc->port, " --> l3id:%x\n", p->l3id);
  6289. chan_misdn_log(3, bc->port, " --> cause:%d\n", bc->cause);
  6290. chan_misdn_log(2, bc->port, " --> out_cause:%d\n", bc->out_cause);
  6291. switch (p->state) {
  6292. case MISDN_INCOMING_SETUP:
  6293. /*
  6294. * This is the only place in misdn_hangup, where we
  6295. * can call release_chan, else it might create a lot of trouble.
  6296. */
  6297. ast_log(LOG_NOTICE, "release channel, in INCOMING_SETUP state.. no other events happened\n");
  6298. release_chan(p, bc);
  6299. misdn_lib_send_event(bc, EVENT_RELEASE_COMPLETE);
  6300. ast_mutex_unlock(&release_lock);
  6301. chan_list_unref(p, "Release ast_channel reference");
  6302. return 0;
  6303. case MISDN_DIALING:
  6304. if (p->hold.state == MISDN_HOLD_IDLE) {
  6305. start_bc_tones(p);
  6306. hanguptone_indicate(p);
  6307. }
  6308. if (bc->need_disconnect) {
  6309. misdn_lib_send_event(bc, EVENT_DISCONNECT);
  6310. }
  6311. break;
  6312. case MISDN_CALLING_ACKNOWLEDGE:
  6313. if (p->hold.state == MISDN_HOLD_IDLE) {
  6314. start_bc_tones(p);
  6315. hanguptone_indicate(p);
  6316. }
  6317. if (bc->need_disconnect) {
  6318. misdn_lib_send_event(bc, EVENT_DISCONNECT);
  6319. }
  6320. break;
  6321. case MISDN_CALLING:
  6322. case MISDN_ALERTING:
  6323. case MISDN_PROGRESS:
  6324. case MISDN_PROCEEDING:
  6325. if (p->originator != ORG_AST && p->hold.state == MISDN_HOLD_IDLE) {
  6326. hanguptone_indicate(p);
  6327. }
  6328. if (bc->need_disconnect) {
  6329. misdn_lib_send_event(bc, EVENT_DISCONNECT);
  6330. }
  6331. break;
  6332. case MISDN_CONNECTED:
  6333. /* Alerting or Disconnect */
  6334. if (bc->nt && p->hold.state == MISDN_HOLD_IDLE) {
  6335. start_bc_tones(p);
  6336. hanguptone_indicate(p);
  6337. bc->progress_indicator = INFO_PI_INBAND_AVAILABLE;
  6338. }
  6339. if (bc->need_disconnect) {
  6340. misdn_lib_send_event(bc, EVENT_DISCONNECT);
  6341. }
  6342. break;
  6343. case MISDN_DISCONNECTED:
  6344. if (bc->need_release) {
  6345. misdn_lib_send_event(bc, EVENT_RELEASE);
  6346. }
  6347. break;
  6348. case MISDN_CLEANING:
  6349. ast_mutex_unlock(&release_lock);
  6350. chan_list_unref(p, "Release ast_channel reference");
  6351. return 0;
  6352. case MISDN_BUSY:
  6353. break;
  6354. default:
  6355. if (bc->nt) {
  6356. bc->out_cause = -1;
  6357. if (bc->need_release) {
  6358. misdn_lib_send_event(bc, EVENT_RELEASE);
  6359. }
  6360. } else {
  6361. if (bc->need_disconnect) {
  6362. misdn_lib_send_event(bc, EVENT_DISCONNECT);
  6363. }
  6364. }
  6365. break;
  6366. }
  6367. p->state = MISDN_CLEANING;
  6368. chan_misdn_log(3, bc->port, " --> Channel: %s hungup new state:%s\n", ast_channel_name(ast),
  6369. misdn_get_ch_state(p));
  6370. ast_mutex_unlock(&release_lock);
  6371. chan_list_unref(p, "Release ast_channel reference");
  6372. return 0;
  6373. }
  6374. static struct ast_frame *process_ast_dsp(struct chan_list *tmp, struct ast_frame *frame)
  6375. {
  6376. struct ast_frame *f;
  6377. if (tmp->dsp) {
  6378. f = ast_dsp_process(tmp->ast, tmp->dsp, frame);
  6379. } else {
  6380. chan_misdn_log(0, tmp->bc->port, "No DSP-Path found\n");
  6381. return NULL;
  6382. }
  6383. if (!f || (f->frametype != AST_FRAME_DTMF)) {
  6384. return f;
  6385. }
  6386. ast_debug(1, "Detected inband DTMF digit: %c\n", f->subclass.integer);
  6387. if (tmp->faxdetect && (f->subclass.integer == 'f')) {
  6388. /* Fax tone -- Handle and return NULL */
  6389. if (!tmp->faxhandled) {
  6390. struct ast_channel *ast = tmp->ast;
  6391. tmp->faxhandled++;
  6392. chan_misdn_log(0, tmp->bc->port, "Fax detected, preparing %s for fax transfer.\n", ast_channel_name(ast));
  6393. tmp->bc->rxgain = 0;
  6394. isdn_lib_update_rxgain(tmp->bc);
  6395. tmp->bc->txgain = 0;
  6396. isdn_lib_update_txgain(tmp->bc);
  6397. #ifdef MISDN_1_2
  6398. *tmp->bc->pipeline = 0;
  6399. #else
  6400. tmp->bc->ec_enable = 0;
  6401. #endif
  6402. isdn_lib_update_ec(tmp->bc);
  6403. isdn_lib_stop_dtmf(tmp->bc);
  6404. switch (tmp->faxdetect) {
  6405. case 1:
  6406. if (strcmp(ast_channel_exten(ast), "fax")) {
  6407. const char *context;
  6408. char context_tmp[BUFFERSIZE];
  6409. misdn_cfg_get(tmp->bc->port, MISDN_CFG_FAXDETECT_CONTEXT, &context_tmp, sizeof(context_tmp));
  6410. context = S_OR(context_tmp, S_OR(ast_channel_macrocontext(ast), ast_channel_context(ast)));
  6411. if (ast_exists_extension(ast, context, "fax", 1,
  6412. S_COR(ast_channel_caller(ast)->id.number.valid, ast_channel_caller(ast)->id.number.str, NULL))) {
  6413. ast_verb(3, "Redirecting %s to fax extension (context:%s)\n", ast_channel_name(ast), context);
  6414. /* Save the DID/DNIS when we transfer the fax call to a "fax" extension */
  6415. pbx_builtin_setvar_helper(ast,"FAXEXTEN",ast_channel_exten(ast));
  6416. if (ast_async_goto(ast, context, "fax", 1)) {
  6417. ast_log(LOG_WARNING, "Failed to async goto '%s' into fax of '%s'\n", ast_channel_name(ast), context);
  6418. }
  6419. } else {
  6420. ast_log(LOG_NOTICE, "Fax detected but no fax extension, context:%s exten:%s\n", context, ast_channel_exten(ast));
  6421. }
  6422. } else {
  6423. ast_debug(1, "Already in a fax extension, not redirecting\n");
  6424. }
  6425. break;
  6426. case 2:
  6427. ast_verb(3, "Not redirecting %s to fax extension, nojump is set.\n", ast_channel_name(ast));
  6428. break;
  6429. default:
  6430. break;
  6431. }
  6432. } else {
  6433. ast_debug(1, "Fax already handled\n");
  6434. }
  6435. }
  6436. if (tmp->ast_dsp && (f->subclass.integer != 'f')) {
  6437. chan_misdn_log(2, tmp->bc->port, " --> * SEND: DTMF (AST_DSP) :%c\n", f->subclass.integer);
  6438. }
  6439. return f;
  6440. }
  6441. static struct ast_frame *misdn_read(struct ast_channel *ast)
  6442. {
  6443. struct chan_list *tmp;
  6444. int len, t;
  6445. struct pollfd pfd = { .fd = -1, .events = POLLIN };
  6446. if (!ast) {
  6447. chan_misdn_log(1, 0, "misdn_read called without ast\n");
  6448. return NULL;
  6449. }
  6450. if (!(tmp = MISDN_ASTERISK_TECH_PVT(ast))) {
  6451. chan_misdn_log(1, 0, "misdn_read called without ast->pvt\n");
  6452. return NULL;
  6453. }
  6454. if (!tmp->bc && tmp->hold.state == MISDN_HOLD_IDLE) {
  6455. chan_misdn_log(1, 0, "misdn_read called without bc\n");
  6456. return NULL;
  6457. }
  6458. pfd.fd = tmp->pipe[0];
  6459. t = ast_poll(&pfd, 1, 20);
  6460. if (t < 0) {
  6461. chan_misdn_log(-1, tmp->bc->port, "poll() error (err=%s)\n", strerror(errno));
  6462. return NULL;
  6463. }
  6464. if (!t) {
  6465. chan_misdn_log(3, tmp->bc->port, "poll() timed out\n");
  6466. len = 160;
  6467. } else if (pfd.revents & POLLIN) {
  6468. len = read(tmp->pipe[0], tmp->ast_rd_buf, sizeof(tmp->ast_rd_buf));
  6469. if (len <= 0) {
  6470. /* we hangup here, since our pipe is closed */
  6471. chan_misdn_log(2, tmp->bc->port, "misdn_read: Pipe closed, hanging up\n");
  6472. return NULL;
  6473. }
  6474. } else {
  6475. return NULL;
  6476. }
  6477. tmp->frame.frametype = AST_FRAME_VOICE;
  6478. ast_format_set(&tmp->frame.subclass.format, AST_FORMAT_ALAW, 0);
  6479. tmp->frame.datalen = len;
  6480. tmp->frame.samples = len;
  6481. tmp->frame.mallocd = 0;
  6482. tmp->frame.offset = 0;
  6483. tmp->frame.delivery = ast_tv(0, 0);
  6484. tmp->frame.src = NULL;
  6485. tmp->frame.data.ptr = tmp->ast_rd_buf;
  6486. if (tmp->faxdetect && !tmp->faxhandled) {
  6487. if (tmp->faxdetect_timeout) {
  6488. if (ast_tvzero(tmp->faxdetect_tv)) {
  6489. tmp->faxdetect_tv = ast_tvnow();
  6490. chan_misdn_log(2, tmp->bc->port, "faxdetect: starting detection with timeout: %ds ...\n", tmp->faxdetect_timeout);
  6491. return process_ast_dsp(tmp, &tmp->frame);
  6492. } else {
  6493. struct timeval tv_now = ast_tvnow();
  6494. int diff = ast_tvdiff_ms(tv_now, tmp->faxdetect_tv);
  6495. if (diff <= (tmp->faxdetect_timeout * 1000)) {
  6496. chan_misdn_log(5, tmp->bc->port, "faxdetect: detecting ...\n");
  6497. return process_ast_dsp(tmp, &tmp->frame);
  6498. } else {
  6499. chan_misdn_log(2, tmp->bc->port, "faxdetect: stopping detection (time ran out) ...\n");
  6500. tmp->faxdetect = 0;
  6501. return &tmp->frame;
  6502. }
  6503. }
  6504. } else {
  6505. chan_misdn_log(5, tmp->bc->port, "faxdetect: detecting ... (no timeout)\n");
  6506. return process_ast_dsp(tmp, &tmp->frame);
  6507. }
  6508. } else {
  6509. if (tmp->ast_dsp) {
  6510. return process_ast_dsp(tmp, &tmp->frame);
  6511. } else {
  6512. return &tmp->frame;
  6513. }
  6514. }
  6515. }
  6516. static int misdn_write(struct ast_channel *ast, struct ast_frame *frame)
  6517. {
  6518. struct chan_list *ch;
  6519. if (!ast || !(ch = MISDN_ASTERISK_TECH_PVT(ast))) {
  6520. return -1;
  6521. }
  6522. if (ch->hold.state != MISDN_HOLD_IDLE) {
  6523. chan_misdn_log(7, 0, "misdn_write: Returning because hold active\n");
  6524. return 0;
  6525. }
  6526. if (!ch->bc) {
  6527. ast_log(LOG_WARNING, "private but no bc\n");
  6528. return -1;
  6529. }
  6530. if (ch->notxtone) {
  6531. chan_misdn_log(7, ch->bc->port, "misdn_write: Returning because notxtone\n");
  6532. return 0;
  6533. }
  6534. if (!frame->subclass.format.id) {
  6535. chan_misdn_log(4, ch->bc->port, "misdn_write: * prods us\n");
  6536. return 0;
  6537. }
  6538. if (ast_format_cmp(&frame->subclass.format, &prefformat) == AST_FORMAT_CMP_NOT_EQUAL) {
  6539. chan_misdn_log(-1, ch->bc->port, "Got Unsupported Frame with Format:%s\n", ast_getformatname(&frame->subclass.format));
  6540. return 0;
  6541. }
  6542. if (!frame->samples) {
  6543. chan_misdn_log(4, ch->bc->port, "misdn_write: zero write\n");
  6544. if (!strcmp(frame->src,"ast_prod")) {
  6545. chan_misdn_log(1, ch->bc->port, "misdn_write: state (%s) prodded.\n", misdn_get_ch_state(ch));
  6546. if (ch->ts) {
  6547. chan_misdn_log(4, ch->bc->port, "Starting Playtones\n");
  6548. misdn_lib_tone_generator_start(ch->bc);
  6549. }
  6550. return 0;
  6551. }
  6552. return -1;
  6553. }
  6554. if (!ch->bc->addr) {
  6555. chan_misdn_log(8, ch->bc->port, "misdn_write: no addr for bc dropping:%d\n", frame->samples);
  6556. return 0;
  6557. }
  6558. #ifdef MISDN_DEBUG
  6559. {
  6560. int i;
  6561. int max = 5 > frame->samples ? frame->samples : 5;
  6562. ast_debug(1, "write2mISDN %p %d bytes: ", p, frame->samples);
  6563. for (i = 0; i < max; i++) {
  6564. ast_debug(1, "%02hhx ", ((unsigned char *) frame->data.ptr)[i]);
  6565. }
  6566. }
  6567. #endif
  6568. switch (ch->bc->bc_state) {
  6569. case BCHAN_ACTIVATED:
  6570. case BCHAN_BRIDGED:
  6571. break;
  6572. default:
  6573. if (!ch->dropped_frame_cnt) {
  6574. chan_misdn_log(5, ch->bc->port,
  6575. "BC not active (nor bridged) dropping: %d frames addr:%x exten:%s cid:%s ch->state:%s bc_state:%d l3id:%x\n",
  6576. frame->samples, ch->bc->addr, ast_channel_exten(ast),
  6577. S_COR(ast_channel_caller(ast)->id.number.valid, ast_channel_caller(ast)->id.number.str, ""),
  6578. misdn_get_ch_state(ch), ch->bc->bc_state, ch->bc->l3_id);
  6579. }
  6580. if (++ch->dropped_frame_cnt > 100) {
  6581. ch->dropped_frame_cnt = 0;
  6582. chan_misdn_log(5, ch->bc->port, "BC not active (nor bridged) dropping: %d frames addr:%x dropped > 100 frames!\n", frame->samples, ch->bc->addr);
  6583. }
  6584. return 0;
  6585. }
  6586. chan_misdn_log(9, ch->bc->port, "Sending :%d bytes to MISDN\n", frame->samples);
  6587. if (!ch->bc->nojitter && misdn_cap_is_speech(ch->bc->capability)) {
  6588. /* Buffered Transmit (triggered by read from isdn side)*/
  6589. if (misdn_jb_fill(ch->jb, frame->data.ptr, frame->samples) < 0) {
  6590. if (ch->bc->active) {
  6591. cb_log(0, ch->bc->port, "Misdn Jitterbuffer Overflow.\n");
  6592. }
  6593. }
  6594. } else {
  6595. /* transmit without jitterbuffer */
  6596. misdn_lib_tx2misdn_frm(ch->bc, frame->data.ptr, frame->samples);
  6597. }
  6598. return 0;
  6599. }
  6600. #if defined(mISDN_NATIVE_BRIDGING)
  6601. static enum ast_bridge_result misdn_bridge(struct ast_channel *c0,
  6602. struct ast_channel *c1, int flags,
  6603. struct ast_frame **fo,
  6604. struct ast_channel **rc,
  6605. int timeoutms)
  6606. {
  6607. struct chan_list *ch1, *ch2;
  6608. struct ast_channel *carr[2], *who;
  6609. int to = -1;
  6610. struct ast_frame *f;
  6611. int p1_b, p2_b;
  6612. int bridging;
  6613. misdn_cfg_get(0, MISDN_GEN_BRIDGING, &bridging, sizeof(bridging));
  6614. if (!bridging) {
  6615. /* Native mISDN bridging globally disabled. */
  6616. return AST_BRIDGE_FAILED_NOWARN;
  6617. }
  6618. ch1 = get_chan_by_ast(c0);
  6619. if (!ch1) {
  6620. return AST_BRIDGE_FAILED;
  6621. }
  6622. ch2 = get_chan_by_ast(c1);
  6623. if (!ch2) {
  6624. chan_list_unref(ch1, "Failed to find ch2");
  6625. return AST_BRIDGE_FAILED;
  6626. }
  6627. carr[0] = c0;
  6628. carr[1] = c1;
  6629. misdn_cfg_get(ch1->bc->port, MISDN_CFG_BRIDGING, &p1_b, sizeof(p1_b));
  6630. misdn_cfg_get(ch2->bc->port, MISDN_CFG_BRIDGING, &p2_b, sizeof(p2_b));
  6631. if (!p1_b || !p2_b) {
  6632. ast_log(LOG_NOTICE, "Falling back to Asterisk bridging\n");
  6633. chan_list_unref(ch1, "Bridge fallback ch1");
  6634. chan_list_unref(ch2, "Bridge fallback ch2");
  6635. return AST_BRIDGE_FAILED_NOWARN;
  6636. }
  6637. /* make a mISDN_dsp conference */
  6638. chan_misdn_log(1, ch1->bc->port, "I SEND: Making conference with Number:%d\n", ch1->bc->pid + 1);
  6639. misdn_lib_bridge(ch1->bc, ch2->bc);
  6640. ast_verb(3, "Native bridging %s and %s\n", ast_channel_name(c0), ast_channel_name(c1));
  6641. chan_misdn_log(1, ch1->bc->port, "* Making Native Bridge between \"%s\" <%s> and \"%s\" <%s>\n",
  6642. ch1->bc->caller.name,
  6643. ch1->bc->caller.number,
  6644. ch2->bc->caller.name,
  6645. ch2->bc->caller.number);
  6646. if (!(flags & AST_BRIDGE_DTMF_CHANNEL_0)) {
  6647. ch1->ignore_dtmf = 1;
  6648. }
  6649. if (!(flags & AST_BRIDGE_DTMF_CHANNEL_1)) {
  6650. ch2->ignore_dtmf = 1;
  6651. }
  6652. for (;/*ever*/;) {
  6653. to = -1;
  6654. who = ast_waitfor_n(carr, 2, &to);
  6655. if (!who) {
  6656. ast_log(LOG_NOTICE, "misdn_bridge: empty read, breaking out\n");
  6657. break;
  6658. }
  6659. f = ast_read(who);
  6660. if (!f || (f->frametype == AST_FRAME_CONTROL && f->subclass.integer != AST_CONTROL_PVT_CAUSE_CODE)) {
  6661. /* got hangup .. */
  6662. if (!f) {
  6663. chan_misdn_log(4, ch1->bc->port, "Read Null Frame\n");
  6664. } else {
  6665. chan_misdn_log(4, ch1->bc->port, "Read Frame Control class:%d\n", f->subclass.integer);
  6666. }
  6667. *fo = f;
  6668. *rc = who;
  6669. break;
  6670. }
  6671. if (f->frametype == AST_FRAME_DTMF) {
  6672. chan_misdn_log(1, 0, "Read DTMF %d from %s\n", f->subclass.integer, ast_channel_exten(who));
  6673. *fo = f;
  6674. *rc = who;
  6675. break;
  6676. }
  6677. #if 0
  6678. if (f->frametype == AST_FRAME_VOICE) {
  6679. chan_misdn_log(1, ch1->bc->port, "I SEND: Splitting conference with Number:%d\n", ch1->bc->pid +1);
  6680. continue;
  6681. }
  6682. #endif
  6683. if (f->frametype == AST_FRAME_CONTROL && f->subclass.integer == AST_CONTROL_PVT_CAUSE_CODE) {
  6684. ast_channel_hangupcause_hash_set((who == c0) ? c1 : c0, f->data.ptr, f->datalen);
  6685. } else {
  6686. ast_write((who == c0) ? c1 : c0, f);
  6687. }
  6688. }
  6689. chan_misdn_log(1, ch1->bc->port, "I SEND: Splitting conference with Number:%d\n", ch1->bc->pid + 1);
  6690. misdn_lib_split_bridge(ch1->bc, ch2->bc);
  6691. chan_list_unref(ch1, "Bridge complete ch1");
  6692. chan_list_unref(ch2, "Bridge complete ch2");
  6693. return AST_BRIDGE_COMPLETE;
  6694. }
  6695. #endif /* defined(mISDN_NATIVE_BRIDGING) */
  6696. /** AST INDICATIONS END **/
  6697. static int dialtone_indicate(struct chan_list *cl)
  6698. {
  6699. struct ast_channel *ast = cl->ast;
  6700. int nd = 0;
  6701. if (!ast) {
  6702. chan_misdn_log(0, cl->bc->port, "No Ast in dialtone_indicate\n");
  6703. return -1;
  6704. }
  6705. misdn_cfg_get(cl->bc->port, MISDN_CFG_NODIALTONE, &nd, sizeof(nd));
  6706. if (nd) {
  6707. chan_misdn_log(1, cl->bc->port, "Not sending Dialtone, because config wants it\n");
  6708. return 0;
  6709. }
  6710. chan_misdn_log(3, cl->bc->port, " --> Dial\n");
  6711. cl->ts = ast_get_indication_tone(ast_channel_zone(ast), "dial");
  6712. if (cl->ts) {
  6713. cl->notxtone = 0;
  6714. cl->norxtone = 0;
  6715. /* This prods us in misdn_write */
  6716. ast_playtones_start(ast, 0, cl->ts->data, 0);
  6717. }
  6718. return 0;
  6719. }
  6720. static void hanguptone_indicate(struct chan_list *cl)
  6721. {
  6722. misdn_lib_send_tone(cl->bc, TONE_HANGUP);
  6723. }
  6724. static int stop_indicate(struct chan_list *cl)
  6725. {
  6726. struct ast_channel *ast = cl->ast;
  6727. if (!ast) {
  6728. chan_misdn_log(0, cl->bc->port, "No Ast in stop_indicate\n");
  6729. return -1;
  6730. }
  6731. chan_misdn_log(3, cl->bc->port, " --> None\n");
  6732. misdn_lib_tone_generator_stop(cl->bc);
  6733. ast_playtones_stop(ast);
  6734. if (cl->ts) {
  6735. cl->ts = ast_tone_zone_sound_unref(cl->ts);
  6736. }
  6737. return 0;
  6738. }
  6739. static int start_bc_tones(struct chan_list* cl)
  6740. {
  6741. misdn_lib_tone_generator_stop(cl->bc);
  6742. cl->notxtone = 0;
  6743. cl->norxtone = 0;
  6744. return 0;
  6745. }
  6746. static int stop_bc_tones(struct chan_list *cl)
  6747. {
  6748. if (!cl) {
  6749. return -1;
  6750. }
  6751. cl->notxtone = 1;
  6752. cl->norxtone = 1;
  6753. return 0;
  6754. }
  6755. /*!
  6756. * \internal
  6757. * \brief Destroy the chan_list object.
  6758. *
  6759. * \param obj chan_list object to destroy.
  6760. *
  6761. * \return Nothing
  6762. */
  6763. static void chan_list_destructor(void *obj)
  6764. {
  6765. struct chan_list *ch = obj;
  6766. #if defined(AST_MISDN_ENHANCEMENTS)
  6767. if (ch->peer) {
  6768. ao2_ref(ch->peer, -1);
  6769. ch->peer = NULL;
  6770. }
  6771. #endif /* AST_MISDN_ENHANCEMENTS */
  6772. if (ch->dsp) {
  6773. ast_dsp_free(ch->dsp);
  6774. ch->dsp = NULL;
  6775. }
  6776. /* releasing jitterbuffer */
  6777. if (ch->jb) {
  6778. misdn_jb_destroy(ch->jb);
  6779. ch->jb = NULL;
  6780. }
  6781. if (ch->overlap_dial) {
  6782. if (ch->overlap_dial_task != -1) {
  6783. misdn_tasks_remove(ch->overlap_dial_task);
  6784. ch->overlap_dial_task = -1;
  6785. }
  6786. ast_mutex_destroy(&ch->overlap_tv_lock);
  6787. }
  6788. if (-1 < ch->pipe[0]) {
  6789. close(ch->pipe[0]);
  6790. }
  6791. if (-1 < ch->pipe[1]) {
  6792. close(ch->pipe[1]);
  6793. }
  6794. }
  6795. /*! Returns a reference to the new chan_list. */
  6796. static struct chan_list *chan_list_init(int orig)
  6797. {
  6798. struct chan_list *cl;
  6799. cl = ao2_alloc(sizeof(*cl), chan_list_destructor);
  6800. if (!cl) {
  6801. chan_misdn_log(-1, 0, "misdn_request: malloc failed!");
  6802. return NULL;
  6803. }
  6804. cl->originator = orig;
  6805. cl->need_queue_hangup = 1;
  6806. cl->need_hangup = 1;
  6807. cl->need_busy = 1;
  6808. cl->overlap_dial_task = -1;
  6809. #if defined(AST_MISDN_ENHANCEMENTS)
  6810. cl->record_id = -1;
  6811. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  6812. cl->pipe[0] = -1;
  6813. cl->pipe[1] = -1;
  6814. return cl;
  6815. }
  6816. static struct ast_channel *misdn_request(const char *type, struct ast_format_cap *cap, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *data, int *cause)
  6817. {
  6818. struct ast_channel *ast;
  6819. char group[BUFFERSIZE + 1] = "";
  6820. char dial_str[128];
  6821. char *dest_cp;
  6822. char *p = NULL;
  6823. int channel = 0;
  6824. int port = 0;
  6825. struct misdn_bchannel *newbc = NULL;
  6826. int dec = 0;
  6827. #if defined(AST_MISDN_ENHANCEMENTS)
  6828. int cc_retry_call = 0; /* TRUE if this is a call completion retry call */
  6829. long record_id = -1;
  6830. struct misdn_cc_record *cc_record;
  6831. const char *err_msg;
  6832. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  6833. struct chan_list *cl;
  6834. AST_DECLARE_APP_ARGS(args,
  6835. AST_APP_ARG(intf); /* interface token */
  6836. AST_APP_ARG(ext); /* extension token */
  6837. AST_APP_ARG(opts); /* options token */
  6838. );
  6839. snprintf(dial_str, sizeof(dial_str), "%s/%s", misdn_type, data);
  6840. /*
  6841. * data is ---v
  6842. * Dial(mISDN/g:group_name[/extension[/options]])
  6843. * Dial(mISDN/port[:preselected_channel][/extension[/options]])
  6844. * Dial(mISDN/cc/cc-record-id)
  6845. *
  6846. * The dial extension could be empty if you are using MISDN_KEYPAD
  6847. * to control ISDN provider features.
  6848. */
  6849. dest_cp = ast_strdupa(data);
  6850. AST_NONSTANDARD_APP_ARGS(args, dest_cp, '/');
  6851. if (!args.ext) {
  6852. args.ext = "";
  6853. }
  6854. if (!ast_strlen_zero(args.intf)) {
  6855. if (args.intf[0] == 'g' && args.intf[1] == ':') {
  6856. /* We make a group call lets checkout which ports are in my group */
  6857. args.intf += 2;
  6858. ast_copy_string(group, args.intf, sizeof(group));
  6859. chan_misdn_log(2, 0, " --> Group Call group: %s\n", group);
  6860. #if defined(AST_MISDN_ENHANCEMENTS)
  6861. } else if (strcmp(args.intf, "cc") == 0) {
  6862. cc_retry_call = 1;
  6863. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  6864. } else if ((p = strchr(args.intf, ':'))) {
  6865. /* we have a preselected channel */
  6866. *p++ = 0;
  6867. channel = atoi(p);
  6868. port = atoi(args.intf);
  6869. chan_misdn_log(2, port, " --> Call on preselected Channel (%d).\n", channel);
  6870. } else {
  6871. port = atoi(args.intf);
  6872. }
  6873. } else {
  6874. ast_log(LOG_WARNING, " --> ! IND : Dial(%s) WITHOUT Port or Group, check extensions.conf\n", dial_str);
  6875. return NULL;
  6876. }
  6877. #if defined(AST_MISDN_ENHANCEMENTS)
  6878. if (cc_retry_call) {
  6879. if (ast_strlen_zero(args.ext)) {
  6880. ast_log(LOG_WARNING, " --> ! IND : Dial(%s) WITHOUT cc-record-id, check extensions.conf\n", dial_str);
  6881. return NULL;
  6882. }
  6883. if (!isdigit(*args.ext)) {
  6884. ast_log(LOG_WARNING, " --> ! IND : Dial(%s) cc-record-id must be a number.\n", dial_str);
  6885. return NULL;
  6886. }
  6887. record_id = atol(args.ext);
  6888. AST_LIST_LOCK(&misdn_cc_records_db);
  6889. cc_record = misdn_cc_find_by_id(record_id);
  6890. if (!cc_record) {
  6891. AST_LIST_UNLOCK(&misdn_cc_records_db);
  6892. err_msg = misdn_cc_record_not_found;
  6893. ast_log(LOG_WARNING, " --> ! IND : Dial(%s) %s.\n", dial_str, err_msg);
  6894. return NULL;
  6895. }
  6896. if (!cc_record->activated) {
  6897. AST_LIST_UNLOCK(&misdn_cc_records_db);
  6898. err_msg = "Call completion has not been activated";
  6899. ast_log(LOG_WARNING, " --> ! IND : Dial(%s) %s.\n", dial_str, err_msg);
  6900. return NULL;
  6901. }
  6902. port = cc_record->port;
  6903. AST_LIST_UNLOCK(&misdn_cc_records_db);
  6904. }
  6905. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  6906. if (misdn_cfg_is_group_method(group, METHOD_STANDARD_DEC)) {
  6907. chan_misdn_log(4, port, " --> STARTING STANDARD DEC...\n");
  6908. dec = 1;
  6909. }
  6910. if (!ast_strlen_zero(group)) {
  6911. char cfg_group[BUFFERSIZE + 1];
  6912. struct robin_list *rr = NULL;
  6913. /* Group dial */
  6914. if (misdn_cfg_is_group_method(group, METHOD_ROUND_ROBIN)) {
  6915. chan_misdn_log(4, port, " --> STARTING ROUND ROBIN...\n");
  6916. rr = get_robin_position(group);
  6917. }
  6918. if (rr) {
  6919. int port_start;
  6920. int bchan_start;
  6921. int port_up;
  6922. int check;
  6923. int maxbchans;
  6924. int wraped = 0;
  6925. if (!rr->port) {
  6926. rr->port = misdn_cfg_get_next_port_spin(0);
  6927. }
  6928. if (!rr->channel) {
  6929. rr->channel = 1;
  6930. }
  6931. bchan_start = rr->channel;
  6932. port_start = rr->port;
  6933. do {
  6934. misdn_cfg_get(rr->port, MISDN_CFG_GROUPNAME, cfg_group, sizeof(cfg_group));
  6935. if (strcasecmp(cfg_group, group)) {
  6936. wraped = 1;
  6937. rr->port = misdn_cfg_get_next_port_spin(rr->port);
  6938. rr->channel = 1;
  6939. continue;
  6940. }
  6941. misdn_cfg_get(rr->port, MISDN_CFG_PMP_L1_CHECK, &check, sizeof(check));
  6942. port_up = misdn_lib_port_up(rr->port, check);
  6943. if (!port_up) {
  6944. chan_misdn_log(1, rr->port, "L1 is not Up on this Port\n");
  6945. rr->port = misdn_cfg_get_next_port_spin(rr->port);
  6946. rr->channel = 1;
  6947. } else if (port_up < 0) {
  6948. ast_log(LOG_WARNING, "This port (%d) is blocked\n", rr->port);
  6949. rr->port = misdn_cfg_get_next_port_spin(rr->port);
  6950. rr->channel = 1;
  6951. } else {
  6952. chan_misdn_log(4, rr->port, "portup\n");
  6953. maxbchans = misdn_lib_get_maxchans(rr->port);
  6954. for (;rr->channel <= maxbchans;rr->channel++) {
  6955. /* ive come full circle and can stop now */
  6956. if (wraped && (rr->port == port_start) && (rr->channel == bchan_start)) {
  6957. break;
  6958. }
  6959. chan_misdn_log(4, rr->port, "Checking channel %d\n", rr->channel);
  6960. if ((newbc = misdn_lib_get_free_bc(rr->port, rr->channel, 0, 0))) {
  6961. chan_misdn_log(4, rr->port, " Success! Found port:%d channel:%d\n", newbc->port, newbc->channel);
  6962. rr->channel++;
  6963. break;
  6964. }
  6965. }
  6966. if (wraped && (rr->port == port_start) && (rr->channel <= bchan_start)) {
  6967. break;
  6968. } else if (!newbc || (rr->channel == maxbchans)) {
  6969. rr->port = misdn_cfg_get_next_port_spin(rr->port);
  6970. rr->channel = 1;
  6971. }
  6972. }
  6973. wraped = 1;
  6974. } while (!newbc && (rr->port > 0));
  6975. } else {
  6976. for (port = misdn_cfg_get_next_port(0); port > 0;
  6977. port = misdn_cfg_get_next_port(port)) {
  6978. misdn_cfg_get(port, MISDN_CFG_GROUPNAME, cfg_group, sizeof(cfg_group));
  6979. chan_misdn_log(3, port, "Group [%s] Port [%d]\n", group, port);
  6980. if (!strcasecmp(cfg_group, group)) {
  6981. int port_up;
  6982. int check;
  6983. misdn_cfg_get(port, MISDN_CFG_PMP_L1_CHECK, &check, sizeof(check));
  6984. port_up = misdn_lib_port_up(port, check);
  6985. chan_misdn_log(4, port, "portup:%d\n", port_up);
  6986. if (port_up > 0) {
  6987. newbc = misdn_lib_get_free_bc(port, 0, 0, dec);
  6988. if (newbc) {
  6989. break;
  6990. }
  6991. }
  6992. }
  6993. }
  6994. }
  6995. /* Group dial failed ?*/
  6996. if (!newbc) {
  6997. ast_log(LOG_WARNING,
  6998. "Could not Dial out on group '%s'.\n"
  6999. "\tEither the L2 and L1 on all of these ports where DOWN (see 'show application misdn_check_l2l1')\n"
  7000. "\tOr there was no free channel on none of the ports\n\n",
  7001. group);
  7002. return NULL;
  7003. }
  7004. } else {
  7005. /* 'Normal' Port dial * Port dial */
  7006. if (channel) {
  7007. chan_misdn_log(1, port, " --> preselected_channel: %d\n", channel);
  7008. }
  7009. newbc = misdn_lib_get_free_bc(port, channel, 0, dec);
  7010. if (!newbc) {
  7011. ast_log(LOG_WARNING, "Could not create channel on port:%d for Dial(%s)\n", port, dial_str);
  7012. return NULL;
  7013. }
  7014. }
  7015. /* create ast_channel and link all the objects together */
  7016. cl = chan_list_init(ORG_AST);
  7017. if (!cl) {
  7018. misdn_lib_release(newbc);
  7019. ast_log(LOG_ERROR, "Could not create call record for Dial(%s)\n", dial_str);
  7020. return NULL;
  7021. }
  7022. cl->bc = newbc;
  7023. ast = misdn_new(cl, AST_STATE_RESERVED, args.ext, NULL, cap, assignedids, requestor, port, channel);
  7024. if (!ast) {
  7025. chan_list_unref(cl, "Failed to create a new channel");
  7026. misdn_lib_release(newbc);
  7027. ast_log(LOG_ERROR, "Could not create Asterisk channel for Dial(%s)\n", dial_str);
  7028. return NULL;
  7029. }
  7030. #if defined(AST_MISDN_ENHANCEMENTS)
  7031. cl->record_id = record_id;
  7032. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  7033. /* register chan in local list */
  7034. cl_queue_chan(cl);
  7035. /* fill in the config into the objects */
  7036. read_config(cl);
  7037. /* important */
  7038. cl->need_hangup = 0;
  7039. chan_list_unref(cl, "Successful misdn_request()");
  7040. return ast;
  7041. }
  7042. static int misdn_send_text(struct ast_channel *chan, const char *text)
  7043. {
  7044. struct chan_list *tmp = MISDN_ASTERISK_TECH_PVT(chan);
  7045. if (tmp && tmp->bc) {
  7046. ast_copy_string(tmp->bc->display, text, sizeof(tmp->bc->display));
  7047. misdn_lib_send_event(tmp->bc, EVENT_INFORMATION);
  7048. } else {
  7049. ast_log(LOG_WARNING, "No chan_list but send_text request?\n");
  7050. return -1;
  7051. }
  7052. return 0;
  7053. }
  7054. static struct ast_channel_tech misdn_tech = {
  7055. .type = misdn_type,
  7056. .description = "Channel driver for mISDN Support (Bri/Pri)",
  7057. .requester = misdn_request,
  7058. .send_digit_begin = misdn_digit_begin,
  7059. .send_digit_end = misdn_digit_end,
  7060. .call = misdn_call,
  7061. .hangup = misdn_hangup,
  7062. .answer = misdn_answer,
  7063. .read = misdn_read,
  7064. .write = misdn_write,
  7065. .indicate = misdn_indication,
  7066. .fixup = misdn_fixup,
  7067. .send_text = misdn_send_text,
  7068. .properties = 0,
  7069. };
  7070. static int glob_channel = 0;
  7071. static void update_name(struct ast_channel *tmp, int port, int c)
  7072. {
  7073. int chan_offset = 0;
  7074. int tmp_port = misdn_cfg_get_next_port(0);
  7075. char newname[255];
  7076. for (; tmp_port > 0; tmp_port = misdn_cfg_get_next_port(tmp_port)) {
  7077. if (tmp_port == port) {
  7078. break;
  7079. }
  7080. chan_offset += misdn_lib_port_is_pri(tmp_port) ? 30 : 2;
  7081. }
  7082. if (c < 0) {
  7083. c = 0;
  7084. }
  7085. snprintf(newname, sizeof(newname), "%s/%d-", misdn_type, chan_offset + c);
  7086. if (strncmp(ast_channel_name(tmp), newname, strlen(newname))) {
  7087. snprintf(newname, sizeof(newname), "%s/%d-u%d", misdn_type, chan_offset + c, glob_channel++);
  7088. ast_change_name(tmp, newname);
  7089. chan_misdn_log(3, port, " --> updating channel name to [%s]\n", ast_channel_name(tmp));
  7090. }
  7091. }
  7092. static struct ast_channel *misdn_new(struct chan_list *chlist, int state, char *exten, char *callerid, struct ast_format_cap *cap, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, int port, int c)
  7093. {
  7094. struct ast_channel *tmp;
  7095. char *cid_name = NULL;
  7096. char *cid_num = NULL;
  7097. int chan_offset = 0;
  7098. int tmp_port = misdn_cfg_get_next_port(0);
  7099. struct ast_format tmpfmt;
  7100. for (; tmp_port > 0; tmp_port = misdn_cfg_get_next_port(tmp_port)) {
  7101. if (tmp_port == port) {
  7102. break;
  7103. }
  7104. chan_offset += misdn_lib_port_is_pri(tmp_port) ? 30 : 2;
  7105. }
  7106. if (c < 0) {
  7107. c = 0;
  7108. }
  7109. if (callerid) {
  7110. ast_callerid_parse(callerid, &cid_name, &cid_num);
  7111. }
  7112. tmp = ast_channel_alloc(1, state, cid_num, cid_name, "", exten, "", assignedids, requestor, 0, "%s/%s%d-u%d", misdn_type, c ? "" : "tmp", chan_offset + c, glob_channel++);
  7113. if (tmp) {
  7114. chan_misdn_log(2, port, " --> * NEW CHANNEL dialed:%s caller:%s\n", exten, callerid);
  7115. ast_best_codec(cap, &tmpfmt);
  7116. ast_format_cap_add(ast_channel_nativeformats(tmp), &prefformat);
  7117. ast_format_copy(ast_channel_writeformat(tmp), &tmpfmt);
  7118. ast_format_copy(ast_channel_rawwriteformat(tmp), &tmpfmt);
  7119. ast_format_copy(ast_channel_readformat(tmp), &tmpfmt);
  7120. ast_format_copy(ast_channel_rawreadformat(tmp), &tmpfmt);
  7121. /* Link the channel and private together */
  7122. chan_list_ref(chlist, "Give a reference to ast_channel");
  7123. MISDN_ASTERISK_TECH_PVT_SET(tmp, chlist);
  7124. chlist->ast = tmp;
  7125. ast_channel_tech_set(tmp, &misdn_tech);
  7126. ast_channel_priority_set(tmp, 1);
  7127. if (exten) {
  7128. ast_channel_exten_set(tmp, exten);
  7129. } else {
  7130. chan_misdn_log(1, 0, "misdn_new: no exten given.\n");
  7131. }
  7132. if (!ast_strlen_zero(cid_num)) {
  7133. /* Don't use ast_set_callerid() here because it will
  7134. * generate a needless NewCallerID event */
  7135. ast_channel_caller(tmp)->ani.number.valid = 1;
  7136. ast_channel_caller(tmp)->ani.number.str = ast_strdup(cid_num);
  7137. }
  7138. if (pipe(chlist->pipe) < 0) {
  7139. ast_log(LOG_ERROR, "Pipe failed\n");
  7140. }
  7141. ast_channel_set_fd(tmp, 0, chlist->pipe[0]);
  7142. ast_channel_rings_set(tmp, (state == AST_STATE_RING) ? 1 : 0);
  7143. ast_jb_configure(tmp, misdn_get_global_jbconf());
  7144. ast_channel_unlock(tmp);
  7145. } else {
  7146. chan_misdn_log(-1, 0, "Unable to allocate channel structure\n");
  7147. }
  7148. return tmp;
  7149. }
  7150. /*! Returns a reference to the found chan_list. */
  7151. static struct chan_list *find_chan_by_bc(struct misdn_bchannel *bc)
  7152. {
  7153. struct chan_list *help;
  7154. ast_mutex_lock(&cl_te_lock);
  7155. for (help = cl_te; help; help = help->next) {
  7156. if (help->bc == bc) {
  7157. chan_list_ref(help, "Found chan_list by bc");
  7158. ast_mutex_unlock(&cl_te_lock);
  7159. return help;
  7160. }
  7161. }
  7162. ast_mutex_unlock(&cl_te_lock);
  7163. chan_misdn_log(6, bc->port,
  7164. "$$$ find_chan_by_bc: No channel found for dialed:%s caller:\"%s\" <%s>\n",
  7165. bc->dialed.number,
  7166. bc->caller.name,
  7167. bc->caller.number);
  7168. return NULL;
  7169. }
  7170. /*! Returns a reference to the found chan_list. */
  7171. static struct chan_list *find_hold_call(struct misdn_bchannel *bc)
  7172. {
  7173. struct chan_list *help;
  7174. if (bc->pri) {
  7175. return NULL;
  7176. }
  7177. chan_misdn_log(6, bc->port, "$$$ find_hold_call: channel:%d dialed:%s caller:\"%s\" <%s>\n",
  7178. bc->channel,
  7179. bc->dialed.number,
  7180. bc->caller.name,
  7181. bc->caller.number);
  7182. ast_mutex_lock(&cl_te_lock);
  7183. for (help = cl_te; help; help = help->next) {
  7184. chan_misdn_log(4, bc->port, "$$$ find_hold_call: --> hold:%d channel:%d\n", help->hold.state, help->hold.channel);
  7185. if (help->hold.state == MISDN_HOLD_ACTIVE && help->hold.port == bc->port) {
  7186. chan_list_ref(help, "Found chan_list hold call");
  7187. ast_mutex_unlock(&cl_te_lock);
  7188. return help;
  7189. }
  7190. }
  7191. ast_mutex_unlock(&cl_te_lock);
  7192. chan_misdn_log(6, bc->port,
  7193. "$$$ find_hold_call: No channel found for dialed:%s caller:\"%s\" <%s>\n",
  7194. bc->dialed.number,
  7195. bc->caller.name,
  7196. bc->caller.number);
  7197. return NULL;
  7198. }
  7199. /*! Returns a reference to the found chan_list. */
  7200. static struct chan_list *find_hold_call_l3(unsigned long l3_id)
  7201. {
  7202. struct chan_list *help;
  7203. ast_mutex_lock(&cl_te_lock);
  7204. for (help = cl_te; help; help = help->next) {
  7205. if (help->hold.state != MISDN_HOLD_IDLE && help->l3id == l3_id) {
  7206. chan_list_ref(help, "Found chan_list hold call l3");
  7207. ast_mutex_unlock(&cl_te_lock);
  7208. return help;
  7209. }
  7210. }
  7211. ast_mutex_unlock(&cl_te_lock);
  7212. return NULL;
  7213. }
  7214. #define TRANSFER_ON_HELD_CALL_HANGUP 1
  7215. #if defined(TRANSFER_ON_HELD_CALL_HANGUP)
  7216. /*!
  7217. * \internal
  7218. * \brief Find a suitable active call to go with a held call so we could try a transfer.
  7219. *
  7220. * \param bc B channel record.
  7221. *
  7222. * \return Found call record or NULL.
  7223. *
  7224. * \note Returns a reference to the found chan_list.
  7225. *
  7226. * \note There could be a possibility where we find the wrong active call to transfer.
  7227. * This concern is mitigated by the fact that there could be at most one other call
  7228. * on a PTMP BRI link to another device. Maybe the l3_id could help in locating an
  7229. * active call on the same TEI?
  7230. */
  7231. static struct chan_list *find_hold_active_call(struct misdn_bchannel *bc)
  7232. {
  7233. struct chan_list *list;
  7234. ast_mutex_lock(&cl_te_lock);
  7235. for (list = cl_te; list; list = list->next) {
  7236. if (list->hold.state == MISDN_HOLD_IDLE && list->bc && list->bc->port == bc->port
  7237. && list->ast) {
  7238. switch (list->state) {
  7239. case MISDN_PROCEEDING:
  7240. case MISDN_PROGRESS:
  7241. case MISDN_ALERTING:
  7242. case MISDN_CONNECTED:
  7243. chan_list_ref(list, "Found chan_list hold active call");
  7244. ast_mutex_unlock(&cl_te_lock);
  7245. return list;
  7246. default:
  7247. break;
  7248. }
  7249. }
  7250. }
  7251. ast_mutex_unlock(&cl_te_lock);
  7252. return NULL;
  7253. }
  7254. #endif /* defined(TRANSFER_ON_HELD_CALL_HANGUP) */
  7255. static void cl_queue_chan(struct chan_list *chan)
  7256. {
  7257. chan_misdn_log(4, chan->bc ? chan->bc->port : 0, "* Queuing chan %p\n", chan);
  7258. chan_list_ref(chan, "Adding chan_list to list");
  7259. ast_mutex_lock(&cl_te_lock);
  7260. chan->next = NULL;
  7261. if (!cl_te) {
  7262. /* List is empty, make head of list. */
  7263. cl_te = chan;
  7264. } else {
  7265. struct chan_list *help;
  7266. /* Put at end of list. */
  7267. for (help = cl_te; help->next; help = help->next) {
  7268. }
  7269. help->next = chan;
  7270. }
  7271. ast_mutex_unlock(&cl_te_lock);
  7272. }
  7273. static int cl_dequeue_chan(struct chan_list *chan)
  7274. {
  7275. int found_it;
  7276. struct chan_list *help;
  7277. ast_mutex_lock(&cl_te_lock);
  7278. if (!cl_te) {
  7279. /* List is empty. */
  7280. ast_mutex_unlock(&cl_te_lock);
  7281. return 0;
  7282. }
  7283. if (cl_te == chan) {
  7284. /* What we want is the head of the list. */
  7285. cl_te = cl_te->next;
  7286. ast_mutex_unlock(&cl_te_lock);
  7287. chan_list_unref(chan, "Removed chan_list from list head");
  7288. return 1;
  7289. }
  7290. found_it = 0;
  7291. for (help = cl_te; help->next; help = help->next) {
  7292. if (help->next == chan) {
  7293. /* Found it in the list. */
  7294. help->next = help->next->next;
  7295. found_it = 1;
  7296. break;
  7297. }
  7298. }
  7299. ast_mutex_unlock(&cl_te_lock);
  7300. if (found_it) {
  7301. chan_list_unref(chan, "Removed chan_list from list");
  7302. }
  7303. return found_it;
  7304. }
  7305. /** Channel Queue End **/
  7306. static int pbx_start_chan(struct chan_list *ch)
  7307. {
  7308. int ret = ast_pbx_start(ch->ast);
  7309. ch->need_hangup = (ret >= 0) ? 0 : 1;
  7310. return ret;
  7311. }
  7312. static void hangup_chan(struct chan_list *ch, struct misdn_bchannel *bc)
  7313. {
  7314. int port = bc->port;
  7315. if (!ch) {
  7316. cb_log(1, port, "Cannot hangup chan, no ch\n");
  7317. return;
  7318. }
  7319. cb_log(5, port, "hangup_chan called\n");
  7320. if (ch->need_hangup) {
  7321. cb_log(2, port, " --> hangup\n");
  7322. ch->need_hangup = 0;
  7323. ch->need_queue_hangup = 0;
  7324. if (ch->ast && send_cause2ast(ch->ast, bc, ch)) {
  7325. ast_hangup(ch->ast);
  7326. }
  7327. return;
  7328. }
  7329. if (!ch->need_queue_hangup) {
  7330. cb_log(2, port, " --> No need to queue hangup\n");
  7331. return;
  7332. }
  7333. ch->need_queue_hangup = 0;
  7334. if (ch->ast) {
  7335. if (send_cause2ast(ch->ast, bc, ch)) {
  7336. ast_queue_hangup_with_cause(ch->ast, bc->cause);
  7337. cb_log(2, port, " --> queue_hangup\n");
  7338. }
  7339. } else {
  7340. cb_log(1, port, "Cannot hangup chan, no ast\n");
  7341. }
  7342. }
  7343. /*!
  7344. * \internal
  7345. * \brief ISDN asked us to release channel, pendant to misdn_hangup.
  7346. *
  7347. * \param ch Call channel record to release.
  7348. * \param bc Current B channel record associated with ch.
  7349. *
  7350. * \return Nothing
  7351. *
  7352. * \note The only valid thing to do with ch after calling is to chan_list_unref(ch, "").
  7353. */
  7354. static void release_chan(struct chan_list *ch, struct misdn_bchannel *bc)
  7355. {
  7356. struct ast_channel *ast;
  7357. chan_misdn_log(5, bc->port, "release_chan: bc with pid:%d l3id: %x\n", bc->pid, bc->l3_id);
  7358. ast_mutex_lock(&release_lock);
  7359. for (;;) {
  7360. ast = ch->ast;
  7361. if (!ast || !ast_channel_trylock(ast)) {
  7362. break;
  7363. }
  7364. DEADLOCK_AVOIDANCE(&release_lock);
  7365. }
  7366. if (!cl_dequeue_chan(ch)) {
  7367. /* Someone already released it. */
  7368. if (ast) {
  7369. ast_channel_unlock(ast);
  7370. }
  7371. ast_mutex_unlock(&release_lock);
  7372. return;
  7373. }
  7374. ch->state = MISDN_CLEANING;
  7375. ch->ast = NULL;
  7376. if (ast) {
  7377. struct chan_list *ast_ch;
  7378. ast_ch = MISDN_ASTERISK_TECH_PVT(ast);
  7379. MISDN_ASTERISK_TECH_PVT_SET(ast, NULL);
  7380. chan_misdn_log(1, bc->port,
  7381. "* RELEASING CHANNEL pid:%d context:%s dialed:%s caller:\"%s\" <%s>\n",
  7382. bc->pid,
  7383. ast_channel_context(ast),
  7384. ast_channel_exten(ast),
  7385. S_COR(ast_channel_caller(ast)->id.name.valid, ast_channel_caller(ast)->id.name.str, ""),
  7386. S_COR(ast_channel_caller(ast)->id.number.valid, ast_channel_caller(ast)->id.number.str, ""));
  7387. if (ast_channel_state(ast) != AST_STATE_RESERVED) {
  7388. chan_misdn_log(3, bc->port, " --> Setting AST State to down\n");
  7389. ast_setstate(ast, AST_STATE_DOWN);
  7390. }
  7391. ast_channel_unlock(ast);
  7392. if (ast_ch) {
  7393. chan_list_unref(ast_ch, "Release ast_channel reference.");
  7394. }
  7395. }
  7396. if (ch->originator == ORG_AST) {
  7397. --misdn_out_calls[bc->port];
  7398. } else {
  7399. --misdn_in_calls[bc->port];
  7400. }
  7401. ast_mutex_unlock(&release_lock);
  7402. }
  7403. /*!
  7404. * \internal
  7405. * \brief Do everything in release_chan() that makes sense without a bc.
  7406. *
  7407. * \param ch Call channel record to release.
  7408. *
  7409. * \return Nothing
  7410. *
  7411. * \note The only valid thing to do with ch after calling is to chan_list_unref(ch, "").
  7412. */
  7413. static void release_chan_early(struct chan_list *ch)
  7414. {
  7415. struct ast_channel *ast;
  7416. ast_mutex_lock(&release_lock);
  7417. for (;;) {
  7418. ast = ch->ast;
  7419. if (!ast || !ast_channel_trylock(ast)) {
  7420. break;
  7421. }
  7422. DEADLOCK_AVOIDANCE(&release_lock);
  7423. }
  7424. if (!cl_dequeue_chan(ch)) {
  7425. /* Someone already released it. */
  7426. if (ast) {
  7427. ast_channel_unlock(ast);
  7428. }
  7429. ast_mutex_unlock(&release_lock);
  7430. return;
  7431. }
  7432. ch->state = MISDN_CLEANING;
  7433. ch->ast = NULL;
  7434. if (ast) {
  7435. struct chan_list *ast_ch;
  7436. ast_ch = MISDN_ASTERISK_TECH_PVT(ast);
  7437. MISDN_ASTERISK_TECH_PVT_SET(ast, NULL);
  7438. if (ast_channel_state(ast) != AST_STATE_RESERVED) {
  7439. ast_setstate(ast, AST_STATE_DOWN);
  7440. }
  7441. ast_channel_unlock(ast);
  7442. if (ast_ch) {
  7443. chan_list_unref(ast_ch, "Release ast_channel reference.");
  7444. }
  7445. }
  7446. if (ch->hold.state != MISDN_HOLD_IDLE) {
  7447. if (ch->originator == ORG_AST) {
  7448. --misdn_out_calls[ch->hold.port];
  7449. } else {
  7450. --misdn_in_calls[ch->hold.port];
  7451. }
  7452. }
  7453. ast_mutex_unlock(&release_lock);
  7454. }
  7455. /*!
  7456. * \internal
  7457. * \brief Attempt to transfer the active channel party to the held channel party.
  7458. *
  7459. * \param active_ch Channel currently connected.
  7460. * \param held_ch Channel currently on hold.
  7461. *
  7462. * \retval 0 on success.
  7463. * \retval -1 on error.
  7464. */
  7465. static int misdn_attempt_transfer(struct chan_list *active_ch, struct chan_list *held_ch)
  7466. {
  7467. int retval;
  7468. enum ast_transfer_result xfer_res;
  7469. struct ast_channel *to_target;
  7470. struct ast_channel *to_transferee;
  7471. switch (active_ch->state) {
  7472. case MISDN_PROCEEDING:
  7473. case MISDN_PROGRESS:
  7474. case MISDN_ALERTING:
  7475. case MISDN_CONNECTED:
  7476. break;
  7477. default:
  7478. return -1;
  7479. }
  7480. ast_channel_lock_both(held_ch->ast, active_ch->ast);
  7481. to_target = active_ch->ast;
  7482. to_transferee = held_ch->ast;
  7483. chan_misdn_log(1, held_ch->hold.port, "TRANSFERRING %s to %s\n",
  7484. ast_channel_name(to_transferee), ast_channel_name(to_target));
  7485. held_ch->hold.state = MISDN_HOLD_TRANSFER;
  7486. ast_channel_ref(to_target);
  7487. ast_channel_ref(to_transferee);
  7488. ast_channel_unlock(to_target);
  7489. ast_channel_unlock(to_transferee);
  7490. retval = 0;
  7491. xfer_res = ast_bridge_transfer_attended(to_transferee, to_target);
  7492. if (xfer_res != AST_BRIDGE_TRANSFER_SUCCESS) {
  7493. retval = -1;
  7494. }
  7495. ast_channel_unref(to_target);
  7496. ast_channel_unref(to_transferee);
  7497. return retval;
  7498. }
  7499. static void do_immediate_setup(struct misdn_bchannel *bc, struct chan_list *ch, struct ast_channel *ast)
  7500. {
  7501. char *predial;
  7502. struct ast_frame fr;
  7503. predial = ast_strdupa(ast_channel_exten(ast));
  7504. ch->state = MISDN_DIALING;
  7505. if (!ch->noautorespond_on_setup) {
  7506. if (bc->nt) {
  7507. misdn_lib_send_event(bc, EVENT_SETUP_ACKNOWLEDGE);
  7508. } else {
  7509. if (misdn_lib_is_ptp(bc->port)) {
  7510. misdn_lib_send_event(bc, EVENT_SETUP_ACKNOWLEDGE);
  7511. } else {
  7512. misdn_lib_send_event(bc, EVENT_PROCEEDING);
  7513. }
  7514. }
  7515. } else {
  7516. ch->state = MISDN_INCOMING_SETUP;
  7517. }
  7518. chan_misdn_log(1, bc->port,
  7519. "* Starting Ast context:%s dialed:%s caller:\"%s\" <%s> with 's' extension\n",
  7520. ast_channel_context(ast),
  7521. ast_channel_exten(ast),
  7522. (ast_channel_caller(ast)->id.name.valid && ast_channel_caller(ast)->id.name.str)
  7523. ? ast_channel_caller(ast)->id.name.str : "",
  7524. (ast_channel_caller(ast)->id.number.valid && ast_channel_caller(ast)->id.number.str)
  7525. ? ast_channel_caller(ast)->id.number.str : "");
  7526. ast_channel_exten_set(ast, "s");
  7527. if (!ast_canmatch_extension(ast, ast_channel_context(ast), ast_channel_exten(ast), 1, bc->caller.number) || pbx_start_chan(ch) < 0) {
  7528. ast = NULL;
  7529. bc->out_cause = AST_CAUSE_UNALLOCATED;
  7530. hangup_chan(ch, bc);
  7531. hanguptone_indicate(ch);
  7532. misdn_lib_send_event(bc, bc->nt ? EVENT_RELEASE_COMPLETE : EVENT_DISCONNECT);
  7533. }
  7534. while (!ast_strlen_zero(predial)) {
  7535. fr.frametype = AST_FRAME_DTMF;
  7536. fr.subclass.integer = *predial;
  7537. fr.src = NULL;
  7538. fr.data.ptr = NULL;
  7539. fr.datalen = 0;
  7540. fr.samples = 0;
  7541. fr.mallocd = 0;
  7542. fr.offset = 0;
  7543. fr.delivery = ast_tv(0,0);
  7544. if (ch->ast && MISDN_ASTERISK_TECH_PVT(ch->ast)) {
  7545. ast_queue_frame(ch->ast, &fr);
  7546. }
  7547. predial++;
  7548. }
  7549. }
  7550. /*!
  7551. * \retval -1 if can hangup after calling.
  7552. * \retval 0 if cannot hangup after calling.
  7553. */
  7554. static int send_cause2ast(struct ast_channel *ast, struct misdn_bchannel *bc, struct chan_list *ch)
  7555. {
  7556. int can_hangup;
  7557. if (!ast) {
  7558. chan_misdn_log(1, 0, "send_cause2ast: No Ast\n");
  7559. return 0;
  7560. }
  7561. if (!bc) {
  7562. chan_misdn_log(1, 0, "send_cause2ast: No BC\n");
  7563. return 0;
  7564. }
  7565. if (!ch) {
  7566. chan_misdn_log(1, 0, "send_cause2ast: No Ch\n");
  7567. return 0;
  7568. }
  7569. ast_channel_hangupcause_set(ast, bc->cause);
  7570. can_hangup = -1;
  7571. switch (bc->cause) {
  7572. case AST_CAUSE_UNALLOCATED:
  7573. case AST_CAUSE_NO_ROUTE_TRANSIT_NET:
  7574. case AST_CAUSE_NO_ROUTE_DESTINATION:
  7575. case 4: /* Send special information tone */
  7576. case AST_CAUSE_NUMBER_CHANGED:
  7577. case AST_CAUSE_DESTINATION_OUT_OF_ORDER:
  7578. /* Congestion Cases */
  7579. /*
  7580. * Not Queueing the Congestion anymore, since we want to hear
  7581. * the inband message
  7582. *
  7583. chan_misdn_log(1, bc ? bc->port : 0, " --> * SEND: Queue Congestion pid:%d\n", bc ? bc->pid : -1);
  7584. ch->state = MISDN_BUSY;
  7585. ast_queue_control(ast, AST_CONTROL_CONGESTION);
  7586. */
  7587. break;
  7588. case AST_CAUSE_CALL_REJECTED:
  7589. case AST_CAUSE_USER_BUSY:
  7590. ch->state = MISDN_BUSY;
  7591. if (!ch->need_busy) {
  7592. chan_misdn_log(1, bc ? bc->port : 0, "Queued busy already\n");
  7593. break;
  7594. }
  7595. ch->need_busy = 0;
  7596. chan_misdn_log(1, bc ? bc->port : 0, " --> * SEND: Queue Busy pid:%d\n", bc ? bc->pid : -1);
  7597. ast_queue_control(ast, AST_CONTROL_BUSY);
  7598. /* The BUSY is likely to cause a hangup or the user needs to hear it. */
  7599. can_hangup = 0;
  7600. break;
  7601. }
  7602. return can_hangup;
  7603. }
  7604. /*! \brief Import parameters from the dialplan environment variables */
  7605. void import_ch(struct ast_channel *chan, struct misdn_bchannel *bc, struct chan_list *ch)
  7606. {
  7607. const char *tmp;
  7608. ast_channel_lock(chan);
  7609. tmp = pbx_builtin_getvar_helper(chan, "MISDN_ADDRESS_COMPLETE");
  7610. if (tmp && (atoi(tmp) == 1)) {
  7611. bc->sending_complete = 1;
  7612. }
  7613. tmp = pbx_builtin_getvar_helper(chan, "MISDN_USERUSER");
  7614. if (tmp) {
  7615. ast_log(LOG_NOTICE, "MISDN_USERUSER: %s\n", tmp);
  7616. ast_copy_string(bc->uu, tmp, sizeof(bc->uu));
  7617. bc->uulen = strlen(bc->uu);
  7618. }
  7619. tmp = pbx_builtin_getvar_helper(chan, "MISDN_KEYPAD");
  7620. if (tmp) {
  7621. ast_copy_string(bc->keypad, tmp, sizeof(bc->keypad));
  7622. }
  7623. ast_channel_unlock(chan);
  7624. }
  7625. /*! \brief Export parameters to the dialplan environment variables */
  7626. void export_ch(struct ast_channel *chan, struct misdn_bchannel *bc, struct chan_list *ch)
  7627. {
  7628. char tmp[32];
  7629. /*
  7630. * The only use for MISDN_PID is if there is a problem and you
  7631. * have to use the "misdn restart pid" CLI command. Otherwise,
  7632. * the pid is not used by anyone. The internal use of MISDN_PID
  7633. * has been deleted.
  7634. */
  7635. chan_misdn_log(3, bc->port, " --> EXPORT_PID: pid:%d\n", bc->pid);
  7636. snprintf(tmp, sizeof(tmp), "%d", bc->pid);
  7637. pbx_builtin_setvar_helper(chan, "_MISDN_PID", tmp);
  7638. if (bc->sending_complete) {
  7639. snprintf(tmp, sizeof(tmp), "%d", bc->sending_complete);
  7640. pbx_builtin_setvar_helper(chan, "MISDN_ADDRESS_COMPLETE", tmp);
  7641. }
  7642. if (bc->urate) {
  7643. snprintf(tmp, sizeof(tmp), "%d", bc->urate);
  7644. pbx_builtin_setvar_helper(chan, "MISDN_URATE", tmp);
  7645. }
  7646. if (bc->uulen) {
  7647. pbx_builtin_setvar_helper(chan, "MISDN_USERUSER", bc->uu);
  7648. }
  7649. if (!ast_strlen_zero(bc->keypad)) {
  7650. pbx_builtin_setvar_helper(chan, "MISDN_KEYPAD", bc->keypad);
  7651. }
  7652. }
  7653. int add_in_calls(int port)
  7654. {
  7655. int max_in_calls;
  7656. misdn_cfg_get(port, MISDN_CFG_MAX_IN, &max_in_calls, sizeof(max_in_calls));
  7657. misdn_in_calls[port]++;
  7658. if (max_in_calls >= 0 && max_in_calls < misdn_in_calls[port]) {
  7659. ast_log(LOG_NOTICE, "Marking Incoming Call on port[%d]\n", port);
  7660. return misdn_in_calls[port] - max_in_calls;
  7661. }
  7662. return 0;
  7663. }
  7664. int add_out_calls(int port)
  7665. {
  7666. int max_out_calls;
  7667. misdn_cfg_get(port, MISDN_CFG_MAX_OUT, &max_out_calls, sizeof(max_out_calls));
  7668. if (max_out_calls >= 0 && max_out_calls <= misdn_out_calls[port]) {
  7669. ast_log(LOG_NOTICE, "Rejecting Outgoing Call on port[%d]\n", port);
  7670. return (misdn_out_calls[port] + 1) - max_out_calls;
  7671. }
  7672. misdn_out_calls[port]++;
  7673. return 0;
  7674. }
  7675. static void start_pbx(struct chan_list *ch, struct misdn_bchannel *bc, struct ast_channel *chan)
  7676. {
  7677. if (pbx_start_chan(ch) < 0) {
  7678. hangup_chan(ch, bc);
  7679. chan_misdn_log(-1, bc->port, "ast_pbx_start returned <0 in SETUP\n");
  7680. if (bc->nt) {
  7681. hanguptone_indicate(ch);
  7682. misdn_lib_send_event(bc, EVENT_RELEASE_COMPLETE);
  7683. } else {
  7684. misdn_lib_send_event(bc, EVENT_RELEASE);
  7685. }
  7686. }
  7687. }
  7688. static void wait_for_digits(struct chan_list *ch, struct misdn_bchannel *bc, struct ast_channel *chan)
  7689. {
  7690. ch->state = MISDN_WAITING4DIGS;
  7691. misdn_lib_send_event(bc, EVENT_SETUP_ACKNOWLEDGE);
  7692. if (bc->nt && !bc->dialed.number[0]) {
  7693. dialtone_indicate(ch);
  7694. }
  7695. }
  7696. #if defined(AST_MISDN_ENHANCEMENTS)
  7697. /*!
  7698. * \internal
  7699. * \brief Handle the FACILITY CCBSStatusRequest message.
  7700. *
  7701. * \param port Logical port number.
  7702. * \param facility Facility ie contents.
  7703. *
  7704. * \return Nothing
  7705. */
  7706. static void misdn_cc_handle_ccbs_status_request(int port, const struct FacParm *facility)
  7707. {
  7708. struct misdn_cc_record *cc_record;
  7709. struct misdn_bchannel dummy;
  7710. switch (facility->u.CCBSStatusRequest.ComponentType) {
  7711. case FacComponent_Invoke:
  7712. /* Build message */
  7713. misdn_make_dummy(&dummy, port, 0, misdn_lib_port_is_nt(port), 0);
  7714. dummy.fac_out.Function = Fac_CCBSStatusRequest;
  7715. dummy.fac_out.u.CCBSStatusRequest.InvokeID = facility->u.CCBSStatusRequest.InvokeID;
  7716. dummy.fac_out.u.CCBSStatusRequest.ComponentType = FacComponent_Result;
  7717. /* Answer User-A free question */
  7718. AST_LIST_LOCK(&misdn_cc_records_db);
  7719. cc_record = misdn_cc_find_by_reference(port, facility->u.CCBSStatusRequest.Component.Invoke.CCBSReference);
  7720. if (cc_record) {
  7721. dummy.fac_out.u.CCBSStatusRequest.Component.Result.Free = cc_record->party_a_free;
  7722. } else {
  7723. /* No record so say User-A is free */
  7724. dummy.fac_out.u.CCBSStatusRequest.Component.Result.Free = 1;
  7725. }
  7726. AST_LIST_UNLOCK(&misdn_cc_records_db);
  7727. /* Send message */
  7728. print_facility(&dummy.fac_out, &dummy);
  7729. misdn_lib_send_event(&dummy, EVENT_FACILITY);
  7730. break;
  7731. default:
  7732. chan_misdn_log(0, port, " --> not yet handled: facility type:0x%04X\n", facility->Function);
  7733. break;
  7734. }
  7735. }
  7736. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  7737. #if defined(AST_MISDN_ENHANCEMENTS)
  7738. /*!
  7739. * \internal
  7740. * \brief Start a PBX to notify that User-B is available.
  7741. *
  7742. * \param record_id Call completion record ID
  7743. * \param notify Dialplan location to start processing.
  7744. *
  7745. * \return Nothing
  7746. */
  7747. static void misdn_cc_pbx_notify(long record_id, const struct misdn_cc_notify *notify)
  7748. {
  7749. struct ast_channel *chan;
  7750. char id_str[32];
  7751. static unsigned short sequence = 0;
  7752. /* Create a channel to notify with */
  7753. snprintf(id_str, sizeof(id_str), "%ld", record_id);
  7754. chan = ast_channel_alloc(0, AST_STATE_DOWN, id_str, NULL, NULL,
  7755. notify->exten, notify->context, NULL, 0,
  7756. "mISDN-CC/%ld-%X", record_id, (unsigned) ++sequence);
  7757. if (!chan) {
  7758. ast_log(LOG_ERROR, "Unable to allocate channel!\n");
  7759. return;
  7760. }
  7761. ast_channel_priority_set(chan, notify->priority);
  7762. ast_free(ast_channel_dialed(chan)->number.str);
  7763. ast_channel_dialed(chan)->number.str = ast_strdup(notify->exten);
  7764. ast_channel_unlock(chan);
  7765. if (ast_pbx_start(chan)) {
  7766. ast_log(LOG_WARNING, "Unable to start pbx channel %s!\n", ast_channel_name(chan));
  7767. ast_channel_release(chan);
  7768. } else {
  7769. ast_verb(1, "Started pbx for call completion notify channel %s\n", ast_channel_name(chan));
  7770. }
  7771. }
  7772. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  7773. #if defined(AST_MISDN_ENHANCEMENTS)
  7774. /*!
  7775. * \internal
  7776. * \brief Handle the FACILITY CCBS_T_RemoteUserFree message.
  7777. *
  7778. * \param bc B channel control structure message came in on
  7779. *
  7780. * \return Nothing
  7781. */
  7782. static void misdn_cc_handle_T_remote_user_free(struct misdn_bchannel *bc)
  7783. {
  7784. struct misdn_cc_record *cc_record;
  7785. struct misdn_cc_notify notify;
  7786. long record_id;
  7787. AST_LIST_LOCK(&misdn_cc_records_db);
  7788. cc_record = misdn_cc_find_by_bc(bc);
  7789. if (cc_record) {
  7790. if (cc_record->party_a_free) {
  7791. notify = cc_record->remote_user_free;
  7792. } else {
  7793. /* Send CCBS_T_Suspend message */
  7794. bc->fac_out.Function = Fac_CCBS_T_Suspend;
  7795. bc->fac_out.u.CCBS_T_Suspend.InvokeID = ++misdn_invoke_id;
  7796. print_facility(&bc->fac_out, bc);
  7797. misdn_lib_send_event(bc, EVENT_FACILITY);
  7798. notify = cc_record->b_free;
  7799. }
  7800. record_id = cc_record->record_id;
  7801. AST_LIST_UNLOCK(&misdn_cc_records_db);
  7802. if (notify.context[0]) {
  7803. /* Party A is free or B-Free notify has been setup. */
  7804. misdn_cc_pbx_notify(record_id, &notify);
  7805. }
  7806. } else {
  7807. AST_LIST_UNLOCK(&misdn_cc_records_db);
  7808. }
  7809. }
  7810. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  7811. #if defined(AST_MISDN_ENHANCEMENTS)
  7812. /*!
  7813. * \internal
  7814. * \brief Handle the FACILITY CCBSRemoteUserFree message.
  7815. *
  7816. * \param port Logical port number.
  7817. * \param facility Facility ie contents.
  7818. *
  7819. * \return Nothing
  7820. */
  7821. static void misdn_cc_handle_remote_user_free(int port, const struct FacParm *facility)
  7822. {
  7823. struct misdn_cc_record *cc_record;
  7824. struct misdn_cc_notify notify;
  7825. long record_id;
  7826. AST_LIST_LOCK(&misdn_cc_records_db);
  7827. cc_record = misdn_cc_find_by_reference(port, facility->u.CCBSRemoteUserFree.CCBSReference);
  7828. if (cc_record) {
  7829. notify = cc_record->remote_user_free;
  7830. record_id = cc_record->record_id;
  7831. AST_LIST_UNLOCK(&misdn_cc_records_db);
  7832. misdn_cc_pbx_notify(record_id, &notify);
  7833. } else {
  7834. AST_LIST_UNLOCK(&misdn_cc_records_db);
  7835. }
  7836. }
  7837. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  7838. #if defined(AST_MISDN_ENHANCEMENTS)
  7839. /*!
  7840. * \internal
  7841. * \brief Handle the FACILITY CCBSBFree message.
  7842. *
  7843. * \param port Logical port number.
  7844. * \param facility Facility ie contents.
  7845. *
  7846. * \return Nothing
  7847. */
  7848. static void misdn_cc_handle_b_free(int port, const struct FacParm *facility)
  7849. {
  7850. struct misdn_cc_record *cc_record;
  7851. struct misdn_cc_notify notify;
  7852. long record_id;
  7853. AST_LIST_LOCK(&misdn_cc_records_db);
  7854. cc_record = misdn_cc_find_by_reference(port, facility->u.CCBSBFree.CCBSReference);
  7855. if (cc_record && cc_record->b_free.context[0]) {
  7856. /* B-Free notify has been setup. */
  7857. notify = cc_record->b_free;
  7858. record_id = cc_record->record_id;
  7859. AST_LIST_UNLOCK(&misdn_cc_records_db);
  7860. misdn_cc_pbx_notify(record_id, &notify);
  7861. } else {
  7862. AST_LIST_UNLOCK(&misdn_cc_records_db);
  7863. }
  7864. }
  7865. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  7866. /*!
  7867. * \internal
  7868. * \brief Handle the incoming facility ie contents
  7869. *
  7870. * \param event Message type facility ie came in on
  7871. * \param bc B channel control structure message came in on
  7872. * \param ch Associated channel call record
  7873. *
  7874. * \return Nothing
  7875. */
  7876. static void misdn_facility_ie_handler(enum event_e event, struct misdn_bchannel *bc, struct chan_list *ch)
  7877. {
  7878. #if defined(AST_MISDN_ENHANCEMENTS)
  7879. const char *diagnostic_msg;
  7880. struct misdn_cc_record *cc_record;
  7881. char buf[32];
  7882. struct misdn_party_id party_id;
  7883. long new_record_id;
  7884. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  7885. print_facility(&bc->fac_in, bc);
  7886. switch (bc->fac_in.Function) {
  7887. #if defined(AST_MISDN_ENHANCEMENTS)
  7888. case Fac_ActivationDiversion:
  7889. switch (bc->fac_in.u.ActivationDiversion.ComponentType) {
  7890. case FacComponent_Result:
  7891. /* Positive ACK to activation */
  7892. /* We don't handle this yet */
  7893. break;
  7894. default:
  7895. chan_misdn_log(0, bc->port," --> not yet handled: facility type:0x%04X\n",
  7896. bc->fac_in.Function);
  7897. break;
  7898. }
  7899. break;
  7900. case Fac_DeactivationDiversion:
  7901. switch (bc->fac_in.u.DeactivationDiversion.ComponentType) {
  7902. case FacComponent_Result:
  7903. /* Positive ACK to deactivation */
  7904. /* We don't handle this yet */
  7905. break;
  7906. default:
  7907. chan_misdn_log(0, bc->port," --> not yet handled: facility type:0x%04X\n",
  7908. bc->fac_in.Function);
  7909. break;
  7910. }
  7911. break;
  7912. case Fac_ActivationStatusNotificationDiv:
  7913. /* Sent to other MSN numbers on the line when a user activates call forwarding. */
  7914. /* Sent in the first call control message of an outgoing call from the served user. */
  7915. /* We do not have anything to do for this message. */
  7916. break;
  7917. case Fac_DeactivationStatusNotificationDiv:
  7918. /* Sent to other MSN numbers on the line when a user deactivates call forwarding. */
  7919. /* We do not have anything to do for this message. */
  7920. break;
  7921. #if 0 /* We don't handle this yet */
  7922. case Fac_InterrogationDiversion:
  7923. /* We don't handle this yet */
  7924. break;
  7925. case Fac_InterrogateServedUserNumbers:
  7926. /* We don't handle this yet */
  7927. break;
  7928. #endif /* We don't handle this yet */
  7929. case Fac_DiversionInformation:
  7930. /* Sent to the served user when a call is forwarded. */
  7931. /* We do not have anything to do for this message. */
  7932. break;
  7933. case Fac_CallDeflection:
  7934. if (ch && ch->ast) {
  7935. switch (bc->fac_in.u.CallDeflection.ComponentType) {
  7936. case FacComponent_Invoke:
  7937. ast_copy_string(bc->redirecting.from.number, bc->dialed.number,
  7938. sizeof(bc->redirecting.from.number));
  7939. bc->redirecting.from.name[0] = 0;
  7940. bc->redirecting.from.number_plan = bc->dialed.number_plan;
  7941. bc->redirecting.from.number_type = bc->dialed.number_type;
  7942. bc->redirecting.from.screening = 0;/* Unscreened */
  7943. if (bc->fac_in.u.CallDeflection.Component.Invoke.PresentationAllowedToDivertedToUserPresent) {
  7944. bc->redirecting.from.presentation =
  7945. bc->fac_in.u.CallDeflection.Component.Invoke.PresentationAllowedToDivertedToUser
  7946. ? 0 /* Allowed */ : 1 /* Restricted */;
  7947. } else {
  7948. bc->redirecting.from.presentation = 0;/* Allowed */
  7949. }
  7950. /* Add configured prefix to the call deflection number */
  7951. memset(&party_id, 0, sizeof(party_id));
  7952. misdn_PartyNumber_extract(&party_id,
  7953. &bc->fac_in.u.CallDeflection.Component.Invoke.Deflection.Party);
  7954. misdn_add_number_prefix(bc->port, party_id.number_type,
  7955. party_id.number, sizeof(party_id.number));
  7956. //party_id.presentation = 0;/* Allowed */
  7957. //party_id.screening = 0;/* Unscreened */
  7958. bc->redirecting.to = party_id;
  7959. ++bc->redirecting.count;
  7960. bc->redirecting.reason = mISDN_REDIRECTING_REASON_DEFLECTION;
  7961. misdn_copy_redirecting_to_ast(ch->ast, &bc->redirecting, bc->incoming_cid_tag);
  7962. ast_channel_call_forward_set(ch->ast, bc->redirecting.to.number);
  7963. /* Send back positive ACK */
  7964. #if 1
  7965. /*
  7966. * Since there are no return result arguments it must be a
  7967. * generic result message. ETSI 300-196
  7968. */
  7969. bc->fac_out.Function = Fac_RESULT;
  7970. bc->fac_out.u.RESULT.InvokeID = bc->fac_in.u.CallDeflection.InvokeID;
  7971. #else
  7972. bc->fac_out.Function = Fac_CallDeflection;
  7973. bc->fac_out.u.CallDeflection.InvokeID = bc->fac_in.u.CallDeflection.InvokeID;
  7974. bc->fac_out.u.CallDeflection.ComponentType = FacComponent_Result;
  7975. #endif
  7976. print_facility(&bc->fac_out, bc);
  7977. misdn_lib_send_event(bc, EVENT_DISCONNECT);
  7978. /* This line is BUSY to further attempts by this dialing attempt. */
  7979. ast_queue_control(ch->ast, AST_CONTROL_BUSY);
  7980. break;
  7981. case FacComponent_Result:
  7982. /* Positive ACK to call deflection */
  7983. /*
  7984. * Sent in DISCONNECT or FACILITY message depending upon network option.
  7985. * It is in the FACILITY message if the call is still offered to the user
  7986. * while trying to alert the deflected to party.
  7987. */
  7988. /* Ignore the ACK */
  7989. break;
  7990. default:
  7991. break;
  7992. }
  7993. }
  7994. break;
  7995. #if 0 /* We don't handle this yet */
  7996. case Fac_CallRerouteing:
  7997. /* Private-Public ISDN interworking message */
  7998. /* We don't handle this yet */
  7999. break;
  8000. #endif /* We don't handle this yet */
  8001. case Fac_DivertingLegInformation1:
  8002. /* Private-Public ISDN interworking message */
  8003. bc->div_leg_3_rx_wanted = 0;
  8004. if (ch && ch->ast) {
  8005. bc->redirecting.reason =
  8006. diversion_reason_to_misdn(bc->fac_in.u.DivertingLegInformation1.DiversionReason);
  8007. if (bc->fac_in.u.DivertingLegInformation1.DivertedToPresent) {
  8008. misdn_PresentedNumberUnscreened_extract(&bc->redirecting.to,
  8009. &bc->fac_in.u.DivertingLegInformation1.DivertedTo);
  8010. /* Add configured prefix to redirecting.to.number */
  8011. misdn_add_number_prefix(bc->port, bc->redirecting.to.number_type,
  8012. bc->redirecting.to.number, sizeof(bc->redirecting.to.number));
  8013. } else {
  8014. bc->redirecting.to.number[0] = '\0';
  8015. bc->redirecting.to.number_plan = NUMPLAN_ISDN;
  8016. bc->redirecting.to.number_type = NUMTYPE_UNKNOWN;
  8017. bc->redirecting.to.presentation = 1;/* restricted */
  8018. bc->redirecting.to.screening = 0;/* unscreened */
  8019. }
  8020. misdn_copy_redirecting_to_ast(ch->ast, &bc->redirecting, bc->incoming_cid_tag);
  8021. bc->div_leg_3_rx_wanted = 1;
  8022. }
  8023. break;
  8024. case Fac_DivertingLegInformation2:
  8025. /* Private-Public ISDN interworking message */
  8026. switch (event) {
  8027. case EVENT_SETUP:
  8028. /* Comes in on a SETUP with redirecting.from information */
  8029. bc->div_leg_3_tx_pending = 1;
  8030. if (ch && ch->ast) {
  8031. /*
  8032. * Setup the redirecting.to informtion so we can identify
  8033. * if the user wants to manually supply the COLR for this
  8034. * redirected to number if further redirects could happen.
  8035. *
  8036. * All the user needs to do is set the REDIRECTING(to-pres)
  8037. * to the COLR and REDIRECTING(to-num) = ${EXTEN} to be safe
  8038. * after determining that the incoming call was redirected by
  8039. * checking if there is a REDIRECTING(from-num).
  8040. */
  8041. ast_copy_string(bc->redirecting.to.number, bc->dialed.number,
  8042. sizeof(bc->redirecting.to.number));
  8043. bc->redirecting.to.number_plan = bc->dialed.number_plan;
  8044. bc->redirecting.to.number_type = bc->dialed.number_type;
  8045. bc->redirecting.to.presentation = 1;/* restricted */
  8046. bc->redirecting.to.screening = 0;/* unscreened */
  8047. bc->redirecting.reason =
  8048. diversion_reason_to_misdn(bc->fac_in.u.DivertingLegInformation2.DiversionReason);
  8049. bc->redirecting.count = bc->fac_in.u.DivertingLegInformation2.DiversionCounter;
  8050. if (bc->fac_in.u.DivertingLegInformation2.DivertingPresent) {
  8051. /* This information is redundant if there was a redirecting ie in the SETUP. */
  8052. misdn_PresentedNumberUnscreened_extract(&bc->redirecting.from,
  8053. &bc->fac_in.u.DivertingLegInformation2.Diverting);
  8054. /* Add configured prefix to redirecting.from.number */
  8055. misdn_add_number_prefix(bc->port, bc->redirecting.from.number_type,
  8056. bc->redirecting.from.number, sizeof(bc->redirecting.from.number));
  8057. }
  8058. #if 0
  8059. if (bc->fac_in.u.DivertingLegInformation2.OriginalCalledPresent) {
  8060. /* We have no place to put the OriginalCalled number */
  8061. }
  8062. #endif
  8063. misdn_copy_redirecting_to_ast(ch->ast, &bc->redirecting, bc->incoming_cid_tag);
  8064. }
  8065. break;
  8066. default:
  8067. chan_misdn_log(0, bc->port," --> Expected in a SETUP message: facility type:0x%04X\n",
  8068. bc->fac_in.Function);
  8069. break;
  8070. }
  8071. break;
  8072. case Fac_DivertingLegInformation3:
  8073. /* Private-Public ISDN interworking message */
  8074. if (bc->div_leg_3_rx_wanted) {
  8075. bc->div_leg_3_rx_wanted = 0;
  8076. if (ch && ch->ast) {
  8077. struct ast_party_redirecting redirecting;
  8078. ast_channel_redirecting(ch->ast)->to.number.presentation =
  8079. bc->fac_in.u.DivertingLegInformation3.PresentationAllowedIndicator
  8080. ? AST_PRES_ALLOWED | AST_PRES_USER_NUMBER_UNSCREENED
  8081. : AST_PRES_RESTRICTED | AST_PRES_USER_NUMBER_UNSCREENED;
  8082. ast_party_redirecting_init(&redirecting);
  8083. ast_party_redirecting_copy(&redirecting, ast_channel_redirecting(ch->ast));
  8084. /*
  8085. * Reset any earlier private redirecting id representations and
  8086. * make sure that it is invalidated at the remote end.
  8087. */
  8088. ast_party_id_reset(&redirecting.priv_orig);
  8089. ast_party_id_reset(&redirecting.priv_from);
  8090. ast_party_id_reset(&redirecting.priv_to);
  8091. ast_channel_queue_redirecting_update(ch->ast, &redirecting, NULL);
  8092. ast_party_redirecting_free(&redirecting);
  8093. }
  8094. }
  8095. break;
  8096. #else /* !defined(AST_MISDN_ENHANCEMENTS) */
  8097. case Fac_CD:
  8098. if (ch && ch->ast) {
  8099. ast_copy_string(bc->redirecting.from.number, bc->dialed.number,
  8100. sizeof(bc->redirecting.from.number));
  8101. bc->redirecting.from.name[0] = 0;
  8102. bc->redirecting.from.number_plan = bc->dialed.number_plan;
  8103. bc->redirecting.from.number_type = bc->dialed.number_type;
  8104. bc->redirecting.from.screening = 0;/* Unscreened */
  8105. bc->redirecting.from.presentation =
  8106. bc->fac_in.u.CDeflection.PresentationAllowed
  8107. ? 0 /* Allowed */ : 1 /* Restricted */;
  8108. ast_copy_string(bc->redirecting.to.number,
  8109. (char *) bc->fac_in.u.CDeflection.DeflectedToNumber,
  8110. sizeof(bc->redirecting.to.number));
  8111. bc->redirecting.to.name[0] = 0;
  8112. bc->redirecting.to.number_plan = NUMPLAN_UNKNOWN;
  8113. bc->redirecting.to.number_type = NUMTYPE_UNKNOWN;
  8114. bc->redirecting.to.presentation = 0;/* Allowed */
  8115. bc->redirecting.to.screening = 0;/* Unscreened */
  8116. ++bc->redirecting.count;
  8117. bc->redirecting.reason = mISDN_REDIRECTING_REASON_DEFLECTION;
  8118. misdn_copy_redirecting_to_ast(ch->ast, &bc->redirecting, bc->incoming_cid_tag);
  8119. ast_channel_call_forward_set(ch->ast, bc->redirecting.to.number);
  8120. misdn_lib_send_event(bc, EVENT_DISCONNECT);
  8121. /* This line is BUSY to further attempts by this dialing attempt. */
  8122. ast_queue_control(ch->ast, AST_CONTROL_BUSY);
  8123. }
  8124. break;
  8125. #endif /* !defined(AST_MISDN_ENHANCEMENTS) */
  8126. case Fac_AOCDCurrency:
  8127. if (ch && ch->ast) {
  8128. bc->AOCDtype = Fac_AOCDCurrency;
  8129. memcpy(&bc->AOCD.currency, &bc->fac_in.u.AOCDcur, sizeof(bc->AOCD.currency));
  8130. bc->AOCD_need_export = 1;
  8131. export_aoc_vars(ch->originator, ch->ast, bc);
  8132. }
  8133. break;
  8134. case Fac_AOCDChargingUnit:
  8135. if (ch && ch->ast) {
  8136. bc->AOCDtype = Fac_AOCDChargingUnit;
  8137. memcpy(&bc->AOCD.chargingUnit, &bc->fac_in.u.AOCDchu, sizeof(bc->AOCD.chargingUnit));
  8138. bc->AOCD_need_export = 1;
  8139. export_aoc_vars(ch->originator, ch->ast, bc);
  8140. }
  8141. break;
  8142. #if defined(AST_MISDN_ENHANCEMENTS)
  8143. case Fac_ERROR:
  8144. diagnostic_msg = misdn_to_str_error_code(bc->fac_in.u.ERROR.errorValue);
  8145. chan_misdn_log(1, bc->port, " --> Facility error code: %s\n", diagnostic_msg);
  8146. switch (event) {
  8147. case EVENT_DISCONNECT:
  8148. case EVENT_RELEASE:
  8149. case EVENT_RELEASE_COMPLETE:
  8150. /* Possible call failure as a result of Fac_CCBSCall/Fac_CCBS_T_Call */
  8151. if (ch && ch->peer) {
  8152. misdn_cc_set_peer_var(ch->peer, MISDN_ERROR_MSG, diagnostic_msg);
  8153. }
  8154. break;
  8155. default:
  8156. break;
  8157. }
  8158. AST_LIST_LOCK(&misdn_cc_records_db);
  8159. cc_record = misdn_cc_find_by_invoke(bc->port, bc->fac_in.u.ERROR.invokeId);
  8160. if (cc_record) {
  8161. cc_record->outstanding_message = 0;
  8162. cc_record->error_code = bc->fac_in.u.ERROR.errorValue;
  8163. }
  8164. AST_LIST_UNLOCK(&misdn_cc_records_db);
  8165. break;
  8166. case Fac_REJECT:
  8167. diagnostic_msg = misdn_to_str_reject_code(bc->fac_in.u.REJECT.Code);
  8168. chan_misdn_log(1, bc->port, " --> Facility reject code: %s\n", diagnostic_msg);
  8169. switch (event) {
  8170. case EVENT_DISCONNECT:
  8171. case EVENT_RELEASE:
  8172. case EVENT_RELEASE_COMPLETE:
  8173. /* Possible call failure as a result of Fac_CCBSCall/Fac_CCBS_T_Call */
  8174. if (ch && ch->peer) {
  8175. misdn_cc_set_peer_var(ch->peer, MISDN_ERROR_MSG, diagnostic_msg);
  8176. }
  8177. break;
  8178. default:
  8179. break;
  8180. }
  8181. if (bc->fac_in.u.REJECT.InvokeIDPresent) {
  8182. AST_LIST_LOCK(&misdn_cc_records_db);
  8183. cc_record = misdn_cc_find_by_invoke(bc->port, bc->fac_in.u.REJECT.InvokeID);
  8184. if (cc_record) {
  8185. cc_record->outstanding_message = 0;
  8186. cc_record->reject_code = bc->fac_in.u.REJECT.Code;
  8187. }
  8188. AST_LIST_UNLOCK(&misdn_cc_records_db);
  8189. }
  8190. break;
  8191. case Fac_RESULT:
  8192. AST_LIST_LOCK(&misdn_cc_records_db);
  8193. cc_record = misdn_cc_find_by_invoke(bc->port, bc->fac_in.u.RESULT.InvokeID);
  8194. if (cc_record) {
  8195. cc_record->outstanding_message = 0;
  8196. }
  8197. AST_LIST_UNLOCK(&misdn_cc_records_db);
  8198. break;
  8199. #if 0 /* We don't handle this yet */
  8200. case Fac_EctExecute:
  8201. /* We don't handle this yet */
  8202. break;
  8203. case Fac_ExplicitEctExecute:
  8204. /* We don't handle this yet */
  8205. break;
  8206. case Fac_EctLinkIdRequest:
  8207. /* We don't handle this yet */
  8208. break;
  8209. #endif /* We don't handle this yet */
  8210. case Fac_SubaddressTransfer:
  8211. /* We do not have anything to do for this message since we do not handle subaddreses. */
  8212. break;
  8213. case Fac_RequestSubaddress:
  8214. /*
  8215. * We do not have anything to do for this message since we do not handle subaddreses.
  8216. * However, we do care about some other ie's that should be present.
  8217. */
  8218. if (bc->redirecting.to_changed) {
  8219. /* Add configured prefix to redirecting.to.number */
  8220. misdn_add_number_prefix(bc->port, bc->redirecting.to.number_type,
  8221. bc->redirecting.to.number, sizeof(bc->redirecting.to.number));
  8222. }
  8223. switch (bc->notify_description_code) {
  8224. case mISDN_NOTIFY_CODE_INVALID:
  8225. /* Notify ie was not present. */
  8226. bc->redirecting.to_changed = 0;
  8227. break;
  8228. case mISDN_NOTIFY_CODE_CALL_TRANSFER_ALERTING:
  8229. /*
  8230. * It would be preferable to update the connected line information
  8231. * only when the message callStatus is active. However, the
  8232. * optional redirection number may not be present in the active
  8233. * message if an alerting message were received earlier.
  8234. *
  8235. * The consequences if we wind up sending two updates is benign.
  8236. * The other end will think that it got transferred twice.
  8237. */
  8238. if (!bc->redirecting.to_changed) {
  8239. break;
  8240. }
  8241. bc->redirecting.to_changed = 0;
  8242. if (!ch || !ch->ast) {
  8243. break;
  8244. }
  8245. misdn_update_remote_party(ch->ast, &bc->redirecting.to,
  8246. AST_CONNECTED_LINE_UPDATE_SOURCE_TRANSFER_ALERTING,
  8247. bc->incoming_cid_tag);
  8248. break;
  8249. case mISDN_NOTIFY_CODE_CALL_TRANSFER_ACTIVE:
  8250. if (!bc->redirecting.to_changed) {
  8251. break;
  8252. }
  8253. bc->redirecting.to_changed = 0;
  8254. if (!ch || !ch->ast) {
  8255. break;
  8256. }
  8257. misdn_update_remote_party(ch->ast, &bc->redirecting.to,
  8258. AST_CONNECTED_LINE_UPDATE_SOURCE_TRANSFER, bc->incoming_cid_tag);
  8259. break;
  8260. default:
  8261. bc->redirecting.to_changed = 0;
  8262. chan_misdn_log(0, bc->port," --> not yet handled: notify code:0x%02X\n",
  8263. bc->notify_description_code);
  8264. break;
  8265. }
  8266. bc->notify_description_code = mISDN_NOTIFY_CODE_INVALID;
  8267. break;
  8268. case Fac_EctInform:
  8269. /* Private-Public ISDN interworking message */
  8270. if (ch && ch->ast && bc->fac_in.u.EctInform.RedirectionPresent) {
  8271. /* Add configured prefix to the redirection number */
  8272. memset(&party_id, 0, sizeof(party_id));
  8273. misdn_PresentedNumberUnscreened_extract(&party_id,
  8274. &bc->fac_in.u.EctInform.Redirection);
  8275. misdn_add_number_prefix(bc->port, party_id.number_type,
  8276. party_id.number, sizeof(party_id.number));
  8277. /*
  8278. * It would be preferable to update the connected line information
  8279. * only when the message callStatus is active. However, the
  8280. * optional redirection number may not be present in the active
  8281. * message if an alerting message were received earlier.
  8282. *
  8283. * The consequences if we wind up sending two updates is benign.
  8284. * The other end will think that it got transferred twice.
  8285. */
  8286. misdn_update_remote_party(ch->ast, &party_id,
  8287. (bc->fac_in.u.EctInform.Status == 0 /* alerting */)
  8288. ? AST_CONNECTED_LINE_UPDATE_SOURCE_TRANSFER_ALERTING
  8289. : AST_CONNECTED_LINE_UPDATE_SOURCE_TRANSFER,
  8290. bc->incoming_cid_tag);
  8291. }
  8292. break;
  8293. #if 0 /* We don't handle this yet */
  8294. case Fac_EctLoopTest:
  8295. /* The use of this message is unclear on how it works to detect loops. */
  8296. /* We don't handle this yet */
  8297. break;
  8298. #endif /* We don't handle this yet */
  8299. case Fac_CallInfoRetain:
  8300. switch (event) {
  8301. case EVENT_ALERTING:
  8302. case EVENT_DISCONNECT:
  8303. /* CCBS/CCNR is available */
  8304. if (ch && ch->peer) {
  8305. AST_LIST_LOCK(&misdn_cc_records_db);
  8306. if (ch->record_id == -1) {
  8307. cc_record = misdn_cc_new();
  8308. } else {
  8309. /*
  8310. * We are doing a call-completion attempt
  8311. * or the switch is sending us extra call-completion
  8312. * availability indications (erroneously?).
  8313. *
  8314. * Assume that the network request retention option
  8315. * is not on and that the current call-completion
  8316. * request is disabled.
  8317. */
  8318. cc_record = misdn_cc_find_by_id(ch->record_id);
  8319. if (cc_record) {
  8320. if (cc_record->ptp && cc_record->mode.ptp.bc) {
  8321. /*
  8322. * What? We are getting mixed messages from the
  8323. * switch. We are currently setup for
  8324. * point-to-point. Now we are switching to
  8325. * point-to-multipoint.
  8326. *
  8327. * Close the call-completion signaling link
  8328. */
  8329. cc_record->mode.ptp.bc->fac_out.Function = Fac_None;
  8330. cc_record->mode.ptp.bc->out_cause = AST_CAUSE_NORMAL_CLEARING;
  8331. misdn_lib_send_event(cc_record->mode.ptp.bc, EVENT_RELEASE_COMPLETE);
  8332. }
  8333. /*
  8334. * Resetup the existing record for a possible new
  8335. * call-completion request.
  8336. */
  8337. new_record_id = misdn_cc_record_id_new();
  8338. if (new_record_id < 0) {
  8339. /* Looks like we must keep the old id anyway. */
  8340. } else {
  8341. cc_record->record_id = new_record_id;
  8342. ch->record_id = new_record_id;
  8343. }
  8344. cc_record->ptp = 0;
  8345. cc_record->port = bc->port;
  8346. memset(&cc_record->mode, 0, sizeof(cc_record->mode));
  8347. cc_record->mode.ptmp.linkage_id = bc->fac_in.u.CallInfoRetain.CallLinkageID;
  8348. cc_record->invoke_id = ++misdn_invoke_id;
  8349. cc_record->activated = 0;
  8350. cc_record->outstanding_message = 0;
  8351. cc_record->activation_requested = 0;
  8352. cc_record->error_code = FacError_None;
  8353. cc_record->reject_code = FacReject_None;
  8354. memset(&cc_record->remote_user_free, 0, sizeof(cc_record->remote_user_free));
  8355. memset(&cc_record->b_free, 0, sizeof(cc_record->b_free));
  8356. cc_record->time_created = time(NULL);
  8357. cc_record = NULL;
  8358. } else {
  8359. /*
  8360. * Where did the record go? We will have to recapture
  8361. * the call setup information. Unfortunately, some
  8362. * setup information may have been changed.
  8363. */
  8364. ch->record_id = -1;
  8365. cc_record = misdn_cc_new();
  8366. }
  8367. }
  8368. if (cc_record) {
  8369. ch->record_id = cc_record->record_id;
  8370. cc_record->ptp = 0;
  8371. cc_record->port = bc->port;
  8372. cc_record->mode.ptmp.linkage_id = bc->fac_in.u.CallInfoRetain.CallLinkageID;
  8373. /* Record call information for possible call-completion attempt. */
  8374. cc_record->redial.caller = bc->caller;
  8375. cc_record->redial.dialed = bc->dialed;
  8376. cc_record->redial.setup_bc_hlc_llc = bc->setup_bc_hlc_llc;
  8377. cc_record->redial.capability = bc->capability;
  8378. cc_record->redial.hdlc = bc->hdlc;
  8379. }
  8380. AST_LIST_UNLOCK(&misdn_cc_records_db);
  8381. /* Set MISDN_CC_RECORD_ID in original channel */
  8382. if (ch->record_id != -1) {
  8383. snprintf(buf, sizeof(buf), "%ld", ch->record_id);
  8384. } else {
  8385. buf[0] = 0;
  8386. }
  8387. misdn_cc_set_peer_var(ch->peer, MISDN_CC_RECORD_ID, buf);
  8388. }
  8389. break;
  8390. default:
  8391. chan_misdn_log(0, bc->port,
  8392. " --> Expected in a DISCONNECT or ALERTING message: facility type:0x%04X\n",
  8393. bc->fac_in.Function);
  8394. break;
  8395. }
  8396. break;
  8397. case Fac_CCBS_T_Call:
  8398. case Fac_CCBSCall:
  8399. switch (event) {
  8400. case EVENT_SETUP:
  8401. /*
  8402. * This is a call completion retry call.
  8403. * If we had anything to do we would do it here.
  8404. */
  8405. break;
  8406. default:
  8407. chan_misdn_log(0, bc->port, " --> Expected in a SETUP message: facility type:0x%04X\n",
  8408. bc->fac_in.Function);
  8409. break;
  8410. }
  8411. break;
  8412. case Fac_CCBSDeactivate:
  8413. switch (bc->fac_in.u.CCBSDeactivate.ComponentType) {
  8414. case FacComponent_Result:
  8415. AST_LIST_LOCK(&misdn_cc_records_db);
  8416. cc_record = misdn_cc_find_by_invoke(bc->port, bc->fac_in.u.CCBSDeactivate.InvokeID);
  8417. if (cc_record) {
  8418. cc_record->outstanding_message = 0;
  8419. }
  8420. AST_LIST_UNLOCK(&misdn_cc_records_db);
  8421. break;
  8422. default:
  8423. chan_misdn_log(0, bc->port, " --> not yet handled: facility type:0x%04X\n",
  8424. bc->fac_in.Function);
  8425. break;
  8426. }
  8427. break;
  8428. case Fac_CCBSErase:
  8429. AST_LIST_LOCK(&misdn_cc_records_db);
  8430. cc_record = misdn_cc_find_by_reference(bc->port, bc->fac_in.u.CCBSErase.CCBSReference);
  8431. if (cc_record) {
  8432. misdn_cc_delete(cc_record);
  8433. }
  8434. AST_LIST_UNLOCK(&misdn_cc_records_db);
  8435. break;
  8436. case Fac_CCBSRemoteUserFree:
  8437. misdn_cc_handle_remote_user_free(bc->port, &bc->fac_in);
  8438. break;
  8439. case Fac_CCBSBFree:
  8440. misdn_cc_handle_b_free(bc->port, &bc->fac_in);
  8441. break;
  8442. case Fac_CCBSStatusRequest:
  8443. misdn_cc_handle_ccbs_status_request(bc->port, &bc->fac_in);
  8444. break;
  8445. case Fac_EraseCallLinkageID:
  8446. AST_LIST_LOCK(&misdn_cc_records_db);
  8447. cc_record = misdn_cc_find_by_linkage(bc->port,
  8448. bc->fac_in.u.EraseCallLinkageID.CallLinkageID);
  8449. if (cc_record && !cc_record->activation_requested) {
  8450. /*
  8451. * The T-RETENTION timer expired before we requested
  8452. * call completion activation. Call completion is no
  8453. * longer available.
  8454. */
  8455. misdn_cc_delete(cc_record);
  8456. }
  8457. AST_LIST_UNLOCK(&misdn_cc_records_db);
  8458. break;
  8459. case Fac_CCBSStopAlerting:
  8460. /* We do not have anything to do for this message. */
  8461. break;
  8462. case Fac_CCBSRequest:
  8463. case Fac_CCNRRequest:
  8464. switch (bc->fac_in.u.CCBSRequest.ComponentType) {
  8465. case FacComponent_Result:
  8466. AST_LIST_LOCK(&misdn_cc_records_db);
  8467. cc_record = misdn_cc_find_by_invoke(bc->port, bc->fac_in.u.CCBSRequest.InvokeID);
  8468. if (cc_record && !cc_record->ptp) {
  8469. cc_record->outstanding_message = 0;
  8470. cc_record->activated = 1;
  8471. cc_record->mode.ptmp.recall_mode = bc->fac_in.u.CCBSRequest.Component.Result.RecallMode;
  8472. cc_record->mode.ptmp.reference_id = bc->fac_in.u.CCBSRequest.Component.Result.CCBSReference;
  8473. }
  8474. AST_LIST_UNLOCK(&misdn_cc_records_db);
  8475. break;
  8476. default:
  8477. chan_misdn_log(0, bc->port, " --> not yet handled: facility type:0x%04X\n",
  8478. bc->fac_in.Function);
  8479. break;
  8480. }
  8481. break;
  8482. #if 0 /* We don't handle this yet */
  8483. case Fac_CCBSInterrogate:
  8484. case Fac_CCNRInterrogate:
  8485. /* We don't handle this yet */
  8486. break;
  8487. case Fac_StatusRequest:
  8488. /* We don't handle this yet */
  8489. break;
  8490. #endif /* We don't handle this yet */
  8491. #if 0 /* We don't handle this yet */
  8492. case Fac_CCBS_T_Suspend:
  8493. case Fac_CCBS_T_Resume:
  8494. /* We don't handle this yet */
  8495. break;
  8496. #endif /* We don't handle this yet */
  8497. case Fac_CCBS_T_RemoteUserFree:
  8498. misdn_cc_handle_T_remote_user_free(bc);
  8499. break;
  8500. case Fac_CCBS_T_Available:
  8501. switch (event) {
  8502. case EVENT_ALERTING:
  8503. case EVENT_DISCONNECT:
  8504. /* CCBS-T/CCNR-T is available */
  8505. if (ch && ch->peer) {
  8506. int set_id = 1;
  8507. AST_LIST_LOCK(&misdn_cc_records_db);
  8508. if (ch->record_id == -1) {
  8509. cc_record = misdn_cc_new();
  8510. } else {
  8511. /*
  8512. * We are doing a call-completion attempt
  8513. * or the switch is sending us extra call-completion
  8514. * availability indications (erroneously?).
  8515. */
  8516. cc_record = misdn_cc_find_by_id(ch->record_id);
  8517. if (cc_record) {
  8518. if (cc_record->ptp && cc_record->mode.ptp.retention_enabled) {
  8519. /*
  8520. * Call-completion is still activated.
  8521. * The user does not have to request it again.
  8522. */
  8523. chan_misdn_log(1, bc->port, " --> Call-completion request retention option is enabled\n");
  8524. set_id = 0;
  8525. } else {
  8526. if (cc_record->ptp && cc_record->mode.ptp.bc) {
  8527. /*
  8528. * The network request retention option
  8529. * is not on and the current call-completion
  8530. * request is to be disabled.
  8531. *
  8532. * We should get here only if EVENT_DISCONNECT
  8533. *
  8534. * Close the call-completion signaling link
  8535. */
  8536. cc_record->mode.ptp.bc->fac_out.Function = Fac_None;
  8537. cc_record->mode.ptp.bc->out_cause = AST_CAUSE_NORMAL_CLEARING;
  8538. misdn_lib_send_event(cc_record->mode.ptp.bc, EVENT_RELEASE_COMPLETE);
  8539. }
  8540. /*
  8541. * Resetup the existing record for a possible new
  8542. * call-completion request.
  8543. */
  8544. new_record_id = misdn_cc_record_id_new();
  8545. if (new_record_id < 0) {
  8546. /* Looks like we must keep the old id anyway. */
  8547. } else {
  8548. cc_record->record_id = new_record_id;
  8549. ch->record_id = new_record_id;
  8550. }
  8551. cc_record->ptp = 1;
  8552. cc_record->port = bc->port;
  8553. memset(&cc_record->mode, 0, sizeof(cc_record->mode));
  8554. cc_record->invoke_id = ++misdn_invoke_id;
  8555. cc_record->activated = 0;
  8556. cc_record->outstanding_message = 0;
  8557. cc_record->activation_requested = 0;
  8558. cc_record->error_code = FacError_None;
  8559. cc_record->reject_code = FacReject_None;
  8560. memset(&cc_record->remote_user_free, 0, sizeof(cc_record->remote_user_free));
  8561. memset(&cc_record->b_free, 0, sizeof(cc_record->b_free));
  8562. cc_record->time_created = time(NULL);
  8563. }
  8564. cc_record = NULL;
  8565. } else {
  8566. /*
  8567. * Where did the record go? We will have to recapture
  8568. * the call setup information. Unfortunately, some
  8569. * setup information may have been changed.
  8570. */
  8571. ch->record_id = -1;
  8572. cc_record = misdn_cc_new();
  8573. }
  8574. }
  8575. if (cc_record) {
  8576. ch->record_id = cc_record->record_id;
  8577. cc_record->ptp = 1;
  8578. cc_record->port = bc->port;
  8579. /* Record call information for possible call-completion attempt. */
  8580. cc_record->redial.caller = bc->caller;
  8581. cc_record->redial.dialed = bc->dialed;
  8582. cc_record->redial.setup_bc_hlc_llc = bc->setup_bc_hlc_llc;
  8583. cc_record->redial.capability = bc->capability;
  8584. cc_record->redial.hdlc = bc->hdlc;
  8585. }
  8586. AST_LIST_UNLOCK(&misdn_cc_records_db);
  8587. /* Set MISDN_CC_RECORD_ID in original channel */
  8588. if (ch->record_id != -1 && set_id) {
  8589. snprintf(buf, sizeof(buf), "%ld", ch->record_id);
  8590. } else {
  8591. buf[0] = 0;
  8592. }
  8593. misdn_cc_set_peer_var(ch->peer, MISDN_CC_RECORD_ID, buf);
  8594. }
  8595. break;
  8596. default:
  8597. chan_misdn_log(0, bc->port,
  8598. " --> Expected in a DISCONNECT or ALERTING message: facility type:0x%04X\n",
  8599. bc->fac_in.Function);
  8600. break;
  8601. }
  8602. break;
  8603. case Fac_CCBS_T_Request:
  8604. case Fac_CCNR_T_Request:
  8605. switch (bc->fac_in.u.CCBS_T_Request.ComponentType) {
  8606. case FacComponent_Result:
  8607. AST_LIST_LOCK(&misdn_cc_records_db);
  8608. cc_record = misdn_cc_find_by_invoke(bc->port, bc->fac_in.u.CCBS_T_Request.InvokeID);
  8609. if (cc_record && cc_record->ptp) {
  8610. cc_record->outstanding_message = 0;
  8611. cc_record->activated = 1;
  8612. cc_record->mode.ptp.retention_enabled =
  8613. cc_record->mode.ptp.requested_retention
  8614. ? bc->fac_in.u.CCBS_T_Request.Component.Result.RetentionSupported
  8615. ? 1 : 0
  8616. : 0;
  8617. }
  8618. AST_LIST_UNLOCK(&misdn_cc_records_db);
  8619. break;
  8620. case FacComponent_Invoke:
  8621. /* We cannot be User-B in ptp mode. */
  8622. default:
  8623. chan_misdn_log(0, bc->port, " --> not yet handled: facility type:0x%04X\n",
  8624. bc->fac_in.Function);
  8625. break;
  8626. }
  8627. break;
  8628. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  8629. case Fac_None:
  8630. break;
  8631. default:
  8632. chan_misdn_log(0, bc->port, " --> not yet handled: facility type:0x%04X\n",
  8633. bc->fac_in.Function);
  8634. break;
  8635. }
  8636. }
  8637. /*!
  8638. * \internal
  8639. * \brief Determine if the given dialed party matches our MSN.
  8640. * \since 1.8
  8641. *
  8642. * \param port ISDN port
  8643. * \param dialed Dialed party information of incoming call.
  8644. *
  8645. * \retval non-zero if MSN is valid.
  8646. * \retval 0 if MSN invalid.
  8647. */
  8648. static int misdn_is_msn_valid(int port, const struct misdn_party_dialing *dialed)
  8649. {
  8650. char number[sizeof(dialed->number)];
  8651. ast_copy_string(number, dialed->number, sizeof(number));
  8652. misdn_add_number_prefix(port, dialed->number_type, number, sizeof(number));
  8653. return misdn_cfg_is_msn_valid(port, number);
  8654. }
  8655. /************************************************************/
  8656. /* Receive Events from isdn_lib here */
  8657. /************************************************************/
  8658. static enum event_response_e
  8659. cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
  8660. {
  8661. #if defined(AST_MISDN_ENHANCEMENTS)
  8662. struct misdn_cc_record *cc_record;
  8663. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  8664. struct chan_list *held_ch;
  8665. struct chan_list *ch = find_chan_by_bc(bc);
  8666. if (event != EVENT_BCHAN_DATA && event != EVENT_TONE_GENERATE) {
  8667. int debuglevel = 1;
  8668. /* Debug Only Non-Bchan */
  8669. if (event == EVENT_CLEANUP && !user_data) {
  8670. debuglevel = 5;
  8671. }
  8672. chan_misdn_log(debuglevel, bc->port,
  8673. "I IND :%s caller:\"%s\" <%s> dialed:%s pid:%d state:%s\n",
  8674. manager_isdn_get_info(event),
  8675. bc->caller.name,
  8676. bc->caller.number,
  8677. bc->dialed.number,
  8678. bc->pid,
  8679. ch ? misdn_get_ch_state(ch) : "none");
  8680. if (debuglevel == 1) {
  8681. misdn_lib_log_ies(bc);
  8682. chan_misdn_log(4, bc->port, " --> bc_state:%s\n", bc_state2str(bc->bc_state));
  8683. }
  8684. }
  8685. if (!ch) {
  8686. switch(event) {
  8687. case EVENT_SETUP:
  8688. case EVENT_DISCONNECT:
  8689. case EVENT_RELEASE:
  8690. case EVENT_RELEASE_COMPLETE:
  8691. case EVENT_PORT_ALARM:
  8692. case EVENT_RETRIEVE:
  8693. case EVENT_NEW_BC:
  8694. case EVENT_FACILITY:
  8695. case EVENT_REGISTER:
  8696. break;
  8697. case EVENT_CLEANUP:
  8698. case EVENT_TONE_GENERATE:
  8699. case EVENT_BCHAN_DATA:
  8700. return -1;
  8701. default:
  8702. 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);
  8703. return -1;
  8704. }
  8705. } else {
  8706. switch (event) {
  8707. case EVENT_TONE_GENERATE:
  8708. break;
  8709. case EVENT_DISCONNECT:
  8710. case EVENT_RELEASE:
  8711. case EVENT_RELEASE_COMPLETE:
  8712. case EVENT_CLEANUP:
  8713. case EVENT_TIMEOUT:
  8714. if (!ch->ast) {
  8715. 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));
  8716. }
  8717. break;
  8718. default:
  8719. if (!ch->ast || !MISDN_ASTERISK_TECH_PVT(ch->ast)) {
  8720. if (event != EVENT_BCHAN_DATA) {
  8721. ast_log(LOG_NOTICE, "No Ast or No private Pointer in Event (%d:%s)\n", event, manager_isdn_get_info(event));
  8722. }
  8723. chan_list_unref(ch, "No Ast or Ast private pointer");
  8724. return -1;
  8725. }
  8726. break;
  8727. }
  8728. }
  8729. switch (event) {
  8730. case EVENT_PORT_ALARM:
  8731. {
  8732. int boa = 0;
  8733. misdn_cfg_get(bc->port, MISDN_CFG_ALARM_BLOCK, &boa, sizeof(boa));
  8734. if (boa) {
  8735. cb_log(1, bc->port, " --> blocking\n");
  8736. misdn_lib_port_block(bc->port);
  8737. }
  8738. }
  8739. break;
  8740. case EVENT_BCHAN_ACTIVATED:
  8741. break;
  8742. case EVENT_NEW_CHANNEL:
  8743. update_name(ch->ast,bc->port,bc->channel);
  8744. break;
  8745. case EVENT_NEW_L3ID:
  8746. ch->l3id=bc->l3_id;
  8747. ch->addr=bc->addr;
  8748. break;
  8749. case EVENT_NEW_BC:
  8750. if (!ch) {
  8751. ch = find_hold_call(bc);
  8752. }
  8753. if (!ch) {
  8754. ast_log(LOG_WARNING, "NEW_BC without chan_list?\n");
  8755. break;
  8756. }
  8757. if (bc) {
  8758. ch->bc = (struct misdn_bchannel *) user_data;
  8759. }
  8760. break;
  8761. case EVENT_DTMF_TONE:
  8762. {
  8763. /* sending INFOS as DTMF-Frames :) */
  8764. struct ast_frame fr;
  8765. memset(&fr, 0, sizeof(fr));
  8766. fr.frametype = AST_FRAME_DTMF;
  8767. fr.subclass.integer = bc->dtmf ;
  8768. fr.src = NULL;
  8769. fr.data.ptr = NULL;
  8770. fr.datalen = 0;
  8771. fr.samples = 0;
  8772. fr.mallocd = 0;
  8773. fr.offset = 0;
  8774. fr.delivery = ast_tv(0,0);
  8775. if (!ch->ignore_dtmf) {
  8776. chan_misdn_log(2, bc->port, " --> DTMF:%c\n", bc->dtmf);
  8777. ast_queue_frame(ch->ast, &fr);
  8778. } else {
  8779. chan_misdn_log(2, bc->port, " --> Ignoring DTMF:%c due to bridge flags\n", bc->dtmf);
  8780. }
  8781. break;
  8782. }
  8783. case EVENT_STATUS:
  8784. break;
  8785. case EVENT_INFORMATION:
  8786. if (ch->state != MISDN_CONNECTED) {
  8787. stop_indicate(ch);
  8788. }
  8789. if (!ch->ast) {
  8790. break;
  8791. }
  8792. if (ch->state == MISDN_WAITING4DIGS) {
  8793. RAII_VAR(struct ast_features_pickup_config *, pickup_cfg, NULL, ao2_cleanup);
  8794. const char *pickupexten;
  8795. /* Ok, incomplete Setup, waiting till extension exists */
  8796. if (ast_strlen_zero(bc->info_dad) && ! ast_strlen_zero(bc->keypad)) {
  8797. chan_misdn_log(1, bc->port, " --> using keypad as info\n");
  8798. ast_copy_string(bc->info_dad, bc->keypad, sizeof(bc->info_dad));
  8799. }
  8800. strncat(bc->dialed.number, bc->info_dad, sizeof(bc->dialed.number) - strlen(bc->dialed.number) - 1);
  8801. ast_channel_exten_set(ch->ast, bc->dialed.number);
  8802. ast_channel_lock(ch->ast);
  8803. pickup_cfg = ast_get_chan_features_pickup_config(ch->ast);
  8804. if (!pickup_cfg) {
  8805. ast_log(LOG_ERROR, "Unable to retrieve pickup configuration options. Unable to detect call pickup extension\n");
  8806. pickupexten = "";
  8807. } else {
  8808. pickupexten = ast_strdupa(pickup_cfg->pickupexten);
  8809. }
  8810. ast_channel_unlock(ch->ast);
  8811. /* Check for Pickup Request first */
  8812. if (!strcmp(ast_channel_exten(ch->ast), pickupexten)) {
  8813. if (ast_pickup_call(ch->ast)) {
  8814. hangup_chan(ch, bc);
  8815. } else {
  8816. ch->state = MISDN_CALLING_ACKNOWLEDGE;
  8817. hangup_chan(ch, bc);
  8818. ch->ast = NULL;
  8819. break;
  8820. }
  8821. }
  8822. if (!ast_canmatch_extension(ch->ast, ch->context, bc->dialed.number, 1, bc->caller.number)) {
  8823. if (ast_exists_extension(ch->ast, ch->context, "i", 1, bc->caller.number)) {
  8824. ast_log(LOG_WARNING,
  8825. "Extension '%s@%s' can never match. Jumping to 'i' extension. port:%d\n",
  8826. bc->dialed.number, ch->context, bc->port);
  8827. pbx_builtin_setvar_helper(ch->ast, "INVALID_EXTEN", bc->dialed.number);
  8828. ast_channel_exten_set(ch->ast, "i");
  8829. ch->state = MISDN_DIALING;
  8830. start_pbx(ch, bc, ch->ast);
  8831. break;
  8832. }
  8833. ast_log(LOG_WARNING,
  8834. "Extension '%s@%s' can never match. Disconnecting. port:%d\n"
  8835. "\tMaybe you want to add an 'i' extension to catch this case.\n",
  8836. bc->dialed.number, ch->context, bc->port);
  8837. if (bc->nt) {
  8838. hanguptone_indicate(ch);
  8839. }
  8840. ch->state = MISDN_EXTCANTMATCH;
  8841. bc->out_cause = AST_CAUSE_UNALLOCATED;
  8842. misdn_lib_send_event(bc, EVENT_DISCONNECT);
  8843. break;
  8844. }
  8845. if (ch->overlap_dial) {
  8846. ast_mutex_lock(&ch->overlap_tv_lock);
  8847. ch->overlap_tv = ast_tvnow();
  8848. ast_mutex_unlock(&ch->overlap_tv_lock);
  8849. if (ch->overlap_dial_task == -1) {
  8850. ch->overlap_dial_task =
  8851. misdn_tasks_add_variable(ch->overlap_dial, misdn_overlap_dial_task, ch);
  8852. }
  8853. break;
  8854. }
  8855. if (ast_exists_extension(ch->ast, ch->context, bc->dialed.number, 1, bc->caller.number)) {
  8856. ch->state = MISDN_DIALING;
  8857. start_pbx(ch, bc, ch->ast);
  8858. }
  8859. } else {
  8860. /* sending INFOS as DTMF-Frames :) */
  8861. struct ast_frame fr;
  8862. int digits;
  8863. memset(&fr, 0, sizeof(fr));
  8864. fr.frametype = AST_FRAME_DTMF;
  8865. fr.subclass.integer = bc->info_dad[0] ;
  8866. fr.src = NULL;
  8867. fr.data.ptr = NULL;
  8868. fr.datalen = 0;
  8869. fr.samples = 0;
  8870. fr.mallocd = 0;
  8871. fr.offset = 0;
  8872. fr.delivery = ast_tv(0,0);
  8873. misdn_cfg_get(0, MISDN_GEN_APPEND_DIGITS2EXTEN, &digits, sizeof(digits));
  8874. if (ch->state != MISDN_CONNECTED) {
  8875. if (digits) {
  8876. strncat(bc->dialed.number, bc->info_dad, sizeof(bc->dialed.number) - strlen(bc->dialed.number) - 1);
  8877. ast_channel_exten_set(ch->ast, bc->dialed.number);
  8878. }
  8879. ast_queue_frame(ch->ast, &fr);
  8880. }
  8881. }
  8882. break;
  8883. case EVENT_SETUP:
  8884. {
  8885. struct ast_channel *chan;
  8886. int exceed;
  8887. int ai;
  8888. int im;
  8889. int append_msn = 0;
  8890. RAII_VAR(struct ast_features_pickup_config *, pickup_cfg, NULL, ao2_cleanup);
  8891. const char *pickupexten;
  8892. if (ch) {
  8893. switch (ch->state) {
  8894. case MISDN_NOTHING:
  8895. chan_list_unref(ch, "Ignore found ch. Is it for an outgoing call?");
  8896. ch = NULL;
  8897. break;
  8898. default:
  8899. chan_list_unref(ch, "Already have a call.");
  8900. chan_misdn_log(1, bc->port, " --> Ignoring Call we have already one\n");
  8901. return RESPONSE_IGNORE_SETUP_WITHOUT_CLOSE; /* Ignore MSNs which are not in our List */
  8902. }
  8903. }
  8904. if (!bc->nt && !misdn_is_msn_valid(bc->port, &bc->dialed)) {
  8905. chan_misdn_log(1, bc->port, " --> Ignoring Call, its not in our MSN List\n");
  8906. return RESPONSE_IGNORE_SETUP; /* Ignore MSNs which are not in our List */
  8907. }
  8908. if (bc->cw) {
  8909. int cause;
  8910. chan_misdn_log(0, bc->port, " --> Call Waiting on PMP sending RELEASE_COMPLETE\n");
  8911. misdn_cfg_get(bc->port, MISDN_CFG_REJECT_CAUSE, &cause, sizeof(cause));
  8912. bc->out_cause = cause ? cause : AST_CAUSE_NORMAL_CLEARING;
  8913. return RESPONSE_RELEASE_SETUP;
  8914. }
  8915. print_bearer(bc);
  8916. ch = chan_list_init(ORG_MISDN);
  8917. if (!ch) {
  8918. chan_misdn_log(-1, bc->port, "cb_events: malloc for chan_list failed!\n");
  8919. return RESPONSE_RELEASE_SETUP;
  8920. }
  8921. ch->bc = bc;
  8922. ch->l3id = bc->l3_id;
  8923. ch->addr = bc->addr;
  8924. {
  8925. struct ast_format_cap *cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK);
  8926. struct ast_format tmpfmt;
  8927. if (!(cap)) {
  8928. return RESPONSE_ERR;
  8929. }
  8930. ast_format_cap_add(cap, ast_format_set(&tmpfmt, AST_FORMAT_ALAW, 0));
  8931. chan = misdn_new(ch, AST_STATE_RESERVED, bc->dialed.number, bc->caller.number, cap, NULL, NULL, bc->port, bc->channel);
  8932. cap = ast_format_cap_destroy(cap);
  8933. }
  8934. if (!chan) {
  8935. chan_list_unref(ch, "Failed to create a new channel");
  8936. ast_log(LOG_ERROR, "cb_events: misdn_new failed!\n");
  8937. return RESPONSE_RELEASE_SETUP;
  8938. }
  8939. ast_channel_lock(chan);
  8940. pickup_cfg = ast_get_chan_features_pickup_config(chan);
  8941. if (!pickup_cfg) {
  8942. ast_log(LOG_ERROR, "Unable to retrieve pickup configuration options. Unable to detect call pickup extension\n");
  8943. pickupexten = "";
  8944. } else {
  8945. pickupexten = ast_strdupa(pickup_cfg->pickupexten);
  8946. }
  8947. ast_channel_unlock(chan);
  8948. if ((exceed = add_in_calls(bc->port))) {
  8949. char tmp[16];
  8950. snprintf(tmp, sizeof(tmp), "%d", exceed);
  8951. pbx_builtin_setvar_helper(chan, "MAX_OVERFLOW", tmp);
  8952. }
  8953. read_config(ch);
  8954. export_ch(chan, bc, ch);
  8955. ast_channel_lock(ch->ast);
  8956. ast_channel_rings_set(ch->ast, 1);
  8957. ast_setstate(ch->ast, AST_STATE_RINGING);
  8958. ast_channel_unlock(ch->ast);
  8959. /* Update asterisk channel caller information */
  8960. chan_misdn_log(2, bc->port, " --> TON: %s(%d)\n", misdn_to_str_ton(bc->caller.number_type), bc->caller.number_type);
  8961. chan_misdn_log(2, bc->port, " --> PLAN: %s(%d)\n", misdn_to_str_plan(bc->caller.number_plan), bc->caller.number_plan);
  8962. ast_channel_caller(chan)->id.number.plan = misdn_to_ast_ton(bc->caller.number_type)
  8963. | misdn_to_ast_plan(bc->caller.number_plan);
  8964. chan_misdn_log(2, bc->port, " --> PRES: %s(%d)\n", misdn_to_str_pres(bc->caller.presentation), bc->caller.presentation);
  8965. chan_misdn_log(2, bc->port, " --> SCREEN: %s(%d)\n", misdn_to_str_screen(bc->caller.screening), bc->caller.screening);
  8966. ast_channel_caller(chan)->id.number.presentation = misdn_to_ast_pres(bc->caller.presentation)
  8967. | misdn_to_ast_screen(bc->caller.screening);
  8968. ast_set_callerid(chan, bc->caller.number, NULL, bc->caller.number);
  8969. misdn_cfg_get(bc->port, MISDN_CFG_APPEND_MSN_TO_CALLERID_TAG, &append_msn, sizeof(append_msn));
  8970. if (append_msn) {
  8971. strncat(bc->incoming_cid_tag, "_", sizeof(bc->incoming_cid_tag) - strlen(bc->incoming_cid_tag) - 1);
  8972. strncat(bc->incoming_cid_tag, bc->dialed.number, sizeof(bc->incoming_cid_tag) - strlen(bc->incoming_cid_tag) - 1);
  8973. }
  8974. ast_channel_lock(chan);
  8975. ast_channel_caller(chan)->id.tag = ast_strdup(bc->incoming_cid_tag);
  8976. ast_channel_unlock(chan);
  8977. if (!ast_strlen_zero(bc->redirecting.from.number)) {
  8978. /* Add configured prefix to redirecting.from.number */
  8979. misdn_add_number_prefix(bc->port, bc->redirecting.from.number_type, bc->redirecting.from.number, sizeof(bc->redirecting.from.number));
  8980. /* Update asterisk channel redirecting information */
  8981. misdn_copy_redirecting_to_ast(chan, &bc->redirecting, bc->incoming_cid_tag);
  8982. }
  8983. pbx_builtin_setvar_helper(chan, "TRANSFERCAPABILITY", ast_transfercapability2str(bc->capability));
  8984. ast_channel_transfercapability_set(chan, bc->capability);
  8985. switch (bc->capability) {
  8986. case INFO_CAPABILITY_DIGITAL_UNRESTRICTED:
  8987. pbx_builtin_setvar_helper(chan, "CALLTYPE", "DIGITAL");
  8988. break;
  8989. default:
  8990. pbx_builtin_setvar_helper(chan, "CALLTYPE", "SPEECH");
  8991. break;
  8992. }
  8993. if (!strstr(ch->allowed_bearers, "all")) {
  8994. int i;
  8995. for (i = 0; i < ARRAY_LEN(allowed_bearers_array); ++i) {
  8996. if (allowed_bearers_array[i].cap == bc->capability) {
  8997. if (strstr(ch->allowed_bearers, allowed_bearers_array[i].name)) {
  8998. /* The bearer capability is allowed */
  8999. if (allowed_bearers_array[i].deprecated) {
  9000. chan_misdn_log(0, bc->port, "%s in allowed_bearers list is deprecated\n",
  9001. allowed_bearers_array[i].name);
  9002. }
  9003. break;
  9004. }
  9005. }
  9006. }
  9007. if (i == ARRAY_LEN(allowed_bearers_array)) {
  9008. /* We did not find the bearer capability */
  9009. chan_misdn_log(0, bc->port, "Bearer capability not allowed: %s(%d)\n",
  9010. bearer2str(bc->capability), bc->capability);
  9011. ch->state = MISDN_EXTCANTMATCH;
  9012. chan_list_unref(ch, "BC not allowed, releasing call");
  9013. bc->out_cause = AST_CAUSE_INCOMPATIBLE_DESTINATION;
  9014. return RESPONSE_RELEASE_SETUP;
  9015. }
  9016. }
  9017. /** queue new chan **/
  9018. cl_queue_chan(ch);
  9019. if (bc->fac_in.Function != Fac_None) {
  9020. misdn_facility_ie_handler(event, bc, ch);
  9021. }
  9022. /* Check for Pickup Request first */
  9023. if (!strcmp(ast_channel_exten(chan), pickupexten)) {
  9024. if (!ch->noautorespond_on_setup) {
  9025. /* Sending SETUP_ACK */
  9026. misdn_lib_send_event(bc, EVENT_SETUP_ACKNOWLEDGE);
  9027. } else {
  9028. ch->state = MISDN_INCOMING_SETUP;
  9029. }
  9030. if (ast_pickup_call(chan)) {
  9031. hangup_chan(ch, bc);
  9032. } else {
  9033. ch->state = MISDN_CALLING_ACKNOWLEDGE;
  9034. hangup_chan(ch, bc);
  9035. ch->ast = NULL;
  9036. break;
  9037. }
  9038. }
  9039. /*
  9040. * added support for s extension hope it will help those poor cretains
  9041. * which haven't overlap dial.
  9042. */
  9043. misdn_cfg_get(bc->port, MISDN_CFG_ALWAYS_IMMEDIATE, &ai, sizeof(ai));
  9044. if (ai) {
  9045. do_immediate_setup(bc, ch, chan);
  9046. break;
  9047. }
  9048. /* check if we should jump into s when we have no dialed.number */
  9049. misdn_cfg_get(bc->port, MISDN_CFG_IMMEDIATE, &im, sizeof(im));
  9050. if (im && ast_strlen_zero(bc->dialed.number)) {
  9051. do_immediate_setup(bc, ch, chan);
  9052. break;
  9053. }
  9054. chan_misdn_log(5, bc->port, "CONTEXT:%s\n", ch->context);
  9055. if (!ast_canmatch_extension(ch->ast, ch->context, bc->dialed.number, 1, bc->caller.number)) {
  9056. if (ast_exists_extension(ch->ast, ch->context, "i", 1, bc->caller.number)) {
  9057. ast_log(LOG_WARNING,
  9058. "Extension '%s@%s' can never match. Jumping to 'i' extension. port:%d\n",
  9059. bc->dialed.number, ch->context, bc->port);
  9060. pbx_builtin_setvar_helper(ch->ast, "INVALID_EXTEN", bc->dialed.number);
  9061. ast_channel_exten_set(ch->ast, "i");
  9062. misdn_lib_send_event(bc, EVENT_SETUP_ACKNOWLEDGE);
  9063. ch->state = MISDN_DIALING;
  9064. start_pbx(ch, bc, chan);
  9065. break;
  9066. }
  9067. ast_log(LOG_WARNING,
  9068. "Extension '%s@%s' can never match. Disconnecting. port:%d\n"
  9069. "\tMaybe you want to add an 'i' extension to catch this case.\n",
  9070. bc->dialed.number, ch->context, bc->port);
  9071. if (bc->nt) {
  9072. hanguptone_indicate(ch);
  9073. }
  9074. ch->state = MISDN_EXTCANTMATCH;
  9075. bc->out_cause = AST_CAUSE_UNALLOCATED;
  9076. misdn_lib_send_event(bc, bc->nt ? EVENT_RELEASE_COMPLETE : EVENT_RELEASE);
  9077. break;
  9078. }
  9079. /* Whatever happens, when sending_complete is set or we are PTMP TE, we will definitely
  9080. * jump into the dialplan, when the dialed extension does not exist, the 's' extension
  9081. * will be used by Asterisk automatically. */
  9082. if (bc->sending_complete || (!bc->nt && !misdn_lib_is_ptp(bc->port))) {
  9083. if (!ch->noautorespond_on_setup) {
  9084. ch->state=MISDN_DIALING;
  9085. misdn_lib_send_event(bc, EVENT_PROCEEDING);
  9086. } else {
  9087. ch->state = MISDN_INCOMING_SETUP;
  9088. }
  9089. start_pbx(ch, bc, chan);
  9090. break;
  9091. }
  9092. /*
  9093. * When we are NT and overlapdial is set and if
  9094. * the number is empty, we wait for the ISDN timeout
  9095. * instead of our own timer.
  9096. */
  9097. if (ch->overlap_dial && bc->nt && !bc->dialed.number[0]) {
  9098. wait_for_digits(ch, bc, chan);
  9099. break;
  9100. }
  9101. /*
  9102. * If overlapdial we will definitely send a SETUP_ACKNOWLEDGE and wait for more
  9103. * Infos with a Interdigit Timeout.
  9104. * */
  9105. if (ch->overlap_dial) {
  9106. ast_mutex_lock(&ch->overlap_tv_lock);
  9107. ch->overlap_tv = ast_tvnow();
  9108. ast_mutex_unlock(&ch->overlap_tv_lock);
  9109. wait_for_digits(ch, bc, chan);
  9110. if (ch->overlap_dial_task == -1) {
  9111. ch->overlap_dial_task =
  9112. misdn_tasks_add_variable(ch->overlap_dial, misdn_overlap_dial_task, ch);
  9113. }
  9114. break;
  9115. }
  9116. /* If the extension does not exist and we're not TE_PTMP we wait for more digits
  9117. * without interdigit timeout.
  9118. * */
  9119. if (!ast_exists_extension(ch->ast, ch->context, bc->dialed.number, 1, bc->caller.number)) {
  9120. wait_for_digits(ch, bc, chan);
  9121. break;
  9122. }
  9123. /*
  9124. * If the extension exists let's just jump into it.
  9125. * */
  9126. if (ast_exists_extension(ch->ast, ch->context, bc->dialed.number, 1, bc->caller.number)) {
  9127. misdn_lib_send_event(bc, bc->need_more_infos ? EVENT_SETUP_ACKNOWLEDGE : EVENT_PROCEEDING);
  9128. ch->state = MISDN_DIALING;
  9129. start_pbx(ch, bc, chan);
  9130. break;
  9131. }
  9132. break;
  9133. }
  9134. #if defined(AST_MISDN_ENHANCEMENTS)
  9135. case EVENT_REGISTER:
  9136. if (bc->fac_in.Function != Fac_None) {
  9137. misdn_facility_ie_handler(event, bc, ch);
  9138. }
  9139. /*
  9140. * Shut down this connection immediately.
  9141. * The current design of chan_misdn data structures
  9142. * does not allow the proper handling of inbound call records
  9143. * without an assigned B channel. Therefore, we cannot
  9144. * be the CCBS User-B party in a point-to-point setup.
  9145. */
  9146. bc->fac_out.Function = Fac_None;
  9147. bc->out_cause = AST_CAUSE_NORMAL_CLEARING;
  9148. misdn_lib_send_event(bc, EVENT_RELEASE_COMPLETE);
  9149. break;
  9150. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  9151. case EVENT_SETUP_ACKNOWLEDGE:
  9152. ch->state = MISDN_CALLING_ACKNOWLEDGE;
  9153. if (bc->channel) {
  9154. update_name(ch->ast,bc->port,bc->channel);
  9155. }
  9156. if (bc->fac_in.Function != Fac_None) {
  9157. misdn_facility_ie_handler(event, bc, ch);
  9158. }
  9159. if (!ast_strlen_zero(bc->infos_pending)) {
  9160. /* TX Pending Infos */
  9161. strncat(bc->dialed.number, bc->infos_pending, sizeof(bc->dialed.number) - strlen(bc->dialed.number) - 1);
  9162. if (!ch->ast) {
  9163. break;
  9164. }
  9165. ast_channel_exten_set(ch->ast, bc->dialed.number);
  9166. ast_copy_string(bc->info_dad, bc->infos_pending, sizeof(bc->info_dad));
  9167. ast_copy_string(bc->infos_pending, "", sizeof(bc->infos_pending));
  9168. misdn_lib_send_event(bc, EVENT_INFORMATION);
  9169. }
  9170. break;
  9171. case EVENT_PROCEEDING:
  9172. if (misdn_cap_is_speech(bc->capability) &&
  9173. misdn_inband_avail(bc)) {
  9174. start_bc_tones(ch);
  9175. }
  9176. ch->state = MISDN_PROCEEDING;
  9177. if (bc->fac_in.Function != Fac_None) {
  9178. misdn_facility_ie_handler(event, bc, ch);
  9179. }
  9180. if (!ch->ast) {
  9181. break;
  9182. }
  9183. ast_queue_control(ch->ast, AST_CONTROL_PROCEEDING);
  9184. break;
  9185. case EVENT_PROGRESS:
  9186. if (bc->channel) {
  9187. update_name(ch->ast, bc->port, bc->channel);
  9188. }
  9189. if (bc->fac_in.Function != Fac_None) {
  9190. misdn_facility_ie_handler(event, bc, ch);
  9191. }
  9192. if (!bc->nt) {
  9193. if (misdn_cap_is_speech(bc->capability) &&
  9194. misdn_inband_avail(bc)) {
  9195. start_bc_tones(ch);
  9196. }
  9197. ch->state = MISDN_PROGRESS;
  9198. if (!ch->ast) {
  9199. break;
  9200. }
  9201. ast_queue_control(ch->ast, AST_CONTROL_PROGRESS);
  9202. }
  9203. break;
  9204. case EVENT_ALERTING:
  9205. ch->state = MISDN_ALERTING;
  9206. if (!ch->ast) {
  9207. break;
  9208. }
  9209. if (bc->fac_in.Function != Fac_None) {
  9210. misdn_facility_ie_handler(event, bc, ch);
  9211. }
  9212. ast_queue_control(ch->ast, AST_CONTROL_RINGING);
  9213. ast_channel_lock(ch->ast);
  9214. ast_setstate(ch->ast, AST_STATE_RINGING);
  9215. ast_channel_unlock(ch->ast);
  9216. cb_log(7, bc->port, " --> Set State Ringing\n");
  9217. if (misdn_cap_is_speech(bc->capability) && misdn_inband_avail(bc)) {
  9218. cb_log(1, bc->port, "Starting Tones, we have inband Data\n");
  9219. start_bc_tones(ch);
  9220. } else {
  9221. cb_log(3, bc->port, " --> We have no inband Data, the other end must create ringing\n");
  9222. if (ch->far_alerting) {
  9223. cb_log(1, bc->port, " --> The other end can not do ringing eh ?.. we must do all ourself..");
  9224. start_bc_tones(ch);
  9225. /*tone_indicate(ch, TONE_FAR_ALERTING);*/
  9226. }
  9227. }
  9228. break;
  9229. case EVENT_CONNECT:
  9230. if (bc->fac_in.Function != Fac_None) {
  9231. misdn_facility_ie_handler(event, bc, ch);
  9232. }
  9233. #if defined(AST_MISDN_ENHANCEMENTS)
  9234. if (bc->div_leg_3_rx_wanted) {
  9235. bc->div_leg_3_rx_wanted = 0;
  9236. if (ch->ast) {
  9237. struct ast_party_redirecting redirecting;
  9238. ast_channel_redirecting(ch->ast)->to.number.presentation =
  9239. AST_PRES_RESTRICTED | AST_PRES_USER_NUMBER_UNSCREENED;
  9240. ast_party_redirecting_init(&redirecting);
  9241. ast_party_redirecting_copy(&redirecting, ast_channel_redirecting(ch->ast));
  9242. /*
  9243. * Reset any earlier private redirecting id representations and
  9244. * make sure that it is invalidated at the remote end.
  9245. */
  9246. ast_party_id_reset(&redirecting.priv_orig);
  9247. ast_party_id_reset(&redirecting.priv_from);
  9248. ast_party_id_reset(&redirecting.priv_to);
  9249. ast_channel_queue_redirecting_update(ch->ast, &redirecting, NULL);
  9250. ast_party_redirecting_free(&redirecting);
  9251. }
  9252. }
  9253. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  9254. /* we answer when we've got our very new L3 ID from the NT stack */
  9255. misdn_lib_send_event(bc, EVENT_CONNECT_ACKNOWLEDGE);
  9256. if (!ch->ast) {
  9257. break;
  9258. }
  9259. stop_indicate(ch);
  9260. #if defined(AST_MISDN_ENHANCEMENTS)
  9261. if (ch->record_id != -1) {
  9262. /*
  9263. * We will delete the associated call completion
  9264. * record since we now have a completed call.
  9265. * We will not wait/depend on the network to tell
  9266. * us to delete it.
  9267. */
  9268. AST_LIST_LOCK(&misdn_cc_records_db);
  9269. cc_record = misdn_cc_find_by_id(ch->record_id);
  9270. if (cc_record) {
  9271. if (cc_record->ptp && cc_record->mode.ptp.bc) {
  9272. /* Close the call-completion signaling link */
  9273. cc_record->mode.ptp.bc->fac_out.Function = Fac_None;
  9274. cc_record->mode.ptp.bc->out_cause = AST_CAUSE_NORMAL_CLEARING;
  9275. misdn_lib_send_event(cc_record->mode.ptp.bc, EVENT_RELEASE_COMPLETE);
  9276. }
  9277. misdn_cc_delete(cc_record);
  9278. }
  9279. AST_LIST_UNLOCK(&misdn_cc_records_db);
  9280. ch->record_id = -1;
  9281. if (ch->peer) {
  9282. misdn_cc_set_peer_var(ch->peer, MISDN_CC_RECORD_ID, "");
  9283. ao2_ref(ch->peer, -1);
  9284. ch->peer = NULL;
  9285. }
  9286. }
  9287. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  9288. if (!ast_strlen_zero(bc->connected.number)) {
  9289. /* Add configured prefix to connected.number */
  9290. misdn_add_number_prefix(bc->port, bc->connected.number_type, bc->connected.number, sizeof(bc->connected.number));
  9291. /* Update the connected line information on the other channel */
  9292. misdn_update_remote_party(ch->ast, &bc->connected, AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER, bc->incoming_cid_tag);
  9293. }
  9294. ch->l3id = bc->l3_id;
  9295. ch->addr = bc->addr;
  9296. start_bc_tones(ch);
  9297. ch->state = MISDN_CONNECTED;
  9298. ast_queue_control(ch->ast, AST_CONTROL_ANSWER);
  9299. break;
  9300. case EVENT_CONNECT_ACKNOWLEDGE:
  9301. ch->l3id = bc->l3_id;
  9302. ch->addr = bc->addr;
  9303. start_bc_tones(ch);
  9304. ch->state = MISDN_CONNECTED;
  9305. break;
  9306. case EVENT_DISCONNECT:
  9307. /* we might not have an ch->ast ptr here anymore */
  9308. if (ch) {
  9309. if (bc->fac_in.Function != Fac_None) {
  9310. misdn_facility_ie_handler(event, bc, ch);
  9311. }
  9312. 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);
  9313. if (ch->originator == ORG_AST && !bc->nt && misdn_inband_avail(bc) && ch->state != MISDN_CONNECTED) {
  9314. /* If there's inband information available (e.g. a
  9315. recorded message saying what was wrong with the
  9316. dialled number, or perhaps even giving an
  9317. alternative number, then play it instead of
  9318. immediately releasing the call */
  9319. chan_misdn_log(1, bc->port, " --> Inband Info Avail, not sending RELEASE\n");
  9320. ch->state = MISDN_DISCONNECTED;
  9321. start_bc_tones(ch);
  9322. if (ch->ast) {
  9323. ast_channel_hangupcause_set(ch->ast, bc->cause);
  9324. if (bc->cause == AST_CAUSE_USER_BUSY) {
  9325. ast_queue_control(ch->ast, AST_CONTROL_BUSY);
  9326. }
  9327. }
  9328. ch->need_busy = 0;
  9329. break;
  9330. }
  9331. bc->need_disconnect = 0;
  9332. stop_bc_tones(ch);
  9333. /* Check for held channel, to implement transfer */
  9334. held_ch = find_hold_call(bc);
  9335. if (!held_ch || !ch->ast || misdn_attempt_transfer(ch, held_ch)) {
  9336. hangup_chan(ch, bc);
  9337. }
  9338. } else {
  9339. held_ch = find_hold_call_l3(bc->l3_id);
  9340. if (held_ch) {
  9341. if (bc->fac_in.Function != Fac_None) {
  9342. misdn_facility_ie_handler(event, bc, held_ch);
  9343. }
  9344. if (held_ch->hold.state == MISDN_HOLD_ACTIVE) {
  9345. bc->need_disconnect = 0;
  9346. #if defined(TRANSFER_ON_HELD_CALL_HANGUP)
  9347. /*
  9348. * Some phones disconnect the held call and the active call at the
  9349. * same time to do the transfer. Unfortunately, either call could
  9350. * be disconnected first.
  9351. */
  9352. ch = find_hold_active_call(bc);
  9353. if (!ch || misdn_attempt_transfer(ch, held_ch)) {
  9354. held_ch->hold.state = MISDN_HOLD_DISCONNECT;
  9355. hangup_chan(held_ch, bc);
  9356. }
  9357. #else
  9358. hangup_chan(held_ch, bc);
  9359. #endif /* defined(TRANSFER_ON_HELD_CALL_HANGUP) */
  9360. }
  9361. }
  9362. }
  9363. if (held_ch) {
  9364. chan_list_unref(held_ch, "Done with held call");
  9365. }
  9366. bc->out_cause = -1;
  9367. if (bc->need_release) {
  9368. misdn_lib_send_event(bc, EVENT_RELEASE);
  9369. }
  9370. break;
  9371. case EVENT_RELEASE:
  9372. if (!ch) {
  9373. ch = find_hold_call_l3(bc->l3_id);
  9374. if (!ch) {
  9375. chan_misdn_log(1, bc->port,
  9376. " --> no Ch, so we've already released. (%s)\n",
  9377. manager_isdn_get_info(event));
  9378. return -1;
  9379. }
  9380. }
  9381. if (bc->fac_in.Function != Fac_None) {
  9382. misdn_facility_ie_handler(event, bc, ch);
  9383. }
  9384. bc->need_disconnect = 0;
  9385. bc->need_release = 0;
  9386. hangup_chan(ch, bc);
  9387. release_chan(ch, bc);
  9388. break;
  9389. case EVENT_RELEASE_COMPLETE:
  9390. if (!ch) {
  9391. ch = find_hold_call_l3(bc->l3_id);
  9392. }
  9393. bc->need_disconnect = 0;
  9394. bc->need_release = 0;
  9395. bc->need_release_complete = 0;
  9396. if (ch) {
  9397. if (bc->fac_in.Function != Fac_None) {
  9398. misdn_facility_ie_handler(event, bc, ch);
  9399. }
  9400. stop_bc_tones(ch);
  9401. hangup_chan(ch, bc);
  9402. release_chan(ch, bc);
  9403. } else {
  9404. #if defined(AST_MISDN_ENHANCEMENTS)
  9405. /*
  9406. * A call-completion signaling link established with
  9407. * REGISTER does not have a struct chan_list record
  9408. * associated with it.
  9409. */
  9410. AST_LIST_LOCK(&misdn_cc_records_db);
  9411. cc_record = misdn_cc_find_by_bc(bc);
  9412. if (cc_record) {
  9413. /* The call-completion signaling link is closed. */
  9414. misdn_cc_delete(cc_record);
  9415. }
  9416. AST_LIST_UNLOCK(&misdn_cc_records_db);
  9417. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  9418. chan_misdn_log(1, bc->port,
  9419. " --> no Ch, so we've already released. (%s)\n",
  9420. manager_isdn_get_info(event));
  9421. }
  9422. break;
  9423. case EVENT_BCHAN_ERROR:
  9424. case EVENT_CLEANUP:
  9425. stop_bc_tones(ch);
  9426. switch (ch->state) {
  9427. case MISDN_CALLING:
  9428. bc->cause = AST_CAUSE_DESTINATION_OUT_OF_ORDER;
  9429. break;
  9430. default:
  9431. break;
  9432. }
  9433. hangup_chan(ch, bc);
  9434. release_chan(ch, bc);
  9435. break;
  9436. case EVENT_TONE_GENERATE:
  9437. {
  9438. int tone_len = bc->tone_cnt;
  9439. struct ast_channel *ast = ch->ast;
  9440. void *tmp;
  9441. int res;
  9442. int (*generate)(struct ast_channel *chan, void *tmp, int datalen, int samples);
  9443. chan_misdn_log(9, bc->port, "TONE_GEN: len:%d\n", tone_len);
  9444. if (!ast) {
  9445. break;
  9446. }
  9447. if (!ast_channel_generator(ast)) {
  9448. break;
  9449. }
  9450. tmp = ast_channel_generatordata(ast);
  9451. ast_channel_generatordata_set(ast, NULL);
  9452. generate = ast_channel_generator(ast)->generate;
  9453. if (tone_len < 0 || tone_len > 512) {
  9454. ast_log(LOG_NOTICE, "TONE_GEN: len was %d, set to 128\n", tone_len);
  9455. tone_len = 128;
  9456. }
  9457. res = generate(ast, tmp, tone_len, tone_len);
  9458. ast_channel_generatordata_set(ast, tmp);
  9459. if (res) {
  9460. ast_log(LOG_WARNING, "Auto-deactivating generator\n");
  9461. ast_deactivate_generator(ast);
  9462. } else {
  9463. bc->tone_cnt = 0;
  9464. }
  9465. break;
  9466. }
  9467. case EVENT_BCHAN_DATA:
  9468. if (ch->bc->AOCD_need_export) {
  9469. export_aoc_vars(ch->originator, ch->ast, ch->bc);
  9470. }
  9471. if (!misdn_cap_is_speech(ch->bc->capability)) {
  9472. struct ast_frame frame;
  9473. /* In Data Modes we queue frames */
  9474. memset(&frame, 0, sizeof(frame));
  9475. frame.frametype = AST_FRAME_VOICE; /* we have no data frames yet */
  9476. ast_format_set(&frame.subclass.format, AST_FORMAT_ALAW, 0);
  9477. frame.datalen = bc->bframe_len;
  9478. frame.samples = bc->bframe_len;
  9479. frame.mallocd = 0;
  9480. frame.offset = 0;
  9481. frame.delivery = ast_tv(0, 0);
  9482. frame.src = NULL;
  9483. frame.data.ptr = bc->bframe;
  9484. if (ch->ast) {
  9485. ast_queue_frame(ch->ast, &frame);
  9486. }
  9487. } else {
  9488. struct pollfd pfd = { .fd = ch->pipe[1], .events = POLLOUT };
  9489. int t;
  9490. t = ast_poll(&pfd, 1, 0);
  9491. if (t < 0) {
  9492. chan_misdn_log(-1, bc->port, "poll() error (err=%s)\n", strerror(errno));
  9493. break;
  9494. }
  9495. if (!t) {
  9496. chan_misdn_log(9, bc->port, "poll() timed out\n");
  9497. break;
  9498. }
  9499. if (pfd.revents & POLLOUT) {
  9500. chan_misdn_log(9, bc->port, "writing %d bytes to asterisk\n", bc->bframe_len);
  9501. if (write(ch->pipe[1], bc->bframe, bc->bframe_len) <= 0) {
  9502. chan_misdn_log(0, bc->port, "Write returned <=0 (err=%s) --> hanging up channel\n", strerror(errno));
  9503. stop_bc_tones(ch);
  9504. hangup_chan(ch, bc);
  9505. release_chan(ch, bc);
  9506. }
  9507. } else {
  9508. chan_misdn_log(1, bc->port, "Write Pipe full!\n");
  9509. }
  9510. }
  9511. break;
  9512. case EVENT_TIMEOUT:
  9513. if (ch && bc) {
  9514. chan_misdn_log(1, bc->port, "--> state: %s\n", misdn_get_ch_state(ch));
  9515. }
  9516. switch (ch->state) {
  9517. case MISDN_DIALING:
  9518. case MISDN_PROGRESS:
  9519. if (bc->nt && !ch->nttimeout) {
  9520. break;
  9521. }
  9522. /* fall-through */
  9523. case MISDN_CALLING:
  9524. case MISDN_ALERTING:
  9525. case MISDN_PROCEEDING:
  9526. case MISDN_CALLING_ACKNOWLEDGE:
  9527. if (bc->nt) {
  9528. bc->progress_indicator = INFO_PI_INBAND_AVAILABLE;
  9529. hanguptone_indicate(ch);
  9530. }
  9531. bc->out_cause = AST_CAUSE_UNALLOCATED;
  9532. misdn_lib_send_event(bc, EVENT_DISCONNECT);
  9533. break;
  9534. case MISDN_WAITING4DIGS:
  9535. if (bc->nt) {
  9536. bc->progress_indicator = INFO_PI_INBAND_AVAILABLE;
  9537. bc->out_cause = AST_CAUSE_UNALLOCATED;
  9538. hanguptone_indicate(ch);
  9539. misdn_lib_send_event(bc, EVENT_DISCONNECT);
  9540. } else {
  9541. bc->out_cause = AST_CAUSE_NORMAL_CLEARING;
  9542. misdn_lib_send_event(bc, EVENT_RELEASE);
  9543. }
  9544. break;
  9545. case MISDN_CLEANING:
  9546. chan_misdn_log(1, bc->port, " --> in state cleaning .. so ignoring, the stack should clean it for us\n");
  9547. break;
  9548. default:
  9549. misdn_lib_send_event(bc, EVENT_RELEASE_COMPLETE);
  9550. break;
  9551. }
  9552. break;
  9553. /****************************/
  9554. /** Supplementary Services **/
  9555. /****************************/
  9556. case EVENT_RETRIEVE:
  9557. if (!ch) {
  9558. chan_misdn_log(4, bc->port, " --> no CH, searching for held call\n");
  9559. ch = find_hold_call_l3(bc->l3_id);
  9560. if (!ch || ch->hold.state != MISDN_HOLD_ACTIVE) {
  9561. ast_log(LOG_WARNING, "No held call found, cannot Retrieve\n");
  9562. misdn_lib_send_event(bc, EVENT_RETRIEVE_REJECT);
  9563. break;
  9564. }
  9565. }
  9566. /* remember the channel again */
  9567. ch->bc = bc;
  9568. ch->hold.state = MISDN_HOLD_IDLE;
  9569. ch->hold.port = 0;
  9570. ch->hold.channel = 0;
  9571. ast_queue_unhold(ch->ast);
  9572. if (misdn_lib_send_event(bc, EVENT_RETRIEVE_ACKNOWLEDGE) < 0) {
  9573. chan_misdn_log(4, bc->port, " --> RETRIEVE_ACK failed\n");
  9574. misdn_lib_send_event(bc, EVENT_RETRIEVE_REJECT);
  9575. }
  9576. break;
  9577. case EVENT_HOLD:
  9578. {
  9579. int hold_allowed;
  9580. RAII_VAR(struct ast_channel *, bridged, NULL, ast_channel_cleanup);
  9581. misdn_cfg_get(bc->port, MISDN_CFG_HOLD_ALLOWED, &hold_allowed, sizeof(hold_allowed));
  9582. if (!hold_allowed) {
  9583. chan_misdn_log(-1, bc->port, "Hold not allowed this port.\n");
  9584. misdn_lib_send_event(bc, EVENT_HOLD_REJECT);
  9585. break;
  9586. }
  9587. bridged = ast_channel_bridge_peer(ch->ast);
  9588. if (bridged) {
  9589. chan_misdn_log(2, bc->port, "Bridge Partner is of type: %s\n", ast_channel_tech(bridged)->type);
  9590. ch->l3id = bc->l3_id;
  9591. /* forget the channel now */
  9592. ch->bc = NULL;
  9593. ch->hold.state = MISDN_HOLD_ACTIVE;
  9594. ch->hold.port = bc->port;
  9595. ch->hold.channel = bc->channel;
  9596. ast_queue_hold(ch->ast, NULL);
  9597. misdn_lib_send_event(bc, EVENT_HOLD_ACKNOWLEDGE);
  9598. } else {
  9599. misdn_lib_send_event(bc, EVENT_HOLD_REJECT);
  9600. chan_misdn_log(0, bc->port, "We aren't bridged to anybody\n");
  9601. }
  9602. break;
  9603. }
  9604. case EVENT_NOTIFY:
  9605. if (bc->redirecting.to_changed) {
  9606. /* Add configured prefix to redirecting.to.number */
  9607. misdn_add_number_prefix(bc->port, bc->redirecting.to.number_type,
  9608. bc->redirecting.to.number, sizeof(bc->redirecting.to.number));
  9609. }
  9610. switch (bc->notify_description_code) {
  9611. case mISDN_NOTIFY_CODE_DIVERSION_ACTIVATED:
  9612. /* Ignore for now. */
  9613. bc->redirecting.to_changed = 0;
  9614. break;
  9615. case mISDN_NOTIFY_CODE_CALL_IS_DIVERTING:
  9616. {
  9617. struct ast_party_redirecting redirecting;
  9618. if (!bc->redirecting.to_changed) {
  9619. break;
  9620. }
  9621. bc->redirecting.to_changed = 0;
  9622. if (!ch || !ch->ast) {
  9623. break;
  9624. }
  9625. switch (ch->state) {
  9626. case MISDN_ALERTING:
  9627. /* Call is deflecting after we have seen an ALERTING message */
  9628. bc->redirecting.reason = mISDN_REDIRECTING_REASON_NO_REPLY;
  9629. break;
  9630. default:
  9631. /* Call is deflecting for call forwarding unconditional or busy reason. */
  9632. bc->redirecting.reason = mISDN_REDIRECTING_REASON_UNKNOWN;
  9633. break;
  9634. }
  9635. misdn_copy_redirecting_to_ast(ch->ast, &bc->redirecting, bc->incoming_cid_tag);
  9636. ast_party_redirecting_init(&redirecting);
  9637. ast_party_redirecting_copy(&redirecting, ast_channel_redirecting(ch->ast));
  9638. /*
  9639. * Reset any earlier private redirecting id representations and
  9640. * make sure that it is invalidated at the remote end.
  9641. */
  9642. ast_party_id_reset(&redirecting.priv_orig);
  9643. ast_party_id_reset(&redirecting.priv_from);
  9644. ast_party_id_reset(&redirecting.priv_to);
  9645. ast_channel_queue_redirecting_update(ch->ast, &redirecting, NULL);
  9646. ast_party_redirecting_free(&redirecting);
  9647. break;
  9648. }
  9649. case mISDN_NOTIFY_CODE_CALL_TRANSFER_ALERTING:
  9650. /*
  9651. * It would be preferable to update the connected line information
  9652. * only when the message callStatus is active. However, the
  9653. * optional redirection number may not be present in the active
  9654. * message if an alerting message were received earlier.
  9655. *
  9656. * The consequences if we wind up sending two updates is benign.
  9657. * The other end will think that it got transferred twice.
  9658. */
  9659. if (!bc->redirecting.to_changed) {
  9660. break;
  9661. }
  9662. bc->redirecting.to_changed = 0;
  9663. if (!ch || !ch->ast) {
  9664. break;
  9665. }
  9666. misdn_update_remote_party(ch->ast, &bc->redirecting.to,
  9667. AST_CONNECTED_LINE_UPDATE_SOURCE_TRANSFER_ALERTING,
  9668. bc->incoming_cid_tag);
  9669. break;
  9670. case mISDN_NOTIFY_CODE_CALL_TRANSFER_ACTIVE:
  9671. if (!bc->redirecting.to_changed) {
  9672. break;
  9673. }
  9674. bc->redirecting.to_changed = 0;
  9675. if (!ch || !ch->ast) {
  9676. break;
  9677. }
  9678. misdn_update_remote_party(ch->ast, &bc->redirecting.to,
  9679. AST_CONNECTED_LINE_UPDATE_SOURCE_TRANSFER, bc->incoming_cid_tag);
  9680. break;
  9681. default:
  9682. bc->redirecting.to_changed = 0;
  9683. chan_misdn_log(0, bc->port," --> not yet handled: notify code:0x%02X\n",
  9684. bc->notify_description_code);
  9685. break;
  9686. }
  9687. bc->notify_description_code = mISDN_NOTIFY_CODE_INVALID;
  9688. break;
  9689. case EVENT_FACILITY:
  9690. if (bc->fac_in.Function == Fac_None) {
  9691. /* This is a FACILITY message so we MUST have a facility ie */
  9692. chan_misdn_log(0, bc->port," --> Missing facility ie or unknown facility ie contents.\n");
  9693. } else {
  9694. misdn_facility_ie_handler(event, bc, ch);
  9695. }
  9696. /* In case it came in on a FACILITY message and we did not handle it. */
  9697. bc->redirecting.to_changed = 0;
  9698. bc->notify_description_code = mISDN_NOTIFY_CODE_INVALID;
  9699. break;
  9700. case EVENT_RESTART:
  9701. if (!bc->dummy) {
  9702. stop_bc_tones(ch);
  9703. release_chan(ch, bc);
  9704. }
  9705. break;
  9706. default:
  9707. chan_misdn_log(1, 0, "Got Unknown Event\n");
  9708. break;
  9709. }
  9710. if (ch) {
  9711. chan_list_unref(ch, "cb_event complete OK");
  9712. }
  9713. return RESPONSE_OK;
  9714. }
  9715. /** TE STUFF END **/
  9716. #if defined(AST_MISDN_ENHANCEMENTS)
  9717. /*!
  9718. * \internal
  9719. * \brief Get call completion record information.
  9720. *
  9721. * \param chan Asterisk channel to operate upon. (Not used)
  9722. * \param function_name Name of the function that called us.
  9723. * \param function_args Argument string passed to function (Could be NULL)
  9724. * \param buf Buffer to put returned string.
  9725. * \param size Size of the supplied buffer including the null terminator.
  9726. *
  9727. * \retval 0 on success.
  9728. * \retval -1 on error.
  9729. */
  9730. static int misdn_cc_read(struct ast_channel *chan, const char *function_name,
  9731. char *function_args, char *buf, size_t size)
  9732. {
  9733. char *parse;
  9734. struct misdn_cc_record *cc_record;
  9735. AST_DECLARE_APP_ARGS(args,
  9736. AST_APP_ARG(cc_id); /* Call completion record ID value. */
  9737. AST_APP_ARG(get_name); /* Name of what to get */
  9738. AST_APP_ARG(other); /* Any extraneous garbage arguments */
  9739. );
  9740. /* Ensure that the buffer is empty */
  9741. *buf = 0;
  9742. if (ast_strlen_zero(function_args)) {
  9743. ast_log(LOG_ERROR, "Function '%s' requires arguments.\n", function_name);
  9744. return -1;
  9745. }
  9746. parse = ast_strdupa(function_args);
  9747. AST_STANDARD_APP_ARGS(args, parse);
  9748. if (!args.argc || ast_strlen_zero(args.cc_id)) {
  9749. ast_log(LOG_ERROR, "Function '%s' missing call completion record ID.\n",
  9750. function_name);
  9751. return -1;
  9752. }
  9753. if (!isdigit(*args.cc_id)) {
  9754. ast_log(LOG_ERROR, "Function '%s' call completion record ID must be numeric.\n",
  9755. function_name);
  9756. return -1;
  9757. }
  9758. if (ast_strlen_zero(args.get_name)) {
  9759. ast_log(LOG_ERROR, "Function '%s' missing what-to-get parameter.\n",
  9760. function_name);
  9761. return -1;
  9762. }
  9763. AST_LIST_LOCK(&misdn_cc_records_db);
  9764. cc_record = misdn_cc_find_by_id(atoi(args.cc_id));
  9765. if (cc_record) {
  9766. if (!strcasecmp("a-all", args.get_name)) {
  9767. snprintf(buf, size, "\"%s\" <%s>", cc_record->redial.caller.name,
  9768. cc_record->redial.caller.number);
  9769. } else if (!strcasecmp("a-name", args.get_name)) {
  9770. ast_copy_string(buf, cc_record->redial.caller.name, size);
  9771. } else if (!strncasecmp("a-num", args.get_name, 5)) {
  9772. ast_copy_string(buf, cc_record->redial.caller.number, size);
  9773. } else if (!strcasecmp("a-ton", args.get_name)) {
  9774. snprintf(buf, size, "%d",
  9775. misdn_to_ast_plan(cc_record->redial.caller.number_plan)
  9776. | misdn_to_ast_ton(cc_record->redial.caller.number_type));
  9777. } else if (!strncasecmp("a-pres", args.get_name, 6)) {
  9778. ast_copy_string(buf, ast_named_caller_presentation(
  9779. misdn_to_ast_pres(cc_record->redial.caller.presentation)
  9780. | misdn_to_ast_screen(cc_record->redial.caller.screening)), size);
  9781. } else if (!strcasecmp("a-busy", args.get_name)) {
  9782. ast_copy_string(buf, cc_record->party_a_free ? "no" : "yes", size);
  9783. } else if (!strncasecmp("b-num", args.get_name, 5)) {
  9784. ast_copy_string(buf, cc_record->redial.dialed.number, size);
  9785. } else if (!strcasecmp("b-ton", args.get_name)) {
  9786. snprintf(buf, size, "%d",
  9787. misdn_to_ast_plan(cc_record->redial.dialed.number_plan)
  9788. | misdn_to_ast_ton(cc_record->redial.dialed.number_type));
  9789. } else if (!strcasecmp("port", args.get_name)) {
  9790. snprintf(buf, size, "%d", cc_record->port);
  9791. } else if (!strcasecmp("available-notify-priority", args.get_name)) {
  9792. snprintf(buf, size, "%d", cc_record->remote_user_free.priority);
  9793. } else if (!strcasecmp("available-notify-exten", args.get_name)) {
  9794. ast_copy_string(buf, cc_record->remote_user_free.exten, size);
  9795. } else if (!strcasecmp("available-notify-context", args.get_name)) {
  9796. ast_copy_string(buf, cc_record->remote_user_free.context, size);
  9797. } else if (!strcasecmp("busy-notify-priority", args.get_name)) {
  9798. snprintf(buf, size, "%d", cc_record->b_free.priority);
  9799. } else if (!strcasecmp("busy-notify-exten", args.get_name)) {
  9800. ast_copy_string(buf, cc_record->b_free.exten, size);
  9801. } else if (!strcasecmp("busy-notify-context", args.get_name)) {
  9802. ast_copy_string(buf, cc_record->b_free.context, size);
  9803. } else {
  9804. AST_LIST_UNLOCK(&misdn_cc_records_db);
  9805. ast_log(LOG_ERROR, "Function '%s': Unknown what-to-get '%s'.\n", function_name, args.get_name);
  9806. return -1;
  9807. }
  9808. }
  9809. AST_LIST_UNLOCK(&misdn_cc_records_db);
  9810. return 0;
  9811. }
  9812. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  9813. #if defined(AST_MISDN_ENHANCEMENTS)
  9814. static struct ast_custom_function misdn_cc_function = {
  9815. .name = "mISDN_CC",
  9816. .synopsis = "Get call completion record information.",
  9817. .syntax = "mISDN_CC(${MISDN_CC_RECORD_ID},<what-to-get>)",
  9818. .desc =
  9819. "mISDN_CC(${MISDN_CC_RECORD_ID},<what-to-get>)\n"
  9820. "The following can be retrieved:\n"
  9821. "\"a-num\", \"a-name\", \"a-all\", \"a-ton\", \"a-pres\", \"a-busy\",\n"
  9822. "\"b-num\", \"b-ton\", \"port\",\n"
  9823. " User-A is available for call completion:\n"
  9824. " \"available-notify-priority\",\n"
  9825. " \"available-notify-exten\",\n"
  9826. " \"available-notify-context\",\n"
  9827. " User-A is busy:\n"
  9828. " \"busy-notify-priority\",\n"
  9829. " \"busy-notify-exten\",\n"
  9830. " \"busy-notify-context\"\n",
  9831. .read = misdn_cc_read,
  9832. };
  9833. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  9834. /******************************************
  9835. *
  9836. * Asterisk Channel Endpoint END
  9837. *
  9838. *
  9839. *******************************************/
  9840. static int unload_module(void)
  9841. {
  9842. /* First, take us out of the channel loop */
  9843. ast_verb(0, "-- Unregistering mISDN Channel Driver --\n");
  9844. misdn_tasks_destroy();
  9845. if (!g_config_initialized) {
  9846. return 0;
  9847. }
  9848. ast_cli_unregister_multiple(chan_misdn_clis, sizeof(chan_misdn_clis) / sizeof(struct ast_cli_entry));
  9849. /* ast_unregister_application("misdn_crypt"); */
  9850. ast_unregister_application("misdn_set_opt");
  9851. ast_unregister_application("misdn_facility");
  9852. ast_unregister_application("misdn_check_l2l1");
  9853. #if defined(AST_MISDN_ENHANCEMENTS)
  9854. ast_unregister_application(misdn_command_name);
  9855. ast_custom_function_unregister(&misdn_cc_function);
  9856. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  9857. ast_channel_unregister(&misdn_tech);
  9858. free_robin_list();
  9859. misdn_cfg_destroy();
  9860. misdn_lib_destroy();
  9861. ast_free(misdn_out_calls);
  9862. ast_free(misdn_in_calls);
  9863. ast_free(misdn_debug_only);
  9864. ast_free(misdn_ports);
  9865. ast_free(misdn_debug);
  9866. #if defined(AST_MISDN_ENHANCEMENTS)
  9867. misdn_cc_destroy();
  9868. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  9869. misdn_tech.capabilities = ast_format_cap_destroy(misdn_tech.capabilities);
  9870. return 0;
  9871. }
  9872. /*!
  9873. * \brief Load the module
  9874. *
  9875. * Module loading including tests for configuration or dependencies.
  9876. * This function can return AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_DECLINE,
  9877. * or AST_MODULE_LOAD_SUCCESS. If a dependency or environment variable fails
  9878. * tests return AST_MODULE_LOAD_FAILURE. If the module can not load the
  9879. * configuration file or other non-critical problem return
  9880. * AST_MODULE_LOAD_DECLINE. On success return AST_MODULE_LOAD_SUCCESS.
  9881. */
  9882. static int load_module(void)
  9883. {
  9884. int i, port;
  9885. int ntflags = 0, ntkc = 0;
  9886. char ports[256] = "";
  9887. char tempbuf[BUFFERSIZE + 1];
  9888. char ntfile[BUFFERSIZE + 1];
  9889. struct misdn_lib_iface iface = {
  9890. .cb_event = cb_events,
  9891. .cb_log = chan_misdn_log,
  9892. .cb_jb_empty = chan_misdn_jb_empty,
  9893. };
  9894. if (!(misdn_tech.capabilities = ast_format_cap_alloc(0))) {
  9895. return AST_MODULE_LOAD_DECLINE;
  9896. }
  9897. ast_format_set(&prefformat, AST_FORMAT_ALAW, 0);
  9898. ast_format_cap_add(misdn_tech.capabilities, &prefformat);
  9899. max_ports = misdn_lib_maxports_get();
  9900. if (max_ports <= 0) {
  9901. ast_log(LOG_ERROR, "Unable to initialize mISDN\n");
  9902. return AST_MODULE_LOAD_DECLINE;
  9903. }
  9904. if (misdn_cfg_init(max_ports, 0)) {
  9905. ast_log(LOG_ERROR, "Unable to initialize misdn_config.\n");
  9906. return AST_MODULE_LOAD_DECLINE;
  9907. }
  9908. g_config_initialized = 1;
  9909. #if defined(AST_MISDN_ENHANCEMENTS)
  9910. misdn_cc_init();
  9911. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  9912. misdn_debug = ast_malloc(sizeof(int) * (max_ports + 1));
  9913. if (!misdn_debug) {
  9914. ast_log(LOG_ERROR, "Out of memory for misdn_debug\n");
  9915. return AST_MODULE_LOAD_DECLINE;
  9916. }
  9917. misdn_ports = ast_malloc(sizeof(int) * (max_ports + 1));
  9918. if (!misdn_ports) {
  9919. ast_free(misdn_debug);
  9920. ast_log(LOG_ERROR, "Out of memory for misdn_ports\n");
  9921. return AST_MODULE_LOAD_DECLINE;
  9922. }
  9923. misdn_cfg_get(0, MISDN_GEN_DEBUG, &misdn_debug[0], sizeof(misdn_debug[0]));
  9924. for (i = 1; i <= max_ports; i++) {
  9925. misdn_debug[i] = misdn_debug[0];
  9926. misdn_ports[i] = i;
  9927. }
  9928. *misdn_ports = 0;
  9929. misdn_debug_only = ast_calloc(max_ports + 1, sizeof(int));
  9930. if (!misdn_debug_only) {
  9931. ast_free(misdn_ports);
  9932. ast_free(misdn_debug);
  9933. ast_log(LOG_ERROR, "Out of memory for misdn_debug_only\n");
  9934. return AST_MODULE_LOAD_DECLINE;
  9935. }
  9936. misdn_cfg_get(0, MISDN_GEN_TRACEFILE, tempbuf, sizeof(tempbuf));
  9937. if (!ast_strlen_zero(tempbuf)) {
  9938. tracing = 1;
  9939. }
  9940. misdn_in_calls = ast_malloc(sizeof(int) * (max_ports + 1));
  9941. if (!misdn_in_calls) {
  9942. ast_free(misdn_debug_only);
  9943. ast_free(misdn_ports);
  9944. ast_free(misdn_debug);
  9945. ast_log(LOG_ERROR, "Out of memory for misdn_in_calls\n");
  9946. return AST_MODULE_LOAD_DECLINE;
  9947. }
  9948. misdn_out_calls = ast_malloc(sizeof(int) * (max_ports + 1));
  9949. if (!misdn_out_calls) {
  9950. ast_free(misdn_in_calls);
  9951. ast_free(misdn_debug_only);
  9952. ast_free(misdn_ports);
  9953. ast_free(misdn_debug);
  9954. ast_log(LOG_ERROR, "Out of memory for misdn_out_calls\n");
  9955. return AST_MODULE_LOAD_DECLINE;
  9956. }
  9957. for (i = 1; i <= max_ports; i++) {
  9958. misdn_in_calls[i] = 0;
  9959. misdn_out_calls[i] = 0;
  9960. }
  9961. ast_mutex_init(&cl_te_lock);
  9962. ast_mutex_init(&release_lock);
  9963. misdn_cfg_update_ptp();
  9964. misdn_cfg_get_ports_string(ports);
  9965. if (!ast_strlen_zero(ports)) {
  9966. chan_misdn_log(0, 0, "Got: %s from get_ports\n", ports);
  9967. }
  9968. if (misdn_lib_init(ports, &iface, NULL)) {
  9969. chan_misdn_log(0, 0, "No te ports initialized\n");
  9970. }
  9971. misdn_cfg_get(0, MISDN_GEN_NTDEBUGFLAGS, &ntflags, sizeof(ntflags));
  9972. misdn_cfg_get(0, MISDN_GEN_NTDEBUGFILE, &ntfile, sizeof(ntfile));
  9973. misdn_cfg_get(0, MISDN_GEN_NTKEEPCALLS, &ntkc, sizeof(ntkc));
  9974. misdn_lib_nt_keepcalls(ntkc);
  9975. misdn_lib_nt_debug_init(ntflags, ntfile);
  9976. if (ast_channel_register(&misdn_tech)) {
  9977. ast_log(LOG_ERROR, "Unable to register channel class %s\n", misdn_type);
  9978. unload_module();
  9979. return AST_MODULE_LOAD_DECLINE;
  9980. }
  9981. ast_cli_register_multiple(chan_misdn_clis, sizeof(chan_misdn_clis) / sizeof(struct ast_cli_entry));
  9982. ast_register_application("misdn_set_opt", misdn_set_opt_exec, "misdn_set_opt",
  9983. "misdn_set_opt(:<opt><optarg>:<opt><optarg>...):\n"
  9984. "Sets mISDN opts. and optargs\n"
  9985. "\n"
  9986. "The available options are:\n"
  9987. " a - Have Asterisk detect DTMF tones on called channel\n"
  9988. " c - Make crypted outgoing call, optarg is keyindex\n"
  9989. " d - Send display text to called phone, text is the optarg\n"
  9990. " e - Perform echo cancellation on this channel,\n"
  9991. " takes taps as optarg (32,64,128,256)\n"
  9992. " e! - Disable echo cancellation on this channel\n"
  9993. " f - Enable fax detection\n"
  9994. " h - Make digital outgoing call\n"
  9995. " h1 - Make HDLC mode digital outgoing call\n"
  9996. " i - Ignore detected DTMF tones, don't signal them to Asterisk,\n"
  9997. " they will be transported inband.\n"
  9998. " jb - Set jitter buffer length, optarg is length\n"
  9999. " jt - Set jitter buffer upper threshold, optarg is threshold\n"
  10000. " jn - Disable jitter buffer\n"
  10001. " n - Disable mISDN DSP on channel.\n"
  10002. " Disables: echo cancel, DTMF detection, and volume control.\n"
  10003. " p - Caller ID presentation,\n"
  10004. " optarg is either 'allowed' or 'restricted'\n"
  10005. " s - Send Non-inband DTMF as inband\n"
  10006. " vr - Rx gain control, optarg is gain\n"
  10007. " vt - Tx gain control, optarg is gain\n"
  10008. );
  10009. ast_register_application("misdn_facility", misdn_facility_exec, "misdn_facility",
  10010. "misdn_facility(<FACILITY_TYPE>|<ARG1>|..)\n"
  10011. "Sends the Facility Message FACILITY_TYPE with \n"
  10012. "the given Arguments to the current ISDN Channel\n"
  10013. "Supported Facilities are:\n"
  10014. "\n"
  10015. "type=calldeflect args=Nr where to deflect\n"
  10016. #if defined(AST_MISDN_ENHANCEMENTS)
  10017. "type=callrerouting args=Nr where to deflect\n"
  10018. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  10019. );
  10020. ast_register_application("misdn_check_l2l1", misdn_check_l2l1, "misdn_check_l2l1",
  10021. "misdn_check_l2l1(<port>||g:<groupname>,timeout)\n"
  10022. "Checks if the L2 and L1 are up on either the given <port> or\n"
  10023. "on the ports in the group with <groupname>\n"
  10024. "If the L1/L2 are down, check_l2l1 gets up the L1/L2 and waits\n"
  10025. "for <timeout> seconds that this happens. Otherwise, nothing happens\n"
  10026. "\n"
  10027. "This application, ensures the L1/L2 state of the Ports in a group\n"
  10028. "it is intended to make the pmp_l1_check option redundant and to\n"
  10029. "fix a buggy switch config from your provider\n"
  10030. "\n"
  10031. "a sample dialplan would look like:\n\n"
  10032. "exten => _X.,1,misdn_check_l2l1(g:out|2)\n"
  10033. "exten => _X.,n,dial(mISDN/g:out/${EXTEN})\n"
  10034. );
  10035. #if defined(AST_MISDN_ENHANCEMENTS)
  10036. ast_register_application(misdn_command_name, misdn_command_exec, misdn_command_name,
  10037. "misdn_command(<command>[,<options>])\n"
  10038. "The following commands are defined:\n"
  10039. "cc-initialize\n"
  10040. " Setup mISDN support for call completion\n"
  10041. " Must call before doing any Dial() involving call completion.\n"
  10042. "ccnr-request,${MISDN_CC_RECORD_ID},<notify-context>,<user-a-extension>,<priority>\n"
  10043. " Request Call Completion No Reply activation\n"
  10044. "ccbs-request,${MISDN_CC_RECORD_ID},<notify-context>,<user-a-extension>,<priority>\n"
  10045. " Request Call Completion Busy Subscriber activation\n"
  10046. "cc-b-free,${MISDN_CC_RECORD_ID},<notify-context>,<user-a-extension>,<priority>\n"
  10047. " Set the dialplan location to notify when User-B is available but User-A is busy.\n"
  10048. " Setting this dialplan location is optional.\n"
  10049. "cc-a-busy,${MISDN_CC_RECORD_ID},<yes/no>\n"
  10050. " Set the busy status of call completion User-A\n"
  10051. "cc-deactivate,${MISDN_CC_RECORD_ID}\n"
  10052. " Deactivate the identified call completion request\n"
  10053. "\n"
  10054. "MISDN_CC_RECORD_ID is set when Dial() returns and call completion is possible\n"
  10055. "MISDN_CC_STATUS is set to ACTIVATED or ERROR after the call completion\n"
  10056. "activation request.\n"
  10057. "MISDN_ERROR_MSG is set to a descriptive message on error.\n"
  10058. );
  10059. ast_custom_function_register(&misdn_cc_function);
  10060. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  10061. misdn_cfg_get(0, MISDN_GEN_TRACEFILE, global_tracefile, sizeof(global_tracefile));
  10062. /* start the l1 watchers */
  10063. for (port = misdn_cfg_get_next_port(0); port >= 0; port = misdn_cfg_get_next_port(port)) {
  10064. int l1timeout;
  10065. misdn_cfg_get(port, MISDN_CFG_L1_TIMEOUT, &l1timeout, sizeof(l1timeout));
  10066. if (l1timeout) {
  10067. chan_misdn_log(4, 0, "Adding L1watcher task: port:%d timeout:%ds\n", port, l1timeout);
  10068. misdn_tasks_add(l1timeout * 1000, misdn_l1_task, &misdn_ports[port]);
  10069. }
  10070. }
  10071. chan_misdn_log(0, 0, "-- mISDN Channel Driver Registered --\n");
  10072. return 0;
  10073. }
  10074. static int reload(void)
  10075. {
  10076. reload_config();
  10077. return 0;
  10078. }
  10079. /*** SOME APPS ;)***/
  10080. #if defined(AST_MISDN_ENHANCEMENTS)
  10081. /*!
  10082. * \brief misdn_command arguments container.
  10083. */
  10084. AST_DEFINE_APP_ARGS_TYPE(misdn_command_args,
  10085. AST_APP_ARG(name); /* Subcommand name */
  10086. AST_APP_ARG(arg)[10 + 1]; /* Subcommand arguments */
  10087. );
  10088. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  10089. #if defined(AST_MISDN_ENHANCEMENTS)
  10090. static void misdn_cc_caller_destroy(void *obj)
  10091. {
  10092. /* oh snap! */
  10093. }
  10094. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  10095. #if defined(AST_MISDN_ENHANCEMENTS)
  10096. static struct misdn_cc_caller *misdn_cc_caller_alloc(struct ast_channel *chan)
  10097. {
  10098. struct misdn_cc_caller *cc_caller;
  10099. if (!(cc_caller = ao2_alloc(sizeof(*cc_caller), misdn_cc_caller_destroy))) {
  10100. return NULL;
  10101. }
  10102. cc_caller->chan = chan;
  10103. return cc_caller;
  10104. }
  10105. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  10106. #if defined(AST_MISDN_ENHANCEMENTS)
  10107. /*!
  10108. * \internal
  10109. * \brief misdn_command(cc-initialize) subcommand handler
  10110. *
  10111. * \param chan Asterisk channel to operate upon.
  10112. * \param subcommand Arguments for the subcommand
  10113. *
  10114. * \retval 0 on success.
  10115. * \retval -1 on error.
  10116. */
  10117. static int misdn_command_cc_initialize(struct ast_channel *chan, struct misdn_command_args *subcommand)
  10118. {
  10119. struct misdn_cc_caller *cc_caller;
  10120. struct ast_datastore *datastore;
  10121. if (!(cc_caller = misdn_cc_caller_alloc(chan))) {
  10122. return -1;
  10123. }
  10124. if (!(datastore = ast_datastore_alloc(&misdn_cc_ds_info, NULL))) {
  10125. ao2_ref(cc_caller, -1);
  10126. return -1;
  10127. }
  10128. ast_channel_lock(chan);
  10129. /* Inherit reference */
  10130. datastore->data = cc_caller;
  10131. cc_caller = NULL;
  10132. datastore->inheritance = DATASTORE_INHERIT_FOREVER;
  10133. ast_channel_datastore_add(chan, datastore);
  10134. ast_channel_unlock(chan);
  10135. return 0;
  10136. }
  10137. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  10138. #if defined(AST_MISDN_ENHANCEMENTS)
  10139. /*!
  10140. * \internal
  10141. * \brief misdn_command(cc-deactivate) subcommand handler
  10142. *
  10143. * \details
  10144. * misdn_command(cc-deactivate,${MISDN_CC_RECORD_ID})
  10145. * Deactivate a call completion service instance.
  10146. *
  10147. * \param chan Asterisk channel to operate upon.
  10148. * \param subcommand Arguments for the subcommand
  10149. *
  10150. * \retval 0 on success.
  10151. * \retval -1 on error.
  10152. */
  10153. static int misdn_command_cc_deactivate(struct ast_channel *chan, struct misdn_command_args *subcommand)
  10154. {
  10155. long record_id;
  10156. const char *error_str;
  10157. struct misdn_cc_record *cc_record;
  10158. struct misdn_bchannel *bc;
  10159. struct misdn_bchannel dummy;
  10160. static const char cmd_help[] = "%s(%s,${MISDN_CC_RECORD_ID})\n";
  10161. if (ast_strlen_zero(subcommand->arg[0]) || !isdigit(*subcommand->arg[0])) {
  10162. ast_log(LOG_WARNING, cmd_help, misdn_command_name, subcommand->name);
  10163. return -1;
  10164. }
  10165. record_id = atol(subcommand->arg[0]);
  10166. AST_LIST_LOCK(&misdn_cc_records_db);
  10167. cc_record = misdn_cc_find_by_id(record_id);
  10168. if (cc_record && 0 <= cc_record->port) {
  10169. if (cc_record->ptp) {
  10170. if (cc_record->mode.ptp.bc) {
  10171. /* Close the call-completion signaling link */
  10172. bc = cc_record->mode.ptp.bc;
  10173. bc->fac_out.Function = Fac_None;
  10174. bc->out_cause = AST_CAUSE_NORMAL_CLEARING;
  10175. misdn_lib_send_event(bc, EVENT_RELEASE_COMPLETE);
  10176. }
  10177. misdn_cc_delete(cc_record);
  10178. } else if (cc_record->activated) {
  10179. cc_record->error_code = FacError_None;
  10180. cc_record->reject_code = FacReject_None;
  10181. cc_record->invoke_id = ++misdn_invoke_id;
  10182. cc_record->outstanding_message = 1;
  10183. /* Build message */
  10184. misdn_make_dummy(&dummy, cc_record->port, 0, misdn_lib_port_is_nt(cc_record->port), 0);
  10185. dummy.fac_out.Function = Fac_CCBSDeactivate;
  10186. dummy.fac_out.u.CCBSDeactivate.InvokeID = cc_record->invoke_id;
  10187. dummy.fac_out.u.CCBSDeactivate.ComponentType = FacComponent_Invoke;
  10188. dummy.fac_out.u.CCBSDeactivate.Component.Invoke.CCBSReference = cc_record->mode.ptmp.reference_id;
  10189. /* Send message */
  10190. print_facility(&dummy.fac_out, &dummy);
  10191. misdn_lib_send_event(&dummy, EVENT_FACILITY);
  10192. }
  10193. }
  10194. AST_LIST_UNLOCK(&misdn_cc_records_db);
  10195. /* Wait for the response to the call completion deactivation request. */
  10196. misdn_cc_response_wait(chan, MISDN_CC_REQUEST_WAIT_MAX, record_id);
  10197. AST_LIST_LOCK(&misdn_cc_records_db);
  10198. cc_record = misdn_cc_find_by_id(record_id);
  10199. if (cc_record) {
  10200. if (cc_record->port < 0) {
  10201. /* The network did not tell us that call completion was available. */
  10202. error_str = NULL;
  10203. } else if (cc_record->outstanding_message) {
  10204. cc_record->outstanding_message = 0;
  10205. error_str = misdn_no_response_from_network;
  10206. } else if (cc_record->reject_code != FacReject_None) {
  10207. error_str = misdn_to_str_reject_code(cc_record->reject_code);
  10208. } else if (cc_record->error_code != FacError_None) {
  10209. error_str = misdn_to_str_error_code(cc_record->error_code);
  10210. } else {
  10211. error_str = NULL;
  10212. }
  10213. misdn_cc_delete(cc_record);
  10214. } else {
  10215. error_str = NULL;
  10216. }
  10217. AST_LIST_UNLOCK(&misdn_cc_records_db);
  10218. if (error_str) {
  10219. ast_verb(1, "%s(%s) diagnostic '%s' on channel %s\n",
  10220. misdn_command_name, subcommand->name, error_str, ast_channel_name(chan));
  10221. pbx_builtin_setvar_helper(chan, MISDN_ERROR_MSG, error_str);
  10222. }
  10223. return 0;
  10224. }
  10225. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  10226. #if defined(AST_MISDN_ENHANCEMENTS)
  10227. /*!
  10228. * \internal
  10229. * \brief misdn_command(cc-a-busy) subcommand handler
  10230. *
  10231. * \details
  10232. * misdn_command(cc-a-busy,${MISDN_CC_RECORD_ID},<yes/no>)
  10233. * Set the status of User-A for a call completion service instance.
  10234. *
  10235. * \param chan Asterisk channel to operate upon.
  10236. * \param subcommand Arguments for the subcommand
  10237. *
  10238. * \retval 0 on success.
  10239. * \retval -1 on error.
  10240. */
  10241. static int misdn_command_cc_a_busy(struct ast_channel *chan, struct misdn_command_args *subcommand)
  10242. {
  10243. long record_id;
  10244. int party_a_free;
  10245. struct misdn_cc_record *cc_record;
  10246. struct misdn_bchannel *bc;
  10247. static const char cmd_help[] = "%s(%s,${MISDN_CC_RECORD_ID},<yes/no>)\n";
  10248. if (ast_strlen_zero(subcommand->arg[0]) || !isdigit(*subcommand->arg[0])) {
  10249. ast_log(LOG_WARNING, cmd_help, misdn_command_name, subcommand->name);
  10250. return -1;
  10251. }
  10252. record_id = atol(subcommand->arg[0]);
  10253. if (ast_true(subcommand->arg[1])) {
  10254. party_a_free = 0;
  10255. } else if (ast_false(subcommand->arg[1])) {
  10256. party_a_free = 1;
  10257. } else {
  10258. ast_log(LOG_WARNING, cmd_help, misdn_command_name, subcommand->name);
  10259. return -1;
  10260. }
  10261. AST_LIST_LOCK(&misdn_cc_records_db);
  10262. cc_record = misdn_cc_find_by_id(record_id);
  10263. if (cc_record && cc_record->party_a_free != party_a_free) {
  10264. /* User-A's status has changed */
  10265. cc_record->party_a_free = party_a_free;
  10266. if (cc_record->ptp && cc_record->mode.ptp.bc) {
  10267. cc_record->error_code = FacError_None;
  10268. cc_record->reject_code = FacReject_None;
  10269. /* Build message */
  10270. bc = cc_record->mode.ptp.bc;
  10271. if (cc_record->party_a_free) {
  10272. bc->fac_out.Function = Fac_CCBS_T_Resume;
  10273. bc->fac_out.u.CCBS_T_Resume.InvokeID = ++misdn_invoke_id;
  10274. } else {
  10275. bc->fac_out.Function = Fac_CCBS_T_Suspend;
  10276. bc->fac_out.u.CCBS_T_Suspend.InvokeID = ++misdn_invoke_id;
  10277. }
  10278. /* Send message */
  10279. print_facility(&bc->fac_out, bc);
  10280. misdn_lib_send_event(bc, EVENT_FACILITY);
  10281. }
  10282. }
  10283. AST_LIST_UNLOCK(&misdn_cc_records_db);
  10284. return 0;
  10285. }
  10286. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  10287. #if defined(AST_MISDN_ENHANCEMENTS)
  10288. /*!
  10289. * \internal
  10290. * \brief misdn_command(cc-b-free) subcommand handler
  10291. *
  10292. * \details
  10293. * misdn_command(cc-b-free,${MISDN_CC_RECORD_ID},<notify-context>,<user-a-extension>,<priority>)
  10294. * Set the dialplan location to notify when User-B is free and User-A is busy.
  10295. *
  10296. * \param chan Asterisk channel to operate upon.
  10297. * \param subcommand Arguments for the subcommand
  10298. *
  10299. * \retval 0 on success.
  10300. * \retval -1 on error.
  10301. */
  10302. static int misdn_command_cc_b_free(struct ast_channel *chan, struct misdn_command_args *subcommand)
  10303. {
  10304. unsigned index;
  10305. long record_id;
  10306. int priority;
  10307. char *context;
  10308. char *exten;
  10309. struct misdn_cc_record *cc_record;
  10310. static const char cmd_help[] = "%s(%s,${MISDN_CC_RECORD_ID},<notify-context>,<user-a-extension>,<priority>)\n";
  10311. /* Check that all arguments are present */
  10312. for (index = 0; index < 4; ++index) {
  10313. if (ast_strlen_zero(subcommand->arg[index])) {
  10314. ast_log(LOG_WARNING, cmd_help, misdn_command_name, subcommand->name);
  10315. return -1;
  10316. }
  10317. }
  10318. /* These must be numeric */
  10319. if (!isdigit(*subcommand->arg[0]) || !isdigit(*subcommand->arg[3])) {
  10320. ast_log(LOG_WARNING, cmd_help, misdn_command_name, subcommand->name);
  10321. return -1;
  10322. }
  10323. record_id = atol(subcommand->arg[0]);
  10324. context = subcommand->arg[1];
  10325. exten = subcommand->arg[2];
  10326. priority = atoi(subcommand->arg[3]);
  10327. AST_LIST_LOCK(&misdn_cc_records_db);
  10328. cc_record = misdn_cc_find_by_id(record_id);
  10329. if (cc_record) {
  10330. /* Save User-B free information */
  10331. ast_copy_string(cc_record->b_free.context, context, sizeof(cc_record->b_free.context));
  10332. ast_copy_string(cc_record->b_free.exten, exten, sizeof(cc_record->b_free.exten));
  10333. cc_record->b_free.priority = priority;
  10334. }
  10335. AST_LIST_UNLOCK(&misdn_cc_records_db);
  10336. return 0;
  10337. }
  10338. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  10339. #if defined(AST_MISDN_ENHANCEMENTS)
  10340. struct misdn_cc_request {
  10341. enum FacFunction ptmp;
  10342. enum FacFunction ptp;
  10343. };
  10344. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  10345. #if defined(AST_MISDN_ENHANCEMENTS)
  10346. /*!
  10347. * \internal
  10348. * \brief misdn_command(ccbs-request/ccnr-request) subcommand handler helper
  10349. *
  10350. * \details
  10351. * misdn_command(ccbs-request,${MISDN_CC_RECORD_ID},<notify-context>,<user-a-extension>,<priority>)
  10352. * misdn_command(ccnr-request,${MISDN_CC_RECORD_ID},<notify-context>,<user-a-extension>,<priority>)
  10353. * Set the dialplan location to notify when User-B is free and User-A is free.
  10354. *
  10355. * \param chan Asterisk channel to operate upon.
  10356. * \param subcommand Arguments for the subcommand
  10357. * \param request Which call-completion request message to generate.
  10358. *
  10359. * \retval 0 on success.
  10360. * \retval -1 on error.
  10361. */
  10362. static int misdn_command_cc_request(struct ast_channel *chan, struct misdn_command_args *subcommand, const struct misdn_cc_request *request)
  10363. {
  10364. unsigned index;
  10365. int request_retention;
  10366. long record_id;
  10367. int priority;
  10368. char *context;
  10369. char *exten;
  10370. const char *error_str;
  10371. struct misdn_cc_record *cc_record;
  10372. struct misdn_bchannel *bc;
  10373. struct misdn_bchannel dummy;
  10374. struct misdn_party_id id;
  10375. static const char cmd_help[] = "%s(%s,${MISDN_CC_RECORD_ID},<notify-context>,<user-a-extension>,<priority>)\n";
  10376. /* Check that all arguments are present */
  10377. for (index = 0; index < 4; ++index) {
  10378. if (ast_strlen_zero(subcommand->arg[index])) {
  10379. ast_log(LOG_WARNING, cmd_help, misdn_command_name, subcommand->name);
  10380. return -1;
  10381. }
  10382. }
  10383. /* These must be numeric */
  10384. if (!isdigit(*subcommand->arg[0]) || !isdigit(*subcommand->arg[3])) {
  10385. ast_log(LOG_WARNING, cmd_help, misdn_command_name, subcommand->name);
  10386. return -1;
  10387. }
  10388. record_id = atol(subcommand->arg[0]);
  10389. context = subcommand->arg[1];
  10390. exten = subcommand->arg[2];
  10391. priority = atoi(subcommand->arg[3]);
  10392. AST_LIST_LOCK(&misdn_cc_records_db);
  10393. cc_record = misdn_cc_find_by_id(record_id);
  10394. if (cc_record) {
  10395. /* Save User-B free information */
  10396. ast_copy_string(cc_record->remote_user_free.context, context,
  10397. sizeof(cc_record->remote_user_free.context));
  10398. ast_copy_string(cc_record->remote_user_free.exten, exten,
  10399. sizeof(cc_record->remote_user_free.exten));
  10400. cc_record->remote_user_free.priority = priority;
  10401. if (0 <= cc_record->port) {
  10402. if (cc_record->ptp) {
  10403. if (!cc_record->mode.ptp.bc) {
  10404. bc = misdn_lib_get_register_bc(cc_record->port);
  10405. if (bc) {
  10406. cc_record->mode.ptp.bc = bc;
  10407. cc_record->error_code = FacError_None;
  10408. cc_record->reject_code = FacReject_None;
  10409. cc_record->invoke_id = ++misdn_invoke_id;
  10410. cc_record->outstanding_message = 1;
  10411. cc_record->activation_requested = 1;
  10412. misdn_cfg_get(bc->port, MISDN_CFG_CC_REQUEST_RETENTION,
  10413. &request_retention, sizeof(request_retention));
  10414. cc_record->mode.ptp.requested_retention = request_retention ? 1 : 0;
  10415. /* Build message */
  10416. bc->fac_out.Function = request->ptp;
  10417. bc->fac_out.u.CCBS_T_Request.InvokeID = cc_record->invoke_id;
  10418. bc->fac_out.u.CCBS_T_Request.ComponentType = FacComponent_Invoke;
  10419. bc->fac_out.u.CCBS_T_Request.Component.Invoke.Q931ie =
  10420. cc_record->redial.setup_bc_hlc_llc;
  10421. memset(&id, 0, sizeof(id));
  10422. id.number_plan = cc_record->redial.dialed.number_plan;
  10423. id.number_type = cc_record->redial.dialed.number_type;
  10424. ast_copy_string(id.number, cc_record->redial.dialed.number,
  10425. sizeof(id.number));
  10426. misdn_Address_fill(
  10427. &bc->fac_out.u.CCBS_T_Request.Component.Invoke.Destination,
  10428. &id);
  10429. misdn_Address_fill(
  10430. &bc->fac_out.u.CCBS_T_Request.Component.Invoke.Originating,
  10431. &cc_record->redial.caller);
  10432. bc->fac_out.u.CCBS_T_Request.Component.Invoke.PresentationAllowedIndicatorPresent = 1;
  10433. bc->fac_out.u.CCBS_T_Request.Component.Invoke.PresentationAllowedIndicator =
  10434. (cc_record->redial.caller.presentation != 0) ? 0 : 1;
  10435. bc->fac_out.u.CCBS_T_Request.Component.Invoke.RetentionSupported =
  10436. request_retention ? 1 : 0;
  10437. /* Send message */
  10438. print_facility(&bc->fac_out, bc);
  10439. misdn_lib_send_event(bc, EVENT_REGISTER);
  10440. }
  10441. }
  10442. } else {
  10443. cc_record->error_code = FacError_None;
  10444. cc_record->reject_code = FacReject_None;
  10445. cc_record->invoke_id = ++misdn_invoke_id;
  10446. cc_record->outstanding_message = 1;
  10447. cc_record->activation_requested = 1;
  10448. /* Build message */
  10449. misdn_make_dummy(&dummy, cc_record->port, 0,
  10450. misdn_lib_port_is_nt(cc_record->port), 0);
  10451. dummy.fac_out.Function = request->ptmp;
  10452. dummy.fac_out.u.CCBSRequest.InvokeID = cc_record->invoke_id;
  10453. dummy.fac_out.u.CCBSRequest.ComponentType = FacComponent_Invoke;
  10454. dummy.fac_out.u.CCBSRequest.Component.Invoke.CallLinkageID =
  10455. cc_record->mode.ptmp.linkage_id;
  10456. /* Send message */
  10457. print_facility(&dummy.fac_out, &dummy);
  10458. misdn_lib_send_event(&dummy, EVENT_FACILITY);
  10459. }
  10460. }
  10461. }
  10462. AST_LIST_UNLOCK(&misdn_cc_records_db);
  10463. /* Wait for the response to the call completion request. */
  10464. misdn_cc_response_wait(chan, MISDN_CC_REQUEST_WAIT_MAX, record_id);
  10465. AST_LIST_LOCK(&misdn_cc_records_db);
  10466. cc_record = misdn_cc_find_by_id(record_id);
  10467. if (cc_record) {
  10468. if (!cc_record->activated) {
  10469. if (cc_record->port < 0) {
  10470. /* The network did not tell us that call completion was available. */
  10471. error_str = "No port number";
  10472. } else if (cc_record->outstanding_message) {
  10473. cc_record->outstanding_message = 0;
  10474. error_str = misdn_no_response_from_network;
  10475. } else if (cc_record->reject_code != FacReject_None) {
  10476. error_str = misdn_to_str_reject_code(cc_record->reject_code);
  10477. } else if (cc_record->error_code != FacError_None) {
  10478. error_str = misdn_to_str_error_code(cc_record->error_code);
  10479. } else if (cc_record->ptp) {
  10480. if (cc_record->mode.ptp.bc) {
  10481. error_str = "Call-completion already requested";
  10482. } else {
  10483. error_str = "Could not allocate call-completion signaling link";
  10484. }
  10485. } else {
  10486. /* Should never happen. */
  10487. error_str = "Unexpected error";
  10488. }
  10489. /* No need to keep the call completion record. */
  10490. if (cc_record->ptp && cc_record->mode.ptp.bc) {
  10491. /* Close the call-completion signaling link */
  10492. bc = cc_record->mode.ptp.bc;
  10493. bc->fac_out.Function = Fac_None;
  10494. bc->out_cause = AST_CAUSE_NORMAL_CLEARING;
  10495. misdn_lib_send_event(bc, EVENT_RELEASE_COMPLETE);
  10496. }
  10497. misdn_cc_delete(cc_record);
  10498. } else {
  10499. error_str = NULL;
  10500. }
  10501. } else {
  10502. error_str = misdn_cc_record_not_found;
  10503. }
  10504. AST_LIST_UNLOCK(&misdn_cc_records_db);
  10505. if (error_str) {
  10506. ast_verb(1, "%s(%s) diagnostic '%s' on channel %s\n",
  10507. misdn_command_name, subcommand->name, error_str, ast_channel_name(chan));
  10508. pbx_builtin_setvar_helper(chan, MISDN_ERROR_MSG, error_str);
  10509. pbx_builtin_setvar_helper(chan, MISDN_CC_STATUS, "ERROR");
  10510. } else {
  10511. pbx_builtin_setvar_helper(chan, MISDN_CC_STATUS, "ACTIVATED");
  10512. }
  10513. return 0;
  10514. }
  10515. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  10516. #if defined(AST_MISDN_ENHANCEMENTS)
  10517. /*!
  10518. * \internal
  10519. * \brief misdn_command(ccbs-request) subcommand handler
  10520. *
  10521. * \details
  10522. * misdn_command(ccbs-request,${MISDN_CC_RECORD_ID},<notify-context>,<user-a-extension>,<priority>)
  10523. * Set the dialplan location to notify when User-B is free and User-A is free.
  10524. *
  10525. * \param chan Asterisk channel to operate upon.
  10526. * \param subcommand Arguments for the subcommand
  10527. *
  10528. * \retval 0 on success.
  10529. * \retval -1 on error.
  10530. */
  10531. static int misdn_command_ccbs_request(struct ast_channel *chan, struct misdn_command_args *subcommand)
  10532. {
  10533. static const struct misdn_cc_request request = {
  10534. .ptmp = Fac_CCBSRequest,
  10535. .ptp = Fac_CCBS_T_Request
  10536. };
  10537. return misdn_command_cc_request(chan, subcommand, &request);
  10538. }
  10539. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  10540. #if defined(AST_MISDN_ENHANCEMENTS)
  10541. /*!
  10542. * \internal
  10543. * \brief misdn_command(ccnr-request) subcommand handler
  10544. *
  10545. * \details
  10546. * misdn_command(ccnr-request,${MISDN_CC_RECORD_ID},<notify-context>,<user-a-extension>,<priority>)
  10547. * Set the dialplan location to notify when User-B is free and User-A is free.
  10548. *
  10549. * \param chan Asterisk channel to operate upon.
  10550. * \param subcommand Arguments for the subcommand
  10551. *
  10552. * \retval 0 on success.
  10553. * \retval -1 on error.
  10554. */
  10555. static int misdn_command_ccnr_request(struct ast_channel *chan, struct misdn_command_args *subcommand)
  10556. {
  10557. static const struct misdn_cc_request request = {
  10558. .ptmp = Fac_CCNRRequest,
  10559. .ptp = Fac_CCNR_T_Request
  10560. };
  10561. return misdn_command_cc_request(chan, subcommand, &request);
  10562. }
  10563. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  10564. #if defined(AST_MISDN_ENHANCEMENTS)
  10565. struct misdn_command_table {
  10566. /*! \brief subcommand name */
  10567. const char *name;
  10568. /*! \brief subcommand handler */
  10569. int (*func)(struct ast_channel *chan, struct misdn_command_args *subcommand);
  10570. /*! \brief TRUE if the subcommand can only be executed on mISDN channels */
  10571. int misdn_only;
  10572. };
  10573. static const struct misdn_command_table misdn_commands[] = {
  10574. /* *INDENT-OFF* */
  10575. /* subcommand-name subcommand-handler mISDN only */
  10576. { "cc-initialize", misdn_command_cc_initialize, 0 },
  10577. { "cc-deactivate", misdn_command_cc_deactivate, 0 },
  10578. { "cc-a-busy", misdn_command_cc_a_busy, 0 },
  10579. { "cc-b-free", misdn_command_cc_b_free, 0 },
  10580. { "ccbs-request", misdn_command_ccbs_request, 0 },
  10581. { "ccnr-request", misdn_command_ccnr_request, 0 },
  10582. /* *INDENT-ON* */
  10583. };
  10584. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  10585. #if defined(AST_MISDN_ENHANCEMENTS)
  10586. /*!
  10587. * \internal
  10588. * \brief misdn_command() dialplan application.
  10589. *
  10590. * \param chan Asterisk channel to operate upon.
  10591. * \param data Application options string.
  10592. *
  10593. * \retval 0 on success.
  10594. * \retval -1 on error.
  10595. */
  10596. static int misdn_command_exec(struct ast_channel *chan, const char *data)
  10597. {
  10598. char *parse;
  10599. unsigned index;
  10600. struct misdn_command_args subcommand;
  10601. if (ast_strlen_zero((char *) data)) {
  10602. ast_log(LOG_ERROR, "%s requires arguments\n", misdn_command_name);
  10603. return -1;
  10604. }
  10605. ast_debug(1, "%s(%s)\n", misdn_command_name, (char *) data);
  10606. parse = ast_strdupa(data);
  10607. AST_STANDARD_APP_ARGS(subcommand, parse);
  10608. if (!subcommand.argc || ast_strlen_zero(subcommand.name)) {
  10609. ast_log(LOG_ERROR, "%s requires a subcommand\n", misdn_command_name);
  10610. return -1;
  10611. }
  10612. for (index = 0; index < ARRAY_LEN(misdn_commands); ++index) {
  10613. if (strcasecmp(misdn_commands[index].name, subcommand.name) == 0) {
  10614. strcpy(subcommand.name, misdn_commands[index].name);
  10615. if (misdn_commands[index].misdn_only
  10616. && strcasecmp(ast_channel_tech(chan)->type, misdn_type) != 0) {
  10617. ast_log(LOG_WARNING,
  10618. "%s(%s) only makes sense with %s channels!\n",
  10619. misdn_command_name, subcommand.name, misdn_type);
  10620. return -1;
  10621. }
  10622. return misdn_commands[index].func(chan, &subcommand);
  10623. }
  10624. }
  10625. ast_log(LOG_WARNING, "%s(%s) subcommand is unknown\n", misdn_command_name,
  10626. subcommand.name);
  10627. return -1;
  10628. }
  10629. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  10630. static int misdn_facility_exec(struct ast_channel *chan, const char *data)
  10631. {
  10632. struct chan_list *ch = MISDN_ASTERISK_TECH_PVT(chan);
  10633. char *parse;
  10634. unsigned max_len;
  10635. AST_DECLARE_APP_ARGS(args,
  10636. AST_APP_ARG(facility_type);
  10637. AST_APP_ARG(arg)[99];
  10638. );
  10639. chan_misdn_log(0, 0, "TYPE: %s\n", ast_channel_tech(chan)->type);
  10640. if (strcasecmp(ast_channel_tech(chan)->type, misdn_type)) {
  10641. ast_log(LOG_WARNING, "misdn_facility only makes sense with %s channels!\n", misdn_type);
  10642. return -1;
  10643. }
  10644. if (ast_strlen_zero((char *) data)) {
  10645. ast_log(LOG_WARNING, "misdn_facility requires arguments: facility_type[,<args>]\n");
  10646. return -1;
  10647. }
  10648. parse = ast_strdupa(data);
  10649. AST_STANDARD_APP_ARGS(args, parse);
  10650. if (ast_strlen_zero(args.facility_type)) {
  10651. ast_log(LOG_WARNING, "misdn_facility requires arguments: facility_type[,<args>]\n");
  10652. return -1;
  10653. }
  10654. if (!strcasecmp(args.facility_type, "calldeflect")) {
  10655. if (ast_strlen_zero(args.arg[0])) {
  10656. ast_log(LOG_WARNING, "Facility: Call Deflection requires an argument: Number\n");
  10657. }
  10658. #if defined(AST_MISDN_ENHANCEMENTS)
  10659. max_len = sizeof(ch->bc->fac_out.u.CallDeflection.Component.Invoke.Deflection.Party.Number) - 1;
  10660. if (max_len < strlen(args.arg[0])) {
  10661. ast_log(LOG_WARNING,
  10662. "Facility: Number argument too long (up to %u digits are allowed). Ignoring.\n",
  10663. max_len);
  10664. return 0;
  10665. }
  10666. ch->bc->fac_out.Function = Fac_CallDeflection;
  10667. ch->bc->fac_out.u.CallDeflection.InvokeID = ++misdn_invoke_id;
  10668. ch->bc->fac_out.u.CallDeflection.ComponentType = FacComponent_Invoke;
  10669. ch->bc->fac_out.u.CallDeflection.Component.Invoke.PresentationAllowedToDivertedToUserPresent = 1;
  10670. ch->bc->fac_out.u.CallDeflection.Component.Invoke.PresentationAllowedToDivertedToUser = 0;
  10671. ch->bc->fac_out.u.CallDeflection.Component.Invoke.Deflection.Party.Type = 0;/* unknown */
  10672. ch->bc->fac_out.u.CallDeflection.Component.Invoke.Deflection.Party.LengthOfNumber = strlen(args.arg[0]);
  10673. strcpy((char *) ch->bc->fac_out.u.CallDeflection.Component.Invoke.Deflection.Party.Number, args.arg[0]);
  10674. ch->bc->fac_out.u.CallDeflection.Component.Invoke.Deflection.Subaddress.Length = 0;
  10675. #else /* !defined(AST_MISDN_ENHANCEMENTS) */
  10676. max_len = sizeof(ch->bc->fac_out.u.CDeflection.DeflectedToNumber) - 1;
  10677. if (max_len < strlen(args.arg[0])) {
  10678. ast_log(LOG_WARNING,
  10679. "Facility: Number argument too long (up to %u digits are allowed). Ignoring.\n",
  10680. max_len);
  10681. return 0;
  10682. }
  10683. ch->bc->fac_out.Function = Fac_CD;
  10684. ch->bc->fac_out.u.CDeflection.PresentationAllowed = 0;
  10685. //ch->bc->fac_out.u.CDeflection.DeflectedToSubaddress[0] = 0;
  10686. strcpy((char *) ch->bc->fac_out.u.CDeflection.DeflectedToNumber, args.arg[0]);
  10687. #endif /* !defined(AST_MISDN_ENHANCEMENTS) */
  10688. /* Send message */
  10689. print_facility(&ch->bc->fac_out, ch->bc);
  10690. misdn_lib_send_event(ch->bc, EVENT_FACILITY);
  10691. #if defined(AST_MISDN_ENHANCEMENTS)
  10692. } else if (!strcasecmp(args.facility_type, "callrerouteing")
  10693. || !strcasecmp(args.facility_type, "callrerouting")) {
  10694. if (ast_strlen_zero(args.arg[0])) {
  10695. ast_log(LOG_WARNING, "Facility: Call rerouting requires an argument: Number\n");
  10696. }
  10697. max_len = sizeof(ch->bc->fac_out.u.CallRerouteing.Component.Invoke.CalledAddress.Party.Number) - 1;
  10698. if (max_len < strlen(args.arg[0])) {
  10699. ast_log(LOG_WARNING,
  10700. "Facility: Number argument too long (up to %u digits are allowed). Ignoring.\n",
  10701. max_len);
  10702. return 0;
  10703. }
  10704. ch->bc->fac_out.Function = Fac_CallRerouteing;
  10705. ch->bc->fac_out.u.CallRerouteing.InvokeID = ++misdn_invoke_id;
  10706. ch->bc->fac_out.u.CallRerouteing.ComponentType = FacComponent_Invoke;
  10707. ch->bc->fac_out.u.CallRerouteing.Component.Invoke.ReroutingReason = 0;/* unknown */
  10708. ch->bc->fac_out.u.CallRerouteing.Component.Invoke.ReroutingCounter = 1;
  10709. ch->bc->fac_out.u.CallRerouteing.Component.Invoke.CalledAddress.Party.Type = 0;/* unknown */
  10710. ch->bc->fac_out.u.CallRerouteing.Component.Invoke.CalledAddress.Party.LengthOfNumber = strlen(args.arg[0]);
  10711. strcpy((char *) ch->bc->fac_out.u.CallRerouteing.Component.Invoke.CalledAddress.Party.Number, args.arg[0]);
  10712. ch->bc->fac_out.u.CallRerouteing.Component.Invoke.CalledAddress.Subaddress.Length = 0;
  10713. ch->bc->fac_out.u.CallRerouteing.Component.Invoke.CallingPartySubaddress.Length = 0;
  10714. /* 0x90 0x90 0xa3 3.1 kHz audio, circuit mode, 64kbit/sec, level1/a-Law */
  10715. ch->bc->fac_out.u.CallRerouteing.Component.Invoke.Q931ie.Bc.Length = 3;
  10716. ch->bc->fac_out.u.CallRerouteing.Component.Invoke.Q931ie.Bc.Contents[0] = 0x90;
  10717. ch->bc->fac_out.u.CallRerouteing.Component.Invoke.Q931ie.Bc.Contents[1] = 0x90;
  10718. ch->bc->fac_out.u.CallRerouteing.Component.Invoke.Q931ie.Bc.Contents[2] = 0xa3;
  10719. ch->bc->fac_out.u.CallRerouteing.Component.Invoke.Q931ie.Hlc.Length = 0;
  10720. ch->bc->fac_out.u.CallRerouteing.Component.Invoke.Q931ie.Llc.Length = 0;
  10721. ch->bc->fac_out.u.CallRerouteing.Component.Invoke.Q931ie.UserInfo.Length = 0;
  10722. ch->bc->fac_out.u.CallRerouteing.Component.Invoke.LastRerouting.Type = 1;/* presentationRestricted */
  10723. ch->bc->fac_out.u.CallRerouteing.Component.Invoke.SubscriptionOption = 0;/* no notification to caller */
  10724. /* Send message */
  10725. print_facility(&ch->bc->fac_out, ch->bc);
  10726. misdn_lib_send_event(ch->bc, EVENT_FACILITY);
  10727. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  10728. } else {
  10729. chan_misdn_log(1, ch->bc->port, "Unknown Facility: %s\n", args.facility_type);
  10730. }
  10731. return 0;
  10732. }
  10733. static int misdn_check_l2l1(struct ast_channel *chan, const char *data)
  10734. {
  10735. char *parse;
  10736. char group[BUFFERSIZE + 1];
  10737. char *port_str;
  10738. int port = 0;
  10739. int timeout;
  10740. int dowait = 0;
  10741. int port_up;
  10742. AST_DECLARE_APP_ARGS(args,
  10743. AST_APP_ARG(grouppar);
  10744. AST_APP_ARG(timeout);
  10745. );
  10746. if (ast_strlen_zero((char *) data)) {
  10747. ast_log(LOG_WARNING, "misdn_check_l2l1 Requires arguments\n");
  10748. return -1;
  10749. }
  10750. parse = ast_strdupa(data);
  10751. AST_STANDARD_APP_ARGS(args, parse);
  10752. if (args.argc != 2) {
  10753. ast_log(LOG_WARNING, "Wrong argument count\n");
  10754. return 0;
  10755. }
  10756. /*ast_log(LOG_NOTICE, "Arguments: group/port '%s' timeout '%s'\n", args.grouppar, args.timeout);*/
  10757. timeout = atoi(args.timeout);
  10758. port_str = args.grouppar;
  10759. if (port_str[0] == 'g' && port_str[1] == ':') {
  10760. /* We make a group call lets checkout which ports are in my group */
  10761. port_str += 2;
  10762. ast_copy_string(group, port_str, sizeof(group));
  10763. chan_misdn_log(2, 0, "Checking Ports in group: %s\n", group);
  10764. for (port = misdn_cfg_get_next_port(port);
  10765. port > 0;
  10766. port = misdn_cfg_get_next_port(port)) {
  10767. char cfg_group[BUFFERSIZE + 1];
  10768. chan_misdn_log(2, 0, "trying port %d\n", port);
  10769. misdn_cfg_get(port, MISDN_CFG_GROUPNAME, cfg_group, sizeof(cfg_group));
  10770. if (!strcasecmp(cfg_group, group)) {
  10771. port_up = misdn_lib_port_up(port, 1);
  10772. if (!port_up) {
  10773. chan_misdn_log(2, 0, " --> port '%d'\n", port);
  10774. misdn_lib_get_port_up(port);
  10775. dowait = 1;
  10776. }
  10777. }
  10778. }
  10779. } else {
  10780. port = atoi(port_str);
  10781. chan_misdn_log(2, 0, "Checking Port: %d\n", port);
  10782. port_up = misdn_lib_port_up(port, 1);
  10783. if (!port_up) {
  10784. misdn_lib_get_port_up(port);
  10785. dowait = 1;
  10786. }
  10787. }
  10788. if (dowait) {
  10789. chan_misdn_log(2, 0, "Waiting for '%d' seconds\n", timeout);
  10790. ast_safe_sleep(chan, timeout * 1000);
  10791. }
  10792. return 0;
  10793. }
  10794. static int misdn_set_opt_exec(struct ast_channel *chan, const char *data)
  10795. {
  10796. struct chan_list *ch = MISDN_ASTERISK_TECH_PVT(chan);
  10797. char *tok;
  10798. char *tokb;
  10799. char *parse;
  10800. int keyidx = 0;
  10801. int rxgain = 0;
  10802. int txgain = 0;
  10803. int change_jitter = 0;
  10804. if (strcasecmp(ast_channel_tech(chan)->type, misdn_type)) {
  10805. ast_log(LOG_WARNING, "misdn_set_opt makes sense only with %s channels!\n", misdn_type);
  10806. return -1;
  10807. }
  10808. if (ast_strlen_zero((char *) data)) {
  10809. ast_log(LOG_WARNING, "misdn_set_opt Requires arguments\n");
  10810. return -1;
  10811. }
  10812. parse = ast_strdupa(data);
  10813. for (tok = strtok_r(parse, ":", &tokb);
  10814. tok;
  10815. tok = strtok_r(NULL, ":", &tokb)) {
  10816. int neglect = 0;
  10817. if (tok[0] == '!') {
  10818. neglect = 1;
  10819. tok++;
  10820. }
  10821. switch(tok[0]) {
  10822. case 'd' :
  10823. ast_copy_string(ch->bc->display, ++tok, sizeof(ch->bc->display));
  10824. chan_misdn_log(1, ch->bc->port, "SETOPT: Display:%s\n", ch->bc->display);
  10825. break;
  10826. case 'n':
  10827. chan_misdn_log(1, ch->bc->port, "SETOPT: No DSP\n");
  10828. ch->bc->nodsp = 1;
  10829. break;
  10830. case 'j':
  10831. chan_misdn_log(1, ch->bc->port, "SETOPT: jitter\n");
  10832. tok++;
  10833. change_jitter = 1;
  10834. switch (tok[0]) {
  10835. case 'b':
  10836. ch->jb_len = atoi(++tok);
  10837. chan_misdn_log(1, ch->bc->port, " --> buffer_len:%d\n", ch->jb_len);
  10838. break;
  10839. case 't' :
  10840. ch->jb_upper_threshold = atoi(++tok);
  10841. chan_misdn_log(1, ch->bc->port, " --> upper_threshold:%d\n", ch->jb_upper_threshold);
  10842. break;
  10843. case 'n':
  10844. ch->bc->nojitter = 1;
  10845. chan_misdn_log(1, ch->bc->port, " --> nojitter\n");
  10846. break;
  10847. default:
  10848. ch->jb_len = 4000;
  10849. ch->jb_upper_threshold = 0;
  10850. chan_misdn_log(1, ch->bc->port, " --> buffer_len:%d (default)\n", ch->jb_len);
  10851. chan_misdn_log(1, ch->bc->port, " --> upper_threshold:%d (default)\n", ch->jb_upper_threshold);
  10852. break;
  10853. }
  10854. break;
  10855. case 'v':
  10856. tok++;
  10857. switch (tok[0]) {
  10858. case 'r' :
  10859. rxgain = atoi(++tok);
  10860. if (rxgain < -8) {
  10861. rxgain = -8;
  10862. }
  10863. if (rxgain > 8) {
  10864. rxgain = 8;
  10865. }
  10866. ch->bc->rxgain = rxgain;
  10867. chan_misdn_log(1, ch->bc->port, "SETOPT: Volume:%d\n", rxgain);
  10868. break;
  10869. case 't':
  10870. txgain = atoi(++tok);
  10871. if (txgain < -8) {
  10872. txgain = -8;
  10873. }
  10874. if (txgain > 8) {
  10875. txgain = 8;
  10876. }
  10877. ch->bc->txgain = txgain;
  10878. chan_misdn_log(1, ch->bc->port, "SETOPT: Volume:%d\n", txgain);
  10879. break;
  10880. }
  10881. break;
  10882. case 'c':
  10883. keyidx = atoi(++tok);
  10884. {
  10885. char keys[4096];
  10886. char *key = NULL;
  10887. char *tmp = keys;
  10888. int i;
  10889. misdn_cfg_get(0, MISDN_GEN_CRYPT_KEYS, keys, sizeof(keys));
  10890. for (i = 0; i < keyidx; i++) {
  10891. key = strsep(&tmp, ",");
  10892. }
  10893. if (key) {
  10894. ast_copy_string(ch->bc->crypt_key, key, sizeof(ch->bc->crypt_key));
  10895. }
  10896. chan_misdn_log(0, ch->bc->port, "SETOPT: crypt with key:%s\n", ch->bc->crypt_key);
  10897. break;
  10898. }
  10899. case 'e':
  10900. chan_misdn_log(1, ch->bc->port, "SETOPT: EchoCancel\n");
  10901. if (neglect) {
  10902. chan_misdn_log(1, ch->bc->port, " --> disabled\n");
  10903. #ifdef MISDN_1_2
  10904. *ch->bc->pipeline = 0;
  10905. #else
  10906. ch->bc->ec_enable = 0;
  10907. #endif
  10908. } else {
  10909. #ifdef MISDN_1_2
  10910. update_pipeline_config(ch->bc);
  10911. #else
  10912. ch->bc->ec_enable = 1;
  10913. ch->bc->orig = ch->originator;
  10914. tok++;
  10915. if (*tok) {
  10916. ch->bc->ec_deftaps = atoi(tok);
  10917. }
  10918. #endif
  10919. }
  10920. break;
  10921. case 'h':
  10922. chan_misdn_log(1, ch->bc->port, "SETOPT: Digital\n");
  10923. if (strlen(tok) > 1 && tok[1] == '1') {
  10924. chan_misdn_log(1, ch->bc->port, "SETOPT: HDLC \n");
  10925. if (!ch->bc->hdlc) {
  10926. ch->bc->hdlc = 1;
  10927. }
  10928. }
  10929. ch->bc->capability = INFO_CAPABILITY_DIGITAL_UNRESTRICTED;
  10930. break;
  10931. case 's':
  10932. chan_misdn_log(1, ch->bc->port, "SETOPT: Send DTMF\n");
  10933. ch->bc->send_dtmf = 1;
  10934. break;
  10935. case 'f':
  10936. chan_misdn_log(1, ch->bc->port, "SETOPT: Faxdetect\n");
  10937. ch->faxdetect = 1;
  10938. misdn_cfg_get(ch->bc->port, MISDN_CFG_FAXDETECT_TIMEOUT, &ch->faxdetect_timeout, sizeof(ch->faxdetect_timeout));
  10939. break;
  10940. case 'a':
  10941. chan_misdn_log(1, ch->bc->port, "SETOPT: AST_DSP (for DTMF)\n");
  10942. ch->ast_dsp = 1;
  10943. break;
  10944. case 'p':
  10945. chan_misdn_log(1, ch->bc->port, "SETOPT: callerpres: %s\n", &tok[1]);
  10946. /* CRICH: callingpres!!! */
  10947. if (strstr(tok, "allowed")) {
  10948. ch->bc->presentation = 0;
  10949. ch->bc->set_presentation = 1;
  10950. } else if (strstr(tok, "restricted")) {
  10951. ch->bc->presentation = 1;
  10952. ch->bc->set_presentation = 1;
  10953. } else if (strstr(tok, "not_screened")) {
  10954. chan_misdn_log(0, ch->bc->port, "SETOPT: callerpres: not_screened is deprecated\n");
  10955. ch->bc->presentation = 1;
  10956. ch->bc->set_presentation = 1;
  10957. }
  10958. break;
  10959. case 'i' :
  10960. chan_misdn_log(1, ch->bc->port, "Ignoring dtmf tones, just use them inband\n");
  10961. ch->ignore_dtmf = 1;
  10962. break;
  10963. default:
  10964. break;
  10965. }
  10966. }
  10967. if (change_jitter) {
  10968. config_jitterbuffer(ch);
  10969. }
  10970. if (ch->faxdetect || ch->ast_dsp) {
  10971. if (!ch->dsp) {
  10972. ch->dsp = ast_dsp_new();
  10973. }
  10974. if (ch->dsp) {
  10975. ast_dsp_set_features(ch->dsp, DSP_FEATURE_DIGIT_DETECT | DSP_FEATURE_FAX_DETECT);
  10976. }
  10977. }
  10978. if (ch->ast_dsp) {
  10979. chan_misdn_log(1, ch->bc->port, "SETOPT: with AST_DSP we deactivate mISDN_dsp\n");
  10980. ch->bc->nodsp = 1;
  10981. }
  10982. return 0;
  10983. }
  10984. int chan_misdn_jb_empty(struct misdn_bchannel *bc, char *buf, int len)
  10985. {
  10986. struct chan_list *ch;
  10987. int res;
  10988. ch = find_chan_by_bc(bc);
  10989. if (!ch) {
  10990. return 0;
  10991. }
  10992. if (ch->jb) {
  10993. res = misdn_jb_empty(ch->jb, buf, len);
  10994. } else {
  10995. res = 0;
  10996. }
  10997. chan_list_unref(ch, "Done emptying jb");
  10998. return res;
  10999. }
  11000. /*******************************************************/
  11001. /***************** JITTERBUFFER ************************/
  11002. /*******************************************************/
  11003. /* allocates the jb-structure and initialize the elements*/
  11004. struct misdn_jb *misdn_jb_init(int size, int upper_threshold)
  11005. {
  11006. struct misdn_jb *jb;
  11007. jb = ast_calloc(1, sizeof(*jb));
  11008. if (!jb) {
  11009. chan_misdn_log(-1, 0, "No free Mem for jb\n");
  11010. return NULL;
  11011. }
  11012. jb->size = size;
  11013. jb->upper_threshold = upper_threshold;
  11014. //jb->wp = 0;
  11015. //jb->rp = 0;
  11016. //jb->state_full = 0;
  11017. //jb->state_empty = 0;
  11018. //jb->bytes_wrote = 0;
  11019. jb->samples = ast_calloc(size, sizeof(*jb->samples));
  11020. if (!jb->samples) {
  11021. ast_free(jb);
  11022. chan_misdn_log(-1, 0, "No free Mem for jb->samples\n");
  11023. return NULL;
  11024. }
  11025. jb->ok = ast_calloc(size, sizeof(*jb->ok));
  11026. if (!jb->ok) {
  11027. ast_free(jb->samples);
  11028. ast_free(jb);
  11029. chan_misdn_log(-1, 0, "No free Mem for jb->ok\n");
  11030. return NULL;
  11031. }
  11032. ast_mutex_init(&jb->mutexjb);
  11033. return jb;
  11034. }
  11035. /* frees the data and destroys the given jitterbuffer struct */
  11036. void misdn_jb_destroy(struct misdn_jb *jb)
  11037. {
  11038. ast_mutex_destroy(&jb->mutexjb);
  11039. ast_free(jb->ok);
  11040. ast_free(jb->samples);
  11041. ast_free(jb);
  11042. }
  11043. /* fills the jitterbuffer with len data returns < 0 if there was an
  11044. error (buffer overflow). */
  11045. int misdn_jb_fill(struct misdn_jb *jb, const char *data, int len)
  11046. {
  11047. int i;
  11048. int j;
  11049. int rp;
  11050. int wp;
  11051. if (!jb || ! data) {
  11052. return 0;
  11053. }
  11054. ast_mutex_lock(&jb->mutexjb);
  11055. wp = jb->wp;
  11056. rp = jb->rp;
  11057. for (i = 0; i < len; i++) {
  11058. jb->samples[wp] = data[i];
  11059. jb->ok[wp] = 1;
  11060. wp = (wp != jb->size - 1) ? wp + 1 : 0;
  11061. if (wp == jb->rp) {
  11062. jb->state_full = 1;
  11063. }
  11064. }
  11065. if (wp >= rp) {
  11066. jb->state_buffer = wp - rp;
  11067. } else {
  11068. jb->state_buffer = jb->size - rp + wp;
  11069. }
  11070. chan_misdn_log(9, 0, "misdn_jb_fill: written:%d | Buffer status:%d p:%p\n", len, jb->state_buffer, jb);
  11071. if (jb->state_full) {
  11072. jb->wp = wp;
  11073. rp = wp;
  11074. for (j = 0; j < jb->upper_threshold; j++) {
  11075. rp = (rp != 0) ? rp - 1 : jb->size - 1;
  11076. }
  11077. jb->rp = rp;
  11078. jb->state_full = 0;
  11079. jb->state_empty = 1;
  11080. ast_mutex_unlock(&jb->mutexjb);
  11081. return -1;
  11082. }
  11083. if (!jb->state_empty) {
  11084. jb->bytes_wrote += len;
  11085. if (jb->bytes_wrote >= jb->upper_threshold) {
  11086. jb->state_empty = 1;
  11087. jb->bytes_wrote = 0;
  11088. }
  11089. }
  11090. jb->wp = wp;
  11091. ast_mutex_unlock(&jb->mutexjb);
  11092. return 0;
  11093. }
  11094. /* gets len bytes out of the jitterbuffer if available, else only the
  11095. available data is returned and the return value indicates the number
  11096. of data. */
  11097. int misdn_jb_empty(struct misdn_jb *jb, char *data, int len)
  11098. {
  11099. int i;
  11100. int wp;
  11101. int rp;
  11102. int read = 0;
  11103. ast_mutex_lock(&jb->mutexjb);
  11104. rp = jb->rp;
  11105. wp = jb->wp;
  11106. if (jb->state_empty) {
  11107. for (i = 0; i < len; i++) {
  11108. if (wp == rp) {
  11109. jb->rp = rp;
  11110. jb->state_empty = 0;
  11111. ast_mutex_unlock(&jb->mutexjb);
  11112. return read;
  11113. } else {
  11114. if (jb->ok[rp] == 1) {
  11115. data[i] = jb->samples[rp];
  11116. jb->ok[rp] = 0;
  11117. rp = (rp != jb->size - 1) ? rp + 1 : 0;
  11118. read += 1;
  11119. }
  11120. }
  11121. }
  11122. if (wp >= rp) {
  11123. jb->state_buffer = wp - rp;
  11124. } else {
  11125. jb->state_buffer = jb->size - rp + wp;
  11126. }
  11127. chan_misdn_log(9, 0, "misdn_jb_empty: read:%d | Buffer status:%d p:%p\n", len, jb->state_buffer, jb);
  11128. jb->rp = rp;
  11129. } else {
  11130. chan_misdn_log(9, 0, "misdn_jb_empty: Wait...requested:%d p:%p\n", len, jb);
  11131. }
  11132. ast_mutex_unlock(&jb->mutexjb);
  11133. return read;
  11134. }
  11135. /*******************************************************/
  11136. /*************** JITTERBUFFER END *********************/
  11137. /*******************************************************/
  11138. static void chan_misdn_log(int level, int port, char *tmpl, ...)
  11139. {
  11140. va_list ap;
  11141. char buf[1024];
  11142. char port_buf[8];
  11143. if (!(0 <= port && port <= max_ports)) {
  11144. ast_log(LOG_WARNING, "chan_misdn_log called with out-of-range port number! (%d)\n", port);
  11145. port = 0;
  11146. level = -1;
  11147. } else if (!(level == -1
  11148. || (misdn_debug_only[port]
  11149. ? (level == 1 && misdn_debug[port]) || level == misdn_debug[port]
  11150. : level <= misdn_debug[port])
  11151. || (level <= misdn_debug[0] && !ast_strlen_zero(global_tracefile)))) {
  11152. /*
  11153. * We are not going to print anything so lets not
  11154. * go to all the work of generating a string.
  11155. */
  11156. return;
  11157. }
  11158. snprintf(port_buf, sizeof(port_buf), "P[%2d] ", port);
  11159. va_start(ap, tmpl);
  11160. vsnprintf(buf, sizeof(buf), tmpl, ap);
  11161. va_end(ap);
  11162. if (level == -1) {
  11163. ast_log(LOG_WARNING, "%s", buf);
  11164. } else if (misdn_debug_only[port]
  11165. ? (level == 1 && misdn_debug[port]) || level == misdn_debug[port]
  11166. : level <= misdn_debug[port]) {
  11167. ast_verbose("%s%s", port_buf, buf);
  11168. }
  11169. if (level <= misdn_debug[0] && !ast_strlen_zero(global_tracefile)) {
  11170. char ctimebuf[30];
  11171. time_t tm;
  11172. char *tmp;
  11173. char *p;
  11174. FILE *fp;
  11175. fp = fopen(global_tracefile, "a+");
  11176. if (!fp) {
  11177. ast_verbose("Error opening Tracefile: [ %s ] %s\n", global_tracefile, strerror(errno));
  11178. return;
  11179. }
  11180. tm = time(NULL);
  11181. tmp = ctime_r(&tm, ctimebuf);
  11182. p = strchr(tmp, '\n');
  11183. if (p) {
  11184. *p = ':';
  11185. }
  11186. fputs(tmp, fp);
  11187. fputs(" ", fp);
  11188. fputs(port_buf, fp);
  11189. fputs(" ", fp);
  11190. fputs(buf, fp);
  11191. fclose(fp);
  11192. }
  11193. }
  11194. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Channel driver for mISDN Support (BRI/PRI)",
  11195. .load = load_module,
  11196. .unload = unload_module,
  11197. .reload = reload,
  11198. .load_pri = AST_MODPRI_CHANNEL_DRIVER,
  11199. );