isdn_lib.c 109 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819
  1. /*
  2. * Chan_Misdn -- Channel Driver for Asterisk
  3. *
  4. * Interface to mISDN
  5. *
  6. * Copyright (C) 2004, Christian Richter
  7. *
  8. * Christian Richter <crich@beronet.com>
  9. *
  10. * This program is free software, distributed under the terms of
  11. * the GNU General Public License
  12. */
  13. /*! \file
  14. * \brief Interface to mISDN
  15. * \author Christian Richter <crich@beronet.com>
  16. */
  17. /*** MODULEINFO
  18. <support_level>extended</support_level>
  19. ***/
  20. #include <syslog.h>
  21. #include <sys/time.h>
  22. #include <mISDNuser/isdn_debug.h>
  23. #include "isdn_lib_intern.h"
  24. #include "isdn_lib.h"
  25. enum event_response_e (*cb_event)(enum event_e event, struct misdn_bchannel *bc, void *user_data);
  26. void (*cb_log)(int level, int port, char *tmpl, ...)
  27. __attribute__ ((format (printf, 3, 4)));
  28. int (*cb_jb_empty)(struct misdn_bchannel *bc, char *buffer, int len);
  29. /*
  30. * Define ARRAY_LEN() because I cannot
  31. * #include "asterisk/utils.h"
  32. */
  33. #define ARRAY_LEN(a) (sizeof(a) / sizeof(a[0]))
  34. #include "asterisk/causes.h"
  35. void misdn_join_conf(struct misdn_bchannel *bc, int conf_id);
  36. void misdn_split_conf(struct misdn_bchannel *bc, int conf_id);
  37. int misdn_lib_get_l2_up(struct misdn_stack *stack);
  38. struct misdn_stack *get_misdn_stack(void);
  39. int misdn_lib_port_is_pri(int port)
  40. {
  41. struct misdn_stack *stack=get_misdn_stack();
  42. for ( ; stack; stack=stack->next) {
  43. if (stack->port == port) {
  44. return stack->pri;
  45. }
  46. }
  47. return -1;
  48. }
  49. int misdn_lib_port_is_nt(int port)
  50. {
  51. struct misdn_stack *stack=get_misdn_stack();
  52. for ( ; stack; stack=stack->next) {
  53. if (stack->port == port) {
  54. return stack->nt;
  55. }
  56. }
  57. return -1;
  58. }
  59. void misdn_make_dummy(struct misdn_bchannel *dummybc, int port, int l3id, int nt, int channel)
  60. {
  61. memset (dummybc,0,sizeof(struct misdn_bchannel));
  62. dummybc->port=port;
  63. if (l3id==0)
  64. dummybc->l3_id = MISDN_ID_DUMMY;
  65. else
  66. dummybc->l3_id=l3id;
  67. dummybc->nt=nt;
  68. dummybc->dummy=1;
  69. dummybc->channel=channel;
  70. }
  71. int misdn_lib_port_block(int port)
  72. {
  73. struct misdn_stack *stack=get_misdn_stack();
  74. for ( ; stack; stack=stack->next) {
  75. if (stack->port == port) {
  76. stack->blocked=1;
  77. return 0;
  78. }
  79. }
  80. return -1;
  81. }
  82. int misdn_lib_port_unblock(int port)
  83. {
  84. struct misdn_stack *stack=get_misdn_stack();
  85. for ( ; stack; stack=stack->next) {
  86. if (stack->port == port) {
  87. stack->blocked=0;
  88. return 0;
  89. }
  90. }
  91. return -1;
  92. }
  93. int misdn_lib_is_port_blocked(int port)
  94. {
  95. struct misdn_stack *stack=get_misdn_stack();
  96. for ( ; stack; stack=stack->next) {
  97. if (stack->port == port) {
  98. return stack->blocked;
  99. }
  100. }
  101. return -1;
  102. }
  103. int misdn_lib_is_ptp(int port)
  104. {
  105. struct misdn_stack *stack=get_misdn_stack();
  106. for ( ; stack; stack=stack->next) {
  107. if (stack->port == port) return stack->ptp;
  108. }
  109. return -1;
  110. }
  111. int misdn_lib_get_maxchans(int port)
  112. {
  113. struct misdn_stack *stack=get_misdn_stack();
  114. for ( ; stack; stack=stack->next) {
  115. if (stack->port == port) {
  116. if (stack->pri)
  117. return 30;
  118. else
  119. return 2;
  120. }
  121. }
  122. return -1;
  123. }
  124. struct misdn_stack *get_stack_by_bc(struct misdn_bchannel *bc)
  125. {
  126. struct misdn_stack *stack = get_misdn_stack();
  127. if (!bc)
  128. return NULL;
  129. for ( ; stack; stack = stack->next) {
  130. if (bc->port == stack->port)
  131. return stack;
  132. }
  133. return NULL;
  134. }
  135. void get_show_stack_details(int port, char *buf)
  136. {
  137. struct misdn_stack *stack = get_misdn_stack();
  138. for (; stack; stack = stack->next) {
  139. if (stack->port == port) {
  140. break;
  141. }
  142. }
  143. if (stack) {
  144. sprintf(buf, "* Port %2d Type %s Prot. %s L2Link %s L1Link:%s Blocked:%d",
  145. stack->port,
  146. stack->nt ? "NT" : "TE",
  147. stack->ptp ? "PTP" : "PMP",
  148. (stack->nt && !stack->ptp) ? "UNKN" : stack->l2link ? "UP " : "DOWN",
  149. stack->l1link ? "UP " : "DOWN",
  150. stack->blocked);
  151. } else {
  152. buf[0] = 0;
  153. }
  154. }
  155. static int nt_err_cnt =0 ;
  156. enum global_states {
  157. MISDN_INITIALIZING,
  158. MISDN_INITIALIZED
  159. } ;
  160. static enum global_states global_state=MISDN_INITIALIZING;
  161. #include <mISDNuser/net_l2.h>
  162. #include <mISDNuser/tone.h>
  163. #include <unistd.h>
  164. #include <semaphore.h>
  165. #include <pthread.h>
  166. #include <signal.h>
  167. #include "isdn_lib.h"
  168. struct misdn_lib {
  169. /*! \brief mISDN device handle returned by mISDN_open() */
  170. int midev;
  171. pthread_t event_thread;
  172. pthread_t event_handler_thread;
  173. void *user_data;
  174. msg_queue_t activatequeue;
  175. sem_t new_msg;
  176. struct misdn_stack *stack_list;
  177. } ;
  178. #ifndef ECHOCAN_ON
  179. #define ECHOCAN_ON 123
  180. #define ECHOCAN_OFF 124
  181. #endif
  182. #define MISDN_DEBUG 0
  183. void misdn_tx_jitter(struct misdn_bchannel *bc, int len);
  184. struct misdn_bchannel *find_bc_by_l3id(struct misdn_stack *stack, unsigned long l3id);
  185. int manager_isdn_handler(iframe_t *frm ,msg_t *msg);
  186. int misdn_lib_port_restart(int port);
  187. int misdn_lib_pid_restart(int pid);
  188. extern struct isdn_msg msgs_g[];
  189. #define ISDN_PID_L3_B_USER 0x430000ff
  190. #define ISDN_PID_L4_B_USER 0x440000ff
  191. /* #define MISDN_IBUF_SIZE 1024 */
  192. #define MISDN_IBUF_SIZE 512
  193. /* Fine Tuning of Inband Signalling time */
  194. #define TONE_ALERT_CNT 41 /* 1 Sec */
  195. #define TONE_ALERT_SILENCE_CNT 200 /* 4 Sec */
  196. #define TONE_BUSY_CNT 20 /* ? */
  197. #define TONE_BUSY_SILENCE_CNT 48 /* ? */
  198. static int entity;
  199. static struct misdn_lib *glob_mgr;
  200. static char tone_425_flip[TONE_425_SIZE];
  201. static char tone_silence_flip[TONE_SILENCE_SIZE];
  202. static void misdn_lib_isdn_event_catcher(void *arg);
  203. static int handle_event_nt(void *dat, void *arg);
  204. void stack_holder_add(struct misdn_stack *stack, struct misdn_bchannel *holder);
  205. void stack_holder_remove(struct misdn_stack *stack, struct misdn_bchannel *holder);
  206. struct misdn_bchannel *stack_holder_find(struct misdn_stack *stack, unsigned long l3id);
  207. /* from isdn_lib.h */
  208. /* user iface */
  209. void te_lib_destroy(int midev) ;
  210. struct misdn_bchannel *manager_find_bc_by_pid(int pid);
  211. void manager_ph_control_block(struct misdn_bchannel *bc, int c1, void *c2, int c2_len);
  212. void manager_clean_bc(struct misdn_bchannel *bc );
  213. void manager_bchannel_setup (struct misdn_bchannel *bc);
  214. void manager_bchannel_cleanup (struct misdn_bchannel *bc);
  215. void ec_chunk( struct misdn_bchannel *bc, unsigned char *rxchunk, unsigned char *txchunk, int chunk_size);
  216. /* end */
  217. int bchdev_echocancel_activate(struct misdn_bchannel* dev);
  218. void bchdev_echocancel_deactivate(struct misdn_bchannel* dev);
  219. /* end */
  220. static char *bearer2str(int cap) {
  221. static char *bearers[]={
  222. "Speech",
  223. "Audio 3.1k",
  224. "Unres Digital",
  225. "Res Digital",
  226. "Unknown Bearer"
  227. };
  228. switch (cap) {
  229. case INFO_CAPABILITY_SPEECH:
  230. return bearers[0];
  231. break;
  232. case INFO_CAPABILITY_AUDIO_3_1K:
  233. return bearers[1];
  234. break;
  235. case INFO_CAPABILITY_DIGITAL_UNRESTRICTED:
  236. return bearers[2];
  237. break;
  238. case INFO_CAPABILITY_DIGITAL_RESTRICTED:
  239. return bearers[3];
  240. break;
  241. default:
  242. return bearers[4];
  243. break;
  244. }
  245. }
  246. static char flip_table[256];
  247. static void init_flip_bits(void)
  248. {
  249. int i,k;
  250. for (i = 0 ; i < 256 ; i++) {
  251. unsigned char sample = 0 ;
  252. for (k = 0; k<8; k++) {
  253. if ( i & 1 << k ) sample |= 0x80 >> k;
  254. }
  255. flip_table[i] = sample;
  256. }
  257. }
  258. static char * flip_buf_bits ( char * buf , int len)
  259. {
  260. int i;
  261. char * start = buf;
  262. for (i = 0 ; i < len; i++) {
  263. buf[i] = flip_table[(unsigned char)buf[i]];
  264. }
  265. return start;
  266. }
  267. static msg_t *create_l2msg(int prim, int dinfo, int size) /* NT only */
  268. {
  269. int i = 0;
  270. msg_t *dmsg;
  271. while(i < 10)
  272. {
  273. dmsg = prep_l3data_msg(prim, dinfo, size, 256, NULL);
  274. if (dmsg)
  275. return(dmsg);
  276. if (!i)
  277. printf("cannot allocate memory, trying again...\n");
  278. i++;
  279. usleep(300000);
  280. }
  281. printf("cannot allocate memory, system overloaded.\n");
  282. exit(-1);
  283. }
  284. msg_t *create_l3msg(int prim, int mt, int dinfo, int size, int ntmode)
  285. {
  286. int i = 0;
  287. msg_t *dmsg;
  288. Q931_info_t *qi;
  289. iframe_t *frm;
  290. if (!ntmode)
  291. size = sizeof(Q931_info_t)+2;
  292. while(i < 10) {
  293. if (ntmode) {
  294. dmsg = prep_l3data_msg(prim, dinfo, size, 256, NULL);
  295. if (dmsg) {
  296. return(dmsg);
  297. }
  298. } else {
  299. dmsg = alloc_msg(size+256+mISDN_HEADER_LEN+DEFAULT_HEADROOM);
  300. if (dmsg)
  301. {
  302. memset(msg_put(dmsg,size+mISDN_HEADER_LEN), 0, size+mISDN_HEADER_LEN);
  303. frm = (iframe_t *)dmsg->data;
  304. frm->prim = prim;
  305. frm->dinfo = dinfo;
  306. qi = (Q931_info_t *)(dmsg->data + mISDN_HEADER_LEN);
  307. qi->type = mt;
  308. return(dmsg);
  309. }
  310. }
  311. if (!i) printf("cannot allocate memory, trying again...\n");
  312. i++;
  313. usleep(300000);
  314. }
  315. printf("cannot allocate memory, system overloaded.\n");
  316. exit(-1);
  317. }
  318. static int send_msg (int midev, struct misdn_bchannel *bc, msg_t *dmsg)
  319. {
  320. iframe_t *frm = (iframe_t *)dmsg->data;
  321. struct misdn_stack *stack=get_stack_by_bc(bc);
  322. if (!stack) {
  323. cb_log(0,bc->port,"send_msg: IEK!! no stack\n ");
  324. return -1;
  325. }
  326. frm->addr = (stack->upper_id | FLG_MSG_DOWN);
  327. frm->dinfo = bc->l3_id;
  328. frm->len = (dmsg->len) - mISDN_HEADER_LEN;
  329. cb_log(4,stack->port,"Sending msg, prim:%x addr:%x dinfo:%x\n",frm->prim,frm->addr,frm->dinfo);
  330. mISDN_write(midev, dmsg->data, dmsg->len, TIMEOUT_1SEC);
  331. free_msg(dmsg);
  332. return 0;
  333. }
  334. static int mypid=1;
  335. int misdn_cap_is_speech(int cap)
  336. /** Poor mans version **/
  337. {
  338. if ( (cap != INFO_CAPABILITY_DIGITAL_UNRESTRICTED) &&
  339. (cap != INFO_CAPABILITY_DIGITAL_RESTRICTED) ) return 1;
  340. return 0;
  341. }
  342. int misdn_inband_avail(struct misdn_bchannel *bc)
  343. {
  344. if (!bc->early_bconnect) {
  345. /* We have opted to never receive any available inband recorded messages */
  346. return 0;
  347. }
  348. switch (bc->progress_indicator) {
  349. case INFO_PI_INBAND_AVAILABLE:
  350. case INFO_PI_CALL_NOT_E2E_ISDN:
  351. case INFO_PI_CALLED_NOT_ISDN:
  352. return 1;
  353. default:
  354. return 0;
  355. }
  356. return 0;
  357. }
  358. static void dump_chan_list(struct misdn_stack *stack)
  359. {
  360. int i;
  361. for (i = 0; i <= stack->b_num; ++i) {
  362. cb_log(6, stack->port, "Idx:%d stack->cchan:%d in_use:%d Chan:%d\n",
  363. i, stack->channels[i], stack->bc[i].in_use, i + 1);
  364. }
  365. #if defined(AST_MISDN_ENHANCEMENTS)
  366. for (i = MAX_BCHANS + 1; i < ARRAY_LEN(stack->bc); ++i) {
  367. if (stack->bc[i].in_use) {
  368. cb_log(6, stack->port, "Idx:%d stack->cchan:%d REGISTER Chan:%d in_use\n",
  369. i, stack->channels[i], i + 1);
  370. }
  371. }
  372. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  373. }
  374. void misdn_dump_chanlist(void)
  375. {
  376. struct misdn_stack *stack=get_misdn_stack();
  377. for ( ; stack; stack=stack->next) {
  378. dump_chan_list(stack);
  379. }
  380. }
  381. static int set_chan_in_stack(struct misdn_stack *stack, int channel)
  382. {
  383. cb_log(4,stack->port,"set_chan_in_stack: %d\n",channel);
  384. dump_chan_list(stack);
  385. if (1 <= channel && channel <= ARRAY_LEN(stack->channels)) {
  386. if (!stack->channels[channel-1])
  387. stack->channels[channel-1] = 1;
  388. else {
  389. cb_log(4,stack->port,"channel already in use:%d\n", channel );
  390. return -1;
  391. }
  392. } else {
  393. cb_log(0,stack->port,"couldn't set channel %d in\n", channel );
  394. return -1;
  395. }
  396. return 0;
  397. }
  398. static int find_free_chan_in_stack(struct misdn_stack *stack, struct misdn_bchannel *bc, int channel, int dec)
  399. {
  400. int i;
  401. int chan = 0;
  402. int bnums;
  403. if (bc->channel_found) {
  404. return 0;
  405. }
  406. bc->channel_found = 1;
  407. #if defined(AST_MISDN_ENHANCEMENTS)
  408. if (bc->is_register_pool) {
  409. pthread_mutex_lock(&stack->st_lock);
  410. for (i = MAX_BCHANS + 1; i < ARRAY_LEN(stack->channels); ++i) {
  411. if (!stack->channels[i]) {
  412. chan = i + 1;
  413. cb_log(3, stack->port, " --> found REGISTER chan: %d\n", chan);
  414. break;
  415. }
  416. }
  417. } else
  418. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  419. {
  420. cb_log(5, stack->port, "find_free_chan: req_chan:%d\n", channel);
  421. if (channel < 0 || channel > MAX_BCHANS) {
  422. cb_log(0, stack->port, " !! out of bound call to find_free_chan_in_stack! (ch:%d)\n", channel);
  423. return 0;
  424. }
  425. --channel;
  426. pthread_mutex_lock(&stack->st_lock);
  427. bnums = stack->pri ? stack->b_num : stack->b_num - 1;
  428. if (dec) {
  429. for (i = bnums; i >= 0; --i) {
  430. if (i != 15 && (channel < 0 || i == channel)) { /* skip E1 D channel ;) and work with chan preselection */
  431. if (!stack->channels[i]) {
  432. chan = i + 1;
  433. cb_log(3, stack->port, " --> found chan%s: %d\n", channel >= 0 ? " (preselected)" : "", chan);
  434. break;
  435. }
  436. }
  437. }
  438. } else {
  439. for (i = 0; i <= bnums; ++i) {
  440. if (i != 15 && (channel < 0 || i == channel)) { /* skip E1 D channel ;) and work with chan preselection */
  441. if (!stack->channels[i]) {
  442. chan = i + 1;
  443. cb_log(3, stack->port, " --> found chan%s: %d\n", channel >= 0 ? " (preselected)" : "", chan);
  444. break;
  445. }
  446. }
  447. }
  448. }
  449. }
  450. if (!chan) {
  451. pthread_mutex_unlock(&stack->st_lock);
  452. cb_log(1, stack->port, " !! NO FREE CHAN IN STACK\n");
  453. dump_chan_list(stack);
  454. bc->out_cause = AST_CAUSE_NORMAL_CIRCUIT_CONGESTION;
  455. return -1;
  456. }
  457. if (set_chan_in_stack(stack, chan) < 0) {
  458. pthread_mutex_unlock(&stack->st_lock);
  459. cb_log(0, stack->port, "Channel Already in use:%d\n", chan);
  460. bc->out_cause = AST_CAUSE_REQUESTED_CHAN_UNAVAIL;
  461. return -1;
  462. }
  463. pthread_mutex_unlock(&stack->st_lock);
  464. bc->channel = chan;
  465. return 0;
  466. }
  467. /*!
  468. * \internal
  469. * \brief Release a B channel to the allocation pool.
  470. *
  471. * \param stack Which port stack B channel belongs.
  472. * \param channel B channel to release. (Range 1-MAX_BCHANS representing B1-Bn)
  473. *
  474. * \return Nothing
  475. *
  476. * \note
  477. * Must be called after clean_up_bc() to make sure that the media stream is
  478. * no longer connected.
  479. */
  480. static void empty_chan_in_stack(struct misdn_stack *stack, int channel)
  481. {
  482. if (channel < 1 || ARRAY_LEN(stack->channels) < channel) {
  483. cb_log(0, stack->port, "empty_chan_in_stack: cannot empty channel %d\n", channel);
  484. return;
  485. }
  486. cb_log(4, stack->port, "empty_chan_in_stack: %d\n", channel);
  487. stack->channels[channel - 1] = 0;
  488. dump_chan_list(stack);
  489. }
  490. char *bc_state2str(enum bchannel_state state) {
  491. int i;
  492. struct bchan_state_s {
  493. char *n;
  494. enum bchannel_state s;
  495. } states[] = {
  496. {"BCHAN_CLEANED", BCHAN_CLEANED },
  497. {"BCHAN_EMPTY", BCHAN_EMPTY},
  498. {"BCHAN_ACTIVATED", BCHAN_ACTIVATED},
  499. {"BCHAN_BRIDGED", BCHAN_BRIDGED},
  500. {"BCHAN_RELEASE", BCHAN_RELEASE},
  501. {"BCHAN_ERROR", BCHAN_ERROR}
  502. };
  503. for (i=0; i< sizeof(states)/sizeof(struct bchan_state_s); i++)
  504. if ( states[i].s == state)
  505. return states[i].n;
  506. return "UNKNOWN";
  507. }
  508. void bc_state_change(struct misdn_bchannel *bc, enum bchannel_state state)
  509. {
  510. cb_log(5,bc->port,"BC_STATE_CHANGE: l3id:%x from:%s to:%s\n",
  511. bc->l3_id,
  512. bc_state2str(bc->bc_state),
  513. bc_state2str(state) );
  514. switch (state) {
  515. case BCHAN_ACTIVATED:
  516. if (bc->next_bc_state == BCHAN_BRIDGED) {
  517. misdn_join_conf(bc, bc->conf_id);
  518. bc->next_bc_state = BCHAN_EMPTY;
  519. return;
  520. }
  521. default:
  522. bc->bc_state=state;
  523. break;
  524. }
  525. }
  526. static void bc_next_state_change(struct misdn_bchannel *bc, enum bchannel_state state)
  527. {
  528. cb_log(5,bc->port,"BC_NEXT_STATE_CHANGE: from:%s to:%s\n",
  529. bc_state2str(bc->next_bc_state),
  530. bc_state2str(state) );
  531. bc->next_bc_state=state;
  532. }
  533. /*!
  534. * \internal
  535. * \brief Empty the B channel record of most call data.
  536. *
  537. * \param bc B channel record to empty of most call data.
  538. *
  539. * \return Nothing
  540. *
  541. * \note
  542. * Sets the last_used time and must be called before clearing bc->in_use.
  543. */
  544. static void empty_bc(struct misdn_bchannel *bc)
  545. {
  546. bc->caller.presentation = 0; /* allowed */
  547. bc->caller.number_plan = NUMPLAN_ISDN;
  548. bc->caller.number_type = NUMTYPE_UNKNOWN;
  549. bc->caller.name[0] = 0;
  550. bc->caller.number[0] = 0;
  551. bc->caller.subaddress[0] = 0;
  552. bc->connected.presentation = 0; /* allowed */
  553. bc->connected.number_plan = NUMPLAN_ISDN;
  554. bc->connected.number_type = NUMTYPE_UNKNOWN;
  555. bc->connected.name[0] = 0;
  556. bc->connected.number[0] = 0;
  557. bc->connected.subaddress[0] = 0;
  558. bc->redirecting.from.presentation = 0; /* allowed */
  559. bc->redirecting.from.number_plan = NUMPLAN_ISDN;
  560. bc->redirecting.from.number_type = NUMTYPE_UNKNOWN;
  561. bc->redirecting.from.name[0] = 0;
  562. bc->redirecting.from.number[0] = 0;
  563. bc->redirecting.from.subaddress[0] = 0;
  564. bc->redirecting.to.presentation = 0; /* allowed */
  565. bc->redirecting.to.number_plan = NUMPLAN_ISDN;
  566. bc->redirecting.to.number_type = NUMTYPE_UNKNOWN;
  567. bc->redirecting.to.name[0] = 0;
  568. bc->redirecting.to.number[0] = 0;
  569. bc->redirecting.to.subaddress[0] = 0;
  570. bc->redirecting.reason = mISDN_REDIRECTING_REASON_UNKNOWN;
  571. bc->redirecting.count = 0;
  572. bc->redirecting.to_changed = 0;
  573. bc->dummy=0;
  574. bc->bframe_len=0;
  575. bc->cw= 0;
  576. bc->dec=0;
  577. bc->channel = 0;
  578. bc->sending_complete = 0;
  579. bc->restart_channel=0;
  580. bc->conf_id = 0;
  581. bc->need_more_infos = 0;
  582. bc->send_dtmf=0;
  583. bc->nodsp=0;
  584. bc->nojitter=0;
  585. bc->time_usec=0;
  586. bc->rxgain=0;
  587. bc->txgain=0;
  588. bc->crypt=0;
  589. bc->curptx=0; bc->curprx=0;
  590. bc->crypt_key[0] = 0;
  591. bc->generate_tone=0;
  592. bc->tone_cnt=0;
  593. bc->active = 0;
  594. bc->early_bconnect = 1;
  595. #ifdef MISDN_1_2
  596. *bc->pipeline = 0;
  597. #else
  598. bc->ec_enable = 0;
  599. bc->ec_deftaps = 128;
  600. #endif
  601. bc->AOCD_need_export = 0;
  602. bc->orig=0;
  603. bc->cause = AST_CAUSE_NORMAL_CLEARING;
  604. bc->out_cause = AST_CAUSE_NORMAL_CLEARING;
  605. bc->display_connected = 0; /* none */
  606. bc->display_setup = 0; /* none */
  607. bc->outgoing_colp = 0;/* pass */
  608. bc->presentation = 0; /* allowed */
  609. bc->set_presentation = 0;
  610. bc->notify_description_code = mISDN_NOTIFY_CODE_INVALID;
  611. bc->progress_coding=0;
  612. bc->progress_location=0;
  613. bc->progress_indicator=0;
  614. #if defined(AST_MISDN_ENHANCEMENTS)
  615. bc->div_leg_3_rx_wanted = 0;
  616. bc->div_leg_3_tx_pending = 0;
  617. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  618. /** Set Default Bearer Caps **/
  619. bc->capability=INFO_CAPABILITY_SPEECH;
  620. bc->law=INFO_CODEC_ALAW;
  621. bc->mode=0;
  622. bc->rate=0x10;
  623. bc->user1=0;
  624. bc->urate=0;
  625. bc->hdlc=0;
  626. bc->dialed.number_plan = NUMPLAN_ISDN;
  627. bc->dialed.number_type = NUMTYPE_UNKNOWN;
  628. bc->dialed.number[0] = 0;
  629. bc->dialed.subaddress[0] = 0;
  630. bc->info_dad[0] = 0;
  631. bc->display[0] = 0;
  632. bc->infos_pending[0] = 0;
  633. bc->uu[0]=0;
  634. bc->uulen=0;
  635. bc->fac_in.Function = Fac_None;
  636. bc->fac_out.Function = Fac_None;
  637. bc->te_choose_channel = 0;
  638. bc->channel_found= 0;
  639. gettimeofday(&bc->last_used, NULL);
  640. }
  641. static int clean_up_bc(struct misdn_bchannel *bc)
  642. {
  643. int ret=0;
  644. unsigned char buff[32];
  645. struct misdn_stack * stack;
  646. cb_log(3, bc->port, "$$$ CLEANUP CALLED pid:%d\n", bc->pid);
  647. stack=get_stack_by_bc(bc);
  648. if (!stack) return -1;
  649. switch (bc->bc_state ) {
  650. case BCHAN_CLEANED:
  651. cb_log(5, stack->port, "$$$ Already cleaned up bc with stid :%x\n", bc->b_stid);
  652. return -1;
  653. default:
  654. break;
  655. }
  656. cb_log(2, stack->port, "$$$ Cleaning up bc with stid :%x pid:%d\n", bc->b_stid, bc->pid);
  657. manager_ec_disable(bc);
  658. manager_bchannel_deactivate(bc);
  659. mISDN_write_frame(stack->midev, buff, bc->layer_id|FLG_MSG_TARGET|FLG_MSG_DOWN, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
  660. bc->b_stid = 0;
  661. bc_state_change(bc, BCHAN_CLEANED);
  662. return ret;
  663. }
  664. static void clear_l3(struct misdn_stack *stack)
  665. {
  666. int i;
  667. if (global_state == MISDN_INITIALIZED) {
  668. for (i = 0; i <= stack->b_num; ++i) {
  669. cb_event(EVENT_CLEANUP, &stack->bc[i], NULL);
  670. empty_bc(&stack->bc[i]);
  671. clean_up_bc(&stack->bc[i]);
  672. empty_chan_in_stack(stack, i + 1);
  673. stack->bc[i].in_use = 0;
  674. }
  675. #if defined(AST_MISDN_ENHANCEMENTS)
  676. for (i = MAX_BCHANS + 1; i < ARRAY_LEN(stack->bc); ++i) {
  677. empty_bc(&stack->bc[i]);
  678. empty_chan_in_stack(stack, i + 1);
  679. stack->bc[i].in_use = 0;
  680. }
  681. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  682. }
  683. }
  684. static int new_te_id = 0;
  685. static int misdn_lib_get_l1_down(struct misdn_stack *stack)
  686. {
  687. /* Pull Up L1 */
  688. iframe_t act;
  689. act.prim = PH_DEACTIVATE | REQUEST;
  690. act.addr = stack->upper_id | FLG_MSG_DOWN;
  691. act.dinfo = 0;
  692. act.len = 0;
  693. cb_log(1, stack->port, "SENDING PH_DEACTIVATE | REQ\n");
  694. return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
  695. }
  696. static int misdn_lib_get_l2_down(struct misdn_stack *stack)
  697. {
  698. if (stack->ptp && stack->nt) {
  699. msg_t *dmsg;
  700. /* L2 */
  701. dmsg = create_l2msg(DL_RELEASE| REQUEST, 0, 0);
  702. pthread_mutex_lock(&stack->nstlock);
  703. if (stack->nst.manager_l3(&stack->nst, dmsg))
  704. free_msg(dmsg);
  705. pthread_mutex_unlock(&stack->nstlock);
  706. } else if (!stack->nt) {
  707. iframe_t act;
  708. act.prim = DL_RELEASE| REQUEST;
  709. act.addr = (stack->upper_id |FLG_MSG_DOWN) ;
  710. act.dinfo = 0;
  711. act.len = 0;
  712. return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
  713. }
  714. /* cannot deestablish L2 for NT PTMP to unknown TE TEIs */
  715. return 0;
  716. }
  717. static int misdn_lib_get_l1_up(struct misdn_stack *stack)
  718. {
  719. /* Pull Up L1 */
  720. iframe_t act;
  721. act.prim = PH_ACTIVATE | REQUEST;
  722. act.addr = stack->upper_id | FLG_MSG_DOWN;
  723. act.dinfo = 0;
  724. act.len = 0;
  725. cb_log(1, stack->port, "SENDING PH_ACTIVATE | REQ\n");
  726. return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
  727. }
  728. int misdn_lib_get_l2_up(struct misdn_stack *stack)
  729. {
  730. if (stack->ptp && stack->nt) {
  731. msg_t *dmsg;
  732. /* L2 */
  733. dmsg = create_l2msg(DL_ESTABLISH | REQUEST, 0, 0);
  734. pthread_mutex_lock(&stack->nstlock);
  735. if (stack->nst.manager_l3(&stack->nst, dmsg))
  736. free_msg(dmsg);
  737. pthread_mutex_unlock(&stack->nstlock);
  738. } else if (!stack->nt) {
  739. iframe_t act;
  740. act.prim = DL_ESTABLISH | REQUEST;
  741. act.addr = (stack->upper_id |FLG_MSG_DOWN) ;
  742. act.dinfo = 0;
  743. act.len = 0;
  744. return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
  745. }
  746. /* cannot establish L2 for NT PTMP to unknown TE TEIs */
  747. return 0;
  748. }
  749. static int misdn_lib_get_short_status(struct misdn_stack *stack)
  750. {
  751. iframe_t act;
  752. act.prim = MGR_SHORTSTATUS | REQUEST;
  753. act.addr = (stack->upper_id | MSG_BROADCAST) ;
  754. act.dinfo = SSTATUS_BROADCAST_BIT | SSTATUS_ALL;
  755. act.len = 0;
  756. return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
  757. }
  758. static int create_process(int midev, struct misdn_bchannel *bc)
  759. {
  760. iframe_t ncr;
  761. int l3_id;
  762. int proc_id;
  763. struct misdn_stack *stack;
  764. stack = get_stack_by_bc(bc);
  765. if (stack->nt) {
  766. if (find_free_chan_in_stack(stack, bc, bc->channel_preselected ? bc->channel : 0, 0) < 0) {
  767. return -1;
  768. }
  769. cb_log(4, stack->port, " --> found channel: %d\n", bc->channel);
  770. for (proc_id = 0; proc_id < MAXPROCS; ++proc_id) {
  771. if (stack->procids[proc_id] == 0) {
  772. break;
  773. }
  774. }
  775. if (proc_id == MAXPROCS) {
  776. cb_log(0, stack->port, "Couldn't Create New ProcId.\n");
  777. return -1;
  778. }
  779. stack->procids[proc_id] = 1;
  780. l3_id = 0xff00 | proc_id;
  781. bc->l3_id = l3_id;
  782. cb_log(3, stack->port, " --> new_l3id %x\n", l3_id);
  783. } else {
  784. if ((stack->pri && stack->ptp) || bc->te_choose_channel) {
  785. /* we know exactly which channels are in use */
  786. if (find_free_chan_in_stack(stack, bc, bc->channel_preselected ? bc->channel : 0, bc->dec) < 0) {
  787. return -1;
  788. }
  789. cb_log(2, stack->port, " --> found channel: %d\n", bc->channel);
  790. } else {
  791. /* other phones could have made a call also on this port (ptmp) */
  792. bc->channel = 0xff;
  793. }
  794. /* if we are in te-mode, we need to create a process first */
  795. if (++new_te_id > 0xffff) {
  796. new_te_id = 0x0001;
  797. }
  798. l3_id = (entity << 16) | new_te_id;
  799. bc->l3_id = l3_id;
  800. cb_log(3, stack->port, "--> new_l3id %x\n", l3_id);
  801. /* send message */
  802. ncr.prim = CC_NEW_CR | REQUEST;
  803. ncr.addr = (stack->upper_id | FLG_MSG_DOWN);
  804. ncr.dinfo = l3_id;
  805. ncr.len = 0;
  806. mISDN_write(midev, &ncr, mISDN_HEADER_LEN + ncr.len, TIMEOUT_1SEC);
  807. }
  808. return l3_id;
  809. }
  810. static int setup_bc(struct misdn_bchannel *bc)
  811. {
  812. unsigned char buff[1025];
  813. int midev;
  814. int channel;
  815. int b_stid;
  816. int i;
  817. mISDN_pid_t pid;
  818. int ret;
  819. struct misdn_stack *stack=get_stack_by_bc(bc);
  820. if (!stack) {
  821. cb_log(0, bc->port, "setup_bc: NO STACK FOUND!!\n");
  822. return -1;
  823. }
  824. midev = stack->midev;
  825. channel = bc->channel - 1 - (bc->channel > 16);
  826. b_stid = stack->b_stids[channel >= 0 ? channel : 0];
  827. switch (bc->bc_state) {
  828. case BCHAN_CLEANED:
  829. break;
  830. default:
  831. cb_log(4, stack->port, "$$$ bc already setup stid :%x (state:%s)\n", b_stid, bc_state2str(bc->bc_state) );
  832. return -1;
  833. }
  834. cb_log(5, stack->port, "$$$ Setting up bc with stid :%x\n", b_stid);
  835. /*check if the b_stid is already initialized*/
  836. for (i=0; i <= stack->b_num; i++) {
  837. if (stack->bc[i].b_stid == b_stid) {
  838. cb_log(0, bc->port, "setup_bc: b_stid:%x already in use !!!\n", b_stid);
  839. return -1;
  840. }
  841. }
  842. if (b_stid <= 0) {
  843. cb_log(0, stack->port," -- Stid <=0 at the moment in channel:%d\n",channel);
  844. bc_state_change(bc,BCHAN_ERROR);
  845. return 1;
  846. }
  847. bc->b_stid = b_stid;
  848. {
  849. layer_info_t li;
  850. memset(&li, 0, sizeof(li));
  851. li.object_id = -1;
  852. li.extentions = 0;
  853. li.st = bc->b_stid; /* given idx */
  854. #define MISDN_DSP
  855. #ifndef MISDN_DSP
  856. bc->nodsp=1;
  857. #endif
  858. if ( bc->hdlc || bc->nodsp) {
  859. cb_log(4, stack->port,"setup_bc: without dsp\n");
  860. {
  861. int l = sizeof(li.name);
  862. strncpy(li.name, "B L3", l);
  863. li.name[l-1] = 0;
  864. }
  865. li.pid.layermask = ISDN_LAYER((3));
  866. li.pid.protocol[3] = ISDN_PID_L3_B_USER;
  867. bc->layer=3;
  868. } else {
  869. cb_log(4, stack->port,"setup_bc: with dsp\n");
  870. {
  871. int l = sizeof(li.name);
  872. strncpy(li.name, "B L4", l);
  873. li.name[l-1] = 0;
  874. }
  875. li.pid.layermask = ISDN_LAYER((4));
  876. li.pid.protocol[4] = ISDN_PID_L4_B_USER;
  877. bc->layer=4;
  878. }
  879. ret = mISDN_new_layer(midev, &li);
  880. if (ret ) {
  881. cb_log(0, stack->port,"New Layer Err: %d %s\n",ret,strerror(errno));
  882. bc_state_change(bc,BCHAN_ERROR);
  883. return(-EINVAL);
  884. }
  885. bc->layer_id = li.id;
  886. }
  887. memset(&pid, 0, sizeof(pid));
  888. cb_log(4, stack->port," --> Channel is %d\n", bc->channel);
  889. if (bc->nodsp && !bc->hdlc) {
  890. cb_log(2, stack->port," --> TRANSPARENT Mode (no DSP, no HDLC)\n");
  891. pid.protocol[1] = ISDN_PID_L1_B_64TRANS;
  892. pid.protocol[2] = ISDN_PID_L2_B_TRANS;
  893. pid.protocol[3] = ISDN_PID_L3_B_USER;
  894. pid.layermask = ISDN_LAYER((1)) | ISDN_LAYER((2)) | ISDN_LAYER((3));
  895. } else if ( bc->hdlc ) {
  896. cb_log(2, stack->port," --> HDLC Mode\n");
  897. pid.protocol[1] = ISDN_PID_L1_B_64HDLC ;
  898. pid.protocol[2] = ISDN_PID_L2_B_TRANS ;
  899. pid.protocol[3] = ISDN_PID_L3_B_USER;
  900. pid.layermask = ISDN_LAYER((1)) | ISDN_LAYER((2)) | ISDN_LAYER((3)) ;
  901. } else {
  902. cb_log(2, stack->port," --> TRANSPARENT Mode\n");
  903. pid.protocol[1] = ISDN_PID_L1_B_64TRANS;
  904. pid.protocol[2] = ISDN_PID_L2_B_TRANS;
  905. pid.protocol[3] = ISDN_PID_L3_B_DSP;
  906. pid.protocol[4] = ISDN_PID_L4_B_USER;
  907. pid.layermask = ISDN_LAYER((1)) | ISDN_LAYER((2)) | ISDN_LAYER((3)) | ISDN_LAYER((4));
  908. }
  909. ret = mISDN_set_stack(midev, bc->b_stid, &pid);
  910. if (ret){
  911. cb_log(0, stack->port,"$$$ Set Stack Err: %d %s\n",ret,strerror(errno));
  912. mISDN_write_frame(midev, buff, bc->layer_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
  913. bc_state_change(bc,BCHAN_ERROR);
  914. cb_event(EVENT_BCHAN_ERROR, bc, glob_mgr->user_data);
  915. return(-EINVAL);
  916. }
  917. ret = mISDN_get_setstack_ind(midev, bc->layer_id);
  918. if (ret) {
  919. cb_log(0, stack->port,"$$$ Set StackIND Err: %d %s\n",ret,strerror(errno));
  920. mISDN_write_frame(midev, buff, bc->layer_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
  921. bc_state_change(bc,BCHAN_ERROR);
  922. cb_event(EVENT_BCHAN_ERROR, bc, glob_mgr->user_data);
  923. return(-EINVAL);
  924. }
  925. ret = mISDN_get_layerid(midev, bc->b_stid, bc->layer) ;
  926. bc->addr = ret>0? ret : 0;
  927. if (!bc->addr) {
  928. cb_log(0, stack->port,"$$$ Get Layerid Err: %d %s\n",ret,strerror(errno));
  929. mISDN_write_frame(midev, buff, bc->layer_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
  930. bc_state_change(bc,BCHAN_ERROR);
  931. cb_event(EVENT_BCHAN_ERROR, bc, glob_mgr->user_data);
  932. return (-EINVAL);
  933. }
  934. manager_bchannel_activate(bc);
  935. bc_state_change(bc,BCHAN_ACTIVATED);
  936. return 0;
  937. }
  938. /** IFACE **/
  939. static int init_bc(struct misdn_stack *stack, struct misdn_bchannel *bc, int midev, int port, int bidx)
  940. {
  941. if (!bc) {
  942. return -1;
  943. }
  944. cb_log(8, port, "Init.BC %d.\n",bidx);
  945. bc->send_lock = malloc(sizeof(struct send_lock)); /* XXX BUG! memory leak never freed */
  946. if (!bc->send_lock) {
  947. return -1;
  948. }
  949. pthread_mutex_init(&bc->send_lock->lock, NULL);
  950. empty_bc(bc);
  951. bc->port=stack->port;
  952. bc_state_change(bc, BCHAN_CLEANED);
  953. bc->nt=stack->nt?1:0;
  954. bc->pri=stack->pri;
  955. {
  956. ibuffer_t* ibuf= init_ibuffer(MISDN_IBUF_SIZE);
  957. if (!ibuf) return -1;
  958. clear_ibuffer( ibuf);
  959. ibuf->rsem=malloc(sizeof(sem_t));
  960. if (!ibuf->rsem) {
  961. return -1;
  962. }
  963. bc->astbuf=ibuf;
  964. if (sem_init(ibuf->rsem,1,0)<0)
  965. sem_init(ibuf->rsem,0,0);
  966. }
  967. #if 0 /* This code does not seem to do anything useful */
  968. if (bidx <= stack->b_num) {
  969. unsigned char buff[1025];
  970. iframe_t *frm = (iframe_t *) buff;
  971. stack_info_t *stinf;
  972. int ret;
  973. ret = mISDN_get_stack_info(midev, stack->port, buff, sizeof(buff));
  974. if (ret < 0) {
  975. cb_log(0, port, "%s: Cannot get stack info for this port. (ret=%d)\n", __FUNCTION__, ret);
  976. return -1;
  977. }
  978. stinf = (stack_info_t *)&frm->data.p;
  979. cb_log(8, port, " --> Child %x\n",stinf->child[bidx]);
  980. }
  981. #endif
  982. return 0;
  983. }
  984. static struct misdn_stack *stack_init(int midev, int port, int ptp)
  985. {
  986. int ret;
  987. unsigned char buff[1025];
  988. iframe_t *frm = (iframe_t *)buff;
  989. stack_info_t *stinf;
  990. struct misdn_stack *stack;
  991. int i;
  992. layer_info_t li;
  993. stack = calloc(1, sizeof(struct misdn_stack));
  994. if (!stack) {
  995. return NULL;
  996. }
  997. cb_log(8, port, "Init. Stack.\n");
  998. stack->port=port;
  999. stack->midev=midev;
  1000. stack->ptp=ptp;
  1001. stack->holding=NULL;
  1002. stack->pri=0;
  1003. msg_queue_init(&stack->downqueue);
  1004. pthread_mutex_init(&stack->st_lock, NULL);
  1005. /* query port's requirements */
  1006. ret = mISDN_get_stack_info(midev, port, buff, sizeof(buff));
  1007. if (ret < 0) {
  1008. cb_log(0, port, "%s: Cannot get stack info for this port. (ret=%d)\n", __FUNCTION__, ret);
  1009. free(stack);
  1010. return(NULL);
  1011. }
  1012. stinf = (stack_info_t *)&frm->data.p;
  1013. stack->d_stid = stinf->id;
  1014. stack->b_num = stinf->childcnt;
  1015. for (i=0; i<=stinf->childcnt; i++)
  1016. stack->b_stids[i] = stinf->child[i];
  1017. switch(stinf->pid.protocol[0] & ~ISDN_PID_FEATURE_MASK) {
  1018. case ISDN_PID_L0_TE_S0:
  1019. cb_log(8, port, "TE Stack\n");
  1020. stack->nt=0;
  1021. break;
  1022. case ISDN_PID_L0_NT_S0:
  1023. cb_log(8, port, "NT Stack\n");
  1024. stack->nt=1;
  1025. break;
  1026. case ISDN_PID_L0_TE_E1:
  1027. cb_log(8, port, "TE S2M Stack\n");
  1028. stack->nt=0;
  1029. stack->pri=1;
  1030. break;
  1031. case ISDN_PID_L0_NT_E1:
  1032. cb_log(8, port, "NT S2M Stack\n");
  1033. stack->nt=1;
  1034. stack->pri=1;
  1035. break;
  1036. default:
  1037. cb_log(0, port, "this is a unknown port type 0x%08x\n", stinf->pid.protocol[0]);
  1038. }
  1039. if (!stack->nt) {
  1040. if (stinf->pid.protocol[2] & ISDN_PID_L2_DF_PTP ) {
  1041. stack->ptp = 1;
  1042. } else {
  1043. stack->ptp = 0;
  1044. }
  1045. }
  1046. {
  1047. int ret;
  1048. int nt=stack->nt;
  1049. memset(&li, 0, sizeof(li));
  1050. {
  1051. int l = sizeof(li.name);
  1052. strncpy(li.name,nt?"net l2":"user l4", l);
  1053. li.name[l-1] = 0;
  1054. }
  1055. li.object_id = -1;
  1056. li.extentions = 0;
  1057. li.pid.protocol[nt?2:4] = nt?ISDN_PID_L2_LAPD_NET:ISDN_PID_L4_CAPI20;
  1058. li.pid.layermask = ISDN_LAYER((nt?2:4));
  1059. li.st = stack->d_stid;
  1060. ret = mISDN_new_layer(midev, &li);
  1061. if (ret) {
  1062. cb_log(0, port, "%s: Cannot add layer %d to this port.\n", __FUNCTION__, nt?2:4);
  1063. free(stack);
  1064. return(NULL);
  1065. }
  1066. ret = mISDN_register_layer(midev, stack->d_stid, li.id);
  1067. if (ret)
  1068. {
  1069. cb_log(0,port,"Cannot register layer %d of this port.\n", nt?2:4);
  1070. free(stack);
  1071. return(NULL);
  1072. }
  1073. stack->lower_id = mISDN_get_layerid(midev, stack->d_stid, nt?1:3);
  1074. if (stack->lower_id < 0) {
  1075. cb_log(0, port, "%s: Cannot get layer(%d) id of this port.\n", __FUNCTION__, nt?1:3);
  1076. free(stack);
  1077. return(NULL);
  1078. }
  1079. stack->upper_id = mISDN_get_layerid(midev, stack->d_stid, nt?2:4);
  1080. if (stack->upper_id < 0) {
  1081. cb_log(0, port, "%s: Cannot get layer(%d) id of this port.\n", __FUNCTION__, nt?2:4);
  1082. free(stack);
  1083. return(NULL);
  1084. }
  1085. /* create nst (nt-mode only) */
  1086. if (nt) {
  1087. memset(&stack->nst, 0, sizeof(net_stack_t));
  1088. memset(&stack->mgr, 0, sizeof(manager_t));
  1089. stack->mgr.nst = &stack->nst;
  1090. stack->nst.manager = &stack->mgr;
  1091. stack->nst.l3_manager = handle_event_nt;
  1092. stack->nst.device = midev;
  1093. stack->nst.cardnr = port;
  1094. stack->nst.d_stid = stack->d_stid;
  1095. stack->nst.feature = FEATURE_NET_HOLD;
  1096. if (stack->ptp)
  1097. stack->nst.feature |= FEATURE_NET_PTP;
  1098. if (stack->pri)
  1099. stack->nst.feature |= FEATURE_NET_CRLEN2 | FEATURE_NET_EXTCID;
  1100. stack->nst.l1_id = stack->lower_id; /* never used */
  1101. stack->nst.l2_id = stack->upper_id;
  1102. msg_queue_init(&stack->nst.down_queue);
  1103. pthread_mutex_init(&stack->nstlock, NULL);
  1104. Isdnl2Init(&stack->nst);
  1105. Isdnl3Init(&stack->nst);
  1106. }
  1107. stack->l1link=0;
  1108. stack->l2link=0;
  1109. #if 0
  1110. if (!stack->nt) {
  1111. misdn_lib_get_short_status(stack);
  1112. } else {
  1113. misdn_lib_get_l1_up(stack);
  1114. if (!stack->ptp) misdn_lib_get_l1_up(stack);
  1115. misdn_lib_get_l2_up(stack);
  1116. }
  1117. #endif
  1118. misdn_lib_get_short_status(stack);
  1119. misdn_lib_get_l1_up(stack);
  1120. /* handle_l1 will start L2 for NT. */
  1121. if (!stack->nt) {
  1122. misdn_lib_get_l2_up(stack);
  1123. }
  1124. }
  1125. cb_log(8, port, "stack_init: lowerId:%x upperId:%x\n", stack->lower_id, stack->upper_id);
  1126. return stack;
  1127. }
  1128. static void stack_destroy(struct misdn_stack *stack)
  1129. {
  1130. char buf[1024];
  1131. if (!stack) return;
  1132. if (stack->nt) {
  1133. pthread_mutex_destroy(&stack->nstlock);
  1134. cleanup_Isdnl2(&stack->nst);
  1135. cleanup_Isdnl3(&stack->nst);
  1136. }
  1137. if (stack->upper_id)
  1138. mISDN_write_frame(stack->midev, buf, stack->upper_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
  1139. pthread_mutex_destroy(&stack->st_lock);
  1140. }
  1141. static struct misdn_stack * find_stack_by_addr(int addr)
  1142. {
  1143. struct misdn_stack *stack;
  1144. for (stack = glob_mgr->stack_list; stack; stack = stack->next) {
  1145. if ((stack->upper_id & STACK_ID_MASK) == (addr & STACK_ID_MASK)) {
  1146. /* Found the stack */
  1147. break;
  1148. }
  1149. }
  1150. return stack;
  1151. }
  1152. static struct misdn_stack *find_stack_by_port(int port)
  1153. {
  1154. struct misdn_stack *stack;
  1155. for (stack = glob_mgr->stack_list; stack; stack = stack->next) {
  1156. if (stack->port == port) {
  1157. /* Found the stack */
  1158. break;
  1159. }
  1160. }
  1161. return stack;
  1162. }
  1163. static struct misdn_stack *find_stack_by_mgr(manager_t *mgr_nt)
  1164. {
  1165. struct misdn_stack *stack;
  1166. for (stack = glob_mgr->stack_list; stack; stack = stack->next) {
  1167. if (&stack->mgr == mgr_nt) {
  1168. /* Found the stack */
  1169. break;
  1170. }
  1171. }
  1172. return stack;
  1173. }
  1174. static struct misdn_bchannel *find_bc_by_masked_l3id(struct misdn_stack *stack, unsigned long l3id, unsigned long mask)
  1175. {
  1176. int i;
  1177. for (i = 0; i <= stack->b_num; ++i) {
  1178. if (stack->bc[i].in_use && (stack->bc[i].l3_id & mask) == (l3id & mask)) {
  1179. return &stack->bc[i];
  1180. }
  1181. }
  1182. #if defined(AST_MISDN_ENHANCEMENTS)
  1183. /* Search the B channel records for a REGISTER signaling link. */
  1184. for (i = MAX_BCHANS + 1; i < ARRAY_LEN(stack->bc); ++i) {
  1185. if (stack->bc[i].in_use && (stack->bc[i].l3_id & mask) == (l3id & mask)) {
  1186. return &stack->bc[i];
  1187. }
  1188. }
  1189. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  1190. return stack_holder_find(stack, l3id);
  1191. }
  1192. struct misdn_bchannel *find_bc_by_l3id(struct misdn_stack *stack, unsigned long l3id)
  1193. {
  1194. int i;
  1195. for (i = 0; i <= stack->b_num; ++i) {
  1196. if (stack->bc[i].in_use && stack->bc[i].l3_id == l3id) {
  1197. return &stack->bc[i];
  1198. }
  1199. }
  1200. #if defined(AST_MISDN_ENHANCEMENTS)
  1201. /* Search the B channel records for a REGISTER signaling link. */
  1202. for (i = MAX_BCHANS + 1; i < ARRAY_LEN(stack->bc); ++i) {
  1203. if (stack->bc[i].in_use && stack->bc[i].l3_id == l3id) {
  1204. return &stack->bc[i];
  1205. }
  1206. }
  1207. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  1208. return stack_holder_find(stack, l3id);
  1209. }
  1210. static struct misdn_bchannel *find_bc_by_addr(unsigned long addr)
  1211. {
  1212. struct misdn_stack *stack;
  1213. int i;
  1214. for (stack = glob_mgr->stack_list; stack; stack = stack->next) {
  1215. for (i = 0; i <= stack->b_num; i++) {
  1216. if (stack->bc[i].in_use
  1217. && ((stack->bc[i].addr & STACK_ID_MASK) == (addr & STACK_ID_MASK)
  1218. || stack->bc[i].layer_id == addr)) {
  1219. return &stack->bc[i];
  1220. }
  1221. }
  1222. }
  1223. return NULL;
  1224. }
  1225. static struct misdn_bchannel *find_bc_by_confid(unsigned long confid)
  1226. {
  1227. struct misdn_stack *stack;
  1228. int i;
  1229. for (stack = glob_mgr->stack_list; stack; stack = stack->next) {
  1230. for (i = 0; i <= stack->b_num; i++) {
  1231. if (stack->bc[i].in_use && stack->bc[i].conf_id == confid) {
  1232. return &stack->bc[i];
  1233. }
  1234. }
  1235. }
  1236. return NULL;
  1237. }
  1238. static int handle_event_te(struct misdn_bchannel *bc, enum event_e event, iframe_t *frm)
  1239. {
  1240. struct misdn_stack *stack=get_stack_by_bc(bc);
  1241. switch (event) {
  1242. case EVENT_CONNECT_ACKNOWLEDGE:
  1243. setup_bc(bc);
  1244. if ( *bc->crypt_key ) {
  1245. cb_log(4, stack->port,
  1246. "ENABLING BLOWFISH channel:%d caller%d:\"%s\" <%s> dialed%d:%s\n",
  1247. bc->channel,
  1248. bc->caller.number_type,
  1249. bc->caller.name,
  1250. bc->caller.number,
  1251. bc->dialed.number_type,
  1252. bc->dialed.number);
  1253. manager_ph_control_block(bc, BF_ENABLE_KEY, bc->crypt_key, strlen(bc->crypt_key) );
  1254. }
  1255. if (misdn_cap_is_speech(bc->capability)) {
  1256. if ( !bc->nodsp) manager_ph_control(bc, DTMF_TONE_START, 0);
  1257. manager_ec_enable(bc);
  1258. if ( bc->txgain != 0 ) {
  1259. cb_log(4, stack->port, "--> Changing txgain to %d\n", bc->txgain);
  1260. manager_ph_control(bc, VOL_CHANGE_TX, bc->txgain);
  1261. }
  1262. if ( bc->rxgain != 0 ) {
  1263. cb_log(4, stack->port, "--> Changing rxgain to %d\n", bc->rxgain);
  1264. manager_ph_control(bc, VOL_CHANGE_RX, bc->rxgain);
  1265. }
  1266. }
  1267. break;
  1268. case EVENT_CONNECT:
  1269. if ( *bc->crypt_key ) {
  1270. cb_log(4, stack->port,
  1271. "ENABLING BLOWFISH channel:%d caller%d:\"%s\" <%s> dialed%d:%s\n",
  1272. bc->channel,
  1273. bc->caller.number_type,
  1274. bc->caller.name,
  1275. bc->caller.number,
  1276. bc->dialed.number_type,
  1277. bc->dialed.number);
  1278. manager_ph_control_block(bc, BF_ENABLE_KEY, bc->crypt_key, strlen(bc->crypt_key) );
  1279. }
  1280. case EVENT_ALERTING:
  1281. case EVENT_PROGRESS:
  1282. case EVENT_PROCEEDING:
  1283. case EVENT_SETUP_ACKNOWLEDGE:
  1284. case EVENT_SETUP:
  1285. {
  1286. if (bc->channel == 0xff || bc->channel<=0)
  1287. bc->channel=0;
  1288. if (find_free_chan_in_stack(stack, bc, bc->channel, 0)<0){
  1289. if (!stack->pri && !stack->ptp) {
  1290. bc->cw=1;
  1291. break;
  1292. }
  1293. if (!bc->channel) {
  1294. cb_log(0, stack->port, "Any Channel Requested, but we have no more!!\n");
  1295. } else {
  1296. cb_log(0, stack->port,
  1297. "Requested Channel Already in Use releasing this call with cause %d!!!!\n",
  1298. bc->out_cause);
  1299. }
  1300. /* when the channel is already in use, we can't
  1301. * simply clear it, we need to make sure that
  1302. * it will still be marked as in_use in the
  1303. * available channels list.*/
  1304. bc->channel=0;
  1305. misdn_lib_send_event(bc,EVENT_RELEASE_COMPLETE);
  1306. return -1;
  1307. }
  1308. if (event != EVENT_SETUP) {
  1309. setup_bc(bc);
  1310. }
  1311. break;
  1312. }
  1313. case EVENT_RELEASE_COMPLETE:
  1314. case EVENT_RELEASE:
  1315. break;
  1316. default:
  1317. break;
  1318. }
  1319. return 0;
  1320. }
  1321. static int handle_cr ( struct misdn_stack *stack, iframe_t *frm)
  1322. {
  1323. struct misdn_bchannel dummybc;
  1324. struct misdn_bchannel *bc;
  1325. int channel;
  1326. if (!stack) return -1;
  1327. switch (frm->prim) {
  1328. case CC_NEW_CR|INDICATION:
  1329. cb_log(7, stack->port, " --> lib: NEW_CR Ind with l3id:%x on this port.\n",frm->dinfo);
  1330. bc = misdn_lib_get_free_bc(stack->port, 0, 1, 0);
  1331. if (!bc) {
  1332. cb_log(0, stack->port, " --> !! lib: No free channel!\n");
  1333. return -1;
  1334. }
  1335. cb_log(7, stack->port, " --> new_process: New L3Id: %x\n",frm->dinfo);
  1336. bc->l3_id=frm->dinfo;
  1337. return 1;
  1338. case CC_NEW_CR|CONFIRM:
  1339. return 1;
  1340. case CC_NEW_CR|REQUEST:
  1341. return 1;
  1342. case CC_RELEASE_CR|REQUEST:
  1343. return 1;
  1344. case CC_RELEASE_CR|CONFIRM:
  1345. break;
  1346. case CC_RELEASE_CR|INDICATION:
  1347. cb_log(4, stack->port, " --> lib: RELEASE_CR Ind with l3id:%x\n", frm->dinfo);
  1348. bc = find_bc_by_l3id(stack, frm->dinfo);
  1349. if (!bc) {
  1350. cb_log(4, stack->port, " --> Didn't find BC so temporarily creating dummy BC (l3id:%x) on this port.\n", frm->dinfo);
  1351. misdn_make_dummy(&dummybc, stack->port, frm->dinfo, stack->nt, 0);
  1352. bc = &dummybc;
  1353. }
  1354. channel = bc->channel;
  1355. cb_log(4, stack->port, " --> lib: CLEANING UP l3id: %x\n", frm->dinfo);
  1356. /* bc->pid = 0; */
  1357. bc->need_disconnect = 0;
  1358. bc->need_release = 0;
  1359. bc->need_release_complete = 0;
  1360. cb_event(EVENT_CLEANUP, bc, glob_mgr->user_data);
  1361. empty_bc(bc);
  1362. clean_up_bc(bc);
  1363. if (channel > 0)
  1364. empty_chan_in_stack(stack, channel);
  1365. bc->in_use = 0;
  1366. dump_chan_list(stack);
  1367. if (bc->stack_holder) {
  1368. cb_log(4, stack->port, "REMOVING Holder\n");
  1369. stack_holder_remove(stack, bc);
  1370. free(bc);
  1371. }
  1372. return 1;
  1373. default:
  1374. break;
  1375. }
  1376. return 0;
  1377. }
  1378. /* Empties bc if it's reserved (no SETUP out yet) */
  1379. void misdn_lib_release(struct misdn_bchannel *bc)
  1380. {
  1381. int channel;
  1382. struct misdn_stack *stack=get_stack_by_bc(bc);
  1383. if (!stack) {
  1384. cb_log(1,0,"misdn_release: No Stack found\n");
  1385. return;
  1386. }
  1387. channel = bc->channel;
  1388. empty_bc(bc);
  1389. clean_up_bc(bc);
  1390. if (channel > 0) {
  1391. empty_chan_in_stack(stack, channel);
  1392. }
  1393. bc->in_use=0;
  1394. }
  1395. int misdn_lib_get_port_up (int port)
  1396. { /* Pull Up L1 */
  1397. struct misdn_stack *stack;
  1398. for (stack=glob_mgr->stack_list;
  1399. stack;
  1400. stack=stack->next) {
  1401. if (stack->port == port) {
  1402. if (!stack->l1link)
  1403. misdn_lib_get_l1_up(stack);
  1404. /* handle_l1 will start L2 for NT. */
  1405. if (!stack->l2link && !stack->nt) {
  1406. misdn_lib_get_l2_up(stack);
  1407. }
  1408. return 0;
  1409. }
  1410. }
  1411. return 0;
  1412. }
  1413. int misdn_lib_get_port_down (int port)
  1414. { /* Pull Down L1 */
  1415. struct misdn_stack *stack;
  1416. for (stack=glob_mgr->stack_list;
  1417. stack;
  1418. stack=stack->next) {
  1419. if (stack->port == port) {
  1420. if (stack->l2link)
  1421. misdn_lib_get_l2_down(stack);
  1422. misdn_lib_get_l1_down(stack);
  1423. return 0;
  1424. }
  1425. }
  1426. return 0;
  1427. }
  1428. int misdn_lib_port_up(int port, int check)
  1429. {
  1430. struct misdn_stack *stack;
  1431. for (stack=glob_mgr->stack_list;
  1432. stack;
  1433. stack=stack->next) {
  1434. if (stack->port == port) {
  1435. if (stack->blocked) {
  1436. cb_log(0,port, "Port Blocked:%d L2:%d L1:%d\n", stack->blocked, stack->l2link, stack->l1link);
  1437. return -1;
  1438. }
  1439. if (stack->ptp ) {
  1440. if (stack->l1link && stack->l2link) {
  1441. return 1;
  1442. } else {
  1443. cb_log(1,port, "Port Down L2:%d L1:%d\n",
  1444. stack->l2link, stack->l1link);
  1445. return 0;
  1446. }
  1447. } else {
  1448. if ( !check || stack->l1link )
  1449. return 1;
  1450. else {
  1451. cb_log(1,port, "Port down PMP\n");
  1452. return 0;
  1453. }
  1454. }
  1455. }
  1456. }
  1457. return -1;
  1458. }
  1459. static int release_cr(struct misdn_stack *stack, mISDNuser_head_t *hh)
  1460. {
  1461. struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
  1462. struct misdn_bchannel dummybc;
  1463. iframe_t frm; /* fake te frm to remove callref from global callreflist */
  1464. frm.dinfo = hh->dinfo;
  1465. frm.addr=stack->upper_id | FLG_MSG_DOWN;
  1466. frm.prim = CC_RELEASE_CR|INDICATION;
  1467. cb_log(4, stack->port, " --> CC_RELEASE_CR: Faking Release_cr for %x l3id:%x\n",frm.addr, frm.dinfo);
  1468. /** removing procid **/
  1469. if (!bc) {
  1470. cb_log(4, stack->port, " --> Didn't find BC so temporarily creating dummy BC (l3id:%x) on this port.\n", hh->dinfo);
  1471. misdn_make_dummy(&dummybc, stack->port, hh->dinfo, stack->nt, 0);
  1472. bc=&dummybc;
  1473. }
  1474. if ((bc->l3_id & 0xff00) == 0xff00) {
  1475. cb_log(4, stack->port, " --> Removing Process Id:%x on this port.\n", bc->l3_id & 0xff);
  1476. stack->procids[bc->l3_id & 0xff] = 0;
  1477. }
  1478. if (handle_cr(stack, &frm)<0) {
  1479. }
  1480. return 0 ;
  1481. }
  1482. static int handle_event_nt(void *dat, void *arg)
  1483. {
  1484. struct misdn_bchannel dummybc;
  1485. struct misdn_bchannel *bc;
  1486. manager_t *mgr = (manager_t *)dat;
  1487. msg_t *msg = (msg_t *)arg;
  1488. msg_t *dmsg;
  1489. mISDNuser_head_t *hh;
  1490. struct misdn_stack *stack;
  1491. enum event_e event;
  1492. int reject=0;
  1493. int l3id;
  1494. int channel;
  1495. int tmpcause;
  1496. if (!msg || !mgr)
  1497. return(-EINVAL);
  1498. stack = find_stack_by_mgr(mgr);
  1499. hh=(mISDNuser_head_t*)msg->data;
  1500. /*
  1501. * When we are called from the mISDNuser lib, the nstlock is held and it
  1502. * must be held when we return. We unlock here because the lib may be
  1503. * entered again recursively.
  1504. */
  1505. pthread_mutex_unlock(&stack->nstlock);
  1506. cb_log(5, stack->port, " --> lib: prim %x dinfo %x\n",hh->prim, hh->dinfo);
  1507. switch(hh->prim) {
  1508. case CC_RETRIEVE|INDICATION:
  1509. {
  1510. struct misdn_bchannel *hold_bc;
  1511. iframe_t frm; /* fake te frm to add callref to global callreflist */
  1512. frm.dinfo = hh->dinfo;
  1513. frm.addr=stack->upper_id | FLG_MSG_DOWN;
  1514. frm.prim = CC_NEW_CR|INDICATION;
  1515. if (handle_cr( stack, &frm)< 0) {
  1516. goto ERR_NO_CHANNEL;
  1517. }
  1518. bc = find_bc_by_l3id(stack, hh->dinfo);
  1519. hold_bc = stack_holder_find(stack, bc->l3_id);
  1520. cb_log(4, stack->port, "bc_l3id:%x holded_bc_l3id:%x\n",bc->l3_id, hold_bc->l3_id);
  1521. if (hold_bc) {
  1522. cb_log(4, stack->port, "REMOVING Holder\n");
  1523. /* swap the backup to our new channel back */
  1524. stack_holder_remove(stack, hold_bc);
  1525. memcpy(bc, hold_bc, sizeof(*bc));
  1526. free(hold_bc);
  1527. bc->holded=0;
  1528. bc->b_stid=0;
  1529. }
  1530. break;
  1531. }
  1532. case CC_SETUP | CONFIRM:
  1533. l3id = *((int *) (msg->data + mISDNUSER_HEAD_SIZE));
  1534. cb_log(4, stack->port, " --> lib: Event_ind:SETUP CONFIRM [NT] : new L3ID is %x\n", l3id);
  1535. bc = find_bc_by_l3id(stack, hh->dinfo);
  1536. if (bc) {
  1537. cb_log (2, bc->port, "I IND :CC_SETUP|CONFIRM: old l3id:%x new l3id:%x\n", bc->l3_id, l3id);
  1538. bc->l3_id = l3id;
  1539. cb_event(EVENT_NEW_L3ID, bc, glob_mgr->user_data);
  1540. } else {
  1541. cb_log(4, stack->port, "Bc Not found (after SETUP CONFIRM)\n");
  1542. }
  1543. free_msg(msg);
  1544. pthread_mutex_lock(&stack->nstlock);
  1545. return 0;
  1546. case CC_SETUP | INDICATION:
  1547. bc = misdn_lib_get_free_bc(stack->port, 0, 1, 0);
  1548. if (!bc) {
  1549. goto ERR_NO_CHANNEL;
  1550. }
  1551. cb_log(4, stack->port, " --> new_process: New L3Id: %x\n",hh->dinfo);
  1552. bc->l3_id=hh->dinfo;
  1553. break;
  1554. #if defined(AST_MISDN_ENHANCEMENTS)
  1555. case CC_REGISTER | CONFIRM:
  1556. l3id = *((int *) (msg->data + mISDNUSER_HEAD_SIZE));
  1557. cb_log(4, stack->port, " --> lib: Event_ind:REGISTER CONFIRM [NT] : new L3ID is %x\n", l3id);
  1558. bc = find_bc_by_l3id(stack, hh->dinfo);
  1559. if (bc) {
  1560. cb_log (2, bc->port, "I IND :CC_REGISTER|CONFIRM: old l3id:%x new l3id:%x\n", bc->l3_id, l3id);
  1561. bc->l3_id = l3id;
  1562. } else {
  1563. cb_log(4, stack->port, "Bc Not found (after REGISTER CONFIRM)\n");
  1564. }
  1565. free_msg(msg);
  1566. pthread_mutex_lock(&stack->nstlock);
  1567. return 0;
  1568. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  1569. #if defined(AST_MISDN_ENHANCEMENTS)
  1570. case CC_REGISTER | INDICATION:
  1571. bc = misdn_lib_get_register_bc(stack->port);
  1572. if (!bc) {
  1573. goto ERR_NO_CHANNEL;
  1574. }
  1575. cb_log(4, stack->port, " --> new_process: New L3Id: %x\n",hh->dinfo);
  1576. bc->l3_id=hh->dinfo;
  1577. break;
  1578. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  1579. case CC_CONNECT_ACKNOWLEDGE|INDICATION:
  1580. break;
  1581. case CC_ALERTING|INDICATION:
  1582. case CC_PROCEEDING|INDICATION:
  1583. case CC_SETUP_ACKNOWLEDGE|INDICATION:
  1584. case CC_CONNECT|INDICATION:
  1585. break;
  1586. case CC_DISCONNECT|INDICATION:
  1587. bc = find_bc_by_l3id(stack, hh->dinfo);
  1588. if (!bc) {
  1589. bc=find_bc_by_masked_l3id(stack, hh->dinfo, 0xffff0000);
  1590. if (bc) {
  1591. int myprocid=bc->l3_id&0x0000ffff;
  1592. hh->dinfo=(hh->dinfo&0xffff0000)|myprocid;
  1593. cb_log(3,stack->port,"Reject dinfo: %x cause:%d\n",hh->dinfo,bc->cause);
  1594. reject=1;
  1595. }
  1596. }
  1597. break;
  1598. case CC_FACILITY|INDICATION:
  1599. bc = find_bc_by_l3id(stack, hh->dinfo);
  1600. if (!bc) {
  1601. bc=find_bc_by_masked_l3id(stack, hh->dinfo, 0xffff0000);
  1602. if (bc) {
  1603. int myprocid=bc->l3_id&0x0000ffff;
  1604. hh->dinfo=(hh->dinfo&0xffff0000)|myprocid;
  1605. cb_log(4,bc->port,"Repaired reject Bug, new dinfo: %x\n",hh->dinfo);
  1606. }
  1607. }
  1608. break;
  1609. case CC_RELEASE_COMPLETE|INDICATION:
  1610. break;
  1611. case CC_SUSPEND|INDICATION:
  1612. cb_log(4, stack->port, " --> Got Suspend, sending Reject for now\n");
  1613. dmsg = create_l3msg(CC_SUSPEND_REJECT | REQUEST,MT_SUSPEND_REJECT, hh->dinfo,sizeof(RELEASE_COMPLETE_t), 1);
  1614. pthread_mutex_lock(&stack->nstlock);
  1615. stack->nst.manager_l3(&stack->nst, dmsg);
  1616. free_msg(msg);
  1617. return 0;
  1618. case CC_RESUME|INDICATION:
  1619. break;
  1620. case CC_RELEASE|CONFIRM:
  1621. bc = find_bc_by_l3id(stack, hh->dinfo);
  1622. if (bc) {
  1623. cb_log(1, stack->port, "CC_RELEASE|CONFIRM (l3id:%x), sending RELEASE_COMPLETE\n", hh->dinfo);
  1624. misdn_lib_send_event(bc, EVENT_RELEASE_COMPLETE);
  1625. }
  1626. break;
  1627. case CC_RELEASE|INDICATION:
  1628. break;
  1629. case CC_RELEASE_CR|INDICATION:
  1630. release_cr(stack, hh);
  1631. free_msg(msg);
  1632. pthread_mutex_lock(&stack->nstlock);
  1633. return 0;
  1634. case CC_NEW_CR|INDICATION:
  1635. /* Got New CR for bchan, for now I handle this one in */
  1636. /* connect_ack, Need to be changed */
  1637. l3id = *((int *) (msg->data + mISDNUSER_HEAD_SIZE));
  1638. bc = find_bc_by_l3id(stack, hh->dinfo);
  1639. if (!bc) {
  1640. cb_log(0, stack->port, " --> In NEW_CR: didn't found bc ??\n");
  1641. pthread_mutex_lock(&stack->nstlock);
  1642. return -1;
  1643. }
  1644. if (((l3id&0xff00)!=0xff00) && ((bc->l3_id&0xff00)==0xff00)) {
  1645. cb_log(4, stack->port, " --> Removing Process Id:%x on this port.\n", 0xff&bc->l3_id);
  1646. stack->procids[bc->l3_id&0xff] = 0 ;
  1647. }
  1648. cb_log(4, stack->port, "lib: Event_ind:CC_NEW_CR : very new L3ID is %x\n",l3id );
  1649. bc->l3_id =l3id;
  1650. if (!bc->is_register_pool) {
  1651. cb_event(EVENT_NEW_L3ID, bc, glob_mgr->user_data);
  1652. }
  1653. free_msg(msg);
  1654. pthread_mutex_lock(&stack->nstlock);
  1655. return 0;
  1656. case DL_ESTABLISH | INDICATION:
  1657. case DL_ESTABLISH | CONFIRM:
  1658. cb_log(3, stack->port, "%% GOT L2 Activate Info.\n");
  1659. if (stack->ptp && stack->l2link) {
  1660. cb_log(0, stack->port, "%% GOT L2 Activate Info. but we're activated already.. this l2 is faulty, blocking port\n");
  1661. cb_event(EVENT_PORT_ALARM, &stack->bc[0], glob_mgr->user_data);
  1662. }
  1663. if (stack->ptp && !stack->restart_sent) {
  1664. /* make sure we restart the interface of the
  1665. * other side */
  1666. stack->restart_sent=1;
  1667. misdn_lib_send_restart(stack->port, -1);
  1668. }
  1669. /* when we get the L2 UP, the L1 is UP definitely too*/
  1670. stack->l2link = 1;
  1671. stack->l2upcnt=0;
  1672. free_msg(msg);
  1673. pthread_mutex_lock(&stack->nstlock);
  1674. return 0;
  1675. case DL_RELEASE | INDICATION:
  1676. case DL_RELEASE | CONFIRM:
  1677. cb_log(3, stack->port, "%% GOT L2 DeActivate Info.\n");
  1678. if (stack->ptp) {
  1679. if (stack->l2upcnt>3) {
  1680. cb_log(0 , stack->port, "!!! Could not Get the L2 up after 3 Attempts!!!\n");
  1681. } else {
  1682. if (stack->l1link) {
  1683. misdn_lib_get_l2_up(stack);
  1684. stack->l2upcnt++;
  1685. }
  1686. }
  1687. }
  1688. stack->l2link = 0;
  1689. free_msg(msg);
  1690. pthread_mutex_lock(&stack->nstlock);
  1691. return 0;
  1692. default:
  1693. break;
  1694. }
  1695. /* Parse Events and fire_up to App. */
  1696. event = isdn_msg_get_event(msgs_g, msg, 1);
  1697. bc = find_bc_by_l3id(stack, hh->dinfo);
  1698. if (!bc) {
  1699. cb_log(4, stack->port, " --> Didn't find BC so temporarily creating dummy BC (l3id:%x).\n", hh->dinfo);
  1700. misdn_make_dummy(&dummybc, stack->port, hh->dinfo, stack->nt, 0);
  1701. bc = &dummybc;
  1702. }
  1703. isdn_msg_parse_event(msgs_g, msg, bc, 1);
  1704. switch (event) {
  1705. case EVENT_SETUP:
  1706. if (bc->channel <= 0 || bc->channel == 0xff) {
  1707. bc->channel = 0;
  1708. }
  1709. if (find_free_chan_in_stack(stack, bc, bc->channel, 0) < 0) {
  1710. goto ERR_NO_CHANNEL;
  1711. }
  1712. break;
  1713. case EVENT_RELEASE:
  1714. case EVENT_RELEASE_COMPLETE:
  1715. channel = bc->channel;
  1716. tmpcause = bc->cause;
  1717. empty_bc(bc);
  1718. bc->cause = tmpcause;
  1719. clean_up_bc(bc);
  1720. if (channel > 0)
  1721. empty_chan_in_stack(stack, channel);
  1722. bc->in_use = 0;
  1723. break;
  1724. default:
  1725. break;
  1726. }
  1727. if(!isdn_get_info(msgs_g, event, 1)) {
  1728. cb_log(4, stack->port, "Unknown Event Ind: prim %x dinfo %x\n", hh->prim, hh->dinfo);
  1729. } else {
  1730. if (reject) {
  1731. switch(bc->cause) {
  1732. case AST_CAUSE_USER_BUSY:
  1733. cb_log(1, stack->port, "Siemens Busy reject..\n");
  1734. break;
  1735. default:
  1736. break;
  1737. }
  1738. }
  1739. cb_event(event, bc, glob_mgr->user_data);
  1740. }
  1741. free_msg(msg);
  1742. pthread_mutex_lock(&stack->nstlock);
  1743. return 0;
  1744. ERR_NO_CHANNEL:
  1745. cb_log(4, stack->port, "Patch from MEIDANIS:Sending RELEASE_COMPLETE %x (No free Chan for you..)\n", hh->dinfo);
  1746. dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST, MT_RELEASE_COMPLETE, hh->dinfo, sizeof(RELEASE_COMPLETE_t), 1);
  1747. pthread_mutex_lock(&stack->nstlock);
  1748. stack->nst.manager_l3(&stack->nst, dmsg);
  1749. free_msg(msg);
  1750. return 0;
  1751. }
  1752. static int handle_timers(msg_t* msg)
  1753. {
  1754. iframe_t *frm= (iframe_t*)msg->data;
  1755. struct misdn_stack *stack;
  1756. /* Timer Stuff */
  1757. switch (frm->prim) {
  1758. case MGR_INITTIMER | CONFIRM:
  1759. case MGR_ADDTIMER | CONFIRM:
  1760. case MGR_DELTIMER | CONFIRM:
  1761. case MGR_REMOVETIMER | CONFIRM:
  1762. free_msg(msg);
  1763. return(1);
  1764. }
  1765. if (frm->prim==(MGR_TIMER | INDICATION) ) {
  1766. for (stack = glob_mgr->stack_list;
  1767. stack;
  1768. stack = stack->next) {
  1769. itimer_t *it;
  1770. if (!stack->nt) continue;
  1771. it = stack->nst.tlist;
  1772. /* find timer */
  1773. for(it=stack->nst.tlist;
  1774. it;
  1775. it=it->next) {
  1776. if (it->id == (int)frm->addr)
  1777. break;
  1778. }
  1779. if (it) {
  1780. mISDN_write_frame(stack->midev, msg->data, frm->addr,
  1781. MGR_TIMER | RESPONSE, 0, 0, NULL, TIMEOUT_1SEC);
  1782. test_and_clear_bit(FLG_TIMER_RUNING, (long unsigned int *)&it->Flags);
  1783. pthread_mutex_lock(&stack->nstlock);
  1784. it->function(it->data);
  1785. pthread_mutex_unlock(&stack->nstlock);
  1786. free_msg(msg);
  1787. return 1;
  1788. }
  1789. }
  1790. cb_log(0, 0, "Timer Msg without Timer ??\n");
  1791. free_msg(msg);
  1792. return 1;
  1793. }
  1794. return 0;
  1795. }
  1796. void misdn_lib_tone_generator_start(struct misdn_bchannel *bc)
  1797. {
  1798. bc->generate_tone=1;
  1799. }
  1800. void misdn_lib_tone_generator_stop(struct misdn_bchannel *bc)
  1801. {
  1802. bc->generate_tone=0;
  1803. }
  1804. static int do_tone(struct misdn_bchannel *bc, int len)
  1805. {
  1806. bc->tone_cnt=len;
  1807. if (bc->generate_tone) {
  1808. cb_event(EVENT_TONE_GENERATE, bc, glob_mgr->user_data);
  1809. if ( !bc->nojitter ) {
  1810. misdn_tx_jitter(bc,len);
  1811. }
  1812. return 1;
  1813. }
  1814. return 0;
  1815. }
  1816. #ifdef MISDN_SAVE_DATA
  1817. static void misdn_save_data(int id, char *p1, int l1, char *p2, int l2)
  1818. {
  1819. char n1[32],n2[32];
  1820. FILE *rx, *tx;
  1821. sprintf(n1,"/tmp/misdn-rx-%d.raw",id);
  1822. sprintf(n2,"/tmp/misdn-tx-%d.raw",id);
  1823. rx = fopen(n1,"a+");
  1824. tx = fopen(n2,"a+");
  1825. if (!rx || !tx) {
  1826. cb_log(0,0,"Couldn't open files: %s\n",strerror(errno));
  1827. if (rx)
  1828. fclose(rx);
  1829. if (tx)
  1830. fclose(tx);
  1831. return ;
  1832. }
  1833. fwrite(p1,1,l1,rx);
  1834. fwrite(p2,1,l2,tx);
  1835. fclose(rx);
  1836. fclose(tx);
  1837. }
  1838. #endif
  1839. void misdn_tx_jitter(struct misdn_bchannel *bc, int len)
  1840. {
  1841. char buf[4096 + mISDN_HEADER_LEN];
  1842. char *data=&buf[mISDN_HEADER_LEN];
  1843. iframe_t *txfrm= (iframe_t*)buf;
  1844. int jlen, r;
  1845. jlen=cb_jb_empty(bc,data,len);
  1846. if (jlen) {
  1847. #ifdef MISDN_SAVE_DATA
  1848. misdn_save_data((bc->port*100+bc->channel), data, jlen, bc->bframe, bc->bframe_len);
  1849. #endif
  1850. flip_buf_bits( data, jlen);
  1851. if (jlen < len) {
  1852. cb_log(1, bc->port, "Jitterbuffer Underrun. Got %d of expected %d\n", jlen, len);
  1853. }
  1854. txfrm->prim = DL_DATA|REQUEST;
  1855. txfrm->dinfo = 0;
  1856. txfrm->addr = bc->addr|FLG_MSG_DOWN; /* | IF_DOWN; */
  1857. txfrm->len =jlen;
  1858. cb_log(9, bc->port, "Transmitting %d samples 2 misdn\n", txfrm->len);
  1859. r=mISDN_write( glob_mgr->midev, buf, txfrm->len + mISDN_HEADER_LEN, 8000 );
  1860. } else {
  1861. #define MISDN_GEN_SILENCE
  1862. #ifdef MISDN_GEN_SILENCE
  1863. int cnt=len/TONE_SILENCE_SIZE;
  1864. int rest=len%TONE_SILENCE_SIZE;
  1865. int i;
  1866. for (i=0; i<cnt; i++) {
  1867. memcpy(data, tone_silence_flip, TONE_SILENCE_SIZE );
  1868. data +=TONE_SILENCE_SIZE;
  1869. }
  1870. if (rest) {
  1871. memcpy(data, tone_silence_flip, rest);
  1872. }
  1873. txfrm->prim = DL_DATA|REQUEST;
  1874. txfrm->dinfo = 0;
  1875. txfrm->addr = bc->addr|FLG_MSG_DOWN; /* | IF_DOWN; */
  1876. txfrm->len =len;
  1877. cb_log(5, bc->port, "Transmitting %d samples of silence to misdn\n", len);
  1878. r=mISDN_write( glob_mgr->midev, buf, txfrm->len + mISDN_HEADER_LEN, 8000 );
  1879. #else
  1880. r = 0;
  1881. #endif
  1882. }
  1883. if (r < 0) {
  1884. cb_log(1, bc->port, "Error in mISDN_write (%s)\n", strerror(errno));
  1885. }
  1886. }
  1887. static int handle_bchan(msg_t *msg)
  1888. {
  1889. iframe_t *frm= (iframe_t*)msg->data;
  1890. struct misdn_bchannel *bc=find_bc_by_addr(frm->addr);
  1891. struct misdn_stack *stack;
  1892. if (!bc) {
  1893. cb_log(1,0,"handle_bchan: BC not found for prim:%x with addr:%x dinfo:%x\n", frm->prim, frm->addr, frm->dinfo);
  1894. return 0 ;
  1895. }
  1896. stack = get_stack_by_bc(bc);
  1897. if (!stack) {
  1898. cb_log(0, bc->port,"handle_bchan: STACK not found for prim:%x with addr:%x dinfo:%x\n", frm->prim, frm->addr, frm->dinfo);
  1899. return 0;
  1900. }
  1901. switch (frm->prim) {
  1902. case MGR_SETSTACK| CONFIRM:
  1903. cb_log(3, stack->port, "BCHAN: MGR_SETSTACK|CONFIRM pid:%d\n",bc->pid);
  1904. break;
  1905. case MGR_SETSTACK| INDICATION:
  1906. cb_log(3, stack->port, "BCHAN: MGR_SETSTACK|IND pid:%d\n",bc->pid);
  1907. break;
  1908. case MGR_DELLAYER| INDICATION:
  1909. cb_log(3, stack->port, "BCHAN: MGR_DELLAYER|IND pid:%d\n",bc->pid);
  1910. break;
  1911. case MGR_DELLAYER| CONFIRM:
  1912. cb_log(3, stack->port, "BCHAN: MGR_DELLAYER|CNF pid:%d\n",bc->pid);
  1913. bc->pid=0;
  1914. bc->addr=0;
  1915. free_msg(msg);
  1916. return 1;
  1917. case PH_ACTIVATE | INDICATION:
  1918. case DL_ESTABLISH | INDICATION:
  1919. cb_log(3, stack->port, "BCHAN: ACT Ind pid:%d\n", bc->pid);
  1920. free_msg(msg);
  1921. return 1;
  1922. case PH_ACTIVATE | CONFIRM:
  1923. case DL_ESTABLISH | CONFIRM:
  1924. cb_log(3, stack->port, "BCHAN: bchan ACT Confirm pid:%d\n",bc->pid);
  1925. free_msg(msg);
  1926. return 1;
  1927. case DL_ESTABLISH | REQUEST:
  1928. {
  1929. char buf[128];
  1930. mISDN_write_frame(stack->midev, buf, bc->addr | FLG_MSG_TARGET | FLG_MSG_DOWN, DL_ESTABLISH | CONFIRM, 0,0, NULL, TIMEOUT_1SEC);
  1931. }
  1932. free_msg(msg);
  1933. return 1;
  1934. case DL_RELEASE|REQUEST:
  1935. {
  1936. char buf[128];
  1937. mISDN_write_frame(stack->midev, buf, bc->addr | FLG_MSG_TARGET | FLG_MSG_DOWN, DL_RELEASE| CONFIRM, 0,0, NULL, TIMEOUT_1SEC);
  1938. }
  1939. free_msg(msg);
  1940. return 1;
  1941. case PH_DEACTIVATE | INDICATION:
  1942. case DL_RELEASE | INDICATION:
  1943. cb_log (3, stack->port, "BCHAN: DeACT Ind pid:%d\n",bc->pid);
  1944. free_msg(msg);
  1945. return 1;
  1946. case PH_DEACTIVATE | CONFIRM:
  1947. case DL_RELEASE | CONFIRM:
  1948. cb_log(3, stack->port, "BCHAN: DeACT Conf pid:%d\n",bc->pid);
  1949. free_msg(msg);
  1950. return 1;
  1951. case PH_CONTROL|INDICATION:
  1952. {
  1953. unsigned int *cont = (unsigned int *) &frm->data.p;
  1954. cb_log(4, stack->port,
  1955. "PH_CONTROL: channel:%d caller%d:\"%s\" <%s> dialed%d:%s \n",
  1956. bc->channel,
  1957. bc->caller.number_type,
  1958. bc->caller.name,
  1959. bc->caller.number,
  1960. bc->dialed.number_type,
  1961. bc->dialed.number);
  1962. if ((*cont & ~DTMF_TONE_MASK) == DTMF_TONE_VAL) {
  1963. int dtmf = *cont & DTMF_TONE_MASK;
  1964. cb_log(4, stack->port, " --> DTMF TONE: %c\n",dtmf);
  1965. bc->dtmf=dtmf;
  1966. cb_event(EVENT_DTMF_TONE, bc, glob_mgr->user_data);
  1967. free_msg(msg);
  1968. return 1;
  1969. }
  1970. if (*cont == BF_REJECT) {
  1971. cb_log(4, stack->port, " --> BF REJECT\n");
  1972. free_msg(msg);
  1973. return 1;
  1974. }
  1975. if (*cont == BF_ACCEPT) {
  1976. cb_log(4, stack->port, " --> BF ACCEPT\n");
  1977. free_msg(msg);
  1978. return 1;
  1979. }
  1980. }
  1981. break;
  1982. case PH_DATA|REQUEST:
  1983. case DL_DATA|REQUEST:
  1984. cb_log(0, stack->port, "DL_DATA REQUEST \n");
  1985. do_tone(bc, 64);
  1986. free_msg(msg);
  1987. return 1;
  1988. case PH_DATA|INDICATION:
  1989. case DL_DATA|INDICATION:
  1990. {
  1991. bc->bframe = (void*)&frm->data.i;
  1992. bc->bframe_len = frm->len;
  1993. /** Anyway flip the bufbits **/
  1994. if ( misdn_cap_is_speech(bc->capability) )
  1995. flip_buf_bits(bc->bframe, bc->bframe_len);
  1996. if (!bc->bframe_len) {
  1997. cb_log(2, stack->port, "DL_DATA INDICATION bc->addr:%x frm->addr:%x\n", bc->addr, frm->addr);
  1998. free_msg(msg);
  1999. return 1;
  2000. }
  2001. if ( (bc->addr&STACK_ID_MASK) != (frm->addr&STACK_ID_MASK) ) {
  2002. cb_log(2, stack->port, "DL_DATA INDICATION bc->addr:%x frm->addr:%x\n", bc->addr, frm->addr);
  2003. free_msg(msg);
  2004. return 1;
  2005. }
  2006. #if MISDN_DEBUG
  2007. cb_log(0, stack->port, "DL_DATA INDICATION Len %d\n", frm->len);
  2008. #endif
  2009. if ( (bc->bc_state == BCHAN_ACTIVATED) && frm->len > 0) {
  2010. int t;
  2011. #ifdef MISDN_B_DEBUG
  2012. cb_log(0,bc->port,"do_tone START\n");
  2013. #endif
  2014. t=do_tone(bc,frm->len);
  2015. #ifdef MISDN_B_DEBUG
  2016. cb_log(0,bc->port,"do_tone STOP (%d)\n",t);
  2017. #endif
  2018. if ( !t ) {
  2019. int i;
  2020. if ( misdn_cap_is_speech(bc->capability)) {
  2021. if ( !bc->nojitter ) {
  2022. #ifdef MISDN_B_DEBUG
  2023. cb_log(0,bc->port,"tx_jitter START\n");
  2024. #endif
  2025. misdn_tx_jitter(bc,frm->len);
  2026. #ifdef MISDN_B_DEBUG
  2027. cb_log(0,bc->port,"tx_jitter STOP\n");
  2028. #endif
  2029. }
  2030. }
  2031. #ifdef MISDN_B_DEBUG
  2032. cb_log(0,bc->port,"EVENT_B_DATA START\n");
  2033. #endif
  2034. i = cb_event(EVENT_BCHAN_DATA, bc, glob_mgr->user_data);
  2035. #ifdef MISDN_B_DEBUG
  2036. cb_log(0,bc->port,"EVENT_B_DATA STOP\n");
  2037. #endif
  2038. if (i<0) {
  2039. cb_log(10,stack->port,"cb_event returned <0\n");
  2040. /*clean_up_bc(bc);*/
  2041. }
  2042. }
  2043. }
  2044. free_msg(msg);
  2045. return 1;
  2046. }
  2047. case PH_CONTROL | CONFIRM:
  2048. cb_log(4, stack->port, "PH_CONTROL|CNF bc->addr:%x\n", frm->addr);
  2049. free_msg(msg);
  2050. return 1;
  2051. case PH_DATA | CONFIRM:
  2052. case DL_DATA|CONFIRM:
  2053. #if MISDN_DEBUG
  2054. cb_log(0, stack->port, "Data confirmed\n");
  2055. #endif
  2056. free_msg(msg);
  2057. return 1;
  2058. case DL_DATA|RESPONSE:
  2059. #if MISDN_DEBUG
  2060. cb_log(0, stack->port, "Data response\n");
  2061. #endif
  2062. break;
  2063. }
  2064. return 0;
  2065. }
  2066. static int handle_frm_nt(msg_t *msg)
  2067. {
  2068. iframe_t *frm= (iframe_t*)msg->data;
  2069. struct misdn_stack *stack;
  2070. int err=0;
  2071. stack=find_stack_by_addr( frm->addr );
  2072. if (!stack || !stack->nt) {
  2073. return 0;
  2074. }
  2075. pthread_mutex_lock(&stack->nstlock);
  2076. if ((err=stack->nst.l1_l2(&stack->nst,msg))) {
  2077. pthread_mutex_unlock(&stack->nstlock);
  2078. if (nt_err_cnt > 0 ) {
  2079. if (nt_err_cnt < 100) {
  2080. nt_err_cnt++;
  2081. cb_log(0, stack->port, "NT Stack sends us error: %d \n", err);
  2082. } else if (nt_err_cnt < 105){
  2083. cb_log(0, stack->port, "NT Stack sends us error: %d over 100 times, so I'll stop this message\n", err);
  2084. nt_err_cnt = - 1;
  2085. }
  2086. }
  2087. free_msg(msg);
  2088. return 1;
  2089. }
  2090. pthread_mutex_unlock(&stack->nstlock);
  2091. return 1;
  2092. }
  2093. static int handle_frm_te(msg_t *msg)
  2094. {
  2095. struct misdn_bchannel dummybc;
  2096. struct misdn_bchannel *bc;
  2097. iframe_t *frm;
  2098. struct misdn_stack *stack;
  2099. enum event_e event;
  2100. enum event_response_e response;
  2101. int ret;
  2102. int channel;
  2103. int tmpcause;
  2104. int tmp_out_cause;
  2105. frm = (iframe_t*) msg->data;
  2106. stack = find_stack_by_addr(frm->addr);
  2107. if (!stack || stack->nt) {
  2108. return 0;
  2109. }
  2110. cb_log(4, stack->port, "handle_frm_te: frm->addr:%x frm->prim:%x\n", frm->addr, frm->prim);
  2111. ret = handle_cr(stack, frm);
  2112. if (ret < 0) {
  2113. cb_log(3, stack->port, "handle_frm_te: handle_cr <0 prim:%x addr:%x\n", frm->prim, frm->addr);
  2114. }
  2115. if (ret) {
  2116. free_msg(msg);
  2117. return 1;
  2118. }
  2119. bc = find_bc_by_l3id(stack, frm->dinfo);
  2120. if (!bc) {
  2121. misdn_make_dummy(&dummybc, stack->port, 0, stack->nt, 0);
  2122. switch (frm->prim) {
  2123. case CC_RESTART | CONFIRM:
  2124. dummybc.l3_id = MISDN_ID_GLOBAL;
  2125. bc = &dummybc;
  2126. break;
  2127. case CC_SETUP | INDICATION:
  2128. dummybc.l3_id = frm->dinfo;
  2129. bc = &dummybc;
  2130. /* set a reasonable cause */
  2131. bc->out_cause = AST_CAUSE_REQUESTED_CHAN_UNAVAIL;
  2132. /* if we want to send something the flag must be set! */
  2133. bc->need_release_complete = 1;
  2134. misdn_lib_send_event(bc, EVENT_RELEASE_COMPLETE);
  2135. free_msg(msg);
  2136. return 1;
  2137. default:
  2138. if (frm->prim == (CC_FACILITY | INDICATION)) {
  2139. cb_log(5, stack->port, " --> Using Dummy BC for FACILITY\n");
  2140. } else {
  2141. cb_log(0, stack->port, " --> Didn't find BC so temporarily creating dummy BC (l3id:%x) on this port.\n", frm->dinfo);
  2142. dummybc.l3_id = frm->dinfo;
  2143. }
  2144. bc = &dummybc;
  2145. break;
  2146. }
  2147. }
  2148. event = isdn_msg_get_event(msgs_g, msg, 0);
  2149. isdn_msg_parse_event(msgs_g, msg, bc, 0);
  2150. /* Preprocess some Events */
  2151. ret = handle_event_te(bc, event, frm);
  2152. if (ret < 0) {
  2153. cb_log(0, stack->port, "couldn't handle event\n");
  2154. free_msg(msg);
  2155. return 1;
  2156. }
  2157. /* shoot up event to App: */
  2158. cb_log(5, stack->port, "lib Got Prim: Addr %x prim %x dinfo %x\n", frm->addr, frm->prim, frm->dinfo);
  2159. if (!isdn_get_info(msgs_g, event, 0)) {
  2160. cb_log(0, stack->port, "Unknown Event Ind: Addr:%x prim %x dinfo %x\n", frm->addr, frm->prim, frm->dinfo);
  2161. response = RESPONSE_OK;
  2162. } else {
  2163. response = cb_event(event, bc, glob_mgr->user_data);
  2164. }
  2165. switch (event) {
  2166. case EVENT_SETUP:
  2167. switch (response) {
  2168. case RESPONSE_IGNORE_SETUP_WITHOUT_CLOSE:
  2169. cb_log(0, stack->port, "TOTALLY IGNORING SETUP\n");
  2170. break;
  2171. case RESPONSE_IGNORE_SETUP:
  2172. cb_log(0, stack->port, "GOT IGNORE SETUP\n");
  2173. /* I think we should send CC_RELEASE_CR, but am not sure*/
  2174. bc->out_cause = AST_CAUSE_NORMAL_CLEARING;
  2175. /* fall through */
  2176. case RESPONSE_RELEASE_SETUP:
  2177. if (response == RESPONSE_RELEASE_SETUP) {
  2178. cb_log(0, stack->port, "GOT RELEASE SETUP\n");
  2179. }
  2180. misdn_lib_send_event(bc, EVENT_RELEASE_COMPLETE);
  2181. break;
  2182. case RESPONSE_OK:
  2183. cb_log(4, stack->port, "GOT SETUP OK\n");
  2184. break;
  2185. default:
  2186. cb_log(0, stack->port, "GOT UNKNOWN SETUP RESPONSE\n");
  2187. break;
  2188. }
  2189. break;
  2190. case EVENT_RELEASE_COMPLETE:
  2191. /* release bchannel only after we've announced the RELEASE_COMPLETE */
  2192. channel = bc->channel;
  2193. tmpcause = bc->cause;
  2194. tmp_out_cause = bc->out_cause;
  2195. empty_bc(bc);
  2196. bc->cause = tmpcause;
  2197. bc->out_cause = tmp_out_cause;
  2198. clean_up_bc(bc);
  2199. bc->in_use = 0;
  2200. if (tmpcause == AST_CAUSE_REQUESTED_CHAN_UNAVAIL) {
  2201. cb_log(0, stack->port, "**** Received CAUSE:%d, restarting channel %d\n", AST_CAUSE_REQUESTED_CHAN_UNAVAIL, channel);
  2202. misdn_lib_send_restart(stack->port, channel);
  2203. }
  2204. if (channel > 0) {
  2205. empty_chan_in_stack(stack, channel);
  2206. }
  2207. break;
  2208. case EVENT_RESTART:
  2209. cb_log(0, stack->port, "**** Received RESTART channel:%d\n", bc->restart_channel);
  2210. empty_chan_in_stack(stack, bc->restart_channel);
  2211. break;
  2212. default:
  2213. break;
  2214. }
  2215. cb_log(5, stack->port, "Freeing Msg on prim:%x \n", frm->prim);
  2216. free_msg(msg);
  2217. return 1;
  2218. }
  2219. static int handle_l1(msg_t *msg)
  2220. {
  2221. iframe_t *frm = (iframe_t*) msg->data;
  2222. struct misdn_stack *stack = find_stack_by_addr(frm->addr);
  2223. if (!stack) return 0 ;
  2224. switch (frm->prim) {
  2225. case PH_ACTIVATE | CONFIRM:
  2226. /* we have to check for errors! */
  2227. if (frm->len) {
  2228. cb_log (3, stack->port, "L1: PH_ACTIVATE|REQUEST returned error!\n");
  2229. free_msg(msg);
  2230. return 1;
  2231. }
  2232. /* fall through */
  2233. case PH_ACTIVATE | INDICATION:
  2234. cb_log (3, stack->port, "L1: PH L1Link Up!\n");
  2235. stack->l1link=1;
  2236. if (stack->nt) {
  2237. pthread_mutex_lock(&stack->nstlock);
  2238. if (stack->nst.l1_l2(&stack->nst, msg))
  2239. free_msg(msg);
  2240. pthread_mutex_unlock(&stack->nstlock);
  2241. if (stack->ptp)
  2242. misdn_lib_get_l2_up(stack);
  2243. } else {
  2244. free_msg(msg);
  2245. }
  2246. return 1;
  2247. case PH_ACTIVATE | REQUEST:
  2248. free_msg(msg);
  2249. cb_log(3,stack->port,"L1: PH_ACTIVATE|REQUEST \n");
  2250. return 1;
  2251. case PH_DEACTIVATE | REQUEST:
  2252. free_msg(msg);
  2253. cb_log(3,stack->port,"L1: PH_DEACTIVATE|REQUEST \n");
  2254. return 1;
  2255. case PH_DEACTIVATE | CONFIRM:
  2256. /* we have to check for errors! */
  2257. if (frm->len) {
  2258. cb_log (3, stack->port, "L1: PH_DEACTIVATE|REQUEST returned error!\n");
  2259. free_msg(msg);
  2260. return 1;
  2261. }
  2262. /* fall through */
  2263. case PH_DEACTIVATE | INDICATION:
  2264. cb_log (3, stack->port, "L1: PH L1Link Down! \n");
  2265. #if 0
  2266. for (i=0; i<=stack->b_num; i++) {
  2267. if (global_state == MISDN_INITIALIZED) {
  2268. cb_event(EVENT_CLEANUP, &stack->bc[i], glob_mgr->user_data);
  2269. }
  2270. }
  2271. #endif
  2272. if (stack->nt) {
  2273. pthread_mutex_lock(&stack->nstlock);
  2274. if (stack->nst.l1_l2(&stack->nst, msg))
  2275. free_msg(msg);
  2276. pthread_mutex_unlock(&stack->nstlock);
  2277. } else {
  2278. free_msg(msg);
  2279. }
  2280. stack->l1link=0;
  2281. stack->l2link=0;
  2282. return 1;
  2283. }
  2284. return 0;
  2285. }
  2286. static int handle_l2(msg_t *msg)
  2287. {
  2288. iframe_t *frm = (iframe_t*) msg->data;
  2289. struct misdn_stack *stack = find_stack_by_addr(frm->addr);
  2290. if (!stack) {
  2291. return 0 ;
  2292. }
  2293. switch(frm->prim) {
  2294. case DL_ESTABLISH | REQUEST:
  2295. cb_log(1,stack->port,"DL_ESTABLISH|REQUEST \n");
  2296. free_msg(msg);
  2297. return 1;
  2298. case DL_RELEASE | REQUEST:
  2299. cb_log(1,stack->port,"DL_RELEASE|REQUEST \n");
  2300. free_msg(msg);
  2301. return 1;
  2302. case DL_ESTABLISH | INDICATION:
  2303. case DL_ESTABLISH | CONFIRM:
  2304. {
  2305. cb_log (3, stack->port, "L2: L2Link Up! \n");
  2306. if (stack->ptp && stack->l2link) {
  2307. cb_log (-1, stack->port, "L2: L2Link Up! but it's already UP.. must be faulty, blocking port\n");
  2308. cb_event(EVENT_PORT_ALARM, &stack->bc[0], glob_mgr->user_data);
  2309. }
  2310. stack->l2link=1;
  2311. free_msg(msg);
  2312. return 1;
  2313. }
  2314. break;
  2315. case DL_RELEASE | INDICATION:
  2316. case DL_RELEASE | CONFIRM:
  2317. {
  2318. cb_log (3, stack->port, "L2: L2Link Down! \n");
  2319. stack->l2link=0;
  2320. free_msg(msg);
  2321. return 1;
  2322. }
  2323. break;
  2324. }
  2325. return 0;
  2326. }
  2327. static int handle_mgmt(msg_t *msg)
  2328. {
  2329. iframe_t *frm = (iframe_t*) msg->data;
  2330. struct misdn_stack *stack;
  2331. if ( (frm->addr == 0) && (frm->prim == (MGR_DELLAYER|CONFIRM)) ) {
  2332. cb_log(2, 0, "MGMT: DELLAYER|CONFIRM Addr: 0 !\n") ;
  2333. free_msg(msg);
  2334. return 1;
  2335. }
  2336. stack = find_stack_by_addr(frm->addr);
  2337. if (!stack) {
  2338. if (frm->prim == (MGR_DELLAYER|CONFIRM)) {
  2339. cb_log(2, 0, "MGMT: DELLAYER|CONFIRM Addr: %x !\n",
  2340. frm->addr) ;
  2341. free_msg(msg);
  2342. return 1;
  2343. }
  2344. return 0;
  2345. }
  2346. switch(frm->prim) {
  2347. case MGR_SHORTSTATUS | INDICATION:
  2348. case MGR_SHORTSTATUS | CONFIRM:
  2349. cb_log(5, stack->port, "MGMT: Short status dinfo %x\n",frm->dinfo);
  2350. switch (frm->dinfo) {
  2351. case SSTATUS_L1_ACTIVATED:
  2352. cb_log(3, stack->port, "MGMT: SSTATUS: L1_ACTIVATED \n");
  2353. stack->l1link=1;
  2354. break;
  2355. case SSTATUS_L1_DEACTIVATED:
  2356. cb_log(3, stack->port, "MGMT: SSTATUS: L1_DEACTIVATED \n");
  2357. stack->l1link=0;
  2358. #if 0
  2359. clear_l3(stack);
  2360. #endif
  2361. break;
  2362. case SSTATUS_L2_ESTABLISHED:
  2363. cb_log(3, stack->port, "MGMT: SSTATUS: L2_ESTABLISH \n");
  2364. stack->l2link=1;
  2365. break;
  2366. case SSTATUS_L2_RELEASED:
  2367. cb_log(3, stack->port, "MGMT: SSTATUS: L2_RELEASED \n");
  2368. stack->l2link=0;
  2369. break;
  2370. }
  2371. free_msg(msg);
  2372. return 1;
  2373. case MGR_SETSTACK | INDICATION:
  2374. cb_log(4, stack->port, "MGMT: SETSTACK|IND dinfo %x\n",frm->dinfo);
  2375. free_msg(msg);
  2376. return 1;
  2377. case MGR_DELLAYER | CONFIRM:
  2378. cb_log(4, stack->port, "MGMT: DELLAYER|CNF dinfo %x\n",frm->dinfo) ;
  2379. free_msg(msg);
  2380. return 1;
  2381. }
  2382. /*
  2383. if ( (frm->prim & 0x0f0000) == 0x0f0000) {
  2384. cb_log(5, 0, "$$$ MGMT FRAME: prim %x addr %x dinfo %x\n",frm->prim, frm->addr, frm->dinfo) ;
  2385. free_msg(msg);
  2386. return 1;
  2387. } */
  2388. return 0;
  2389. }
  2390. static msg_t *fetch_msg(int midev)
  2391. {
  2392. msg_t *msg=alloc_msg(MAX_MSG_SIZE);
  2393. int r;
  2394. if (!msg) {
  2395. cb_log(0, 0, "fetch_msg: alloc msg failed !!");
  2396. return NULL;
  2397. }
  2398. AGAIN:
  2399. r=mISDN_read(midev,msg->data,MAX_MSG_SIZE, TIMEOUT_10SEC);
  2400. msg->len=r;
  2401. if (r==0) {
  2402. free_msg(msg); /* danger, cause usually freeing in main_loop */
  2403. cb_log(6,0,"Got empty Msg..\n");
  2404. return NULL;
  2405. }
  2406. if (r<0) {
  2407. if (errno == EAGAIN) {
  2408. /*we wait for mISDN here*/
  2409. cb_log(4,0,"mISDN_read wants us to wait\n");
  2410. usleep(5000);
  2411. goto AGAIN;
  2412. }
  2413. cb_log(0,0,"mISDN_read returned :%d error:%s (%d)\n",r,strerror(errno),errno);
  2414. }
  2415. #if 0
  2416. if (!(frm->prim == (DL_DATA|INDICATION) )|| (frm->prim == (PH_DATA|INDICATION)))
  2417. cb_log(0,0,"prim: %x dinfo:%x addr:%x msglen:%d frm->len:%d\n",frm->prim, frm->dinfo, frm->addr, msg->len,frm->len );
  2418. #endif
  2419. return msg;
  2420. }
  2421. void misdn_lib_isdn_l1watcher(int port)
  2422. {
  2423. struct misdn_stack *stack;
  2424. for (stack = glob_mgr->stack_list; stack && (stack->port != port); stack = stack->next)
  2425. ;
  2426. if (stack) {
  2427. cb_log(4, port, "Checking L1 State\n");
  2428. if (!stack->l1link) {
  2429. cb_log(4, port, "L1 State Down, trying to get it up again\n");
  2430. misdn_lib_get_short_status(stack);
  2431. misdn_lib_get_l1_up(stack);
  2432. misdn_lib_get_l2_up(stack);
  2433. }
  2434. }
  2435. }
  2436. /* This is a thread */
  2437. static void misdn_lib_isdn_event_catcher(void *arg)
  2438. {
  2439. struct misdn_lib *mgr = arg;
  2440. int zero_frm=0 , fff_frm=0 ;
  2441. int midev= mgr->midev;
  2442. int port=0;
  2443. while (1) {
  2444. msg_t *msg = fetch_msg(midev);
  2445. iframe_t *frm;
  2446. if (!msg) continue;
  2447. frm = (iframe_t*) msg->data;
  2448. /** When we make a call from NT2Ast we get these frames **/
  2449. if (frm->len == 0 && frm->addr == 0 && frm->dinfo == 0 && frm->prim == 0 ) {
  2450. zero_frm++;
  2451. free_msg(msg);
  2452. continue;
  2453. } else {
  2454. if (zero_frm) {
  2455. cb_log(0, port, "*** Alert: %d zero_frms caught\n", zero_frm);
  2456. zero_frm = 0 ;
  2457. }
  2458. }
  2459. /** I get this sometimes after setup_bc **/
  2460. if (frm->len == 0 && frm->dinfo == 0 && frm->prim == 0xffffffff ) {
  2461. fff_frm++;
  2462. free_msg(msg);
  2463. continue;
  2464. } else {
  2465. if (fff_frm) {
  2466. cb_log(0, port, "*** Alert: %d fff_frms caught\n", fff_frm);
  2467. fff_frm = 0 ;
  2468. }
  2469. }
  2470. manager_isdn_handler(frm, msg);
  2471. }
  2472. }
  2473. /** App Interface **/
  2474. static int te_lib_init(void)
  2475. {
  2476. char buff[1025] = "";
  2477. iframe_t *frm = (iframe_t *) buff;
  2478. int midev;
  2479. int ret;
  2480. midev = mISDN_open();
  2481. if (midev <= 0) {
  2482. return midev;
  2483. }
  2484. /* create entity for layer 3 TE-mode */
  2485. mISDN_write_frame(midev, buff, 0, MGR_NEWENTITY | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
  2486. ret = mISDN_read_frame(midev, frm, sizeof(iframe_t), 0, MGR_NEWENTITY | CONFIRM, TIMEOUT_1SEC);
  2487. entity = frm->dinfo & 0xffff;
  2488. if (ret < mISDN_HEADER_LEN || !entity) {
  2489. fprintf(stderr, "cannot request MGR_NEWENTITY from mISDN: %s\n", strerror(errno));
  2490. mISDN_close(midev);
  2491. return -1;
  2492. }
  2493. return midev;
  2494. }
  2495. void te_lib_destroy(int midev)
  2496. {
  2497. char buf[1024];
  2498. mISDN_write_frame(midev, buf, 0, MGR_DELENTITY | REQUEST, entity, 0, NULL, TIMEOUT_1SEC);
  2499. cb_log(4, 0, "Entity deleted\n");
  2500. mISDN_close(midev);
  2501. cb_log(4, 0, "midev closed\n");
  2502. }
  2503. struct misdn_bchannel *manager_find_bc_by_pid(int pid)
  2504. {
  2505. struct misdn_stack *stack;
  2506. int i;
  2507. for (stack = glob_mgr->stack_list; stack; stack = stack->next) {
  2508. for (i = 0; i <= stack->b_num; i++) {
  2509. if (stack->bc[i].in_use && stack->bc[i].pid == pid) {
  2510. return &stack->bc[i];
  2511. }
  2512. }
  2513. }
  2514. return NULL;
  2515. }
  2516. static int test_inuse(struct misdn_bchannel *bc)
  2517. {
  2518. struct timeval now;
  2519. if (!bc->in_use) {
  2520. gettimeofday(&now, NULL);
  2521. if (bc->last_used.tv_sec == now.tv_sec
  2522. && misdn_lib_port_is_pri(bc->port)) {
  2523. cb_log(2, bc->port, "channel with stid:%x for one second still in use! (n:%d lu:%d)\n",
  2524. bc->b_stid, (int) now.tv_sec, (int) bc->last_used.tv_sec);
  2525. return 1;
  2526. }
  2527. cb_log(3,bc->port, "channel with stid:%x not in use!\n", bc->b_stid);
  2528. return 0;
  2529. }
  2530. cb_log(2,bc->port, "channel with stid:%x in use!\n", bc->b_stid);
  2531. return 1;
  2532. }
  2533. static void prepare_bc(struct misdn_bchannel*bc, int channel)
  2534. {
  2535. bc->channel = channel;
  2536. bc->channel_preselected = channel?1:0;
  2537. bc->need_disconnect=1;
  2538. bc->need_release=1;
  2539. bc->need_release_complete=1;
  2540. bc->cause = AST_CAUSE_NORMAL_CLEARING;
  2541. if (++mypid>5000) mypid=1;
  2542. bc->pid=mypid;
  2543. bc->in_use = 1;
  2544. }
  2545. struct misdn_bchannel *misdn_lib_get_free_bc(int port, int channel, int inout, int dec)
  2546. {
  2547. struct misdn_stack *stack;
  2548. int i;
  2549. int maxnum;
  2550. if (channel < 0 || channel > MAX_BCHANS) {
  2551. cb_log(0, port, "Requested channel out of bounds (%d)\n", channel);
  2552. return NULL;
  2553. }
  2554. /* Find the port stack structure */
  2555. stack = find_stack_by_port(port);
  2556. if (!stack) {
  2557. cb_log(0, port, "Port is not configured (%d)\n", port);
  2558. return NULL;
  2559. }
  2560. if (stack->blocked) {
  2561. cb_log(0, port, "Port is blocked\n");
  2562. return NULL;
  2563. }
  2564. pthread_mutex_lock(&stack->st_lock);
  2565. if (channel > 0) {
  2566. if (channel <= stack->b_num) {
  2567. for (i = 0; i < stack->b_num; i++) {
  2568. if (stack->bc[i].channel == channel) {
  2569. if (test_inuse(&stack->bc[i])) {
  2570. pthread_mutex_unlock(&stack->st_lock);
  2571. cb_log(0, port, "Requested channel:%d on port:%d is already in use\n", channel, port);
  2572. return NULL;
  2573. } else {
  2574. prepare_bc(&stack->bc[i], channel);
  2575. pthread_mutex_unlock(&stack->st_lock);
  2576. return &stack->bc[i];
  2577. }
  2578. }
  2579. }
  2580. } else {
  2581. pthread_mutex_unlock(&stack->st_lock);
  2582. cb_log(0, port, "Requested channel:%d is out of bounds on port:%d\n", channel, port);
  2583. return NULL;
  2584. }
  2585. }
  2586. /* Note: channel == 0 here */
  2587. maxnum = (inout && !stack->pri && !stack->ptp) ? stack->b_num + 1 : stack->b_num;
  2588. if (dec) {
  2589. for (i = maxnum - 1; i >= 0; --i) {
  2590. if (!test_inuse(&stack->bc[i])) {
  2591. /* 3. channel on bri means CW*/
  2592. if (!stack->pri && i == stack->b_num) {
  2593. stack->bc[i].cw = 1;
  2594. }
  2595. prepare_bc(&stack->bc[i], channel);
  2596. stack->bc[i].dec = 1;
  2597. pthread_mutex_unlock(&stack->st_lock);
  2598. return &stack->bc[i];
  2599. }
  2600. }
  2601. } else {
  2602. for (i = 0; i < maxnum; ++i) {
  2603. if (!test_inuse(&stack->bc[i])) {
  2604. /* 3. channel on bri means CW */
  2605. if (!stack->pri && i == stack->b_num) {
  2606. stack->bc[i].cw = 1;
  2607. }
  2608. prepare_bc(&stack->bc[i], channel);
  2609. pthread_mutex_unlock(&stack->st_lock);
  2610. return &stack->bc[i];
  2611. }
  2612. }
  2613. }
  2614. pthread_mutex_unlock(&stack->st_lock);
  2615. cb_log(1, port, "There is no free channel on port (%d)\n", port);
  2616. return NULL;
  2617. }
  2618. #if defined(AST_MISDN_ENHANCEMENTS)
  2619. /*!
  2620. * \brief Allocate a B channel struct from the REGISTER pool
  2621. *
  2622. * \param port Logical port number
  2623. *
  2624. * \retval B channel struct on success.
  2625. * \retval NULL on error.
  2626. */
  2627. struct misdn_bchannel *misdn_lib_get_register_bc(int port)
  2628. {
  2629. struct misdn_stack *stack;
  2630. struct misdn_bchannel *bc;
  2631. unsigned index;
  2632. /* Find the port stack structure */
  2633. stack = find_stack_by_port(port);
  2634. if (!stack) {
  2635. cb_log(0, port, "Port is not configured (%d)\n", port);
  2636. return NULL;
  2637. }
  2638. if (stack->blocked) {
  2639. cb_log(0, port, "Port is blocked\n");
  2640. return NULL;
  2641. }
  2642. pthread_mutex_lock(&stack->st_lock);
  2643. for (index = MAX_BCHANS + 1; index < ARRAY_LEN(stack->bc); ++index) {
  2644. bc = &stack->bc[index];
  2645. if (!test_inuse(bc)) {
  2646. prepare_bc(bc, 0);
  2647. bc->need_disconnect = 0;
  2648. bc->need_release = 0;
  2649. pthread_mutex_unlock(&stack->st_lock);
  2650. return bc;
  2651. }
  2652. }
  2653. pthread_mutex_unlock(&stack->st_lock);
  2654. cb_log(1, port, "There is no free REGISTER link on port (%d)\n", port);
  2655. return NULL;
  2656. }
  2657. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2658. /*!
  2659. * \internal
  2660. * \brief Convert the facility function enum value into a string.
  2661. *
  2662. * \return String version of the enum value
  2663. */
  2664. static const char *fac2str(enum FacFunction facility)
  2665. {
  2666. static const struct {
  2667. enum FacFunction facility;
  2668. char *name;
  2669. } arr[] = {
  2670. /* *INDENT-OFF* */
  2671. { Fac_None, "Fac_None" },
  2672. #if defined(AST_MISDN_ENHANCEMENTS)
  2673. { Fac_ERROR, "Fac_ERROR" },
  2674. { Fac_RESULT, "Fac_RESULT" },
  2675. { Fac_REJECT, "Fac_REJECT" },
  2676. { Fac_ActivationDiversion, "Fac_ActivationDiversion" },
  2677. { Fac_DeactivationDiversion, "Fac_DeactivationDiversion" },
  2678. { Fac_ActivationStatusNotificationDiv, "Fac_ActivationStatusNotificationDiv" },
  2679. { Fac_DeactivationStatusNotificationDiv, "Fac_DeactivationStatusNotificationDiv" },
  2680. { Fac_InterrogationDiversion, "Fac_InterrogationDiversion" },
  2681. { Fac_DiversionInformation, "Fac_DiversionInformation" },
  2682. { Fac_CallDeflection, "Fac_CallDeflection" },
  2683. { Fac_CallRerouteing, "Fac_CallRerouteing" },
  2684. { Fac_DivertingLegInformation2, "Fac_DivertingLegInformation2" },
  2685. { Fac_InterrogateServedUserNumbers, "Fac_InterrogateServedUserNumbers" },
  2686. { Fac_DivertingLegInformation1, "Fac_DivertingLegInformation1" },
  2687. { Fac_DivertingLegInformation3, "Fac_DivertingLegInformation3" },
  2688. { Fac_EctExecute, "Fac_EctExecute" },
  2689. { Fac_ExplicitEctExecute, "Fac_ExplicitEctExecute" },
  2690. { Fac_RequestSubaddress, "Fac_RequestSubaddress" },
  2691. { Fac_SubaddressTransfer, "Fac_SubaddressTransfer" },
  2692. { Fac_EctLinkIdRequest, "Fac_EctLinkIdRequest" },
  2693. { Fac_EctInform, "Fac_EctInform" },
  2694. { Fac_EctLoopTest, "Fac_EctLoopTest" },
  2695. { Fac_ChargingRequest, "Fac_ChargingRequest" },
  2696. { Fac_AOCSCurrency, "Fac_AOCSCurrency" },
  2697. { Fac_AOCSSpecialArr, "Fac_AOCSSpecialArr" },
  2698. { Fac_AOCDCurrency, "Fac_AOCDCurrency" },
  2699. { Fac_AOCDChargingUnit, "Fac_AOCDChargingUnit" },
  2700. { Fac_AOCECurrency, "Fac_AOCECurrency" },
  2701. { Fac_AOCEChargingUnit, "Fac_AOCEChargingUnit" },
  2702. { Fac_StatusRequest, "Fac_StatusRequest" },
  2703. { Fac_CallInfoRetain, "Fac_CallInfoRetain" },
  2704. { Fac_EraseCallLinkageID, "Fac_EraseCallLinkageID" },
  2705. { Fac_CCBSDeactivate, "Fac_CCBSDeactivate" },
  2706. { Fac_CCBSErase, "Fac_CCBSErase" },
  2707. { Fac_CCBSRemoteUserFree, "Fac_CCBSRemoteUserFree" },
  2708. { Fac_CCBSCall, "Fac_CCBSCall" },
  2709. { Fac_CCBSStatusRequest, "Fac_CCBSStatusRequest" },
  2710. { Fac_CCBSBFree, "Fac_CCBSBFree" },
  2711. { Fac_CCBSStopAlerting, "Fac_CCBSStopAlerting" },
  2712. { Fac_CCBSRequest, "Fac_CCBSRequest" },
  2713. { Fac_CCBSInterrogate, "Fac_CCBSInterrogate" },
  2714. { Fac_CCNRRequest, "Fac_CCNRRequest" },
  2715. { Fac_CCNRInterrogate, "Fac_CCNRInterrogate" },
  2716. { Fac_CCBS_T_Call, "Fac_CCBS_T_Call" },
  2717. { Fac_CCBS_T_Suspend, "Fac_CCBS_T_Suspend" },
  2718. { Fac_CCBS_T_Resume, "Fac_CCBS_T_Resume" },
  2719. { Fac_CCBS_T_RemoteUserFree, "Fac_CCBS_T_RemoteUserFree" },
  2720. { Fac_CCBS_T_Available, "Fac_CCBS_T_Available" },
  2721. { Fac_CCBS_T_Request, "Fac_CCBS_T_Request" },
  2722. { Fac_CCNR_T_Request, "Fac_CCNR_T_Request" },
  2723. #else
  2724. { Fac_CFActivate, "Fac_CFActivate" },
  2725. { Fac_CFDeactivate, "Fac_CFDeactivate" },
  2726. { Fac_CD, "Fac_CD" },
  2727. { Fac_AOCDCurrency, "Fac_AOCDCurrency" },
  2728. { Fac_AOCDChargingUnit, "Fac_AOCDChargingUnit" },
  2729. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  2730. /* *INDENT-ON* */
  2731. };
  2732. unsigned index;
  2733. for (index = 0; index < ARRAY_LEN(arr); ++index) {
  2734. if (arr[index].facility == facility) {
  2735. return arr[index].name;
  2736. }
  2737. }
  2738. return "unknown";
  2739. }
  2740. void misdn_lib_log_ies(struct misdn_bchannel *bc)
  2741. {
  2742. struct misdn_stack *stack;
  2743. if (!bc) return;
  2744. stack = get_stack_by_bc(bc);
  2745. if (!stack) return;
  2746. cb_log(2, stack->port,
  2747. " --> channel:%d mode:%s cause:%d ocause:%d\n",
  2748. bc->channel,
  2749. stack->nt ? "NT" : "TE",
  2750. bc->cause,
  2751. bc->out_cause);
  2752. cb_log(2, stack->port,
  2753. " --> info_dad:%s dialed numtype:%d plan:%d\n",
  2754. bc->info_dad,
  2755. bc->dialed.number_type,
  2756. bc->dialed.number_plan);
  2757. cb_log(2, stack->port,
  2758. " --> caller:\"%s\" <%s> type:%d plan:%d pres:%d screen:%d\n",
  2759. bc->caller.name,
  2760. bc->caller.number,
  2761. bc->caller.number_type,
  2762. bc->caller.number_plan,
  2763. bc->caller.presentation,
  2764. bc->caller.screening);
  2765. cb_log(2, stack->port,
  2766. " --> redirecting-from:\"%s\" <%s> type:%d plan:%d pres:%d screen:%d\n",
  2767. bc->redirecting.from.name,
  2768. bc->redirecting.from.number,
  2769. bc->redirecting.from.number_type,
  2770. bc->redirecting.from.number_plan,
  2771. bc->redirecting.from.presentation,
  2772. bc->redirecting.from.screening);
  2773. cb_log(2, stack->port,
  2774. " --> redirecting-to:\"%s\" <%s> type:%d plan:%d pres:%d screen:%d\n",
  2775. bc->redirecting.to.name,
  2776. bc->redirecting.to.number,
  2777. bc->redirecting.to.number_type,
  2778. bc->redirecting.to.number_plan,
  2779. bc->redirecting.to.presentation,
  2780. bc->redirecting.to.screening);
  2781. cb_log(2, stack->port,
  2782. " --> redirecting reason:%d count:%d\n",
  2783. bc->redirecting.reason,
  2784. bc->redirecting.count);
  2785. cb_log(2, stack->port,
  2786. " --> connected:\"%s\" <%s> type:%d plan:%d pres:%d screen:%d\n",
  2787. bc->connected.name,
  2788. bc->connected.number,
  2789. bc->connected.number_type,
  2790. bc->connected.number_plan,
  2791. bc->connected.presentation,
  2792. bc->connected.screening);
  2793. cb_log(3, stack->port, " --> caps:%s pi:%x keypad:%s sending_complete:%d\n", bearer2str(bc->capability),bc->progress_indicator, bc->keypad, bc->sending_complete);
  2794. cb_log(4, stack->port, " --> set_pres:%d pres:%d\n", bc->set_presentation, bc->presentation);
  2795. cb_log(4, stack->port, " --> addr:%x l3id:%x b_stid:%x layer_id:%x\n", bc->addr, bc->l3_id, bc->b_stid, bc->layer_id);
  2796. cb_log(4, stack->port, " --> facility in:%s out:%s\n", fac2str(bc->fac_in.Function), fac2str(bc->fac_out.Function));
  2797. cb_log(5, stack->port, " --> urate:%d rate:%d mode:%d user1:%d\n", bc->urate, bc->rate, bc->mode,bc->user1);
  2798. cb_log(5, stack->port, " --> bc:%p h:%d sh:%d\n", bc, bc->holded, bc->stack_holder);
  2799. }
  2800. #define RETURN(a,b) {retval=a; goto b;}
  2801. static void misdn_send_lock(struct misdn_bchannel *bc)
  2802. {
  2803. //cb_log(0,bc->port,"Locking bc->pid:%d\n", bc->pid);
  2804. if (bc->send_lock)
  2805. pthread_mutex_lock(&bc->send_lock->lock);
  2806. }
  2807. static void misdn_send_unlock(struct misdn_bchannel *bc)
  2808. {
  2809. //cb_log(0,bc->port,"UnLocking bc->pid:%d\n", bc->pid);
  2810. if (bc->send_lock)
  2811. pthread_mutex_unlock(&bc->send_lock->lock);
  2812. }
  2813. int misdn_lib_send_event(struct misdn_bchannel *bc, enum event_e event )
  2814. {
  2815. msg_t *msg;
  2816. struct misdn_bchannel *bc2;
  2817. struct misdn_bchannel *held_bc;
  2818. struct misdn_stack *stack;
  2819. int retval = 0;
  2820. int channel;
  2821. int tmpcause;
  2822. int tmp_out_cause;
  2823. if (!bc)
  2824. RETURN(-1,OUT_POST_UNLOCK);
  2825. stack = get_stack_by_bc(bc);
  2826. if (!stack) {
  2827. cb_log(0,bc->port,
  2828. "SENDEVENT: no Stack for event:%s caller:\"%s\" <%s> dialed:%s \n",
  2829. isdn_get_info(msgs_g, event, 0),
  2830. bc->caller.name,
  2831. bc->caller.number,
  2832. bc->dialed.number);
  2833. RETURN(-1,OUT);
  2834. }
  2835. misdn_send_lock(bc);
  2836. cb_log(6,stack->port,"SENDEVENT: stack->nt:%d stack->upperid:%x\n",stack->nt, stack->upper_id);
  2837. if ( stack->nt && !stack->l1link) {
  2838. misdn_lib_get_l1_up(stack);
  2839. }
  2840. cb_log(1, stack->port,
  2841. "I SEND:%s caller:\"%s\" <%s> dialed:%s pid:%d\n",
  2842. isdn_get_info(msgs_g, event, 0),
  2843. bc->caller.name,
  2844. bc->caller.number,
  2845. bc->dialed.number,
  2846. bc->pid);
  2847. cb_log(4, stack->port, " --> bc_state:%s\n",bc_state2str(bc->bc_state));
  2848. misdn_lib_log_ies(bc);
  2849. switch (event) {
  2850. case EVENT_REGISTER:
  2851. case EVENT_SETUP:
  2852. if (create_process(glob_mgr->midev, bc) < 0) {
  2853. cb_log(0, stack->port, " No free channel at the moment @ send_event\n");
  2854. RETURN(-ENOCHAN,OUT);
  2855. }
  2856. break;
  2857. case EVENT_PROGRESS:
  2858. case EVENT_ALERTING:
  2859. case EVENT_PROCEEDING:
  2860. case EVENT_SETUP_ACKNOWLEDGE:
  2861. case EVENT_CONNECT:
  2862. if (!stack->nt) {
  2863. if (stack->ptp) {
  2864. setup_bc(bc);
  2865. }
  2866. break;
  2867. }
  2868. /* fall through */
  2869. case EVENT_RETRIEVE_ACKNOWLEDGE:
  2870. if (stack->nt) {
  2871. if (bc->channel <=0 ) { /* else we have the channel already */
  2872. if (find_free_chan_in_stack(stack, bc, 0, 0)<0) {
  2873. cb_log(0, stack->port, " No free channel at the moment\n");
  2874. /*FIXME: add disconnect*/
  2875. RETURN(-ENOCHAN,OUT);
  2876. }
  2877. }
  2878. /* Its that i generate channels */
  2879. }
  2880. retval=setup_bc(bc);
  2881. if (retval == -EINVAL) {
  2882. cb_log(0,bc->port,"send_event: setup_bc failed\n");
  2883. }
  2884. if (misdn_cap_is_speech(bc->capability)) {
  2885. if ((event==EVENT_CONNECT)||(event==EVENT_RETRIEVE_ACKNOWLEDGE)) {
  2886. if ( *bc->crypt_key ) {
  2887. cb_log(4, stack->port,
  2888. " --> ENABLING BLOWFISH channel:%d caller%d:\"%s\" <%s> dialed%d:%s\n",
  2889. bc->channel,
  2890. bc->caller.number_type,
  2891. bc->caller.name,
  2892. bc->caller.number,
  2893. bc->dialed.number_type,
  2894. bc->dialed.number);
  2895. manager_ph_control_block(bc, BF_ENABLE_KEY, bc->crypt_key, strlen(bc->crypt_key) );
  2896. }
  2897. if (!bc->nodsp) manager_ph_control(bc, DTMF_TONE_START, 0);
  2898. manager_ec_enable(bc);
  2899. if (bc->txgain != 0) {
  2900. cb_log(4, stack->port, "--> Changing txgain to %d\n", bc->txgain);
  2901. manager_ph_control(bc, VOL_CHANGE_TX, bc->txgain);
  2902. }
  2903. if ( bc->rxgain != 0 ) {
  2904. cb_log(4, stack->port, "--> Changing rxgain to %d\n", bc->rxgain);
  2905. manager_ph_control(bc, VOL_CHANGE_RX, bc->rxgain);
  2906. }
  2907. }
  2908. }
  2909. break;
  2910. case EVENT_HOLD_ACKNOWLEDGE:
  2911. held_bc = malloc(sizeof(struct misdn_bchannel));
  2912. if (!held_bc) {
  2913. cb_log(0, bc->port, "Could not allocate held_bc!!!\n");
  2914. RETURN(-1,OUT);
  2915. }
  2916. /* backup the bc and put it in storage */
  2917. *held_bc = *bc;
  2918. held_bc->holded = 1;
  2919. held_bc->channel = 0;/* A held call does not have a channel anymore. */
  2920. held_bc->channel_preselected = 0;
  2921. held_bc->channel_found = 0;
  2922. bc_state_change(held_bc, BCHAN_CLEANED);
  2923. stack_holder_add(stack, held_bc);
  2924. /* kill the bridge and clean the real b-channel record */
  2925. if (stack->nt) {
  2926. if (bc->bc_state == BCHAN_BRIDGED) {
  2927. misdn_split_conf(bc,bc->conf_id);
  2928. bc2 = find_bc_by_confid(bc->conf_id);
  2929. if (!bc2) {
  2930. cb_log(0,bc->port,"We have no second bc in bridge???\n");
  2931. } else {
  2932. misdn_split_conf(bc2,bc->conf_id);
  2933. }
  2934. }
  2935. channel = bc->channel;
  2936. empty_bc(bc);
  2937. clean_up_bc(bc);
  2938. if (channel>0)
  2939. empty_chan_in_stack(stack,channel);
  2940. bc->in_use=0;
  2941. }
  2942. break;
  2943. /* finishing the channel eh ? */
  2944. case EVENT_DISCONNECT:
  2945. if (!bc->need_disconnect) {
  2946. cb_log(0, bc->port, " --> we have already sent DISCONNECT\n");
  2947. RETURN(-1,OUT);
  2948. }
  2949. /* IE cause is mandatory for DISCONNECT, but optional for the answers to DISCONNECT.
  2950. * We must initialize cause, so it is later correctly indicated to ast_channel
  2951. * in case the answer does not include one!
  2952. */
  2953. bc->cause = bc->out_cause;
  2954. bc->need_disconnect=0;
  2955. break;
  2956. case EVENT_RELEASE:
  2957. if (!bc->need_release) {
  2958. cb_log(0, bc->port, " --> we have already sent RELEASE\n");
  2959. RETURN(-1,OUT);
  2960. }
  2961. bc->need_disconnect=0;
  2962. bc->need_release=0;
  2963. break;
  2964. case EVENT_RELEASE_COMPLETE:
  2965. if (!bc->need_release_complete) {
  2966. cb_log(0, bc->port, " --> we have already sent RELEASE_COMPLETE\n");
  2967. RETURN(-1,OUT);
  2968. }
  2969. bc->need_disconnect=0;
  2970. bc->need_release=0;
  2971. bc->need_release_complete=0;
  2972. if (!stack->nt) {
  2973. /* create cleanup in TE */
  2974. channel = bc->channel;
  2975. tmpcause = bc->cause;
  2976. tmp_out_cause = bc->out_cause;
  2977. empty_bc(bc);
  2978. bc->cause=tmpcause;
  2979. bc->out_cause=tmp_out_cause;
  2980. clean_up_bc(bc);
  2981. if (channel>0)
  2982. empty_chan_in_stack(stack,channel);
  2983. bc->in_use=0;
  2984. }
  2985. break;
  2986. case EVENT_CONNECT_ACKNOWLEDGE:
  2987. if ( bc->nt || misdn_cap_is_speech(bc->capability)) {
  2988. int retval=setup_bc(bc);
  2989. if (retval == -EINVAL){
  2990. cb_log(0,bc->port,"send_event: setup_bc failed\n");
  2991. }
  2992. }
  2993. if (misdn_cap_is_speech(bc->capability)) {
  2994. if ( !bc->nodsp) manager_ph_control(bc, DTMF_TONE_START, 0);
  2995. manager_ec_enable(bc);
  2996. if ( bc->txgain != 0 ) {
  2997. cb_log(4, stack->port, "--> Changing txgain to %d\n", bc->txgain);
  2998. manager_ph_control(bc, VOL_CHANGE_TX, bc->txgain);
  2999. }
  3000. if ( bc->rxgain != 0 ) {
  3001. cb_log(4, stack->port, "--> Changing rxgain to %d\n", bc->rxgain);
  3002. manager_ph_control(bc, VOL_CHANGE_RX, bc->rxgain);
  3003. }
  3004. }
  3005. break;
  3006. default:
  3007. break;
  3008. }
  3009. /* Later we should think about sending bchannel data directly to misdn. */
  3010. msg = isdn_msg_build_event(msgs_g, bc, event, stack->nt);
  3011. if (!msg) {
  3012. /*
  3013. * The message was not built.
  3014. *
  3015. * NOTE: The only time that the message will fail to build
  3016. * is because the requested FACILITY message is not supported.
  3017. * A failed malloc() results in exit() being called.
  3018. */
  3019. RETURN(-1, OUT);
  3020. } else {
  3021. msg_queue_tail(&stack->downqueue, msg);
  3022. sem_post(&glob_mgr->new_msg);
  3023. }
  3024. OUT:
  3025. misdn_send_unlock(bc);
  3026. OUT_POST_UNLOCK:
  3027. return retval;
  3028. }
  3029. static int handle_err(msg_t *msg)
  3030. {
  3031. iframe_t *frm = (iframe_t*) msg->data;
  3032. if (!frm->addr) {
  3033. static int cnt=0;
  3034. if (!cnt)
  3035. cb_log(0,0,"mISDN Msg without Address pr:%x dinfo:%x\n",frm->prim,frm->dinfo);
  3036. cnt++;
  3037. if (cnt>100) {
  3038. cb_log(0,0,"mISDN Msg without Address pr:%x dinfo:%x (already more than 100 of them)\n",frm->prim,frm->dinfo);
  3039. cnt=0;
  3040. }
  3041. free_msg(msg);
  3042. return 1;
  3043. }
  3044. switch (frm->prim) {
  3045. case MGR_SETSTACK|INDICATION:
  3046. return handle_bchan(msg);
  3047. break;
  3048. case MGR_SETSTACK|CONFIRM:
  3049. case MGR_CLEARSTACK|CONFIRM:
  3050. free_msg(msg) ;
  3051. return 1;
  3052. break;
  3053. case DL_DATA|CONFIRM:
  3054. cb_log(4,0,"DL_DATA|CONFIRM\n");
  3055. free_msg(msg);
  3056. return 1;
  3057. case PH_CONTROL|CONFIRM:
  3058. cb_log(4,0,"PH_CONTROL|CONFIRM\n");
  3059. free_msg(msg);
  3060. return 1;
  3061. case DL_DATA|INDICATION:
  3062. {
  3063. int port=(frm->addr&MASTER_ID_MASK) >> 8;
  3064. int channel=(frm->addr&CHILD_ID_MASK) >> 16;
  3065. /*we flush the read buffer here*/
  3066. cb_log(9,0,"BCHAN DATA without BC: addr:%x port:%d channel:%d\n",frm->addr, port,channel);
  3067. free_msg(msg);
  3068. return 1;
  3069. }
  3070. }
  3071. return 0;
  3072. }
  3073. int manager_isdn_handler(iframe_t *frm ,msg_t *msg)
  3074. {
  3075. if (frm->dinfo==0xffffffff && frm->prim==(PH_DATA|CONFIRM)) {
  3076. cb_log(0,0,"SERIOUS BUG, dinfo == 0xffffffff, prim == PH_DATA | CONFIRM !!!!\n");
  3077. }
  3078. /* Timer primitives must be handled first, because the frm->addr is a different
  3079. * "address space" than the stack/instance address of other Lx primitives.
  3080. */
  3081. if (handle_timers(msg)) {
  3082. return 0;
  3083. }
  3084. if ( ((frm->addr | ISDN_PID_BCHANNEL_BIT )>> 28 ) == 0x5) {
  3085. static int unhandled_bmsg_count=1000;
  3086. if (handle_bchan(msg)) {
  3087. return 0 ;
  3088. }
  3089. if (unhandled_bmsg_count==1000) {
  3090. cb_log(0, 0, "received 1k Unhandled Bchannel Messages: prim %x len %d from addr %x, dinfo %x on this port.\n",frm->prim, frm->len, frm->addr, frm->dinfo);
  3091. unhandled_bmsg_count=0;
  3092. }
  3093. unhandled_bmsg_count++;
  3094. free_msg(msg);
  3095. return 0;
  3096. }
  3097. #ifdef RECV_FRM_SYSLOG_DEBUG
  3098. syslog(LOG_NOTICE,"mISDN recv: ADDR:%x PRIM:%x DINFO:%x\n", frm->addr, frm->prim, frm->dinfo);
  3099. #endif
  3100. if (handle_mgmt(msg))
  3101. return 0 ;
  3102. if (handle_l2(msg))
  3103. return 0 ;
  3104. /* Its important to handle l1 AFTER l2 */
  3105. if (handle_l1(msg))
  3106. return 0 ;
  3107. if (handle_frm_nt(msg)) {
  3108. return 0;
  3109. }
  3110. if (handle_frm_te(msg)) {
  3111. return 0;
  3112. }
  3113. if (handle_err(msg)) {
  3114. return 0 ;
  3115. }
  3116. cb_log(0, 0, "Unhandled Message: prim %x len %d from addr %x, dinfo %x on this port.\n",frm->prim, frm->len, frm->addr, frm->dinfo);
  3117. free_msg(msg);
  3118. return 0;
  3119. }
  3120. int misdn_lib_get_port_info(int port)
  3121. {
  3122. msg_t *msg=alloc_msg(MAX_MSG_SIZE);
  3123. iframe_t *frm;
  3124. struct misdn_stack *stack=find_stack_by_port(port);
  3125. if (!msg) {
  3126. cb_log(0, port, "misdn_lib_get_port_info: alloc_msg failed!\n");
  3127. return -1;
  3128. }
  3129. frm=(iframe_t*)msg->data;
  3130. if (!stack ) {
  3131. cb_log(0, port, "There is no Stack for this port.\n");
  3132. return -1;
  3133. }
  3134. /* activate bchannel */
  3135. frm->prim = CC_STATUS_ENQUIRY | REQUEST;
  3136. frm->addr = stack->upper_id| FLG_MSG_DOWN;
  3137. frm->dinfo = 0;
  3138. frm->len = 0;
  3139. msg_queue_tail(&glob_mgr->activatequeue, msg);
  3140. sem_post(&glob_mgr->new_msg);
  3141. return 0;
  3142. }
  3143. int misdn_lib_pid_restart(int pid)
  3144. {
  3145. struct misdn_bchannel *bc=manager_find_bc_by_pid(pid);
  3146. if (bc) {
  3147. manager_clean_bc(bc);
  3148. }
  3149. return 0;
  3150. }
  3151. /*Sends Restart message for every bchannel*/
  3152. int misdn_lib_send_restart(int port, int channel)
  3153. {
  3154. struct misdn_stack *stack=find_stack_by_port(port);
  3155. struct misdn_bchannel dummybc;
  3156. /*default is all channels*/
  3157. cb_log(0, port, "Sending Restarts on this port.\n");
  3158. misdn_make_dummy(&dummybc, stack->port, MISDN_ID_GLOBAL, stack->nt, 0);
  3159. /*default is all channels*/
  3160. if (channel <0) {
  3161. dummybc.channel=-1;
  3162. cb_log(0, port, "Restarting and all Interfaces\n");
  3163. misdn_lib_send_event(&dummybc, EVENT_RESTART);
  3164. return 0;
  3165. }
  3166. /*if a channel is specified we restart only this one*/
  3167. if (channel >0) {
  3168. int cnt;
  3169. dummybc.channel=channel;
  3170. cb_log(0, port, "Restarting and cleaning channel %d\n",channel);
  3171. misdn_lib_send_event(&dummybc, EVENT_RESTART);
  3172. /* clean up chan in stack, to be sure we don't think it's
  3173. * in use anymore */
  3174. for (cnt=0; cnt<=stack->b_num; cnt++) {
  3175. if (stack->bc[cnt].in_use && stack->bc[cnt].channel == channel) {
  3176. empty_bc(&stack->bc[cnt]);
  3177. clean_up_bc(&stack->bc[cnt]);
  3178. stack->bc[cnt].in_use=0;
  3179. }
  3180. }
  3181. }
  3182. return 0;
  3183. }
  3184. /*reinitializes the L2/L3*/
  3185. int misdn_lib_port_restart(int port)
  3186. {
  3187. struct misdn_stack *stack=find_stack_by_port(port);
  3188. cb_log(0, port, "Restarting this port.\n");
  3189. if (stack) {
  3190. cb_log(0, port, "Stack:%p\n",stack);
  3191. clear_l3(stack);
  3192. {
  3193. msg_t *msg=alloc_msg(MAX_MSG_SIZE);
  3194. iframe_t *frm;
  3195. if (!msg) {
  3196. cb_log(0, port, "port_restart: alloc_msg failed\n");
  3197. return -1;
  3198. }
  3199. frm=(iframe_t*)msg->data;
  3200. /* we must activate if we are deactivated */
  3201. /* activate bchannel */
  3202. frm->prim = DL_RELEASE | REQUEST;
  3203. frm->addr = stack->upper_id | FLG_MSG_DOWN;
  3204. frm->dinfo = 0;
  3205. frm->len = 0;
  3206. msg_queue_tail(&glob_mgr->activatequeue, msg);
  3207. sem_post(&glob_mgr->new_msg);
  3208. }
  3209. if (stack->nt)
  3210. misdn_lib_reinit_nt_stack(stack->port);
  3211. }
  3212. return 0;
  3213. }
  3214. static sem_t handler_started;
  3215. /* This is a thread */
  3216. static void manager_event_handler(void *arg)
  3217. {
  3218. sem_post(&handler_started);
  3219. while (1) {
  3220. struct misdn_stack *stack;
  3221. msg_t *msg;
  3222. /** wait for events **/
  3223. sem_wait(&glob_mgr->new_msg);
  3224. for (msg=msg_dequeue(&glob_mgr->activatequeue);
  3225. msg;
  3226. msg=msg_dequeue(&glob_mgr->activatequeue)
  3227. )
  3228. {
  3229. iframe_t *frm = (iframe_t*) msg->data ;
  3230. switch ( frm->prim) {
  3231. case MGR_SETSTACK | REQUEST :
  3232. free_msg(msg);
  3233. break;
  3234. default:
  3235. mISDN_write(glob_mgr->midev, frm, mISDN_HEADER_LEN+frm->len, TIMEOUT_1SEC);
  3236. free_msg(msg);
  3237. }
  3238. }
  3239. for (stack=glob_mgr->stack_list;
  3240. stack;
  3241. stack=stack->next ) {
  3242. /* Here we should check if we really want to
  3243. send all the messages we've queued, lets
  3244. assume we've queued a Disconnect, but
  3245. received it already from the other side!*/
  3246. while ( (msg=msg_dequeue(&stack->downqueue)) ) {
  3247. if (stack->nt ) {
  3248. pthread_mutex_lock(&stack->nstlock);
  3249. if (stack->nst.manager_l3(&stack->nst, msg))
  3250. cb_log(0, stack->port, "Error@ Sending Message in NT-Stack.\n");
  3251. pthread_mutex_unlock(&stack->nstlock);
  3252. } else {
  3253. iframe_t *frm = (iframe_t *)msg->data;
  3254. struct misdn_bchannel *bc = find_bc_by_l3id(stack, frm->dinfo);
  3255. if (bc)
  3256. send_msg(glob_mgr->midev, bc, msg);
  3257. else {
  3258. struct misdn_bchannel dummybc;
  3259. misdn_make_dummy(&dummybc, stack->port, frm->dinfo, stack->nt, 0);
  3260. if (frm->dinfo == MISDN_ID_GLOBAL || frm->dinfo == MISDN_ID_DUMMY ) {
  3261. cb_log(5,0," --> GLOBAL/DUMMY\n");
  3262. } else {
  3263. /*
  3264. * We need to be able to at least answer with RELEASE_COMPLETE
  3265. * on SETUP|INDICATION errors so use a dummy bc.
  3266. */
  3267. cb_log(0,0,"No bc for Message. Using dummy_bc\n");
  3268. }
  3269. send_msg(glob_mgr->midev, &dummybc, msg);
  3270. }
  3271. }
  3272. }
  3273. }
  3274. }
  3275. }
  3276. int misdn_lib_maxports_get(void)
  3277. {
  3278. /* BE AWARE WE HAVE NO cb_log() HERE! */
  3279. int i = mISDN_open();
  3280. int max=0;
  3281. if (i<0)
  3282. return -1;
  3283. max = mISDN_get_stack_count(i);
  3284. mISDN_close(i);
  3285. return max;
  3286. }
  3287. void misdn_lib_nt_keepcalls( int kc)
  3288. {
  3289. #ifdef FEATURE_NET_KEEPCALLS
  3290. if (kc) {
  3291. struct misdn_stack *stack=get_misdn_stack();
  3292. for ( ; stack; stack=stack->next) {
  3293. stack->nst.feature |= FEATURE_NET_KEEPCALLS;
  3294. }
  3295. }
  3296. #endif
  3297. }
  3298. void misdn_lib_nt_debug_init( int flags, char *file )
  3299. {
  3300. static int init=0;
  3301. char *f;
  3302. if (!flags)
  3303. f=NULL;
  3304. else
  3305. f=file;
  3306. if (!init) {
  3307. debug_init( flags , f, f, f);
  3308. init=1;
  3309. } else {
  3310. debug_close();
  3311. debug_init( flags , f, f, f);
  3312. }
  3313. }
  3314. int misdn_lib_init(char *portlist, struct misdn_lib_iface *iface, void *user_data)
  3315. {
  3316. struct misdn_lib *mgr;
  3317. char *tok, *tokb;
  3318. char plist[1024];
  3319. int midev;
  3320. int port_count=0;
  3321. cb_log = iface->cb_log;
  3322. cb_event = iface->cb_event;
  3323. cb_jb_empty = iface->cb_jb_empty;
  3324. if (!portlist || (*portlist == 0)) {
  3325. return 1;
  3326. }
  3327. mgr = calloc(1, sizeof(*mgr));
  3328. if (!mgr) {
  3329. return 1;
  3330. }
  3331. glob_mgr = mgr;
  3332. msg_init();
  3333. misdn_lib_nt_debug_init(0,NULL);
  3334. init_flip_bits();
  3335. strncpy(plist, portlist, 1024);
  3336. plist[1023] = 0;
  3337. memcpy(tone_425_flip,tone_425,TONE_425_SIZE);
  3338. flip_buf_bits(tone_425_flip,TONE_425_SIZE);
  3339. memcpy(tone_silence_flip,tone_SILENCE,TONE_SILENCE_SIZE);
  3340. flip_buf_bits(tone_silence_flip,TONE_SILENCE_SIZE);
  3341. midev=te_lib_init();
  3342. if (midev <= 0) {
  3343. free(glob_mgr);
  3344. glob_mgr = NULL;
  3345. return 1;
  3346. }
  3347. mgr->midev=midev;
  3348. port_count=mISDN_get_stack_count(midev);
  3349. msg_queue_init(&mgr->activatequeue);
  3350. if (sem_init(&mgr->new_msg, 1, 0)<0)
  3351. sem_init(&mgr->new_msg, 0, 0);
  3352. for (tok=strtok_r(plist," ,",&tokb );
  3353. tok;
  3354. tok=strtok_r(NULL," ,",&tokb)) {
  3355. int port = atoi(tok);
  3356. struct misdn_stack *stack;
  3357. struct misdn_stack *help;
  3358. int ptp=0;
  3359. int i;
  3360. int r;
  3361. if (strstr(tok, "ptp"))
  3362. ptp=1;
  3363. if (port > port_count) {
  3364. cb_log(0, port, "Couldn't Initialize this port since we have only %d ports\n", port_count);
  3365. continue;
  3366. }
  3367. stack = stack_init(midev, port, ptp);
  3368. if (!stack) {
  3369. cb_log(0, port, "stack_init() failed for this port\n");
  3370. continue;
  3371. }
  3372. /* Initialize the B channel records for real B channels. */
  3373. for (i = 0; i <= stack->b_num; i++) {
  3374. r = init_bc(stack, &stack->bc[i], stack->midev, port, i);
  3375. if (r < 0) {
  3376. cb_log(0, port, "Got Err @ init_bc :%d\n", r);
  3377. break;
  3378. }
  3379. }
  3380. if (i <= stack->b_num) {
  3381. stack_destroy(stack);
  3382. free(stack);
  3383. continue;
  3384. }
  3385. #if defined(AST_MISDN_ENHANCEMENTS)
  3386. /* Initialize the B channel records for REGISTER signaling links. */
  3387. for (i = MAX_BCHANS + 1; i < ARRAY_LEN(stack->bc); ++i) {
  3388. r = init_bc(stack, &stack->bc[i], stack->midev, port, i);
  3389. if (r < 0) {
  3390. cb_log(0, port, "Got Err @ init_bc :%d\n", r);
  3391. break;
  3392. }
  3393. stack->bc[i].is_register_pool = 1;
  3394. }
  3395. if (i < ARRAY_LEN(stack->bc)) {
  3396. stack_destroy(stack);
  3397. free(stack);
  3398. continue;
  3399. }
  3400. #endif /* defined(AST_MISDN_ENHANCEMENTS) */
  3401. /* Add the new stack to the end of the list */
  3402. help = mgr->stack_list;
  3403. if (!help) {
  3404. mgr->stack_list = stack;
  3405. } else {
  3406. while (help->next) {
  3407. help = help->next;
  3408. }
  3409. help->next = stack;
  3410. }
  3411. }
  3412. if (!mgr->stack_list) {
  3413. /* no stacks were successfully initialized !? */
  3414. te_lib_destroy(midev);
  3415. free(glob_mgr);
  3416. glob_mgr = NULL;
  3417. return 1;
  3418. }
  3419. if (sem_init(&handler_started, 1, 0)<0)
  3420. sem_init(&handler_started, 0, 0);
  3421. cb_log(8, 0, "Starting Event Handler\n");
  3422. pthread_create( &mgr->event_handler_thread, NULL,(void*)manager_event_handler, mgr);
  3423. sem_wait(&handler_started) ;
  3424. cb_log(8, 0, "Starting Event Catcher\n");
  3425. pthread_create( &mgr->event_thread, NULL, (void*)misdn_lib_isdn_event_catcher, mgr);
  3426. cb_log(8, 0, "Event Catcher started\n");
  3427. global_state= MISDN_INITIALIZED;
  3428. return (mgr == NULL);
  3429. }
  3430. void misdn_lib_destroy(void)
  3431. {
  3432. struct misdn_stack *help;
  3433. int i;
  3434. for ( help=glob_mgr->stack_list; help; help=help->next ) {
  3435. for(i=0;i<=help->b_num; i++) {
  3436. char buf[1024];
  3437. mISDN_write_frame(help->midev, buf, help->bc[i].addr, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
  3438. help->bc[i].addr = 0;
  3439. }
  3440. cb_log (1, help->port, "Destroying this port.\n");
  3441. stack_destroy(help);
  3442. }
  3443. if (global_state == MISDN_INITIALIZED) {
  3444. cb_log(4, 0, "Killing Handler Thread\n");
  3445. if ( pthread_cancel(glob_mgr->event_handler_thread) == 0 ) {
  3446. cb_log(4, 0, "Joining Handler Thread\n");
  3447. pthread_join(glob_mgr->event_handler_thread, NULL);
  3448. }
  3449. cb_log(4, 0, "Killing Main Thread\n");
  3450. if ( pthread_cancel(glob_mgr->event_thread) == 0 ) {
  3451. cb_log(4, 0, "Joining Main Thread\n");
  3452. pthread_join(glob_mgr->event_thread, NULL);
  3453. }
  3454. }
  3455. cb_log(1, 0, "Closing mISDN device\n");
  3456. te_lib_destroy(glob_mgr->midev);
  3457. while ((help = glob_mgr->stack_list)) {
  3458. glob_mgr->stack_list = help->next;
  3459. free(help);
  3460. }
  3461. free(glob_mgr);
  3462. glob_mgr = NULL;
  3463. }
  3464. char *manager_isdn_get_info(enum event_e event)
  3465. {
  3466. return isdn_get_info(msgs_g , event, 0);
  3467. }
  3468. void manager_bchannel_activate(struct misdn_bchannel *bc)
  3469. {
  3470. char buf[128];
  3471. struct misdn_stack *stack=get_stack_by_bc(bc);
  3472. if (!stack) {
  3473. cb_log(0, bc->port, "bchannel_activate: Stack not found !");
  3474. return ;
  3475. }
  3476. /* we must activate if we are deactivated */
  3477. clear_ibuffer(bc->astbuf);
  3478. cb_log(5, stack->port, "$$$ Bchan Activated addr %x\n", bc->addr);
  3479. mISDN_write_frame(stack->midev, buf, bc->addr | FLG_MSG_DOWN, DL_ESTABLISH | REQUEST, 0,0, NULL, TIMEOUT_1SEC);
  3480. return ;
  3481. }
  3482. void manager_bchannel_deactivate(struct misdn_bchannel * bc)
  3483. {
  3484. struct misdn_stack *stack=get_stack_by_bc(bc);
  3485. char buf[128];
  3486. switch (bc->bc_state) {
  3487. case BCHAN_ACTIVATED:
  3488. break;
  3489. case BCHAN_BRIDGED:
  3490. misdn_split_conf(bc,bc->conf_id);
  3491. break;
  3492. default:
  3493. cb_log( 4, bc->port,"bchan_deactivate: called but not activated\n");
  3494. return ;
  3495. }
  3496. cb_log(5, stack->port, "$$$ Bchan deActivated addr %x\n", bc->addr);
  3497. bc->generate_tone=0;
  3498. mISDN_write_frame(stack->midev, buf, bc->addr | FLG_MSG_DOWN, DL_RELEASE|REQUEST,0,0,NULL, TIMEOUT_1SEC);
  3499. clear_ibuffer(bc->astbuf);
  3500. bc_state_change(bc,BCHAN_RELEASE);
  3501. return;
  3502. }
  3503. int misdn_lib_tx2misdn_frm(struct misdn_bchannel *bc, void *data, int len)
  3504. {
  3505. struct misdn_stack *stack=get_stack_by_bc(bc);
  3506. char buf[4096 + mISDN_HEADER_LEN];
  3507. iframe_t *frm = (iframe_t*)buf;
  3508. switch (bc->bc_state) {
  3509. case BCHAN_ACTIVATED:
  3510. case BCHAN_BRIDGED:
  3511. break;
  3512. default:
  3513. cb_log(3, bc->port, "BC not yet activated (state:%s)\n",bc_state2str(bc->bc_state));
  3514. return -1;
  3515. }
  3516. frm->prim = DL_DATA|REQUEST;
  3517. frm->dinfo = 0;
  3518. frm->addr = bc->addr | FLG_MSG_DOWN ;
  3519. frm->len = len;
  3520. memcpy(&buf[mISDN_HEADER_LEN], data,len);
  3521. if ( misdn_cap_is_speech(bc->capability) )
  3522. flip_buf_bits( &buf[mISDN_HEADER_LEN], len);
  3523. else
  3524. cb_log(6, stack->port, "Writing %d data bytes\n",len);
  3525. cb_log(9, stack->port, "Writing %d bytes 2 mISDN\n",len);
  3526. mISDN_write(stack->midev, buf, frm->len + mISDN_HEADER_LEN, TIMEOUT_INFINIT);
  3527. return 0;
  3528. }
  3529. /*
  3530. * send control information to the channel (dsp-module)
  3531. */
  3532. void manager_ph_control(struct misdn_bchannel *bc, int c1, int c2)
  3533. {
  3534. unsigned char buffer[mISDN_HEADER_LEN+2*sizeof(int)];
  3535. iframe_t *ctrl = (iframe_t *)buffer; /* preload data */
  3536. unsigned int *d = (unsigned int*)&ctrl->data.p;
  3537. /*struct misdn_stack *stack=get_stack_by_bc(bc);*/
  3538. cb_log(4,bc->port,"ph_control: c1:%x c2:%x\n",c1,c2);
  3539. ctrl->prim = PH_CONTROL | REQUEST;
  3540. ctrl->addr = bc->addr | FLG_MSG_DOWN;
  3541. ctrl->dinfo = 0;
  3542. ctrl->len = sizeof(unsigned int)*2;
  3543. *d++ = c1;
  3544. *d++ = c2;
  3545. mISDN_write(glob_mgr->midev, ctrl, mISDN_HEADER_LEN+ctrl->len, TIMEOUT_1SEC);
  3546. }
  3547. /*
  3548. * allow live control of channel parameters
  3549. */
  3550. void isdn_lib_update_rxgain (struct misdn_bchannel *bc)
  3551. {
  3552. manager_ph_control(bc, VOL_CHANGE_RX, bc->rxgain);
  3553. }
  3554. void isdn_lib_update_txgain (struct misdn_bchannel *bc)
  3555. {
  3556. manager_ph_control(bc, VOL_CHANGE_TX, bc->txgain);
  3557. }
  3558. void isdn_lib_update_ec (struct misdn_bchannel *bc)
  3559. {
  3560. #ifdef MISDN_1_2
  3561. if (*bc->pipeline)
  3562. #else
  3563. if (bc->ec_enable)
  3564. #endif
  3565. manager_ec_enable(bc);
  3566. else
  3567. manager_ec_disable(bc);
  3568. }
  3569. void isdn_lib_stop_dtmf (struct misdn_bchannel *bc)
  3570. {
  3571. manager_ph_control(bc, DTMF_TONE_STOP, 0);
  3572. }
  3573. /*
  3574. * send control information to the channel (dsp-module)
  3575. */
  3576. void manager_ph_control_block(struct misdn_bchannel *bc, int c1, void *c2, int c2_len)
  3577. {
  3578. unsigned char buffer[mISDN_HEADER_LEN+sizeof(int)+c2_len];
  3579. iframe_t *ctrl = (iframe_t *)buffer;
  3580. unsigned int *d = (unsigned int *)&ctrl->data.p;
  3581. /*struct misdn_stack *stack=get_stack_by_bc(bc);*/
  3582. ctrl->prim = PH_CONTROL | REQUEST;
  3583. ctrl->addr = bc->addr | FLG_MSG_DOWN;
  3584. ctrl->dinfo = 0;
  3585. ctrl->len = sizeof(unsigned int) + c2_len;
  3586. *d++ = c1;
  3587. memcpy(d, c2, c2_len);
  3588. mISDN_write(glob_mgr->midev, ctrl, mISDN_HEADER_LEN+ctrl->len, TIMEOUT_1SEC);
  3589. }
  3590. void manager_clean_bc(struct misdn_bchannel *bc )
  3591. {
  3592. struct misdn_stack *stack=get_stack_by_bc(bc);
  3593. if (stack && bc->channel > 0) {
  3594. empty_chan_in_stack(stack, bc->channel);
  3595. }
  3596. empty_bc(bc);
  3597. bc->in_use=0;
  3598. cb_event(EVENT_CLEANUP, bc, NULL);
  3599. }
  3600. void stack_holder_add(struct misdn_stack *stack, struct misdn_bchannel *holder)
  3601. {
  3602. struct misdn_bchannel *help;
  3603. cb_log(4,stack->port, "*HOLDER: add %x\n",holder->l3_id);
  3604. holder->stack_holder=1;
  3605. holder->next=NULL;
  3606. if (!stack->holding) {
  3607. stack->holding = holder;
  3608. return;
  3609. }
  3610. for (help=stack->holding;
  3611. help;
  3612. help=help->next) {
  3613. if (!help->next) {
  3614. help->next=holder;
  3615. break;
  3616. }
  3617. }
  3618. }
  3619. void stack_holder_remove(struct misdn_stack *stack, struct misdn_bchannel *holder)
  3620. {
  3621. struct misdn_bchannel *h1;
  3622. if (!holder->stack_holder) return;
  3623. holder->stack_holder=0;
  3624. cb_log(4,stack->port, "*HOLDER: remove %x\n",holder->l3_id);
  3625. if (!stack || ! stack->holding) return;
  3626. if (holder == stack->holding) {
  3627. stack->holding = stack->holding->next;
  3628. return;
  3629. }
  3630. for (h1=stack->holding;
  3631. h1;
  3632. h1=h1->next) {
  3633. if (h1->next == holder) {
  3634. h1->next=h1->next->next;
  3635. return ;
  3636. }
  3637. }
  3638. }
  3639. struct misdn_bchannel *stack_holder_find(struct misdn_stack *stack, unsigned long l3id)
  3640. {
  3641. struct misdn_bchannel *help;
  3642. cb_log(4, stack->port, "*HOLDER: find %lx\n",l3id);
  3643. for (help=stack->holding;
  3644. help;
  3645. help=help->next) {
  3646. if (help->l3_id == l3id) {
  3647. cb_log(4,stack->port, "*HOLDER: found bc\n");
  3648. return help;
  3649. }
  3650. }
  3651. cb_log(4,stack->port, "*HOLDER: find nothing\n");
  3652. return NULL;
  3653. }
  3654. /*!
  3655. * \brief Find a held call's B channel record.
  3656. *
  3657. * \param port Port the call is on.
  3658. * \param l3_id mISDN Layer 3 ID of held call.
  3659. *
  3660. * \return Found bc-record or NULL.
  3661. */
  3662. struct misdn_bchannel *misdn_lib_find_held_bc(int port, int l3_id)
  3663. {
  3664. struct misdn_bchannel *bc;
  3665. struct misdn_stack *stack;
  3666. bc = NULL;
  3667. for (stack = get_misdn_stack(); stack; stack = stack->next) {
  3668. if (stack->port == port) {
  3669. bc = stack_holder_find(stack, l3_id);
  3670. break;
  3671. }
  3672. }
  3673. return bc;
  3674. }
  3675. void misdn_lib_send_tone(struct misdn_bchannel *bc, enum tone_e tone)
  3676. {
  3677. char buf[mISDN_HEADER_LEN + 128] = "";
  3678. iframe_t *frm = (iframe_t*)buf;
  3679. switch(tone) {
  3680. case TONE_DIAL:
  3681. manager_ph_control(bc, TONE_PATT_ON, TONE_GERMAN_DIALTONE);
  3682. break;
  3683. case TONE_ALERTING:
  3684. manager_ph_control(bc, TONE_PATT_ON, TONE_GERMAN_RINGING);
  3685. break;
  3686. case TONE_HANGUP:
  3687. manager_ph_control(bc, TONE_PATT_ON, TONE_GERMAN_HANGUP);
  3688. break;
  3689. case TONE_NONE:
  3690. default:
  3691. manager_ph_control(bc, TONE_PATT_OFF, TONE_GERMAN_HANGUP);
  3692. }
  3693. frm->prim=DL_DATA|REQUEST;
  3694. frm->addr=bc->addr|FLG_MSG_DOWN;
  3695. frm->dinfo=0;
  3696. frm->len=128;
  3697. mISDN_write(glob_mgr->midev, frm, mISDN_HEADER_LEN+frm->len, TIMEOUT_1SEC);
  3698. }
  3699. void manager_ec_enable(struct misdn_bchannel *bc)
  3700. {
  3701. struct misdn_stack *stack=get_stack_by_bc(bc);
  3702. cb_log(4, stack?stack->port:0,"ec_enable\n");
  3703. if (!misdn_cap_is_speech(bc->capability)) {
  3704. cb_log(1, stack?stack->port:0, " --> no speech? cannot enable EC\n");
  3705. } else {
  3706. #ifdef MISDN_1_2
  3707. if (*bc->pipeline) {
  3708. cb_log(3, stack?stack->port:0,"Sending Control PIPELINE_CFG %s\n",bc->pipeline);
  3709. manager_ph_control_block(bc, PIPELINE_CFG, bc->pipeline, strlen(bc->pipeline) + 1);
  3710. }
  3711. #else
  3712. int ec_arr[2];
  3713. if (bc->ec_enable) {
  3714. cb_log(3, stack?stack->port:0,"Sending Control ECHOCAN_ON taps:%d\n",bc->ec_deftaps);
  3715. switch (bc->ec_deftaps) {
  3716. case 4:
  3717. case 8:
  3718. case 16:
  3719. case 32:
  3720. case 64:
  3721. case 128:
  3722. case 256:
  3723. case 512:
  3724. case 1024:
  3725. cb_log(4, stack->port, "Taps is %d\n",bc->ec_deftaps);
  3726. break;
  3727. default:
  3728. cb_log(0, stack->port, "Taps should be power of 2\n");
  3729. bc->ec_deftaps=128;
  3730. }
  3731. ec_arr[0]=bc->ec_deftaps;
  3732. ec_arr[1]=0;
  3733. manager_ph_control_block(bc, ECHOCAN_ON, ec_arr, sizeof(ec_arr));
  3734. }
  3735. #endif
  3736. }
  3737. }
  3738. void manager_ec_disable(struct misdn_bchannel *bc)
  3739. {
  3740. struct misdn_stack *stack=get_stack_by_bc(bc);
  3741. cb_log(4, stack?stack->port:0," --> ec_disable\n");
  3742. if (!misdn_cap_is_speech(bc->capability)) {
  3743. cb_log(1, stack?stack->port:0, " --> no speech? cannot disable EC\n");
  3744. return;
  3745. }
  3746. #ifdef MISDN_1_2
  3747. manager_ph_control_block(bc, PIPELINE_CFG, "", 0);
  3748. #else
  3749. if ( ! bc->ec_enable) {
  3750. cb_log(3, stack?stack->port:0, "Sending Control ECHOCAN_OFF\n");
  3751. manager_ph_control(bc, ECHOCAN_OFF, 0);
  3752. }
  3753. #endif
  3754. }
  3755. struct misdn_stack *get_misdn_stack(void)
  3756. {
  3757. return glob_mgr->stack_list;
  3758. }
  3759. void misdn_join_conf(struct misdn_bchannel *bc, int conf_id)
  3760. {
  3761. char data[16] = "";
  3762. bc_state_change(bc,BCHAN_BRIDGED);
  3763. manager_ph_control(bc, CMX_RECEIVE_OFF, 0);
  3764. manager_ph_control(bc, CMX_CONF_JOIN, conf_id);
  3765. cb_log(3,bc->port, "Joining bc:%x in conf:%d\n",bc->addr,conf_id);
  3766. misdn_lib_tx2misdn_frm(bc, data, sizeof(data) - 1);
  3767. }
  3768. void misdn_split_conf(struct misdn_bchannel *bc, int conf_id)
  3769. {
  3770. bc_state_change(bc,BCHAN_ACTIVATED);
  3771. manager_ph_control(bc, CMX_RECEIVE_ON, 0);
  3772. manager_ph_control(bc, CMX_CONF_SPLIT, conf_id);
  3773. cb_log(4,bc->port, "Splitting bc:%x in conf:%d\n",bc->addr,conf_id);
  3774. }
  3775. void misdn_lib_bridge( struct misdn_bchannel * bc1, struct misdn_bchannel *bc2)
  3776. {
  3777. int conf_id = bc1->pid + 1;
  3778. struct misdn_bchannel *bc_list[] = { bc1, bc2, NULL };
  3779. struct misdn_bchannel **bc;
  3780. cb_log(4, bc1->port, "I Send: BRIDGE from:%d to:%d\n",bc1->port,bc2->port);
  3781. for (bc=bc_list; *bc; bc++) {
  3782. (*bc)->conf_id=conf_id;
  3783. cb_log(4, (*bc)->port, " --> bc_addr:%x\n",(*bc)->addr);
  3784. switch((*bc)->bc_state) {
  3785. case BCHAN_ACTIVATED:
  3786. misdn_join_conf(*bc,conf_id);
  3787. break;
  3788. default:
  3789. bc_next_state_change(*bc,BCHAN_BRIDGED);
  3790. break;
  3791. }
  3792. }
  3793. }
  3794. void misdn_lib_split_bridge( struct misdn_bchannel * bc1, struct misdn_bchannel *bc2)
  3795. {
  3796. struct misdn_bchannel *bc_list[]={
  3797. bc1,bc2,NULL
  3798. };
  3799. struct misdn_bchannel **bc;
  3800. for (bc=bc_list; *bc; bc++) {
  3801. if ( (*bc)->bc_state == BCHAN_BRIDGED){
  3802. misdn_split_conf( *bc, (*bc)->conf_id);
  3803. } else {
  3804. cb_log( 2, (*bc)->port, "BC not bridged (state:%s) so not splitting it\n",bc_state2str((*bc)->bc_state));
  3805. }
  3806. }
  3807. }
  3808. void misdn_lib_echo(struct misdn_bchannel *bc, int onoff)
  3809. {
  3810. cb_log(3,bc->port, " --> ECHO %s\n", onoff?"ON":"OFF");
  3811. manager_ph_control(bc, onoff?CMX_ECHO_ON:CMX_ECHO_OFF, 0);
  3812. }
  3813. void misdn_lib_reinit_nt_stack(int port)
  3814. {
  3815. struct misdn_stack *stack=find_stack_by_port(port);
  3816. if (stack) {
  3817. stack->l2link=0;
  3818. stack->blocked=0;
  3819. cleanup_Isdnl3(&stack->nst);
  3820. cleanup_Isdnl2(&stack->nst);
  3821. memset(&stack->nst, 0, sizeof(net_stack_t));
  3822. memset(&stack->mgr, 0, sizeof(manager_t));
  3823. stack->mgr.nst = &stack->nst;
  3824. stack->nst.manager = &stack->mgr;
  3825. stack->nst.l3_manager = handle_event_nt;
  3826. stack->nst.device = glob_mgr->midev;
  3827. stack->nst.cardnr = port;
  3828. stack->nst.d_stid = stack->d_stid;
  3829. stack->nst.feature = FEATURE_NET_HOLD;
  3830. if (stack->ptp)
  3831. stack->nst.feature |= FEATURE_NET_PTP;
  3832. if (stack->pri)
  3833. stack->nst.feature |= FEATURE_NET_CRLEN2 | FEATURE_NET_EXTCID;
  3834. stack->nst.l1_id = stack->lower_id; /* never used */
  3835. stack->nst.l2_id = stack->upper_id;
  3836. msg_queue_init(&stack->nst.down_queue);
  3837. Isdnl2Init(&stack->nst);
  3838. Isdnl3Init(&stack->nst);
  3839. if (!stack->ptp)
  3840. misdn_lib_get_l1_up(stack);
  3841. }
  3842. }