sig_analog.c 126 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2009, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \brief Analog signaling module
  21. *
  22. * \author Matthew Fredrickson <creslin@digium.com>
  23. */
  24. /*** MODULEINFO
  25. <support_level>core</support_level>
  26. ***/
  27. #include "asterisk.h"
  28. #include <errno.h>
  29. #include <ctype.h>
  30. #include "asterisk/utils.h"
  31. #include "asterisk/options.h"
  32. #include "asterisk/pickup.h"
  33. #include "asterisk/pbx.h"
  34. #include "asterisk/file.h"
  35. #include "asterisk/callerid.h"
  36. #include "asterisk/say.h"
  37. #include "asterisk/manager.h"
  38. #include "asterisk/astdb.h"
  39. #include "asterisk/features.h"
  40. #include "asterisk/causes.h"
  41. #include "asterisk/features_config.h"
  42. #include "asterisk/bridge.h"
  43. #include "asterisk/parking.h"
  44. #include "sig_analog.h"
  45. /*** DOCUMENTATION
  46. ***/
  47. /*! \note
  48. * Define if you want to check the hook state for an FXO (FXS signalled) interface
  49. * before dialing on it. Certain FXO interfaces always think they're out of
  50. * service with this method however.
  51. */
  52. /* #define DAHDI_CHECK_HOOKSTATE */
  53. #define POLARITY_IDLE 0
  54. #define POLARITY_REV 1
  55. #define MIN_MS_SINCE_FLASH ( (2000) ) /*!< 2000 ms */
  56. static int analog_matchdigittimeout = 3000;
  57. static int analog_gendigittimeout = 8000;
  58. static int analog_firstdigittimeout = 16000;
  59. static char analog_defaultcic[64] = "";
  60. static char analog_defaultozz[64] = "";
  61. static const struct {
  62. enum analog_sigtype sigtype;
  63. const char *name;
  64. } sigtypes[] = {
  65. { ANALOG_SIG_FXOLS, "fxo_ls" },
  66. { ANALOG_SIG_FXOKS, "fxo_ks" },
  67. { ANALOG_SIG_FXOGS, "fxo_gs" },
  68. { ANALOG_SIG_FXSLS, "fxs_ls" },
  69. { ANALOG_SIG_FXSKS, "fxs_ks" },
  70. { ANALOG_SIG_FXSGS, "fxs_gs" },
  71. { ANALOG_SIG_EMWINK, "em_w" },
  72. { ANALOG_SIG_EM, "em" },
  73. { ANALOG_SIG_EM_E1, "em_e1" },
  74. { ANALOG_SIG_FEATD, "featd" },
  75. { ANALOG_SIG_FEATDMF, "featdmf" },
  76. { ANALOG_SIG_FEATDMF_TA, "featdmf_ta" },
  77. { ANALOG_SIG_FEATB, "featb" },
  78. { ANALOG_SIG_FGC_CAMA, "fgccama" },
  79. { ANALOG_SIG_FGC_CAMAMF, "fgccamamf" },
  80. { ANALOG_SIG_SF, "sf" },
  81. { ANALOG_SIG_SFWINK, "sf_w" },
  82. { ANALOG_SIG_SF_FEATD, "sf_featd" },
  83. { ANALOG_SIG_SF_FEATDMF, "sf_featdmf" },
  84. { ANALOG_SIG_SF_FEATB, "sf_featb" },
  85. { ANALOG_SIG_E911, "e911" },
  86. };
  87. static const struct {
  88. unsigned int cid_type;
  89. const char *name;
  90. } cidtypes[] = {
  91. { CID_SIG_BELL, "bell" },
  92. { CID_SIG_V23, "v23" },
  93. { CID_SIG_V23_JP, "v23_jp" },
  94. { CID_SIG_DTMF, "dtmf" },
  95. /* "smdi" is intentionally not supported here, as there is a much better
  96. * way to do this in the dialplan now. */
  97. };
  98. #define ISTRUNK(p) ((p->sig == ANALOG_SIG_FXSLS) || (p->sig == ANALOG_SIG_FXSKS) || \
  99. (p->sig == ANALOG_SIG_FXSGS))
  100. enum analog_sigtype analog_str_to_sigtype(const char *name)
  101. {
  102. int i;
  103. for (i = 0; i < ARRAY_LEN(sigtypes); i++) {
  104. if (!strcasecmp(sigtypes[i].name, name)) {
  105. return sigtypes[i].sigtype;
  106. }
  107. }
  108. return 0;
  109. }
  110. const char *analog_sigtype_to_str(enum analog_sigtype sigtype)
  111. {
  112. int i;
  113. for (i = 0; i < ARRAY_LEN(sigtypes); i++) {
  114. if (sigtype == sigtypes[i].sigtype) {
  115. return sigtypes[i].name;
  116. }
  117. }
  118. return "Unknown";
  119. }
  120. unsigned int analog_str_to_cidtype(const char *name)
  121. {
  122. int i;
  123. for (i = 0; i < ARRAY_LEN(cidtypes); i++) {
  124. if (!strcasecmp(cidtypes[i].name, name)) {
  125. return cidtypes[i].cid_type;
  126. }
  127. }
  128. return 0;
  129. }
  130. const char *analog_cidtype_to_str(unsigned int cid_type)
  131. {
  132. int i;
  133. for (i = 0; i < ARRAY_LEN(cidtypes); i++) {
  134. if (cid_type == cidtypes[i].cid_type) {
  135. return cidtypes[i].name;
  136. }
  137. }
  138. return "Unknown";
  139. }
  140. static int analog_start_cid_detect(struct analog_pvt *p, int cid_signalling)
  141. {
  142. if (analog_callbacks.start_cid_detect) {
  143. return analog_callbacks.start_cid_detect(p->chan_pvt, cid_signalling);
  144. }
  145. return -1;
  146. }
  147. static int analog_stop_cid_detect(struct analog_pvt *p)
  148. {
  149. if (analog_callbacks.stop_cid_detect) {
  150. return analog_callbacks.stop_cid_detect(p->chan_pvt);
  151. }
  152. return -1;
  153. }
  154. static int analog_get_callerid(struct analog_pvt *p, char *name, char *number, enum analog_event *ev, size_t timeout)
  155. {
  156. if (analog_callbacks.get_callerid) {
  157. return analog_callbacks.get_callerid(p->chan_pvt, name, number, ev, timeout);
  158. }
  159. return -1;
  160. }
  161. static const char *analog_get_orig_dialstring(struct analog_pvt *p)
  162. {
  163. if (analog_callbacks.get_orig_dialstring) {
  164. return analog_callbacks.get_orig_dialstring(p->chan_pvt);
  165. }
  166. return "";
  167. }
  168. static int analog_get_event(struct analog_pvt *p)
  169. {
  170. if (analog_callbacks.get_event) {
  171. return analog_callbacks.get_event(p->chan_pvt);
  172. }
  173. return -1;
  174. }
  175. static int analog_wait_event(struct analog_pvt *p)
  176. {
  177. if (analog_callbacks.wait_event) {
  178. return analog_callbacks.wait_event(p->chan_pvt);
  179. }
  180. return -1;
  181. }
  182. static int analog_have_progressdetect(struct analog_pvt *p)
  183. {
  184. if (analog_callbacks.have_progressdetect) {
  185. return analog_callbacks.have_progressdetect(p->chan_pvt);
  186. }
  187. /* Don't have progress detection. */
  188. return 0;
  189. }
  190. enum analog_cid_start analog_str_to_cidstart(const char *value)
  191. {
  192. if (!strcasecmp(value, "ring")) {
  193. return ANALOG_CID_START_RING;
  194. } else if (!strcasecmp(value, "polarity")) {
  195. return ANALOG_CID_START_POLARITY;
  196. } else if (!strcasecmp(value, "polarity_in")) {
  197. return ANALOG_CID_START_POLARITY_IN;
  198. } else if (!strcasecmp(value, "dtmf")) {
  199. return ANALOG_CID_START_DTMF_NOALERT;
  200. }
  201. return 0;
  202. }
  203. const char *analog_cidstart_to_str(enum analog_cid_start cid_start)
  204. {
  205. switch (cid_start) {
  206. case ANALOG_CID_START_RING:
  207. return "Ring";
  208. case ANALOG_CID_START_POLARITY:
  209. return "Polarity";
  210. case ANALOG_CID_START_POLARITY_IN:
  211. return "Polarity_In";
  212. case ANALOG_CID_START_DTMF_NOALERT:
  213. return "DTMF";
  214. }
  215. return "Unknown";
  216. }
  217. static char *analog_event2str(enum analog_event event)
  218. {
  219. char *res;
  220. switch (event) {
  221. case ANALOG_EVENT_ONHOOK:
  222. res = "ANALOG_EVENT_ONHOOK";
  223. break;
  224. case ANALOG_EVENT_RINGOFFHOOK:
  225. res = "ANALOG_EVENT_RINGOFFHOOK";
  226. break;
  227. case ANALOG_EVENT_WINKFLASH:
  228. res = "ANALOG_EVENT_WINKFLASH";
  229. break;
  230. case ANALOG_EVENT_ALARM:
  231. res = "ANALOG_EVENT_ALARM";
  232. break;
  233. case ANALOG_EVENT_NOALARM:
  234. res = "ANALOG_EVENT_NOALARM";
  235. break;
  236. case ANALOG_EVENT_DIALCOMPLETE:
  237. res = "ANALOG_EVENT_DIALCOMPLETE";
  238. break;
  239. case ANALOG_EVENT_HOOKCOMPLETE:
  240. res = "ANALOG_EVENT_HOOKCOMPLETE";
  241. break;
  242. case ANALOG_EVENT_PULSE_START:
  243. res = "ANALOG_EVENT_PULSE_START";
  244. break;
  245. case ANALOG_EVENT_POLARITY:
  246. res = "ANALOG_EVENT_POLARITY";
  247. break;
  248. case ANALOG_EVENT_RINGBEGIN:
  249. res = "ANALOG_EVENT_RINGBEGIN";
  250. break;
  251. case ANALOG_EVENT_EC_DISABLED:
  252. res = "ANALOG_EVENT_EC_DISABLED";
  253. break;
  254. case ANALOG_EVENT_RINGERON:
  255. res = "ANALOG_EVENT_RINGERON";
  256. break;
  257. case ANALOG_EVENT_RINGEROFF:
  258. res = "ANALOG_EVENT_RINGEROFF";
  259. break;
  260. case ANALOG_EVENT_REMOVED:
  261. res = "ANALOG_EVENT_REMOVED";
  262. break;
  263. case ANALOG_EVENT_NEONMWI_ACTIVE:
  264. res = "ANALOG_EVENT_NEONMWI_ACTIVE";
  265. break;
  266. case ANALOG_EVENT_NEONMWI_INACTIVE:
  267. res = "ANALOG_EVENT_NEONMWI_INACTIVE";
  268. break;
  269. #ifdef HAVE_DAHDI_ECHOCANCEL_FAX_MODE
  270. case ANALOG_EVENT_TX_CED_DETECTED:
  271. res = "ANALOG_EVENT_TX_CED_DETECTED";
  272. break;
  273. case ANALOG_EVENT_RX_CED_DETECTED:
  274. res = "ANALOG_EVENT_RX_CED_DETECTED";
  275. break;
  276. case ANALOG_EVENT_EC_NLP_DISABLED:
  277. res = "ANALOG_EVENT_EC_NLP_DISABLED";
  278. break;
  279. case ANALOG_EVENT_EC_NLP_ENABLED:
  280. res = "ANALOG_EVENT_EC_NLP_ENABLED";
  281. break;
  282. #endif
  283. case ANALOG_EVENT_PULSEDIGIT:
  284. res = "ANALOG_EVENT_PULSEDIGIT";
  285. break;
  286. case ANALOG_EVENT_DTMFDOWN:
  287. res = "ANALOG_EVENT_DTMFDOWN";
  288. break;
  289. case ANALOG_EVENT_DTMFUP:
  290. res = "ANALOG_EVENT_DTMFUP";
  291. break;
  292. default:
  293. res = "UNKNOWN/OTHER";
  294. break;
  295. }
  296. return res;
  297. }
  298. static void analog_swap_subs(struct analog_pvt *p, enum analog_sub a, enum analog_sub b)
  299. {
  300. int tinthreeway;
  301. struct ast_channel *towner;
  302. ast_debug(1, "Swapping %u and %u\n", a, b);
  303. towner = p->subs[a].owner;
  304. p->subs[a].owner = p->subs[b].owner;
  305. p->subs[b].owner = towner;
  306. tinthreeway = p->subs[a].inthreeway;
  307. p->subs[a].inthreeway = p->subs[b].inthreeway;
  308. p->subs[b].inthreeway = tinthreeway;
  309. if (analog_callbacks.swap_subs) {
  310. analog_callbacks.swap_subs(p->chan_pvt, a, p->subs[a].owner, b, p->subs[b].owner);
  311. }
  312. }
  313. static int analog_alloc_sub(struct analog_pvt *p, enum analog_sub x)
  314. {
  315. if (analog_callbacks.allocate_sub) {
  316. int res;
  317. res = analog_callbacks.allocate_sub(p->chan_pvt, x);
  318. if (!res) {
  319. p->subs[x].allocd = 1;
  320. }
  321. return res;
  322. }
  323. return 0;
  324. }
  325. static int analog_unalloc_sub(struct analog_pvt *p, enum analog_sub x)
  326. {
  327. p->subs[x].allocd = 0;
  328. p->subs[x].owner = NULL;
  329. if (analog_callbacks.unallocate_sub) {
  330. return analog_callbacks.unallocate_sub(p->chan_pvt, x);
  331. }
  332. return 0;
  333. }
  334. static int analog_send_callerid(struct analog_pvt *p, int cwcid, struct ast_party_caller *caller)
  335. {
  336. ast_debug(1, "Sending callerid. CID_NAME: '%s' CID_NUM: '%s'\n",
  337. caller->id.name.str,
  338. caller->id.number.str);
  339. if (cwcid) {
  340. p->callwaitcas = 0;
  341. }
  342. if (analog_callbacks.send_callerid) {
  343. return analog_callbacks.send_callerid(p->chan_pvt, cwcid, caller);
  344. }
  345. return 0;
  346. }
  347. #define analog_get_index(ast, p, nullok) _analog_get_index(ast, p, nullok, __PRETTY_FUNCTION__, __LINE__)
  348. static int _analog_get_index(struct ast_channel *ast, struct analog_pvt *p, int nullok, const char *fname, unsigned long line)
  349. {
  350. int res;
  351. if (p->subs[ANALOG_SUB_REAL].owner == ast) {
  352. res = ANALOG_SUB_REAL;
  353. } else if (p->subs[ANALOG_SUB_CALLWAIT].owner == ast) {
  354. res = ANALOG_SUB_CALLWAIT;
  355. } else if (p->subs[ANALOG_SUB_THREEWAY].owner == ast) {
  356. res = ANALOG_SUB_THREEWAY;
  357. } else {
  358. res = -1;
  359. if (!nullok) {
  360. ast_log(LOG_WARNING,
  361. "Unable to get index for '%s' on channel %d (%s(), line %lu)\n",
  362. ast ? ast_channel_name(ast) : "", p->channel, fname, line);
  363. }
  364. }
  365. return res;
  366. }
  367. static int analog_dsp_reset_and_flush_digits(struct analog_pvt *p)
  368. {
  369. if (analog_callbacks.dsp_reset_and_flush_digits) {
  370. return analog_callbacks.dsp_reset_and_flush_digits(p->chan_pvt);
  371. }
  372. /* Return 0 since I think this is unnecessary to do in most cases it is used. Mostly only for ast_dsp */
  373. return 0;
  374. }
  375. static int analog_play_tone(struct analog_pvt *p, enum analog_sub sub, enum analog_tone tone)
  376. {
  377. if (analog_callbacks.play_tone) {
  378. return analog_callbacks.play_tone(p->chan_pvt, sub, tone);
  379. }
  380. return -1;
  381. }
  382. static void analog_set_new_owner(struct analog_pvt *p, struct ast_channel *new_owner)
  383. {
  384. p->owner = new_owner;
  385. if (analog_callbacks.set_new_owner) {
  386. analog_callbacks.set_new_owner(p->chan_pvt, new_owner);
  387. }
  388. }
  389. static struct ast_channel * analog_new_ast_channel(struct analog_pvt *p, int state, int startpbx, enum analog_sub sub, const struct ast_channel *requestor)
  390. {
  391. struct ast_channel *c;
  392. if (!analog_callbacks.new_ast_channel) {
  393. return NULL;
  394. }
  395. c = analog_callbacks.new_ast_channel(p->chan_pvt, state, startpbx, sub, requestor);
  396. if (c) {
  397. ast_channel_call_forward_set(c, p->call_forward);
  398. }
  399. p->subs[sub].owner = c;
  400. if (!p->owner) {
  401. analog_set_new_owner(p, c);
  402. }
  403. return c;
  404. }
  405. static int analog_set_echocanceller(struct analog_pvt *p, int enable)
  406. {
  407. if (analog_callbacks.set_echocanceller) {
  408. return analog_callbacks.set_echocanceller(p->chan_pvt, enable);
  409. }
  410. return -1;
  411. }
  412. static int analog_train_echocanceller(struct analog_pvt *p)
  413. {
  414. if (analog_callbacks.train_echocanceller) {
  415. return analog_callbacks.train_echocanceller(p->chan_pvt);
  416. }
  417. return -1;
  418. }
  419. static int analog_is_off_hook(struct analog_pvt *p)
  420. {
  421. if (analog_callbacks.is_off_hook) {
  422. return analog_callbacks.is_off_hook(p->chan_pvt);
  423. }
  424. return -1;
  425. }
  426. static int analog_ring(struct analog_pvt *p)
  427. {
  428. if (analog_callbacks.ring) {
  429. return analog_callbacks.ring(p->chan_pvt);
  430. }
  431. return -1;
  432. }
  433. static int analog_flash(struct analog_pvt *p)
  434. {
  435. if (analog_callbacks.flash) {
  436. return analog_callbacks.flash(p->chan_pvt);
  437. }
  438. return -1;
  439. }
  440. static int analog_start(struct analog_pvt *p)
  441. {
  442. if (analog_callbacks.start) {
  443. return analog_callbacks.start(p->chan_pvt);
  444. }
  445. return -1;
  446. }
  447. static int analog_dial_digits(struct analog_pvt *p, enum analog_sub sub, struct analog_dialoperation *dop)
  448. {
  449. if (analog_callbacks.dial_digits) {
  450. return analog_callbacks.dial_digits(p->chan_pvt, sub, dop);
  451. }
  452. return -1;
  453. }
  454. static int analog_on_hook(struct analog_pvt *p)
  455. {
  456. if (analog_callbacks.on_hook) {
  457. return analog_callbacks.on_hook(p->chan_pvt);
  458. }
  459. return -1;
  460. }
  461. static void analog_set_outgoing(struct analog_pvt *p, int is_outgoing)
  462. {
  463. p->outgoing = is_outgoing;
  464. if (analog_callbacks.set_outgoing) {
  465. analog_callbacks.set_outgoing(p->chan_pvt, is_outgoing);
  466. }
  467. }
  468. static int analog_check_for_conference(struct analog_pvt *p)
  469. {
  470. if (analog_callbacks.check_for_conference) {
  471. return analog_callbacks.check_for_conference(p->chan_pvt);
  472. }
  473. return -1;
  474. }
  475. static void analog_all_subchannels_hungup(struct analog_pvt *p)
  476. {
  477. if (analog_callbacks.all_subchannels_hungup) {
  478. analog_callbacks.all_subchannels_hungup(p->chan_pvt);
  479. }
  480. }
  481. static void analog_unlock_private(struct analog_pvt *p)
  482. {
  483. if (analog_callbacks.unlock_private) {
  484. analog_callbacks.unlock_private(p->chan_pvt);
  485. }
  486. }
  487. static void analog_lock_private(struct analog_pvt *p)
  488. {
  489. if (analog_callbacks.lock_private) {
  490. analog_callbacks.lock_private(p->chan_pvt);
  491. }
  492. }
  493. static void analog_deadlock_avoidance_private(struct analog_pvt *p)
  494. {
  495. if (analog_callbacks.deadlock_avoidance_private) {
  496. analog_callbacks.deadlock_avoidance_private(p->chan_pvt);
  497. } else {
  498. /* Fallback to manual avoidance if callback not present. */
  499. analog_unlock_private(p);
  500. usleep(1);
  501. analog_lock_private(p);
  502. }
  503. }
  504. /*!
  505. * \internal
  506. * \brief Obtain the specified subchannel owner lock if the owner exists.
  507. *
  508. * \param pvt Analog private struct.
  509. * \param sub_idx Subchannel owner to lock.
  510. *
  511. * \note Assumes the analog_lock_private(pvt->chan_pvt) is already obtained.
  512. *
  513. * \note
  514. * Because deadlock avoidance may have been necessary, you need to confirm
  515. * the state of things before continuing.
  516. *
  517. * \return Nothing
  518. */
  519. static void analog_lock_sub_owner(struct analog_pvt *pvt, enum analog_sub sub_idx)
  520. {
  521. for (;;) {
  522. if (!pvt->subs[sub_idx].owner) {
  523. /* No subchannel owner pointer */
  524. break;
  525. }
  526. if (!ast_channel_trylock(pvt->subs[sub_idx].owner)) {
  527. /* Got subchannel owner lock */
  528. break;
  529. }
  530. /* We must unlock the private to avoid the possibility of a deadlock */
  531. analog_deadlock_avoidance_private(pvt);
  532. }
  533. }
  534. static int analog_off_hook(struct analog_pvt *p)
  535. {
  536. if (analog_callbacks.off_hook) {
  537. return analog_callbacks.off_hook(p->chan_pvt);
  538. }
  539. return -1;
  540. }
  541. static void analog_set_needringing(struct analog_pvt *p, int value)
  542. {
  543. if (analog_callbacks.set_needringing) {
  544. analog_callbacks.set_needringing(p->chan_pvt, value);
  545. }
  546. }
  547. #if 0
  548. static void analog_set_polarity(struct analog_pvt *p, int value)
  549. {
  550. if (analog_callbacks.set_polarity) {
  551. analog_callbacks.set_polarity(p->chan_pvt, value);
  552. }
  553. }
  554. #endif
  555. static void analog_start_polarityswitch(struct analog_pvt *p)
  556. {
  557. if (analog_callbacks.start_polarityswitch) {
  558. analog_callbacks.start_polarityswitch(p->chan_pvt);
  559. }
  560. }
  561. static void analog_answer_polarityswitch(struct analog_pvt *p)
  562. {
  563. if (analog_callbacks.answer_polarityswitch) {
  564. analog_callbacks.answer_polarityswitch(p->chan_pvt);
  565. }
  566. }
  567. static void analog_hangup_polarityswitch(struct analog_pvt *p)
  568. {
  569. if (analog_callbacks.hangup_polarityswitch) {
  570. analog_callbacks.hangup_polarityswitch(p->chan_pvt);
  571. }
  572. }
  573. static int analog_dsp_set_digitmode(struct analog_pvt *p, enum analog_dsp_digitmode mode)
  574. {
  575. if (analog_callbacks.dsp_set_digitmode) {
  576. return analog_callbacks.dsp_set_digitmode(p->chan_pvt, mode);
  577. }
  578. return -1;
  579. }
  580. static void analog_cb_handle_dtmf(struct analog_pvt *p, struct ast_channel *ast, enum analog_sub analog_index, struct ast_frame **dest)
  581. {
  582. if (analog_callbacks.handle_dtmf) {
  583. analog_callbacks.handle_dtmf(p->chan_pvt, ast, analog_index, dest);
  584. }
  585. }
  586. static int analog_wink(struct analog_pvt *p, enum analog_sub index)
  587. {
  588. if (analog_callbacks.wink) {
  589. return analog_callbacks.wink(p->chan_pvt, index);
  590. }
  591. return -1;
  592. }
  593. static int analog_has_voicemail(struct analog_pvt *p)
  594. {
  595. if (analog_callbacks.has_voicemail) {
  596. return analog_callbacks.has_voicemail(p->chan_pvt);
  597. }
  598. return -1;
  599. }
  600. static int analog_is_dialing(struct analog_pvt *p, enum analog_sub index)
  601. {
  602. if (analog_callbacks.is_dialing) {
  603. return analog_callbacks.is_dialing(p->chan_pvt, index);
  604. }
  605. return -1;
  606. }
  607. /*!
  608. * \internal
  609. * \brief Attempt to transfer 3-way call.
  610. *
  611. * \param p Analog private structure.
  612. *
  613. * \note On entry these locks are held: real-call, private, 3-way call.
  614. * \note On exit these locks are held: real-call, private.
  615. *
  616. * \retval 0 on success.
  617. * \retval -1 on error.
  618. */
  619. static int analog_attempt_transfer(struct analog_pvt *p)
  620. {
  621. struct ast_channel *owner_real;
  622. struct ast_channel *owner_3way;
  623. enum ast_transfer_result xfer_res;
  624. int res = 0;
  625. owner_real = ast_channel_ref(p->subs[ANALOG_SUB_REAL].owner);
  626. owner_3way = ast_channel_ref(p->subs[ANALOG_SUB_THREEWAY].owner);
  627. ast_verb(3, "TRANSFERRING %s to %s\n",
  628. ast_channel_name(owner_3way), ast_channel_name(owner_real));
  629. ast_channel_unlock(owner_real);
  630. ast_channel_unlock(owner_3way);
  631. analog_unlock_private(p);
  632. xfer_res = ast_bridge_transfer_attended(owner_3way, owner_real);
  633. if (xfer_res != AST_BRIDGE_TRANSFER_SUCCESS) {
  634. ast_softhangup(owner_3way, AST_SOFTHANGUP_DEV);
  635. res = -1;
  636. }
  637. /* Must leave with these locked. */
  638. ast_channel_lock(owner_real);
  639. analog_lock_private(p);
  640. ast_channel_unref(owner_real);
  641. ast_channel_unref(owner_3way);
  642. return res;
  643. }
  644. static int analog_update_conf(struct analog_pvt *p)
  645. {
  646. int x;
  647. int needconf = 0;
  648. /* Start with the obvious, general stuff */
  649. for (x = 0; x < 3; x++) {
  650. /* Look for three way calls */
  651. if ((p->subs[x].allocd) && p->subs[x].inthreeway) {
  652. if (analog_callbacks.conf_add) {
  653. analog_callbacks.conf_add(p->chan_pvt, x);
  654. }
  655. needconf++;
  656. } else {
  657. if (analog_callbacks.conf_del) {
  658. analog_callbacks.conf_del(p->chan_pvt, x);
  659. }
  660. }
  661. }
  662. ast_debug(1, "Updated conferencing on %d, with %d conference users\n", p->channel, needconf);
  663. if (analog_callbacks.complete_conference_update) {
  664. analog_callbacks.complete_conference_update(p->chan_pvt, needconf);
  665. }
  666. return 0;
  667. }
  668. struct ast_channel * analog_request(struct analog_pvt *p, int *callwait, const struct ast_channel *requestor)
  669. {
  670. struct ast_channel *ast;
  671. ast_debug(1, "%s %d\n", __FUNCTION__, p->channel);
  672. *callwait = (p->owner != NULL);
  673. if (p->owner) {
  674. if (analog_alloc_sub(p, ANALOG_SUB_CALLWAIT)) {
  675. ast_log(LOG_ERROR, "Unable to alloc subchannel\n");
  676. return NULL;
  677. }
  678. }
  679. analog_set_outgoing(p, 1);
  680. ast = analog_new_ast_channel(p, AST_STATE_RESERVED, 0,
  681. p->owner ? ANALOG_SUB_CALLWAIT : ANALOG_SUB_REAL, requestor);
  682. if (!ast) {
  683. analog_set_outgoing(p, 0);
  684. }
  685. return ast;
  686. }
  687. int analog_available(struct analog_pvt *p)
  688. {
  689. int offhook;
  690. ast_debug(1, "%s %d\n", __FUNCTION__, p->channel);
  691. /* If do not disturb, definitely not */
  692. if (p->dnd) {
  693. return 0;
  694. }
  695. /* If guard time, definitely not */
  696. if (p->guardtime && (time(NULL) < p->guardtime)) {
  697. return 0;
  698. }
  699. /* If no owner definitely available */
  700. if (!p->owner) {
  701. offhook = analog_is_off_hook(p);
  702. /* TDM FXO card, "onhook" means out of service (no battery on the line) */
  703. if ((p->sig == ANALOG_SIG_FXSLS) || (p->sig == ANALOG_SIG_FXSKS) || (p->sig == ANALOG_SIG_FXSGS)) {
  704. #ifdef DAHDI_CHECK_HOOKSTATE
  705. if (offhook) {
  706. return 1;
  707. }
  708. return 0;
  709. #endif
  710. /* TDM FXS card, "offhook" means someone took the hook off so it's unavailable! */
  711. } else if (offhook) {
  712. ast_debug(1, "Channel %d off hook, can't use\n", p->channel);
  713. /* Not available when the other end is off hook */
  714. return 0;
  715. }
  716. return 1;
  717. }
  718. /* If it's not an FXO, forget about call wait */
  719. if ((p->sig != ANALOG_SIG_FXOKS) && (p->sig != ANALOG_SIG_FXOLS) && (p->sig != ANALOG_SIG_FXOGS)) {
  720. return 0;
  721. }
  722. if (!p->callwaiting) {
  723. /* If they don't have call waiting enabled, then for sure they're unavailable at this point */
  724. return 0;
  725. }
  726. if (p->subs[ANALOG_SUB_CALLWAIT].allocd) {
  727. /* If there is already a call waiting call, then we can't take a second one */
  728. return 0;
  729. }
  730. if ((ast_channel_state(p->owner) != AST_STATE_UP) &&
  731. ((ast_channel_state(p->owner) != AST_STATE_RINGING) || p->outgoing)) {
  732. /* If the current call is not up, then don't allow the call */
  733. return 0;
  734. }
  735. if ((p->subs[ANALOG_SUB_THREEWAY].owner) && (!p->subs[ANALOG_SUB_THREEWAY].inthreeway)) {
  736. /* Can't take a call wait when the three way calling hasn't been merged yet. */
  737. return 0;
  738. }
  739. /* We're cool */
  740. return 1;
  741. }
  742. static int analog_stop_callwait(struct analog_pvt *p)
  743. {
  744. p->callwaitcas = 0;
  745. if (analog_callbacks.stop_callwait) {
  746. return analog_callbacks.stop_callwait(p->chan_pvt);
  747. }
  748. return 0;
  749. }
  750. static int analog_callwait(struct analog_pvt *p)
  751. {
  752. p->callwaitcas = p->callwaitingcallerid;
  753. if (analog_callbacks.callwait) {
  754. return analog_callbacks.callwait(p->chan_pvt);
  755. }
  756. return 0;
  757. }
  758. static void analog_set_callwaiting(struct analog_pvt *p, int callwaiting_enable)
  759. {
  760. p->callwaiting = callwaiting_enable;
  761. if (analog_callbacks.set_callwaiting) {
  762. analog_callbacks.set_callwaiting(p->chan_pvt, callwaiting_enable);
  763. }
  764. }
  765. static void analog_set_cadence(struct analog_pvt *p, struct ast_channel *chan)
  766. {
  767. if (analog_callbacks.set_cadence) {
  768. analog_callbacks.set_cadence(p->chan_pvt, &p->cidrings, chan);
  769. }
  770. }
  771. static void analog_set_dialing(struct analog_pvt *p, int is_dialing)
  772. {
  773. p->dialing = is_dialing;
  774. if (analog_callbacks.set_dialing) {
  775. analog_callbacks.set_dialing(p->chan_pvt, is_dialing);
  776. }
  777. }
  778. static void analog_set_alarm(struct analog_pvt *p, int in_alarm)
  779. {
  780. p->inalarm = in_alarm;
  781. if (analog_callbacks.set_alarm) {
  782. analog_callbacks.set_alarm(p->chan_pvt, in_alarm);
  783. }
  784. }
  785. static void analog_set_ringtimeout(struct analog_pvt *p, int ringt)
  786. {
  787. p->ringt = ringt;
  788. if (analog_callbacks.set_ringtimeout) {
  789. analog_callbacks.set_ringtimeout(p->chan_pvt, ringt);
  790. }
  791. }
  792. static void analog_set_waitingfordt(struct analog_pvt *p, struct ast_channel *ast)
  793. {
  794. if (analog_callbacks.set_waitingfordt) {
  795. analog_callbacks.set_waitingfordt(p->chan_pvt, ast);
  796. }
  797. }
  798. static int analog_check_waitingfordt(struct analog_pvt *p)
  799. {
  800. if (analog_callbacks.check_waitingfordt) {
  801. return analog_callbacks.check_waitingfordt(p->chan_pvt);
  802. }
  803. return 0;
  804. }
  805. static void analog_set_confirmanswer(struct analog_pvt *p, int flag)
  806. {
  807. if (analog_callbacks.set_confirmanswer) {
  808. analog_callbacks.set_confirmanswer(p->chan_pvt, flag);
  809. }
  810. }
  811. static int analog_check_confirmanswer(struct analog_pvt *p)
  812. {
  813. if (analog_callbacks.check_confirmanswer) {
  814. return analog_callbacks.check_confirmanswer(p->chan_pvt);
  815. }
  816. return 0;
  817. }
  818. static void analog_cancel_cidspill(struct analog_pvt *p)
  819. {
  820. if (analog_callbacks.cancel_cidspill) {
  821. analog_callbacks.cancel_cidspill(p->chan_pvt);
  822. }
  823. }
  824. static int analog_confmute(struct analog_pvt *p, int mute)
  825. {
  826. if (analog_callbacks.confmute) {
  827. return analog_callbacks.confmute(p->chan_pvt, mute);
  828. }
  829. return 0;
  830. }
  831. static void analog_set_pulsedial(struct analog_pvt *p, int flag)
  832. {
  833. if (analog_callbacks.set_pulsedial) {
  834. analog_callbacks.set_pulsedial(p->chan_pvt, flag);
  835. }
  836. }
  837. static int analog_set_linear_mode(struct analog_pvt *p, enum analog_sub sub, int linear_mode)
  838. {
  839. if (analog_callbacks.set_linear_mode) {
  840. /* Return provides old linear_mode setting or error indication */
  841. return analog_callbacks.set_linear_mode(p->chan_pvt, sub, linear_mode);
  842. }
  843. return -1;
  844. }
  845. static void analog_set_inthreeway(struct analog_pvt *p, enum analog_sub sub, int inthreeway)
  846. {
  847. p->subs[sub].inthreeway = inthreeway;
  848. if (analog_callbacks.set_inthreeway) {
  849. analog_callbacks.set_inthreeway(p->chan_pvt, sub, inthreeway);
  850. }
  851. }
  852. int analog_call(struct analog_pvt *p, struct ast_channel *ast, const char *rdest, int timeout)
  853. {
  854. int res, idx, mysig;
  855. char *c, *n, *l;
  856. char dest[256]; /* must be same length as p->dialdest */
  857. ast_debug(1, "CALLING CID_NAME: %s CID_NUM:: %s\n",
  858. S_COR(ast_channel_connected(ast)->id.name.valid, ast_channel_connected(ast)->id.name.str, ""),
  859. S_COR(ast_channel_connected(ast)->id.number.valid, ast_channel_connected(ast)->id.number.str, ""));
  860. ast_copy_string(dest, rdest, sizeof(dest));
  861. ast_copy_string(p->dialdest, rdest, sizeof(p->dialdest));
  862. if ((ast_channel_state(ast) == AST_STATE_BUSY)) {
  863. ast_queue_control(p->subs[ANALOG_SUB_REAL].owner, AST_CONTROL_BUSY);
  864. return 0;
  865. }
  866. if ((ast_channel_state(ast) != AST_STATE_DOWN) && (ast_channel_state(ast) != AST_STATE_RESERVED)) {
  867. ast_log(LOG_WARNING, "analog_call called on %s, neither down nor reserved\n", ast_channel_name(ast));
  868. return -1;
  869. }
  870. p->dialednone = 0;
  871. analog_set_outgoing(p, 1);
  872. mysig = p->sig;
  873. if (p->outsigmod > -1) {
  874. mysig = p->outsigmod;
  875. }
  876. switch (mysig) {
  877. case ANALOG_SIG_FXOLS:
  878. case ANALOG_SIG_FXOGS:
  879. case ANALOG_SIG_FXOKS:
  880. if (p->owner == ast) {
  881. /* Normal ring, on hook */
  882. /* Don't send audio while on hook, until the call is answered */
  883. analog_set_dialing(p, 1);
  884. analog_set_cadence(p, ast); /* and set p->cidrings */
  885. /* nick@dccinc.com 4/3/03 mods to allow for deferred dialing */
  886. c = strchr(dest, '/');
  887. if (c) {
  888. c++;
  889. }
  890. if (c && (strlen(c) < p->stripmsd)) {
  891. ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
  892. c = NULL;
  893. }
  894. if (c) {
  895. p->dop.op = ANALOG_DIAL_OP_REPLACE;
  896. snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "Tw%s", c);
  897. ast_debug(1, "FXO: setup deferred dialstring: %s\n", c);
  898. } else {
  899. p->dop.dialstr[0] = '\0';
  900. }
  901. if (analog_ring(p)) {
  902. ast_log(LOG_WARNING, "Unable to ring phone: %s\n", strerror(errno));
  903. return -1;
  904. }
  905. analog_set_dialing(p, 1);
  906. } else {
  907. /* Call waiting call */
  908. if (ast_channel_connected(ast)->id.number.valid && ast_channel_connected(ast)->id.number.str) {
  909. ast_copy_string(p->callwait_num, ast_channel_connected(ast)->id.number.str, sizeof(p->callwait_num));
  910. } else {
  911. p->callwait_num[0] = '\0';
  912. }
  913. if (ast_channel_connected(ast)->id.name.valid && ast_channel_connected(ast)->id.name.str) {
  914. ast_copy_string(p->callwait_name, ast_channel_connected(ast)->id.name.str, sizeof(p->callwait_name));
  915. } else {
  916. p->callwait_name[0] = '\0';
  917. }
  918. /* Call waiting tone instead */
  919. if (analog_callwait(p)) {
  920. return -1;
  921. }
  922. /* Make ring-back */
  923. if (analog_play_tone(p, ANALOG_SUB_CALLWAIT, ANALOG_TONE_RINGTONE)) {
  924. ast_log(LOG_WARNING, "Unable to generate call-wait ring-back on channel %s\n", ast_channel_name(ast));
  925. }
  926. }
  927. n = ast_channel_connected(ast)->id.name.valid ? ast_channel_connected(ast)->id.name.str : NULL;
  928. l = ast_channel_connected(ast)->id.number.valid ? ast_channel_connected(ast)->id.number.str : NULL;
  929. if (l) {
  930. ast_copy_string(p->lastcid_num, l, sizeof(p->lastcid_num));
  931. } else {
  932. p->lastcid_num[0] = '\0';
  933. }
  934. if (n) {
  935. ast_copy_string(p->lastcid_name, n, sizeof(p->lastcid_name));
  936. } else {
  937. p->lastcid_name[0] = '\0';
  938. }
  939. if (p->use_callerid) {
  940. p->caller.id.name.str = p->lastcid_name;
  941. p->caller.id.number.str = p->lastcid_num;
  942. }
  943. ast_setstate(ast, AST_STATE_RINGING);
  944. idx = analog_get_index(ast, p, 0);
  945. if (idx > -1) {
  946. struct ast_cc_config_params *cc_params;
  947. /* This is where the initial ringing frame is queued for an analog call.
  948. * As such, this is a great time to offer CCNR to the caller if it's available.
  949. */
  950. cc_params = ast_channel_get_cc_config_params(p->subs[idx].owner);
  951. if (cc_params) {
  952. switch (ast_get_cc_monitor_policy(cc_params)) {
  953. case AST_CC_MONITOR_NEVER:
  954. break;
  955. case AST_CC_MONITOR_NATIVE:
  956. case AST_CC_MONITOR_ALWAYS:
  957. case AST_CC_MONITOR_GENERIC:
  958. ast_queue_cc_frame(p->subs[idx].owner, AST_CC_GENERIC_MONITOR_TYPE,
  959. analog_get_orig_dialstring(p), AST_CC_CCNR, NULL);
  960. break;
  961. }
  962. }
  963. ast_queue_control(p->subs[idx].owner, AST_CONTROL_RINGING);
  964. }
  965. break;
  966. case ANALOG_SIG_FXSLS:
  967. case ANALOG_SIG_FXSGS:
  968. case ANALOG_SIG_FXSKS:
  969. if (p->answeronpolarityswitch || p->hanguponpolarityswitch) {
  970. ast_debug(1, "Ignore possible polarity reversal on line seizure\n");
  971. p->polaritydelaytv = ast_tvnow();
  972. }
  973. /* fall through */
  974. case ANALOG_SIG_EMWINK:
  975. case ANALOG_SIG_EM:
  976. case ANALOG_SIG_EM_E1:
  977. case ANALOG_SIG_FEATD:
  978. case ANALOG_SIG_FEATDMF:
  979. case ANALOG_SIG_E911:
  980. case ANALOG_SIG_FGC_CAMA:
  981. case ANALOG_SIG_FGC_CAMAMF:
  982. case ANALOG_SIG_FEATB:
  983. case ANALOG_SIG_SFWINK:
  984. case ANALOG_SIG_SF:
  985. case ANALOG_SIG_SF_FEATD:
  986. case ANALOG_SIG_SF_FEATDMF:
  987. case ANALOG_SIG_FEATDMF_TA:
  988. case ANALOG_SIG_SF_FEATB:
  989. c = strchr(dest, '/');
  990. if (c) {
  991. c++;
  992. } else {
  993. c = "";
  994. }
  995. if (strlen(c) < p->stripmsd) {
  996. ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
  997. return -1;
  998. }
  999. res = analog_start(p);
  1000. if (res < 0) {
  1001. if (errno != EINPROGRESS) {
  1002. return -1;
  1003. }
  1004. }
  1005. ast_debug(1, "Dialing '%s'\n", c);
  1006. p->dop.op = ANALOG_DIAL_OP_REPLACE;
  1007. c += p->stripmsd;
  1008. switch (mysig) {
  1009. case ANALOG_SIG_FEATD:
  1010. l = ast_channel_connected(ast)->id.number.valid ? ast_channel_connected(ast)->id.number.str : NULL;
  1011. if (l) {
  1012. snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T*%s*%s*", l, c);
  1013. } else {
  1014. snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T**%s*", c);
  1015. }
  1016. break;
  1017. case ANALOG_SIG_FEATDMF:
  1018. l = ast_channel_connected(ast)->id.number.valid ? ast_channel_connected(ast)->id.number.str : NULL;
  1019. if (l) {
  1020. snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*00%s#*%s#", l, c);
  1021. } else {
  1022. snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*02#*%s#", c);
  1023. }
  1024. break;
  1025. case ANALOG_SIG_FEATDMF_TA:
  1026. {
  1027. const char *cic = "", *ozz = "";
  1028. /* If you have to go through a Tandem Access point you need to use this */
  1029. #ifndef STANDALONE
  1030. ozz = pbx_builtin_getvar_helper(p->owner, "FEATDMF_OZZ");
  1031. if (!ozz) {
  1032. ozz = analog_defaultozz;
  1033. }
  1034. cic = pbx_builtin_getvar_helper(p->owner, "FEATDMF_CIC");
  1035. if (!cic) {
  1036. cic = analog_defaultcic;
  1037. }
  1038. #endif
  1039. if (!ozz || !cic) {
  1040. ast_log(LOG_WARNING, "Unable to dial channel of type feature group D MF tandem access without CIC or OZZ set\n");
  1041. return -1;
  1042. }
  1043. snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s%s#", ozz, cic);
  1044. snprintf(p->finaldial, sizeof(p->finaldial), "M*%s#", c);
  1045. p->whichwink = 0;
  1046. }
  1047. break;
  1048. case ANALOG_SIG_E911:
  1049. ast_copy_string(p->dop.dialstr, "M*911#", sizeof(p->dop.dialstr));
  1050. break;
  1051. case ANALOG_SIG_FGC_CAMA:
  1052. snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%s", c);
  1053. break;
  1054. case ANALOG_SIG_FGC_CAMAMF:
  1055. case ANALOG_SIG_FEATB:
  1056. snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s#", c);
  1057. break;
  1058. default:
  1059. if (p->pulse) {
  1060. snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%sw", c);
  1061. } else {
  1062. snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%sw", c);
  1063. }
  1064. break;
  1065. }
  1066. if (p->echotraining && (strlen(p->dop.dialstr) > 4)) {
  1067. memset(p->echorest, 'w', sizeof(p->echorest) - 1);
  1068. strcpy(p->echorest + (p->echotraining / 400) + 1, p->dop.dialstr + strlen(p->dop.dialstr) - 2);
  1069. p->echorest[sizeof(p->echorest) - 1] = '\0';
  1070. p->echobreak = 1;
  1071. p->dop.dialstr[strlen(p->dop.dialstr)-2] = '\0';
  1072. } else {
  1073. p->echobreak = 0;
  1074. }
  1075. analog_set_waitingfordt(p, ast);
  1076. if (!res) {
  1077. if (analog_dial_digits(p, ANALOG_SUB_REAL, &p->dop)) {
  1078. analog_on_hook(p);
  1079. return -1;
  1080. }
  1081. } else {
  1082. ast_debug(1, "Deferring dialing...\n");
  1083. }
  1084. analog_set_dialing(p, 1);
  1085. if (ast_strlen_zero(c)) {
  1086. p->dialednone = 1;
  1087. }
  1088. ast_setstate(ast, AST_STATE_DIALING);
  1089. break;
  1090. default:
  1091. ast_debug(1, "not yet implemented\n");
  1092. return -1;
  1093. }
  1094. return 0;
  1095. }
  1096. int analog_hangup(struct analog_pvt *p, struct ast_channel *ast)
  1097. {
  1098. int res;
  1099. int idx, x;
  1100. ast_debug(1, "%s %d\n", __FUNCTION__, p->channel);
  1101. if (!ast_channel_tech_pvt(ast)) {
  1102. ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
  1103. return 0;
  1104. }
  1105. idx = analog_get_index(ast, p, 1);
  1106. x = 0;
  1107. if (p->origcid_num) {
  1108. ast_copy_string(p->cid_num, p->origcid_num, sizeof(p->cid_num));
  1109. ast_free(p->origcid_num);
  1110. p->origcid_num = NULL;
  1111. }
  1112. if (p->origcid_name) {
  1113. ast_copy_string(p->cid_name, p->origcid_name, sizeof(p->cid_name));
  1114. ast_free(p->origcid_name);
  1115. p->origcid_name = NULL;
  1116. }
  1117. analog_dsp_set_digitmode(p, ANALOG_DIGITMODE_DTMF);
  1118. ast_debug(1, "Hangup: channel: %d index = %d, normal = %d, callwait = %d, thirdcall = %d\n",
  1119. p->channel, idx, p->subs[ANALOG_SUB_REAL].allocd, p->subs[ANALOG_SUB_CALLWAIT].allocd, p->subs[ANALOG_SUB_THREEWAY].allocd);
  1120. if (idx > -1) {
  1121. /* Real channel, do some fixup */
  1122. p->subs[idx].owner = NULL;
  1123. p->polarity = POLARITY_IDLE;
  1124. analog_set_linear_mode(p, idx, 0);
  1125. switch (idx) {
  1126. case ANALOG_SUB_REAL:
  1127. if (p->subs[ANALOG_SUB_CALLWAIT].allocd && p->subs[ANALOG_SUB_THREEWAY].allocd) {
  1128. ast_debug(1, "Normal call hung up with both three way call and a call waiting call in place?\n");
  1129. if (p->subs[ANALOG_SUB_CALLWAIT].inthreeway) {
  1130. /* We had flipped over to answer a callwait and now it's gone */
  1131. ast_debug(1, "We were flipped over to the callwait, moving back and unowning.\n");
  1132. /* Move to the call-wait, but un-own us until they flip back. */
  1133. analog_swap_subs(p, ANALOG_SUB_CALLWAIT, ANALOG_SUB_REAL);
  1134. analog_unalloc_sub(p, ANALOG_SUB_CALLWAIT);
  1135. analog_set_new_owner(p, NULL);
  1136. } else {
  1137. /* The three way hung up, but we still have a call wait */
  1138. ast_debug(1, "We were in the threeway and have a callwait still. Ditching the threeway.\n");
  1139. analog_swap_subs(p, ANALOG_SUB_THREEWAY, ANALOG_SUB_REAL);
  1140. analog_unalloc_sub(p, ANALOG_SUB_THREEWAY);
  1141. if (p->subs[ANALOG_SUB_REAL].inthreeway) {
  1142. /* This was part of a three way call. Immediately make way for
  1143. another call */
  1144. ast_debug(1, "Call was complete, setting owner to former third call\n");
  1145. analog_set_inthreeway(p, ANALOG_SUB_REAL, 0);
  1146. analog_set_new_owner(p, p->subs[ANALOG_SUB_REAL].owner);
  1147. } else {
  1148. /* This call hasn't been completed yet... Set owner to NULL */
  1149. ast_debug(1, "Call was incomplete, setting owner to NULL\n");
  1150. analog_set_new_owner(p, NULL);
  1151. }
  1152. }
  1153. } else if (p->subs[ANALOG_SUB_CALLWAIT].allocd) {
  1154. /* Need to hold the lock for real-call, private, and call-waiting call */
  1155. analog_lock_sub_owner(p, ANALOG_SUB_CALLWAIT);
  1156. if (!p->subs[ANALOG_SUB_CALLWAIT].owner) {
  1157. /* The call waiting call dissappeared. */
  1158. analog_set_new_owner(p, NULL);
  1159. break;
  1160. }
  1161. /* Move to the call-wait and switch back to them. */
  1162. analog_swap_subs(p, ANALOG_SUB_CALLWAIT, ANALOG_SUB_REAL);
  1163. analog_unalloc_sub(p, ANALOG_SUB_CALLWAIT);
  1164. analog_set_new_owner(p, p->subs[ANALOG_SUB_REAL].owner);
  1165. if (ast_channel_state(p->owner) != AST_STATE_UP) {
  1166. ast_queue_control(p->subs[ANALOG_SUB_REAL].owner, AST_CONTROL_ANSWER);
  1167. }
  1168. ast_queue_unhold(p->subs[ANALOG_SUB_REAL].owner);
  1169. /* Unlock the call-waiting call that we swapped to real-call. */
  1170. ast_channel_unlock(p->subs[ANALOG_SUB_REAL].owner);
  1171. } else if (p->subs[ANALOG_SUB_THREEWAY].allocd) {
  1172. analog_swap_subs(p, ANALOG_SUB_THREEWAY, ANALOG_SUB_REAL);
  1173. analog_unalloc_sub(p, ANALOG_SUB_THREEWAY);
  1174. if (p->subs[ANALOG_SUB_REAL].inthreeway) {
  1175. /* This was part of a three way call. Immediately make way for
  1176. another call */
  1177. ast_debug(1, "Call was complete, setting owner to former third call\n");
  1178. analog_set_inthreeway(p, ANALOG_SUB_REAL, 0);
  1179. analog_set_new_owner(p, p->subs[ANALOG_SUB_REAL].owner);
  1180. } else {
  1181. /* This call hasn't been completed yet... Set owner to NULL */
  1182. ast_debug(1, "Call was incomplete, setting owner to NULL\n");
  1183. analog_set_new_owner(p, NULL);
  1184. }
  1185. }
  1186. break;
  1187. case ANALOG_SUB_CALLWAIT:
  1188. /* Ditch the holding callwait call, and immediately make it available */
  1189. if (p->subs[ANALOG_SUB_CALLWAIT].inthreeway) {
  1190. /* Need to hold the lock for call-waiting call, private, and 3-way call */
  1191. analog_lock_sub_owner(p, ANALOG_SUB_THREEWAY);
  1192. /* This is actually part of a three way, placed on hold. Place the third part
  1193. on music on hold now */
  1194. if (p->subs[ANALOG_SUB_THREEWAY].owner) {
  1195. ast_queue_hold(p->subs[ANALOG_SUB_THREEWAY].owner, p->mohsuggest);
  1196. }
  1197. analog_set_inthreeway(p, ANALOG_SUB_THREEWAY, 0);
  1198. /* Make it the call wait now */
  1199. analog_swap_subs(p, ANALOG_SUB_CALLWAIT, ANALOG_SUB_THREEWAY);
  1200. analog_unalloc_sub(p, ANALOG_SUB_THREEWAY);
  1201. if (p->subs[ANALOG_SUB_CALLWAIT].owner) {
  1202. /* Unlock the 3-way call that we swapped to call-waiting call. */
  1203. ast_channel_unlock(p->subs[ANALOG_SUB_CALLWAIT].owner);
  1204. }
  1205. } else {
  1206. analog_unalloc_sub(p, ANALOG_SUB_CALLWAIT);
  1207. }
  1208. break;
  1209. case ANALOG_SUB_THREEWAY:
  1210. /* Need to hold the lock for 3-way call, private, and call-waiting call */
  1211. analog_lock_sub_owner(p, ANALOG_SUB_CALLWAIT);
  1212. if (p->subs[ANALOG_SUB_CALLWAIT].inthreeway) {
  1213. /* The other party of the three way call is currently in a call-wait state.
  1214. Start music on hold for them, and take the main guy out of the third call */
  1215. analog_set_inthreeway(p, ANALOG_SUB_CALLWAIT, 0);
  1216. if (p->subs[ANALOG_SUB_CALLWAIT].owner) {
  1217. ast_queue_hold(p->subs[ANALOG_SUB_CALLWAIT].owner, p->mohsuggest);
  1218. }
  1219. }
  1220. if (p->subs[ANALOG_SUB_CALLWAIT].owner) {
  1221. ast_channel_unlock(p->subs[ANALOG_SUB_CALLWAIT].owner);
  1222. }
  1223. analog_set_inthreeway(p, ANALOG_SUB_REAL, 0);
  1224. /* If this was part of a three way call index, let us make
  1225. another three way call */
  1226. analog_unalloc_sub(p, ANALOG_SUB_THREEWAY);
  1227. break;
  1228. default:
  1229. /*
  1230. * Should never happen.
  1231. * This wasn't any sort of call, so how are we an index?
  1232. */
  1233. ast_log(LOG_ERROR, "Index found but not any type of call?\n");
  1234. break;
  1235. }
  1236. }
  1237. if (!p->subs[ANALOG_SUB_REAL].owner && !p->subs[ANALOG_SUB_CALLWAIT].owner && !p->subs[ANALOG_SUB_THREEWAY].owner) {
  1238. analog_set_new_owner(p, NULL);
  1239. analog_set_ringtimeout(p, 0);
  1240. analog_set_confirmanswer(p, 0);
  1241. analog_set_pulsedial(p, 0);
  1242. analog_set_outgoing(p, 0);
  1243. p->onhooktime = time(NULL);
  1244. p->cidrings = 1;
  1245. /* Perform low level hangup if no owner left */
  1246. res = analog_on_hook(p);
  1247. if (res < 0) {
  1248. ast_log(LOG_WARNING, "Unable to hangup line %s\n", ast_channel_name(ast));
  1249. }
  1250. switch (p->sig) {
  1251. case ANALOG_SIG_FXOGS:
  1252. case ANALOG_SIG_FXOLS:
  1253. case ANALOG_SIG_FXOKS:
  1254. /* If they're off hook, try playing congestion */
  1255. if (analog_is_off_hook(p)) {
  1256. analog_hangup_polarityswitch(p);
  1257. analog_play_tone(p, ANALOG_SUB_REAL, ANALOG_TONE_CONGESTION);
  1258. } else {
  1259. analog_play_tone(p, ANALOG_SUB_REAL, -1);
  1260. }
  1261. break;
  1262. case ANALOG_SIG_FXSGS:
  1263. case ANALOG_SIG_FXSLS:
  1264. case ANALOG_SIG_FXSKS:
  1265. /* Make sure we're not made available for at least two seconds assuming
  1266. we were actually used for an inbound or outbound call. */
  1267. if (ast_channel_state(ast) != AST_STATE_RESERVED) {
  1268. time(&p->guardtime);
  1269. p->guardtime += 2;
  1270. }
  1271. break;
  1272. default:
  1273. analog_play_tone(p, ANALOG_SUB_REAL, -1);
  1274. break;
  1275. }
  1276. analog_set_echocanceller(p, 0);
  1277. x = 0;
  1278. ast_channel_setoption(ast,AST_OPTION_TONE_VERIFY,&x,sizeof(char),0);
  1279. ast_channel_setoption(ast,AST_OPTION_TDD,&x,sizeof(char),0);
  1280. p->callwaitcas = 0;
  1281. analog_set_callwaiting(p, p->permcallwaiting);
  1282. p->hidecallerid = p->permhidecallerid;
  1283. analog_set_dialing(p, 0);
  1284. analog_update_conf(p);
  1285. analog_all_subchannels_hungup(p);
  1286. }
  1287. analog_stop_callwait(p);
  1288. ast_verb(3, "Hanging up on '%s'\n", ast_channel_name(ast));
  1289. return 0;
  1290. }
  1291. int analog_answer(struct analog_pvt *p, struct ast_channel *ast)
  1292. {
  1293. int res = 0;
  1294. int idx;
  1295. int oldstate = ast_channel_state(ast);
  1296. ast_debug(1, "%s %d\n", __FUNCTION__, p->channel);
  1297. ast_setstate(ast, AST_STATE_UP);
  1298. idx = analog_get_index(ast, p, 1);
  1299. if (idx < 0) {
  1300. idx = ANALOG_SUB_REAL;
  1301. }
  1302. switch (p->sig) {
  1303. case ANALOG_SIG_FXSLS:
  1304. case ANALOG_SIG_FXSGS:
  1305. case ANALOG_SIG_FXSKS:
  1306. analog_set_ringtimeout(p, 0);
  1307. /* Fall through */
  1308. case ANALOG_SIG_EM:
  1309. case ANALOG_SIG_EM_E1:
  1310. case ANALOG_SIG_EMWINK:
  1311. case ANALOG_SIG_FEATD:
  1312. case ANALOG_SIG_FEATDMF:
  1313. case ANALOG_SIG_FEATDMF_TA:
  1314. case ANALOG_SIG_E911:
  1315. case ANALOG_SIG_FGC_CAMA:
  1316. case ANALOG_SIG_FGC_CAMAMF:
  1317. case ANALOG_SIG_FEATB:
  1318. case ANALOG_SIG_SF:
  1319. case ANALOG_SIG_SFWINK:
  1320. case ANALOG_SIG_SF_FEATD:
  1321. case ANALOG_SIG_SF_FEATDMF:
  1322. case ANALOG_SIG_SF_FEATB:
  1323. case ANALOG_SIG_FXOLS:
  1324. case ANALOG_SIG_FXOGS:
  1325. case ANALOG_SIG_FXOKS:
  1326. /* Pick up the line */
  1327. ast_debug(1, "Took %s off hook\n", ast_channel_name(ast));
  1328. if (p->hanguponpolarityswitch) {
  1329. gettimeofday(&p->polaritydelaytv, NULL);
  1330. }
  1331. res = analog_off_hook(p);
  1332. analog_play_tone(p, idx, -1);
  1333. analog_set_dialing(p, 0);
  1334. if ((idx == ANALOG_SUB_REAL) && p->subs[ANALOG_SUB_THREEWAY].inthreeway) {
  1335. if (oldstate == AST_STATE_RINGING) {
  1336. ast_debug(1, "Finally swapping real and threeway\n");
  1337. analog_play_tone(p, ANALOG_SUB_THREEWAY, -1);
  1338. analog_swap_subs(p, ANALOG_SUB_THREEWAY, ANALOG_SUB_REAL);
  1339. analog_set_new_owner(p, p->subs[ANALOG_SUB_REAL].owner);
  1340. }
  1341. }
  1342. switch (p->sig) {
  1343. case ANALOG_SIG_FXSLS:
  1344. case ANALOG_SIG_FXSKS:
  1345. case ANALOG_SIG_FXSGS:
  1346. analog_set_echocanceller(p, 1);
  1347. analog_train_echocanceller(p);
  1348. break;
  1349. case ANALOG_SIG_FXOLS:
  1350. case ANALOG_SIG_FXOKS:
  1351. case ANALOG_SIG_FXOGS:
  1352. analog_answer_polarityswitch(p);
  1353. break;
  1354. default:
  1355. break;
  1356. }
  1357. break;
  1358. default:
  1359. ast_log(LOG_WARNING, "Don't know how to answer signalling %d (channel %d)\n", p->sig, p->channel);
  1360. res = -1;
  1361. break;
  1362. }
  1363. ast_setstate(ast, AST_STATE_UP);
  1364. return res;
  1365. }
  1366. static int analog_handles_digit(struct ast_frame *f)
  1367. {
  1368. char subclass = toupper(f->subclass.integer);
  1369. switch (subclass) {
  1370. case '1':
  1371. case '2':
  1372. case '3':
  1373. case '4':
  1374. case '5':
  1375. case '6':
  1376. case '7':
  1377. case '9':
  1378. case 'A':
  1379. case 'B':
  1380. case 'C':
  1381. case 'D':
  1382. case 'E':
  1383. case 'F':
  1384. return 1;
  1385. default:
  1386. return 0;
  1387. }
  1388. }
  1389. void analog_handle_dtmf(struct analog_pvt *p, struct ast_channel *ast, enum analog_sub idx, struct ast_frame **dest)
  1390. {
  1391. struct ast_frame *f = *dest;
  1392. ast_debug(1, "%s DTMF digit: 0x%02X '%c' on %s\n",
  1393. f->frametype == AST_FRAME_DTMF_BEGIN ? "Begin" : "End",
  1394. (unsigned)f->subclass.integer, f->subclass.integer, ast_channel_name(ast));
  1395. if (analog_check_confirmanswer(p)) {
  1396. if (f->frametype == AST_FRAME_DTMF_END) {
  1397. ast_debug(1, "Confirm answer on %s!\n", ast_channel_name(ast));
  1398. /* Upon receiving a DTMF digit, consider this an answer confirmation instead
  1399. of a DTMF digit */
  1400. p->subs[idx].f.frametype = AST_FRAME_CONTROL;
  1401. p->subs[idx].f.subclass.integer = AST_CONTROL_ANSWER;
  1402. /* Reset confirmanswer so DTMF's will behave properly for the duration of the call */
  1403. analog_set_confirmanswer(p, 0);
  1404. } else {
  1405. p->subs[idx].f.frametype = AST_FRAME_NULL;
  1406. p->subs[idx].f.subclass.integer = 0;
  1407. }
  1408. *dest = &p->subs[idx].f;
  1409. } else if (p->callwaitcas) {
  1410. if (f->frametype == AST_FRAME_DTMF_END) {
  1411. if ((f->subclass.integer == 'A') || (f->subclass.integer == 'D')) {
  1412. ast_debug(1, "Got some DTMF, but it's for the CAS\n");
  1413. p->caller.id.name.str = p->callwait_name;
  1414. p->caller.id.number.str = p->callwait_num;
  1415. analog_send_callerid(p, 1, &p->caller);
  1416. }
  1417. if (analog_handles_digit(f)) {
  1418. p->callwaitcas = 0;
  1419. }
  1420. }
  1421. p->subs[idx].f.frametype = AST_FRAME_NULL;
  1422. p->subs[idx].f.subclass.integer = 0;
  1423. *dest = &p->subs[idx].f;
  1424. } else {
  1425. analog_cb_handle_dtmf(p, ast, idx, dest);
  1426. }
  1427. }
  1428. static int analog_my_getsigstr(struct ast_channel *chan, char *str, const char *term, int ms)
  1429. {
  1430. char c;
  1431. *str = 0; /* start with empty output buffer */
  1432. for (;;) {
  1433. /* Wait for the first digit (up to specified ms). */
  1434. c = ast_waitfordigit(chan, ms);
  1435. /* if timeout, hangup or error, return as such */
  1436. if (c < 1) {
  1437. return c;
  1438. }
  1439. *str++ = c;
  1440. *str = 0;
  1441. if (strchr(term, c)) {
  1442. return 1;
  1443. }
  1444. }
  1445. }
  1446. static int analog_handle_notify_message(struct ast_channel *chan, struct analog_pvt *p, int cid_flags, int neon_mwievent)
  1447. {
  1448. if (analog_callbacks.handle_notify_message) {
  1449. analog_callbacks.handle_notify_message(chan, p->chan_pvt, cid_flags, neon_mwievent);
  1450. return 0;
  1451. }
  1452. return -1;
  1453. }
  1454. static void analog_increase_ss_count(void)
  1455. {
  1456. if (analog_callbacks.increase_ss_count) {
  1457. analog_callbacks.increase_ss_count();
  1458. }
  1459. }
  1460. static void analog_decrease_ss_count(void)
  1461. {
  1462. if (analog_callbacks.decrease_ss_count) {
  1463. analog_callbacks.decrease_ss_count();
  1464. }
  1465. }
  1466. static int analog_distinctive_ring(struct ast_channel *chan, struct analog_pvt *p, int idx, int *ringdata)
  1467. {
  1468. if (!p->usedistinctiveringdetection) {
  1469. return 0;
  1470. }
  1471. if (analog_callbacks.distinctive_ring) {
  1472. return analog_callbacks.distinctive_ring(chan, p->chan_pvt, idx, ringdata);
  1473. }
  1474. return -1;
  1475. }
  1476. static void analog_get_and_handle_alarms(struct analog_pvt *p)
  1477. {
  1478. if (analog_callbacks.get_and_handle_alarms) {
  1479. analog_callbacks.get_and_handle_alarms(p->chan_pvt);
  1480. }
  1481. }
  1482. static void *analog_get_bridged_channel(struct ast_channel *chan)
  1483. {
  1484. if (analog_callbacks.get_sigpvt_bridged_channel) {
  1485. return analog_callbacks.get_sigpvt_bridged_channel(chan);
  1486. }
  1487. return NULL;
  1488. }
  1489. static int analog_get_sub_fd(struct analog_pvt *p, enum analog_sub sub)
  1490. {
  1491. if (analog_callbacks.get_sub_fd) {
  1492. return analog_callbacks.get_sub_fd(p->chan_pvt, sub);
  1493. }
  1494. return -1;
  1495. }
  1496. #define ANALOG_NEED_MFDETECT(p) (((p)->sig == ANALOG_SIG_FEATDMF) || ((p)->sig == ANALOG_SIG_FEATDMF_TA) || ((p)->sig == ANALOG_SIG_E911) || ((p)->sig == ANALOG_SIG_FGC_CAMA) || ((p)->sig == ANALOG_SIG_FGC_CAMAMF) || ((p)->sig == ANALOG_SIG_FEATB))
  1497. static int analog_canmatch_featurecode(const char *pickupexten, const char *exten)
  1498. {
  1499. int extlen = strlen(exten);
  1500. if (!extlen) {
  1501. return 1;
  1502. }
  1503. if (extlen < strlen(pickupexten) && !strncmp(pickupexten, exten, extlen)) {
  1504. return 1;
  1505. }
  1506. /* hardcoded features are *60, *67, *69, *70, *72, *73, *78, *79, *82, *0 */
  1507. if (exten[0] == '*' && extlen < 3) {
  1508. if (extlen == 1) {
  1509. return 1;
  1510. }
  1511. /* "*0" should be processed before it gets here */
  1512. switch (exten[1]) {
  1513. case '6':
  1514. case '7':
  1515. case '8':
  1516. return 1;
  1517. }
  1518. }
  1519. return 0;
  1520. }
  1521. static void *__analog_ss_thread(void *data)
  1522. {
  1523. struct analog_pvt *p = data;
  1524. struct ast_channel *chan = p->ss_astchan;
  1525. char exten[AST_MAX_EXTENSION] = "";
  1526. char exten2[AST_MAX_EXTENSION] = "";
  1527. char dtmfcid[300];
  1528. char dtmfbuf[300];
  1529. char namebuf[ANALOG_MAX_CID];
  1530. char numbuf[ANALOG_MAX_CID];
  1531. char *name = NULL, *number = NULL;
  1532. int flags = 0;
  1533. struct ast_smdi_md_message *smdi_msg = NULL;
  1534. int timeout;
  1535. int getforward = 0;
  1536. char *s1, *s2;
  1537. int len = 0;
  1538. int res;
  1539. int idx;
  1540. struct ast_callid *callid;
  1541. RAII_VAR(struct ast_features_pickup_config *, pickup_cfg, NULL, ao2_cleanup);
  1542. const char *pickupexten;
  1543. analog_increase_ss_count();
  1544. ast_debug(1, "%s %d\n", __FUNCTION__, p->channel);
  1545. if (!chan) {
  1546. /* What happened to the channel? */
  1547. goto quit;
  1548. }
  1549. if ((callid = ast_channel_callid(chan))) {
  1550. ast_callid_threadassoc_add(callid);
  1551. ast_callid_unref(callid);
  1552. }
  1553. /* in the bizarre case where the channel has become a zombie before we
  1554. even get started here, abort safely
  1555. */
  1556. if (!ast_channel_tech_pvt(chan)) {
  1557. ast_log(LOG_WARNING, "Channel became a zombie before simple switch could be started (%s)\n", ast_channel_name(chan));
  1558. ast_hangup(chan);
  1559. goto quit;
  1560. }
  1561. ast_verb(3, "Starting simple switch on '%s'\n", ast_channel_name(chan));
  1562. idx = analog_get_index(chan, p, 0);
  1563. if (idx < 0) {
  1564. ast_hangup(chan);
  1565. goto quit;
  1566. }
  1567. ast_channel_lock(chan);
  1568. pickup_cfg = ast_get_chan_features_pickup_config(chan);
  1569. if (!pickup_cfg) {
  1570. ast_log(LOG_ERROR, "Unable to retrieve pickup configuration options. Unable to detect call pickup extension\n");
  1571. pickupexten = "";
  1572. } else {
  1573. pickupexten = ast_strdupa(pickup_cfg->pickupexten);
  1574. }
  1575. ast_channel_unlock(chan);
  1576. analog_dsp_reset_and_flush_digits(p);
  1577. switch (p->sig) {
  1578. case ANALOG_SIG_FEATD:
  1579. case ANALOG_SIG_FEATDMF:
  1580. case ANALOG_SIG_FEATDMF_TA:
  1581. case ANALOG_SIG_E911:
  1582. case ANALOG_SIG_FGC_CAMAMF:
  1583. case ANALOG_SIG_FEATB:
  1584. case ANALOG_SIG_EMWINK:
  1585. case ANALOG_SIG_SF_FEATD:
  1586. case ANALOG_SIG_SF_FEATDMF:
  1587. case ANALOG_SIG_SF_FEATB:
  1588. case ANALOG_SIG_SFWINK:
  1589. if (analog_wink(p, idx))
  1590. goto quit;
  1591. /* Fall through */
  1592. case ANALOG_SIG_EM:
  1593. case ANALOG_SIG_EM_E1:
  1594. case ANALOG_SIG_SF:
  1595. case ANALOG_SIG_FGC_CAMA:
  1596. res = analog_play_tone(p, idx, -1);
  1597. analog_dsp_reset_and_flush_digits(p);
  1598. /* set digit mode appropriately */
  1599. if (ANALOG_NEED_MFDETECT(p)) {
  1600. analog_dsp_set_digitmode(p, ANALOG_DIGITMODE_MF);
  1601. } else {
  1602. analog_dsp_set_digitmode(p, ANALOG_DIGITMODE_DTMF);
  1603. }
  1604. memset(dtmfbuf, 0, sizeof(dtmfbuf));
  1605. /* Wait for the first digit only if immediate=no */
  1606. if (!p->immediate) {
  1607. /* Wait for the first digit (up to 5 seconds). */
  1608. res = ast_waitfordigit(chan, 5000);
  1609. } else {
  1610. res = 0;
  1611. }
  1612. if (res > 0) {
  1613. /* save first char */
  1614. dtmfbuf[0] = res;
  1615. switch (p->sig) {
  1616. case ANALOG_SIG_FEATD:
  1617. case ANALOG_SIG_SF_FEATD:
  1618. res = analog_my_getsigstr(chan, dtmfbuf + 1, "*", 3000);
  1619. if (res > 0) {
  1620. res = analog_my_getsigstr(chan, dtmfbuf + strlen(dtmfbuf), "*", 3000);
  1621. }
  1622. if (res < 1) {
  1623. analog_dsp_reset_and_flush_digits(p);
  1624. }
  1625. break;
  1626. case ANALOG_SIG_FEATDMF_TA:
  1627. res = analog_my_getsigstr(chan, dtmfbuf + 1, "#", 3000);
  1628. if (res < 1) {
  1629. analog_dsp_reset_and_flush_digits(p);
  1630. }
  1631. if (analog_wink(p, idx)) {
  1632. goto quit;
  1633. }
  1634. dtmfbuf[0] = 0;
  1635. /* Wait for the first digit (up to 5 seconds). */
  1636. res = ast_waitfordigit(chan, 5000);
  1637. if (res <= 0) {
  1638. break;
  1639. }
  1640. dtmfbuf[0] = res;
  1641. /* fall through intentionally */
  1642. case ANALOG_SIG_FEATDMF:
  1643. case ANALOG_SIG_E911:
  1644. case ANALOG_SIG_FGC_CAMAMF:
  1645. case ANALOG_SIG_SF_FEATDMF:
  1646. res = analog_my_getsigstr(chan, dtmfbuf + 1, "#", 3000);
  1647. /* if international caca, do it again to get real ANO */
  1648. if ((p->sig == ANALOG_SIG_FEATDMF) && (dtmfbuf[1] != '0')
  1649. && (strlen(dtmfbuf) != 14)) {
  1650. if (analog_wink(p, idx)) {
  1651. goto quit;
  1652. }
  1653. dtmfbuf[0] = 0;
  1654. /* Wait for the first digit (up to 5 seconds). */
  1655. res = ast_waitfordigit(chan, 5000);
  1656. if (res <= 0) {
  1657. break;
  1658. }
  1659. dtmfbuf[0] = res;
  1660. res = analog_my_getsigstr(chan, dtmfbuf + 1, "#", 3000);
  1661. }
  1662. if (res > 0) {
  1663. /* if E911, take off hook */
  1664. if (p->sig == ANALOG_SIG_E911) {
  1665. analog_off_hook(p);
  1666. }
  1667. res = analog_my_getsigstr(chan, dtmfbuf + strlen(dtmfbuf), "#", 3000);
  1668. }
  1669. if (res < 1) {
  1670. analog_dsp_reset_and_flush_digits(p);
  1671. }
  1672. break;
  1673. case ANALOG_SIG_FEATB:
  1674. case ANALOG_SIG_SF_FEATB:
  1675. res = analog_my_getsigstr(chan, dtmfbuf + 1, "#", 3000);
  1676. if (res < 1) {
  1677. analog_dsp_reset_and_flush_digits(p);
  1678. }
  1679. break;
  1680. case ANALOG_SIG_EMWINK:
  1681. /* if we received a '*', we are actually receiving Feature Group D
  1682. dial syntax, so use that mode; otherwise, fall through to normal
  1683. mode
  1684. */
  1685. if (res == '*') {
  1686. res = analog_my_getsigstr(chan, dtmfbuf + 1, "*", 3000);
  1687. if (res > 0) {
  1688. res = analog_my_getsigstr(chan, dtmfbuf + strlen(dtmfbuf), "*", 3000);
  1689. }
  1690. if (res < 1) {
  1691. analog_dsp_reset_and_flush_digits(p);
  1692. }
  1693. break;
  1694. }
  1695. default:
  1696. /* If we got the first digit, get the rest */
  1697. len = 1;
  1698. dtmfbuf[len] = '\0';
  1699. while ((len < AST_MAX_EXTENSION-1) && ast_matchmore_extension(chan, ast_channel_context(chan), dtmfbuf, 1, p->cid_num)) {
  1700. if (ast_exists_extension(chan, ast_channel_context(chan), dtmfbuf, 1, p->cid_num)) {
  1701. timeout = analog_matchdigittimeout;
  1702. } else {
  1703. timeout = analog_gendigittimeout;
  1704. }
  1705. res = ast_waitfordigit(chan, timeout);
  1706. if (res < 0) {
  1707. ast_debug(1, "waitfordigit returned < 0...\n");
  1708. ast_hangup(chan);
  1709. goto quit;
  1710. } else if (res) {
  1711. dtmfbuf[len++] = res;
  1712. dtmfbuf[len] = '\0';
  1713. } else {
  1714. break;
  1715. }
  1716. }
  1717. break;
  1718. }
  1719. }
  1720. if (res == -1) {
  1721. ast_log(LOG_WARNING, "getdtmf on channel %d: %s\n", p->channel, strerror(errno));
  1722. ast_hangup(chan);
  1723. goto quit;
  1724. } else if (res < 0) {
  1725. ast_debug(1, "Got hung up before digits finished\n");
  1726. ast_hangup(chan);
  1727. goto quit;
  1728. }
  1729. if (p->sig == ANALOG_SIG_FGC_CAMA) {
  1730. char anibuf[100];
  1731. if (ast_safe_sleep(chan,1000) == -1) {
  1732. ast_hangup(chan);
  1733. goto quit;
  1734. }
  1735. analog_off_hook(p);
  1736. analog_dsp_set_digitmode(p, ANALOG_DIGITMODE_MF);
  1737. res = analog_my_getsigstr(chan, anibuf, "#", 10000);
  1738. if ((res > 0) && (strlen(anibuf) > 2)) {
  1739. if (anibuf[strlen(anibuf) - 1] == '#') {
  1740. anibuf[strlen(anibuf) - 1] = 0;
  1741. }
  1742. ast_set_callerid(chan, anibuf + 2, NULL, anibuf + 2);
  1743. }
  1744. analog_dsp_set_digitmode(p, ANALOG_DIGITMODE_DTMF);
  1745. }
  1746. ast_copy_string(exten, dtmfbuf, sizeof(exten));
  1747. if (ast_strlen_zero(exten)) {
  1748. ast_copy_string(exten, "s", sizeof(exten));
  1749. }
  1750. if (p->sig == ANALOG_SIG_FEATD || p->sig == ANALOG_SIG_EMWINK) {
  1751. /* Look for Feature Group D on all E&M Wink and Feature Group D trunks */
  1752. if (exten[0] == '*') {
  1753. char *stringp=NULL;
  1754. ast_copy_string(exten2, exten, sizeof(exten2));
  1755. /* Parse out extension and callerid */
  1756. stringp=exten2 +1;
  1757. s1 = strsep(&stringp, "*");
  1758. s2 = strsep(&stringp, "*");
  1759. if (s2) {
  1760. if (!ast_strlen_zero(p->cid_num)) {
  1761. ast_set_callerid(chan, p->cid_num, NULL, p->cid_num);
  1762. } else {
  1763. ast_set_callerid(chan, s1, NULL, s1);
  1764. }
  1765. ast_copy_string(exten, s2, sizeof(exten));
  1766. } else {
  1767. ast_copy_string(exten, s1, sizeof(exten));
  1768. }
  1769. } else if (p->sig == ANALOG_SIG_FEATD) {
  1770. ast_log(LOG_WARNING, "Got a non-Feature Group D input on channel %d. Assuming E&M Wink instead\n", p->channel);
  1771. }
  1772. }
  1773. if ((p->sig == ANALOG_SIG_FEATDMF) || (p->sig == ANALOG_SIG_FEATDMF_TA)) {
  1774. if (exten[0] == '*') {
  1775. char *stringp=NULL;
  1776. struct ast_party_caller *caller;
  1777. ast_copy_string(exten2, exten, sizeof(exten2));
  1778. /* Parse out extension and callerid */
  1779. stringp=exten2 +1;
  1780. s1 = strsep(&stringp, "#");
  1781. s2 = strsep(&stringp, "#");
  1782. if (s2) {
  1783. if (!ast_strlen_zero(p->cid_num)) {
  1784. ast_set_callerid(chan, p->cid_num, NULL, p->cid_num);
  1785. } else {
  1786. if (*(s1 + 2)) {
  1787. ast_set_callerid(chan, s1 + 2, NULL, s1 + 2);
  1788. }
  1789. }
  1790. ast_copy_string(exten, s2 + 1, sizeof(exten));
  1791. } else {
  1792. ast_copy_string(exten, s1 + 2, sizeof(exten));
  1793. }
  1794. /* The first two digits are ani2 information. */
  1795. caller = ast_channel_caller(chan);
  1796. s1[2] = '\0';
  1797. caller->ani2 = atoi(s1);
  1798. } else {
  1799. ast_log(LOG_WARNING, "Got a non-Feature Group D input on channel %d. Assuming E&M Wink instead\n", p->channel);
  1800. }
  1801. }
  1802. if ((p->sig == ANALOG_SIG_E911) || (p->sig == ANALOG_SIG_FGC_CAMAMF)) {
  1803. if (exten[0] == '*') {
  1804. char *stringp=NULL;
  1805. ast_copy_string(exten2, exten, sizeof(exten2));
  1806. /* Parse out extension and callerid */
  1807. stringp=exten2 +1;
  1808. s1 = strsep(&stringp, "#");
  1809. s2 = strsep(&stringp, "#");
  1810. if (s2 && (*(s2 + 1) == '0')) {
  1811. if (*(s2 + 2)) {
  1812. ast_set_callerid(chan, s2 + 2, NULL, s2 + 2);
  1813. }
  1814. }
  1815. if (s1) {
  1816. ast_copy_string(exten, s1, sizeof(exten));
  1817. } else {
  1818. ast_copy_string(exten, "911", sizeof(exten));
  1819. }
  1820. } else {
  1821. ast_log(LOG_WARNING, "Got a non-E911/FGC CAMA input on channel %d. Assuming E&M Wink instead\n", p->channel);
  1822. }
  1823. }
  1824. if (p->sig == ANALOG_SIG_FEATB) {
  1825. if (exten[0] == '*') {
  1826. char *stringp=NULL;
  1827. ast_copy_string(exten2, exten, sizeof(exten2));
  1828. /* Parse out extension and callerid */
  1829. stringp=exten2 +1;
  1830. s1 = strsep(&stringp, "#");
  1831. ast_copy_string(exten, exten2 + 1, sizeof(exten));
  1832. } else {
  1833. ast_log(LOG_WARNING, "Got a non-Feature Group B input on channel %d. Assuming E&M Wink instead\n", p->channel);
  1834. }
  1835. }
  1836. if ((p->sig == ANALOG_SIG_FEATDMF) || (p->sig == ANALOG_SIG_FEATDMF_TA)) {
  1837. analog_wink(p, idx);
  1838. /*
  1839. * Some switches require a minimum guard time between the last
  1840. * FGD wink and something that answers immediately. This
  1841. * ensures it.
  1842. */
  1843. if (ast_safe_sleep(chan, 100)) {
  1844. ast_hangup(chan);
  1845. goto quit;
  1846. }
  1847. }
  1848. analog_set_echocanceller(p, 1);
  1849. analog_dsp_set_digitmode(p, ANALOG_DIGITMODE_DTMF);
  1850. if (ast_exists_extension(chan, ast_channel_context(chan), exten, 1,
  1851. ast_channel_caller(chan)->id.number.valid ? ast_channel_caller(chan)->id.number.str : NULL)) {
  1852. ast_channel_exten_set(chan, exten);
  1853. analog_dsp_reset_and_flush_digits(p);
  1854. res = ast_pbx_run(chan);
  1855. if (res) {
  1856. ast_log(LOG_WARNING, "PBX exited non-zero\n");
  1857. res = analog_play_tone(p, idx, ANALOG_TONE_CONGESTION);
  1858. }
  1859. goto quit;
  1860. } else {
  1861. ast_verb(3, "Unknown extension '%s' in context '%s' requested\n", exten, ast_channel_context(chan));
  1862. sleep(2);
  1863. res = analog_play_tone(p, idx, ANALOG_TONE_INFO);
  1864. if (res < 0) {
  1865. ast_log(LOG_WARNING, "Unable to start special tone on %d\n", p->channel);
  1866. } else {
  1867. sleep(1);
  1868. }
  1869. res = ast_streamfile(chan, "ss-noservice", ast_channel_language(chan));
  1870. if (res >= 0) {
  1871. ast_waitstream(chan, "");
  1872. }
  1873. res = analog_play_tone(p, idx, ANALOG_TONE_CONGESTION);
  1874. ast_hangup(chan);
  1875. goto quit;
  1876. }
  1877. break;
  1878. case ANALOG_SIG_FXOLS:
  1879. case ANALOG_SIG_FXOGS:
  1880. case ANALOG_SIG_FXOKS:
  1881. /* Read the first digit */
  1882. timeout = analog_firstdigittimeout;
  1883. /* If starting a threeway call, never timeout on the first digit so someone
  1884. can use flash-hook as a "hold" feature */
  1885. if (p->subs[ANALOG_SUB_THREEWAY].owner) {
  1886. timeout = 999999;
  1887. }
  1888. while (len < AST_MAX_EXTENSION-1) {
  1889. int is_exten_parking = 0;
  1890. /* Read digit unless it's supposed to be immediate, in which case the
  1891. only answer is 's' */
  1892. if (p->immediate) {
  1893. res = 's';
  1894. } else {
  1895. res = ast_waitfordigit(chan, timeout);
  1896. }
  1897. timeout = 0;
  1898. if (res < 0) {
  1899. ast_debug(1, "waitfordigit returned < 0...\n");
  1900. res = analog_play_tone(p, idx, -1);
  1901. ast_hangup(chan);
  1902. goto quit;
  1903. } else if (res) {
  1904. ast_debug(1,"waitfordigit returned '%c' (%d), timeout = %d\n", res, res, timeout);
  1905. exten[len++]=res;
  1906. exten[len] = '\0';
  1907. }
  1908. if (!ast_ignore_pattern(ast_channel_context(chan), exten)) {
  1909. analog_play_tone(p, idx, -1);
  1910. } else {
  1911. analog_play_tone(p, idx, ANALOG_TONE_DIALTONE);
  1912. }
  1913. if (ast_parking_provider_registered()) {
  1914. is_exten_parking = ast_parking_is_exten_park(ast_channel_context(chan), exten);
  1915. }
  1916. if (ast_exists_extension(chan, ast_channel_context(chan), exten, 1, p->cid_num) && !is_exten_parking) {
  1917. if (!res || !ast_matchmore_extension(chan, ast_channel_context(chan), exten, 1, p->cid_num)) {
  1918. if (getforward) {
  1919. /* Record this as the forwarding extension */
  1920. ast_copy_string(p->call_forward, exten, sizeof(p->call_forward));
  1921. ast_verb(3, "Setting call forward to '%s' on channel %d\n", p->call_forward, p->channel);
  1922. res = analog_play_tone(p, idx, ANALOG_TONE_DIALRECALL);
  1923. if (res) {
  1924. break;
  1925. }
  1926. usleep(500000);
  1927. res = analog_play_tone(p, idx, -1);
  1928. sleep(1);
  1929. memset(exten, 0, sizeof(exten));
  1930. res = analog_play_tone(p, idx, ANALOG_TONE_DIALTONE);
  1931. len = 0;
  1932. getforward = 0;
  1933. } else {
  1934. res = analog_play_tone(p, idx, -1);
  1935. ast_channel_lock(chan);
  1936. ast_channel_exten_set(chan, exten);
  1937. if (!ast_strlen_zero(p->cid_num)) {
  1938. if (!p->hidecallerid) {
  1939. ast_set_callerid(chan, p->cid_num, NULL, p->cid_num);
  1940. } else {
  1941. ast_set_callerid(chan, NULL, NULL, p->cid_num);
  1942. }
  1943. }
  1944. if (!ast_strlen_zero(p->cid_name)) {
  1945. if (!p->hidecallerid) {
  1946. ast_set_callerid(chan, NULL, p->cid_name, NULL);
  1947. }
  1948. }
  1949. ast_setstate(chan, AST_STATE_RING);
  1950. ast_channel_unlock(chan);
  1951. analog_set_echocanceller(p, 1);
  1952. res = ast_pbx_run(chan);
  1953. if (res) {
  1954. ast_log(LOG_WARNING, "PBX exited non-zero\n");
  1955. res = analog_play_tone(p, idx, ANALOG_TONE_CONGESTION);
  1956. }
  1957. goto quit;
  1958. }
  1959. } else {
  1960. /* It's a match, but they just typed a digit, and there is an ambiguous match,
  1961. so just set the timeout to analog_matchdigittimeout and wait some more */
  1962. timeout = analog_matchdigittimeout;
  1963. }
  1964. } else if (res == 0) {
  1965. ast_debug(1, "not enough digits (and no ambiguous match)...\n");
  1966. res = analog_play_tone(p, idx, ANALOG_TONE_CONGESTION);
  1967. analog_wait_event(p);
  1968. ast_hangup(chan);
  1969. goto quit;
  1970. } else if (p->callwaiting && !strcmp(exten, "*70")) {
  1971. ast_verb(3, "Disabling call waiting on %s\n", ast_channel_name(chan));
  1972. /* Disable call waiting if enabled */
  1973. analog_set_callwaiting(p, 0);
  1974. res = analog_play_tone(p, idx, ANALOG_TONE_DIALRECALL);
  1975. if (res) {
  1976. ast_log(LOG_WARNING, "Unable to do dial recall on channel %s: %s\n",
  1977. ast_channel_name(chan), strerror(errno));
  1978. }
  1979. len = 0;
  1980. memset(exten, 0, sizeof(exten));
  1981. timeout = analog_firstdigittimeout;
  1982. } else if (!strcmp(exten, pickupexten)) {
  1983. /* Scan all channels and see if there are any
  1984. * ringing channels that have call groups
  1985. * that equal this channels pickup group
  1986. */
  1987. if (idx == ANALOG_SUB_REAL) {
  1988. /* Switch us from Third call to Call Wait */
  1989. if (p->subs[ANALOG_SUB_THREEWAY].owner) {
  1990. /* If you make a threeway call and the *8# a call, it should actually
  1991. look like a callwait */
  1992. analog_alloc_sub(p, ANALOG_SUB_CALLWAIT);
  1993. analog_swap_subs(p, ANALOG_SUB_CALLWAIT, ANALOG_SUB_THREEWAY);
  1994. analog_unalloc_sub(p, ANALOG_SUB_THREEWAY);
  1995. }
  1996. analog_set_echocanceller(p, 1);
  1997. if (ast_pickup_call(chan)) {
  1998. ast_debug(1, "No call pickup possible...\n");
  1999. res = analog_play_tone(p, idx, ANALOG_TONE_CONGESTION);
  2000. analog_wait_event(p);
  2001. }
  2002. ast_hangup(chan);
  2003. goto quit;
  2004. } else {
  2005. ast_log(LOG_WARNING, "Huh? Got *8# on call not on real\n");
  2006. ast_hangup(chan);
  2007. goto quit;
  2008. }
  2009. } else if (!p->hidecallerid && !strcmp(exten, "*67")) {
  2010. ast_verb(3, "Disabling Caller*ID on %s\n", ast_channel_name(chan));
  2011. /* Disable Caller*ID if enabled */
  2012. p->hidecallerid = 1;
  2013. ast_party_number_free(&ast_channel_caller(chan)->id.number);
  2014. ast_party_number_init(&ast_channel_caller(chan)->id.number);
  2015. ast_party_name_free(&ast_channel_caller(chan)->id.name);
  2016. ast_party_name_init(&ast_channel_caller(chan)->id.name);
  2017. res = analog_play_tone(p, idx, ANALOG_TONE_DIALRECALL);
  2018. if (res) {
  2019. ast_log(LOG_WARNING, "Unable to do dial recall on channel %s: %s\n",
  2020. ast_channel_name(chan), strerror(errno));
  2021. }
  2022. len = 0;
  2023. memset(exten, 0, sizeof(exten));
  2024. timeout = analog_firstdigittimeout;
  2025. } else if (p->callreturn && !strcmp(exten, "*69")) {
  2026. res = 0;
  2027. if (!ast_strlen_zero(p->lastcid_num)) {
  2028. res = ast_say_digit_str(chan, p->lastcid_num, "", ast_channel_language(chan));
  2029. }
  2030. if (!res) {
  2031. res = analog_play_tone(p, idx, ANALOG_TONE_DIALRECALL);
  2032. }
  2033. break;
  2034. } else if (!strcmp(exten, "*78")) {
  2035. /* Do not disturb enabled */
  2036. analog_dnd(p, 1);
  2037. res = analog_play_tone(p, idx, ANALOG_TONE_DIALRECALL);
  2038. getforward = 0;
  2039. memset(exten, 0, sizeof(exten));
  2040. len = 0;
  2041. } else if (!strcmp(exten, "*79")) {
  2042. /* Do not disturb disabled */
  2043. analog_dnd(p, 0);
  2044. res = analog_play_tone(p, idx, ANALOG_TONE_DIALRECALL);
  2045. getforward = 0;
  2046. memset(exten, 0, sizeof(exten));
  2047. len = 0;
  2048. } else if (p->cancallforward && !strcmp(exten, "*72")) {
  2049. res = analog_play_tone(p, idx, ANALOG_TONE_DIALRECALL);
  2050. getforward = 1;
  2051. memset(exten, 0, sizeof(exten));
  2052. len = 0;
  2053. } else if (p->cancallforward && !strcmp(exten, "*73")) {
  2054. ast_verb(3, "Cancelling call forwarding on channel %d\n", p->channel);
  2055. res = analog_play_tone(p, idx, ANALOG_TONE_DIALRECALL);
  2056. memset(p->call_forward, 0, sizeof(p->call_forward));
  2057. getforward = 0;
  2058. memset(exten, 0, sizeof(exten));
  2059. len = 0;
  2060. } else if ((p->transfer || p->canpark) && is_exten_parking
  2061. && p->subs[ANALOG_SUB_THREEWAY].owner) {
  2062. struct ast_bridge_channel *bridge_channel;
  2063. /*
  2064. * This is a three way call, the main call being a real channel,
  2065. * and we're parking the first call.
  2066. */
  2067. ast_channel_lock(p->subs[ANALOG_SUB_THREEWAY].owner);
  2068. bridge_channel = ast_channel_get_bridge_channel(p->subs[ANALOG_SUB_THREEWAY].owner);
  2069. ast_channel_unlock(p->subs[ANALOG_SUB_THREEWAY].owner);
  2070. if (bridge_channel) {
  2071. if (!ast_parking_blind_transfer_park(bridge_channel, ast_channel_context(chan), exten, NULL, NULL)) {
  2072. /*
  2073. * Swap things around between the three-way and real call so we
  2074. * can hear where the channel got parked.
  2075. */
  2076. analog_lock_private(p);
  2077. analog_set_new_owner(p, p->subs[ANALOG_SUB_THREEWAY].owner);
  2078. analog_swap_subs(p, ANALOG_SUB_THREEWAY, ANALOG_SUB_REAL);
  2079. analog_unlock_private(p);
  2080. ast_verb(3, "%s: Parked call\n", ast_channel_name(chan));
  2081. ast_hangup(chan);
  2082. ao2_ref(bridge_channel, -1);
  2083. goto quit;
  2084. }
  2085. ao2_ref(bridge_channel, -1);
  2086. }
  2087. break;
  2088. } else if (!ast_strlen_zero(p->lastcid_num) && !strcmp(exten, "*60")) {
  2089. ast_verb(3, "Blacklisting number %s\n", p->lastcid_num);
  2090. res = ast_db_put("blacklist", p->lastcid_num, "1");
  2091. if (!res) {
  2092. res = analog_play_tone(p, idx, ANALOG_TONE_DIALRECALL);
  2093. memset(exten, 0, sizeof(exten));
  2094. len = 0;
  2095. }
  2096. } else if (p->hidecallerid && !strcmp(exten, "*82")) {
  2097. ast_verb(3, "Enabling Caller*ID on %s\n", ast_channel_name(chan));
  2098. /* Enable Caller*ID if enabled */
  2099. p->hidecallerid = 0;
  2100. ast_set_callerid(chan, p->cid_num, p->cid_name, NULL);
  2101. res = analog_play_tone(p, idx, ANALOG_TONE_DIALRECALL);
  2102. if (res) {
  2103. ast_log(LOG_WARNING, "Unable to do dial recall on channel %s: %s\n",
  2104. ast_channel_name(chan), strerror(errno));
  2105. }
  2106. len = 0;
  2107. memset(exten, 0, sizeof(exten));
  2108. timeout = analog_firstdigittimeout;
  2109. } else if (!strcmp(exten, "*0")) {
  2110. struct ast_channel *nbridge = p->subs[ANALOG_SUB_THREEWAY].owner;
  2111. struct analog_pvt *pbridge = NULL;
  2112. /* set up the private struct of the bridged one, if any */
  2113. if (nbridge) {
  2114. pbridge = analog_get_bridged_channel(nbridge);
  2115. }
  2116. if (pbridge && ISTRUNK(pbridge)) {
  2117. /* Clear out the dial buffer */
  2118. p->dop.dialstr[0] = '\0';
  2119. /* flash hookswitch */
  2120. if ((analog_flash(pbridge) == -1) && (errno != EINPROGRESS)) {
  2121. ast_log(LOG_WARNING,
  2122. "Unable to flash-hook bridged trunk from channel %s: %s\n",
  2123. ast_channel_name(nbridge), strerror(errno));
  2124. }
  2125. analog_swap_subs(p, ANALOG_SUB_REAL, ANALOG_SUB_THREEWAY);
  2126. analog_unalloc_sub(p, ANALOG_SUB_THREEWAY);
  2127. analog_set_new_owner(p, p->subs[ANALOG_SUB_REAL].owner);
  2128. ast_queue_unhold(p->subs[ANALOG_SUB_REAL].owner);
  2129. ast_hangup(chan);
  2130. goto quit;
  2131. } else {
  2132. analog_play_tone(p, idx, ANALOG_TONE_CONGESTION);
  2133. analog_wait_event(p);
  2134. analog_play_tone(p, idx, -1);
  2135. analog_swap_subs(p, ANALOG_SUB_REAL, ANALOG_SUB_THREEWAY);
  2136. analog_unalloc_sub(p, ANALOG_SUB_THREEWAY);
  2137. analog_set_new_owner(p, p->subs[ANALOG_SUB_REAL].owner);
  2138. ast_hangup(chan);
  2139. goto quit;
  2140. }
  2141. } else if (!ast_canmatch_extension(chan, ast_channel_context(chan), exten, 1,
  2142. ast_channel_caller(chan)->id.number.valid ? ast_channel_caller(chan)->id.number.str : NULL)
  2143. && !analog_canmatch_featurecode(pickupexten, exten)) {
  2144. ast_debug(1, "Can't match %s from '%s' in context %s\n", exten,
  2145. ast_channel_caller(chan)->id.number.valid && ast_channel_caller(chan)->id.number.str
  2146. ? ast_channel_caller(chan)->id.number.str : "<Unknown Caller>",
  2147. ast_channel_context(chan));
  2148. break;
  2149. }
  2150. if (!timeout) {
  2151. timeout = analog_gendigittimeout;
  2152. }
  2153. if (len && !ast_ignore_pattern(ast_channel_context(chan), exten)) {
  2154. analog_play_tone(p, idx, -1);
  2155. }
  2156. }
  2157. break;
  2158. case ANALOG_SIG_FXSLS:
  2159. case ANALOG_SIG_FXSGS:
  2160. case ANALOG_SIG_FXSKS:
  2161. /* check for SMDI messages */
  2162. if (p->use_smdi && p->smdi_iface) {
  2163. smdi_msg = ast_smdi_md_message_wait(p->smdi_iface, ANALOG_SMDI_MD_WAIT_TIMEOUT);
  2164. if (smdi_msg != NULL) {
  2165. ast_channel_exten_set(chan, smdi_msg->fwd_st);
  2166. if (smdi_msg->type == 'B')
  2167. pbx_builtin_setvar_helper(chan, "_SMDI_VM_TYPE", "b");
  2168. else if (smdi_msg->type == 'N')
  2169. pbx_builtin_setvar_helper(chan, "_SMDI_VM_TYPE", "u");
  2170. ast_debug(1, "Received SMDI message on %s\n", ast_channel_name(chan));
  2171. } else {
  2172. ast_log(LOG_WARNING, "SMDI enabled but no SMDI message present\n");
  2173. }
  2174. }
  2175. if (p->use_callerid && (p->cid_signalling == CID_SIG_SMDI && smdi_msg)) {
  2176. number = smdi_msg->calling_st;
  2177. /* If we want caller id, we're in a prering state due to a polarity reversal
  2178. * and we're set to use a polarity reversal to trigger the start of caller id,
  2179. * grab the caller id and wait for ringing to start... */
  2180. } else if (p->use_callerid && (ast_channel_state(chan) == AST_STATE_PRERING
  2181. && (p->cid_start == ANALOG_CID_START_POLARITY
  2182. || p->cid_start == ANALOG_CID_START_POLARITY_IN
  2183. || p->cid_start == ANALOG_CID_START_DTMF_NOALERT))) {
  2184. /* If set to use DTMF CID signalling, listen for DTMF */
  2185. if (p->cid_signalling == CID_SIG_DTMF) {
  2186. int k = 0;
  2187. int oldlinearity;
  2188. int timeout_ms;
  2189. int ms;
  2190. struct timeval start = ast_tvnow();
  2191. ast_debug(1, "Receiving DTMF cid on channel %s\n", ast_channel_name(chan));
  2192. oldlinearity = analog_set_linear_mode(p, idx, 0);
  2193. /*
  2194. * We are the only party interested in the Rx stream since
  2195. * we have not answered yet. We don't need or even want DTMF
  2196. * emulation. The DTMF digits can come so fast that emulation
  2197. * can drop some of them.
  2198. */
  2199. ast_channel_lock(chan);
  2200. ast_set_flag(ast_channel_flags(chan), AST_FLAG_END_DTMF_ONLY);
  2201. ast_channel_unlock(chan);
  2202. timeout_ms = 4000;/* This is a typical OFF time between rings. */
  2203. for (;;) {
  2204. struct ast_frame *f;
  2205. ms = ast_remaining_ms(start, timeout_ms);
  2206. res = ast_waitfor(chan, ms);
  2207. if (res <= 0) {
  2208. /*
  2209. * We do not need to restore the analog_set_linear_mode()
  2210. * or AST_FLAG_END_DTMF_ONLY flag settings since we
  2211. * are hanging up the channel.
  2212. */
  2213. ast_log(LOG_WARNING,
  2214. "DTMFCID timed out waiting for ring. Exiting simple switch\n");
  2215. ast_hangup(chan);
  2216. goto quit;
  2217. }
  2218. f = ast_read(chan);
  2219. if (!f) {
  2220. break;
  2221. }
  2222. if (f->frametype == AST_FRAME_DTMF) {
  2223. if (k < ARRAY_LEN(dtmfbuf) - 1) {
  2224. dtmfbuf[k++] = f->subclass.integer;
  2225. }
  2226. ast_debug(1, "CID got digit '%c'\n", f->subclass.integer);
  2227. start = ast_tvnow();
  2228. }
  2229. ast_frfree(f);
  2230. if (ast_channel_state(chan) == AST_STATE_RING ||
  2231. ast_channel_state(chan) == AST_STATE_RINGING) {
  2232. break; /* Got ring */
  2233. }
  2234. }
  2235. ast_channel_lock(chan);
  2236. ast_clear_flag(ast_channel_flags(chan), AST_FLAG_END_DTMF_ONLY);
  2237. ast_channel_unlock(chan);
  2238. dtmfbuf[k] = '\0';
  2239. analog_set_linear_mode(p, idx, oldlinearity);
  2240. /* Got cid and ring. */
  2241. ast_debug(1, "CID got string '%s'\n", dtmfbuf);
  2242. callerid_get_dtmf(dtmfbuf, dtmfcid, &flags);
  2243. ast_debug(1, "CID is '%s', flags %d\n", dtmfcid, flags);
  2244. /* If first byte is NULL, we have no cid */
  2245. if (!ast_strlen_zero(dtmfcid)) {
  2246. number = dtmfcid;
  2247. } else {
  2248. number = NULL;
  2249. }
  2250. /* If set to use V23 Signalling, launch our FSK gubbins and listen for it */
  2251. } else if ((p->cid_signalling == CID_SIG_V23) || (p->cid_signalling == CID_SIG_V23_JP)) {
  2252. namebuf[0] = 0;
  2253. numbuf[0] = 0;
  2254. if (!analog_start_cid_detect(p, p->cid_signalling)) {
  2255. int timeout = 10000; /* Ten seconds */
  2256. struct timeval start = ast_tvnow();
  2257. enum analog_event ev;
  2258. int off_ms;
  2259. int ms;
  2260. struct timeval off_start;
  2261. if (!p->usedistinctiveringdetection) {
  2262. /* Disable distinctive ring timeout count */
  2263. analog_set_ringtimeout(p, 0);
  2264. }
  2265. while ((ms = ast_remaining_ms(start, timeout))) {
  2266. res = analog_get_callerid(p, namebuf, numbuf, &ev, ms);
  2267. if (res < 0) {
  2268. ast_log(LOG_WARNING,
  2269. "CallerID returned with error on channel '%s'\n",
  2270. ast_channel_name(chan));
  2271. break;
  2272. }
  2273. if (res == 0) {
  2274. break;
  2275. }
  2276. if (res != 1) {
  2277. continue;
  2278. }
  2279. if (ev == ANALOG_EVENT_NOALARM) {
  2280. analog_set_alarm(p, 0);
  2281. }
  2282. if (p->cid_signalling == CID_SIG_V23_JP) {
  2283. if (ev == ANALOG_EVENT_RINGBEGIN) {
  2284. analog_off_hook(p);
  2285. usleep(1);
  2286. }
  2287. } else {
  2288. break;
  2289. }
  2290. }
  2291. name = namebuf;
  2292. number = numbuf;
  2293. if (p->cid_signalling == CID_SIG_V23_JP) {
  2294. analog_on_hook(p);
  2295. usleep(1);
  2296. }
  2297. /* Finished with Caller*ID, now wait for a ring to make sure there really is a call coming */
  2298. off_start = ast_tvnow();
  2299. off_ms = 4000;/* This is a typical OFF time between rings. */
  2300. while ((ms = ast_remaining_ms(off_start, off_ms))) {
  2301. struct ast_frame *f;
  2302. res = ast_waitfor(chan, ms);
  2303. if (res <= 0) {
  2304. ast_log(LOG_WARNING,
  2305. "CID timed out waiting for ring. Exiting simple switch\n");
  2306. analog_stop_cid_detect(p);
  2307. ast_hangup(chan);
  2308. goto quit;
  2309. }
  2310. if (!(f = ast_read(chan))) {
  2311. ast_log(LOG_WARNING, "Hangup received waiting for ring. Exiting simple switch\n");
  2312. analog_stop_cid_detect(p);
  2313. ast_hangup(chan);
  2314. goto quit;
  2315. }
  2316. ast_frfree(f);
  2317. if (ast_channel_state(chan) == AST_STATE_RING ||
  2318. ast_channel_state(chan) == AST_STATE_RINGING)
  2319. break; /* Got ring */
  2320. }
  2321. res = analog_distinctive_ring(chan, p, idx, NULL);
  2322. analog_stop_cid_detect(p);
  2323. if (res) {
  2324. goto quit;
  2325. }
  2326. } else {
  2327. ast_log(LOG_WARNING, "Unable to get caller ID space\n");
  2328. }
  2329. } else {
  2330. ast_log(LOG_WARNING,
  2331. "Channel %s in prering state, but I have nothing to do. Terminating simple switch, should be restarted by the actual ring.\n",
  2332. ast_channel_name(chan));
  2333. ast_hangup(chan);
  2334. goto quit;
  2335. }
  2336. } else if (p->use_callerid && p->cid_start == ANALOG_CID_START_RING) {
  2337. namebuf[0] = 0;
  2338. numbuf[0] = 0;
  2339. if (!analog_start_cid_detect(p, p->cid_signalling)) {
  2340. int timeout = 10000; /* Ten seconds */
  2341. struct timeval start = ast_tvnow();
  2342. enum analog_event ev;
  2343. int ring_data[RING_PATTERNS] = { 0 };
  2344. int ring_data_idx = 0;
  2345. int ms;
  2346. if (!p->usedistinctiveringdetection) {
  2347. /* Disable distinctive ring timeout count */
  2348. analog_set_ringtimeout(p, 0);
  2349. }
  2350. /*
  2351. add check /proc/wcfxs_status
  2352. */
  2353. #if 1
  2354. FILE *wcfxs_status_file;
  2355. char status_tmp[30];
  2356. char status_type,status_other[30],status_chan[20];
  2357. int status_no,file_res;
  2358. file_res = 255; status_type = status_other[0] = status_tmp[0] = '\0';
  2359. wcfxs_status_file=fopen("/proc/wcfxs_status","r");
  2360. if (wcfxs_status_file == NULL)
  2361. {
  2362. file_res = -1;//failed ,timeout is not changed
  2363. }
  2364. else
  2365. {
  2366. file_res = 0;//open file ,success
  2367. while (fgets(status_tmp,30,wcfxs_status_file)) {
  2368. sscanf(status_tmp,"%d:%c:%s",&status_no,&status_type,status_other);
  2369. sprintf(status_chan,"DAHDI/%d-1",status_no);
  2370. if (strncasecmp(ast_channel_name(chan),status_chan,strlen(status_chan))==0 && status_type == 'G')
  2371. {
  2372. file_res = 1;timeout = 200;/*chan is G, no FXO, changed timeout to 200*/break;}
  2373. }
  2374. fclose(wcfxs_status_file);
  2375. }
  2376. ast_verb(6, "TEST =====>%d,%d,%d,%c,%s\n", file_res,timeout,status_no, status_type,status_chan);
  2377. #endif
  2378. //end add by qz 2014.02.08
  2379. while ((ms = ast_remaining_ms(start, timeout))) {
  2380. res = analog_get_callerid(p, namebuf, numbuf, &ev, ms);
  2381. if (res < 0) {
  2382. ast_log(LOG_WARNING,
  2383. "CallerID returned with error on channel '%s'\n",
  2384. ast_channel_name(chan));
  2385. break;
  2386. }
  2387. if (res == 0) {
  2388. break;
  2389. }
  2390. if (res != 1) {
  2391. continue;
  2392. }
  2393. if (ev == ANALOG_EVENT_NOALARM) {
  2394. analog_set_alarm(p, 0);
  2395. } else if (ev == ANALOG_EVENT_POLARITY
  2396. && p->hanguponpolarityswitch
  2397. && p->polarity == POLARITY_REV) {
  2398. ast_debug(1,
  2399. "Hanging up due to polarity reversal on channel %d while detecting callerid\n",
  2400. p->channel);
  2401. p->polarity = POLARITY_IDLE;
  2402. analog_stop_cid_detect(p);
  2403. ast_hangup(chan);
  2404. goto quit;
  2405. } else if (ev == ANALOG_EVENT_RINGOFFHOOK
  2406. && p->usedistinctiveringdetection
  2407. && ring_data_idx < RING_PATTERNS) {
  2408. /*
  2409. * Detect callerid while collecting possible
  2410. * distinctive ring pattern.
  2411. */
  2412. ring_data[ring_data_idx] = p->ringt;
  2413. ++ring_data_idx;
  2414. }
  2415. }
  2416. name = namebuf;
  2417. number = numbuf;
  2418. res = analog_distinctive_ring(chan, p, idx, ring_data);
  2419. analog_stop_cid_detect(p);
  2420. if (res) {
  2421. goto quit;
  2422. }
  2423. //process cid num by zhi.qin 2016.07.14 for newRock gateway FXS
  2424. int for_i;
  2425. for (for_i = 0;for_i < strlen(number);for_i++)
  2426. {
  2427. number[for_i] = numbuf[for_i] & 0x3f;
  2428. //ast_verb(8,"===len=%d==>>number[%d]=%d\n",strlen(&number[for_i]),for_i,number[for_i]);
  2429. }
  2430. ast_verb(3, "channel: (%s) show CallerID: %s\n", ast_channel_name(chan),number);
  2431. // number = NULL;
  2432. #define WCGSM_GET_CH1_CALLERID 10
  2433. #define WCGSM_GET_CH2_CALLERID 20
  2434. #define WCGSM_GET_CH3_CALLERID 30
  2435. #define WCGSM_GET_CH4_CALLERID 40
  2436. #define WCGSM_GET_CH5_CALLERID 50
  2437. #define WCGSM_GET_CH6_CALLERID 60
  2438. #define WCGSM_GET_CH7_CALLERID 70
  2439. #define WCGSM_GET_CH8_CALLERID 80
  2440. int wcgsm_fd;
  2441. char gsm1_number[60];
  2442. int wcgsm_res;
  2443. int WCGSM_GET_CALLERID;
  2444. if (number[0]==0x0 ) {
  2445. //Get Caller ID from the GSM1 module
  2446. //At this point we are not sure if this is GSM1 channel but the worst case we
  2447. //will find that the ioctl(/dev/dahdi/wcgsm,callerid) will return empty
  2448. if(!strncasecmp(ast_channel_name(chan),"DAHDI/1-1",9))
  2449. WCGSM_GET_CALLERID = WCGSM_GET_CH1_CALLERID;
  2450. else if (!strncasecmp(ast_channel_name(chan),"DAHDI/2-1",9))
  2451. WCGSM_GET_CALLERID = WCGSM_GET_CH2_CALLERID;
  2452. else if (!strncasecmp(ast_channel_name(chan),"DAHDI/3-1",9))
  2453. WCGSM_GET_CALLERID = WCGSM_GET_CH3_CALLERID;
  2454. else if (!strncasecmp(ast_channel_name(chan) ,"DAHDI/4-1",9))
  2455. WCGSM_GET_CALLERID = WCGSM_GET_CH4_CALLERID;
  2456. else if (!strncasecmp(ast_channel_name(chan) ,"DAHDI/5-1",9))
  2457. WCGSM_GET_CALLERID = WCGSM_GET_CH5_CALLERID;
  2458. else if (!strncasecmp(ast_channel_name(chan) ,"DAHDI/6-1",9))
  2459. WCGSM_GET_CALLERID = WCGSM_GET_CH6_CALLERID;
  2460. else if (!strncasecmp(ast_channel_name(chan) ,"DAHDI/7-1",9))
  2461. WCGSM_GET_CALLERID = WCGSM_GET_CH7_CALLERID;
  2462. else if (!strncasecmp(ast_channel_name(chan) ,"DAHDI/8-1",9))
  2463. WCGSM_GET_CALLERID = WCGSM_GET_CH8_CALLERID;
  2464. //by qz for slot 1 E1 ,slot 2 Ana
  2465. else if (!strncasecmp(ast_channel_name(chan) ,"DAHDI/32-1",9))
  2466. WCGSM_GET_CALLERID = WCGSM_GET_CH5_CALLERID;
  2467. else if (!strncasecmp(ast_channel_name(chan) ,"DAHDI/33-1",9))
  2468. WCGSM_GET_CALLERID = WCGSM_GET_CH6_CALLERID;
  2469. else if (!strncasecmp(ast_channel_name(chan) ,"DAHDI/34-1",9))
  2470. WCGSM_GET_CALLERID = WCGSM_GET_CH7_CALLERID;
  2471. else if (!strncasecmp(ast_channel_name(chan) ,"DAHDI/35-1",9))
  2472. WCGSM_GET_CALLERID = WCGSM_GET_CH8_CALLERID;
  2473. //end
  2474. //by qz for slot 1 T1 , slot 2 Ana
  2475. else if (!strncasecmp(ast_channel_name(chan) ,"DAHDI/25-1",9))
  2476. WCGSM_GET_CALLERID = WCGSM_GET_CH5_CALLERID;
  2477. else if (!strncasecmp(ast_channel_name(chan) ,"DAHDI/26-1",9))
  2478. WCGSM_GET_CALLERID = WCGSM_GET_CH6_CALLERID;
  2479. else if (!strncasecmp(ast_channel_name(chan) ,"DAHDI/27-1",9))
  2480. WCGSM_GET_CALLERID = WCGSM_GET_CH7_CALLERID;
  2481. else{
  2482. WCGSM_GET_CALLERID = WCGSM_GET_CH8_CALLERID;
  2483. }
  2484. //end
  2485. wcgsm_fd=open("/dev/dahdi/wcgsm", O_RDWR);
  2486. if(wcgsm_fd){
  2487. wcgsm_res=ioctl(wcgsm_fd, WCGSM_GET_CALLERID, (long)gsm1_number);
  2488. if(!wcgsm_res) {
  2489. number=gsm1_number;
  2490. if (option_debug) ast_log(LOG_DEBUG, "GSM1 CallerID number: %s\n", number);
  2491. }
  2492. close(wcgsm_fd);
  2493. }else
  2494. ast_verb(3, "Can not open the wcgsm device!\n"); //xuefeng
  2495. }
  2496. ast_verb(3, "channel (%s) show CallerID: %s\n",ast_channel_name(chan), number); //xuefeng
  2497. //changed by qz 2015.06.15
  2498. } else {
  2499. ast_log(LOG_WARNING, "Unable to get caller ID space\n");
  2500. }
  2501. }
  2502. if (number) {
  2503. ast_shrink_phone_number(number);
  2504. }
  2505. ast_set_callerid(chan, number, name, number);
  2506. analog_handle_notify_message(chan, p, flags, -1);
  2507. ast_channel_lock(chan);
  2508. ast_setstate(chan, AST_STATE_RING);
  2509. ast_channel_rings_set(chan, 1);
  2510. ast_channel_unlock(chan);
  2511. analog_set_ringtimeout(p, p->ringt_base);
  2512. res = ast_pbx_run(chan);
  2513. if (res) {
  2514. ast_hangup(chan);
  2515. ast_log(LOG_WARNING, "PBX exited non-zero\n");
  2516. }
  2517. goto quit;
  2518. default:
  2519. ast_log(LOG_WARNING, "Don't know how to handle simple switch with signalling %s on channel %d\n", analog_sigtype_to_str(p->sig), p->channel);
  2520. break;
  2521. }
  2522. res = analog_play_tone(p, idx, ANALOG_TONE_CONGESTION);
  2523. if (res < 0) {
  2524. ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", p->channel);
  2525. }
  2526. ast_hangup(chan);
  2527. quit:
  2528. ao2_cleanup(smdi_msg);
  2529. analog_decrease_ss_count();
  2530. return NULL;
  2531. }
  2532. int analog_ss_thread_start(struct analog_pvt *p, struct ast_channel *chan)
  2533. {
  2534. pthread_t threadid;
  2535. return ast_pthread_create_detached(&threadid, NULL, __analog_ss_thread, p);
  2536. }
  2537. static void analog_publish_channel_alarm_clear(int channel)
  2538. {
  2539. RAII_VAR(struct ast_json *, body, NULL, ast_json_unref);
  2540. ast_log(LOG_NOTICE, "Alarm cleared on channel %d\n", channel);
  2541. body = ast_json_pack("{s: i}", "Channel", channel);
  2542. if (!body) {
  2543. return;
  2544. }
  2545. ast_manager_publish_event("AlarmClear", EVENT_FLAG_SYSTEM, body);
  2546. }
  2547. static struct ast_frame *__analog_handle_event(struct analog_pvt *p, struct ast_channel *ast)
  2548. {
  2549. int res, x;
  2550. int mysig;
  2551. int idx;
  2552. char *c;
  2553. pthread_t threadid;
  2554. struct ast_channel *chan;
  2555. struct ast_frame *f;
  2556. struct ast_control_pvt_cause_code *cause_code = NULL;
  2557. int data_size = sizeof(*cause_code);
  2558. char *subclass = NULL;
  2559. ast_debug(1, "%s %d\n", __FUNCTION__, p->channel);
  2560. idx = analog_get_index(ast, p, 0);
  2561. if (idx < 0) {
  2562. return &ast_null_frame;
  2563. }
  2564. if (idx != ANALOG_SUB_REAL) {
  2565. ast_log(LOG_ERROR, "We got an event on a non real sub. Fix it!\n");
  2566. }
  2567. mysig = p->sig;
  2568. if (p->outsigmod > -1) {
  2569. mysig = p->outsigmod;
  2570. }
  2571. p->subs[idx].f.frametype = AST_FRAME_NULL;
  2572. p->subs[idx].f.subclass.integer = 0;
  2573. p->subs[idx].f.datalen = 0;
  2574. p->subs[idx].f.samples = 0;
  2575. p->subs[idx].f.mallocd = 0;
  2576. p->subs[idx].f.offset = 0;
  2577. p->subs[idx].f.src = "dahdi_handle_event";
  2578. p->subs[idx].f.data.ptr = NULL;
  2579. f = &p->subs[idx].f;
  2580. res = analog_get_event(p);
  2581. ast_debug(1, "Got event %s(%d) on channel %d (index %u)\n", analog_event2str(res), res, p->channel, idx);
  2582. if (res & (ANALOG_EVENT_PULSEDIGIT | ANALOG_EVENT_DTMFUP)) {
  2583. analog_set_pulsedial(p, (res & ANALOG_EVENT_PULSEDIGIT) ? 1 : 0);
  2584. ast_debug(1, "Detected %sdigit '%c'\n", (res & ANALOG_EVENT_PULSEDIGIT) ? "pulse ": "", res & 0xff);
  2585. analog_confmute(p, 0);
  2586. p->subs[idx].f.frametype = AST_FRAME_DTMF_END;
  2587. p->subs[idx].f.subclass.integer = res & 0xff;
  2588. analog_handle_dtmf(p, ast, idx, &f);
  2589. return f;
  2590. }
  2591. if (res & ANALOG_EVENT_DTMFDOWN) {
  2592. ast_debug(1, "DTMF Down '%c'\n", res & 0xff);
  2593. /* Mute conference */
  2594. analog_confmute(p, 1);
  2595. p->subs[idx].f.frametype = AST_FRAME_DTMF_BEGIN;
  2596. p->subs[idx].f.subclass.integer = res & 0xff;
  2597. analog_handle_dtmf(p, ast, idx, &f);
  2598. return f;
  2599. }
  2600. switch (res) {
  2601. case ANALOG_EVENT_ALARM:
  2602. case ANALOG_EVENT_POLARITY:
  2603. case ANALOG_EVENT_ONHOOK:
  2604. /* add length of "ANALOG " */
  2605. data_size += 7;
  2606. subclass = analog_event2str(res);
  2607. data_size += strlen(subclass);
  2608. cause_code = ast_alloca(data_size);
  2609. memset(cause_code, 0, data_size);
  2610. cause_code->ast_cause = AST_CAUSE_NORMAL_CLEARING;
  2611. ast_copy_string(cause_code->chan_name, ast_channel_name(ast), AST_CHANNEL_NAME);
  2612. snprintf(cause_code->code, data_size - sizeof(*cause_code) + 1, "ANALOG %s", subclass);
  2613. break;
  2614. default:
  2615. break;
  2616. }
  2617. switch (res) {
  2618. case ANALOG_EVENT_EC_DISABLED:
  2619. ast_verb(3, "Channel %d echo canceler disabled due to CED detection\n", p->channel);
  2620. analog_set_echocanceller(p, 0);
  2621. break;
  2622. #ifdef HAVE_DAHDI_ECHOCANCEL_FAX_MODE
  2623. case ANALOG_EVENT_TX_CED_DETECTED:
  2624. ast_verb(3, "Channel %d detected a CED tone towards the network.\n", p->channel);
  2625. break;
  2626. case ANALOG_EVENT_RX_CED_DETECTED:
  2627. ast_verb(3, "Channel %d detected a CED tone from the network.\n", p->channel);
  2628. break;
  2629. case ANALOG_EVENT_EC_NLP_DISABLED:
  2630. ast_verb(3, "Channel %d echo canceler disabled its NLP.\n", p->channel);
  2631. break;
  2632. case ANALOG_EVENT_EC_NLP_ENABLED:
  2633. ast_verb(3, "Channel %d echo canceler enabled its NLP.\n", p->channel);
  2634. break;
  2635. #endif
  2636. case ANALOG_EVENT_PULSE_START:
  2637. /* Stop tone if there's a pulse start and the PBX isn't started */
  2638. if (!ast_channel_pbx(ast))
  2639. analog_play_tone(p, ANALOG_SUB_REAL, -1);
  2640. break;
  2641. case ANALOG_EVENT_DIALCOMPLETE:
  2642. if (p->inalarm) {
  2643. break;
  2644. }
  2645. x = analog_is_dialing(p, idx);
  2646. if (!x) { /* if not still dialing in driver */
  2647. analog_set_echocanceller(p, 1);
  2648. if (p->echobreak) {
  2649. analog_train_echocanceller(p);
  2650. ast_copy_string(p->dop.dialstr, p->echorest, sizeof(p->dop.dialstr));
  2651. p->dop.op = ANALOG_DIAL_OP_REPLACE;
  2652. analog_dial_digits(p, ANALOG_SUB_REAL, &p->dop);
  2653. p->echobreak = 0;
  2654. } else {
  2655. analog_set_dialing(p, 0);
  2656. if ((mysig == ANALOG_SIG_E911) || (mysig == ANALOG_SIG_FGC_CAMA) || (mysig == ANALOG_SIG_FGC_CAMAMF)) {
  2657. /* if thru with dialing after offhook */
  2658. if (ast_channel_state(ast) == AST_STATE_DIALING_OFFHOOK) {
  2659. ast_setstate(ast, AST_STATE_UP);
  2660. p->subs[idx].f.frametype = AST_FRAME_CONTROL;
  2661. p->subs[idx].f.subclass.integer = AST_CONTROL_ANSWER;
  2662. break;
  2663. } else { /* if to state wait for offhook to dial rest */
  2664. /* we now wait for off hook */
  2665. ast_setstate(ast,AST_STATE_DIALING_OFFHOOK);
  2666. }
  2667. }
  2668. if (ast_channel_state(ast) == AST_STATE_DIALING) {
  2669. if (analog_have_progressdetect(p)) {
  2670. ast_debug(1, "Done dialing, but waiting for progress detection before doing more...\n");
  2671. } else if (analog_check_confirmanswer(p) || (!p->dialednone
  2672. && ((mysig == ANALOG_SIG_EM) || (mysig == ANALOG_SIG_EM_E1)
  2673. || (mysig == ANALOG_SIG_EMWINK) || (mysig == ANALOG_SIG_FEATD)
  2674. || (mysig == ANALOG_SIG_FEATDMF_TA) || (mysig == ANALOG_SIG_FEATDMF)
  2675. || (mysig == ANALOG_SIG_E911) || (mysig == ANALOG_SIG_FGC_CAMA)
  2676. || (mysig == ANALOG_SIG_FGC_CAMAMF) || (mysig == ANALOG_SIG_FEATB)
  2677. || (mysig == ANALOG_SIG_SF) || (mysig == ANALOG_SIG_SFWINK)
  2678. || (mysig == ANALOG_SIG_SF_FEATD) || (mysig == ANALOG_SIG_SF_FEATDMF)
  2679. || (mysig == ANALOG_SIG_SF_FEATB)))) {
  2680. ast_setstate(ast, AST_STATE_RINGING);
  2681. } else if (!p->answeronpolarityswitch) {
  2682. ast_setstate(ast, AST_STATE_UP);
  2683. p->subs[idx].f.frametype = AST_FRAME_CONTROL;
  2684. p->subs[idx].f.subclass.integer = AST_CONTROL_ANSWER;
  2685. /* If aops=0 and hops=1, this is necessary */
  2686. p->polarity = POLARITY_REV;
  2687. } else {
  2688. /* Start clean, so we can catch the change to REV polarity when party answers */
  2689. p->polarity = POLARITY_IDLE;
  2690. }
  2691. }
  2692. }
  2693. }
  2694. break;
  2695. case ANALOG_EVENT_ALARM:
  2696. analog_set_alarm(p, 1);
  2697. analog_get_and_handle_alarms(p);
  2698. cause_code->ast_cause = AST_CAUSE_NETWORK_OUT_OF_ORDER;
  2699. case ANALOG_EVENT_ONHOOK:
  2700. ast_queue_control_data(ast, AST_CONTROL_PVT_CAUSE_CODE, cause_code, data_size);
  2701. ast_channel_hangupcause_hash_set(ast, cause_code, data_size);
  2702. switch (p->sig) {
  2703. case ANALOG_SIG_FXOLS:
  2704. case ANALOG_SIG_FXOGS:
  2705. case ANALOG_SIG_FXOKS:
  2706. analog_start_polarityswitch(p);
  2707. p->fxsoffhookstate = 0;
  2708. p->onhooktime = time(NULL);
  2709. p->msgstate = -1;
  2710. /* Check for some special conditions regarding call waiting */
  2711. if (idx == ANALOG_SUB_REAL) {
  2712. /* The normal line was hung up */
  2713. if (p->subs[ANALOG_SUB_CALLWAIT].owner) {
  2714. /* Need to hold the lock for real-call, private, and call-waiting call */
  2715. analog_lock_sub_owner(p, ANALOG_SUB_CALLWAIT);
  2716. if (!p->subs[ANALOG_SUB_CALLWAIT].owner) {
  2717. /*
  2718. * The call waiting call dissappeared.
  2719. * This is now a normal hangup.
  2720. */
  2721. analog_set_echocanceller(p, 0);
  2722. return NULL;
  2723. }
  2724. /* There's a call waiting call, so ring the phone, but make it unowned in the mean time */
  2725. analog_swap_subs(p, ANALOG_SUB_CALLWAIT, ANALOG_SUB_REAL);
  2726. ast_verb(3, "Channel %d still has (callwait) call, ringing phone\n", p->channel);
  2727. analog_unalloc_sub(p, ANALOG_SUB_CALLWAIT);
  2728. analog_stop_callwait(p);
  2729. analog_set_new_owner(p, NULL);
  2730. /* Don't start streaming audio yet if the incoming call isn't up yet */
  2731. if (ast_channel_state(p->subs[ANALOG_SUB_REAL].owner) != AST_STATE_UP) {
  2732. analog_set_dialing(p, 1);
  2733. }
  2734. /* Unlock the call-waiting call that we swapped to real-call. */
  2735. ast_channel_unlock(p->subs[ANALOG_SUB_REAL].owner);
  2736. analog_ring(p);
  2737. } else if (p->subs[ANALOG_SUB_THREEWAY].owner) {
  2738. unsigned int mssinceflash;
  2739. /* Need to hold the lock for real-call, private, and 3-way call */
  2740. analog_lock_sub_owner(p, ANALOG_SUB_THREEWAY);
  2741. if (!p->subs[ANALOG_SUB_THREEWAY].owner) {
  2742. ast_log(LOG_NOTICE, "Whoa, threeway disappeared kinda randomly.\n");
  2743. /* Just hangup */
  2744. return NULL;
  2745. }
  2746. if (p->owner != ast) {
  2747. ast_channel_unlock(p->subs[ANALOG_SUB_THREEWAY].owner);
  2748. ast_log(LOG_WARNING, "This isn't good...\n");
  2749. /* Just hangup */
  2750. return NULL;
  2751. }
  2752. mssinceflash = ast_tvdiff_ms(ast_tvnow(), p->flashtime);
  2753. ast_debug(1, "Last flash was %u ms ago\n", mssinceflash);
  2754. if (mssinceflash < MIN_MS_SINCE_FLASH) {
  2755. /* It hasn't been long enough since the last flashook. This is probably a bounce on
  2756. hanging up. Hangup both channels now */
  2757. ast_debug(1, "Looks like a bounced flash, hanging up both calls on %d\n", p->channel);
  2758. ast_queue_hangup_with_cause(p->subs[ANALOG_SUB_THREEWAY].owner, AST_CAUSE_NO_ANSWER);
  2759. ast_softhangup_nolock(p->subs[ANALOG_SUB_THREEWAY].owner, AST_SOFTHANGUP_DEV);
  2760. ast_channel_unlock(p->subs[ANALOG_SUB_THREEWAY].owner);
  2761. } else if ((ast_channel_pbx(ast)) || (ast_channel_state(ast) == AST_STATE_UP)) {
  2762. if (p->transfer) {
  2763. /* In any case this isn't a threeway call anymore */
  2764. analog_set_inthreeway(p, ANALOG_SUB_REAL, 0);
  2765. analog_set_inthreeway(p, ANALOG_SUB_THREEWAY, 0);
  2766. /* Only attempt transfer if the phone is ringing; why transfer to busy tone eh? */
  2767. if (!p->transfertobusy && ast_channel_state(ast) == AST_STATE_BUSY) {
  2768. /* Swap subs and dis-own channel */
  2769. analog_swap_subs(p, ANALOG_SUB_THREEWAY, ANALOG_SUB_REAL);
  2770. /* Unlock the 3-way call that we swapped to real-call. */
  2771. ast_channel_unlock(p->subs[ANALOG_SUB_REAL].owner);
  2772. analog_set_new_owner(p, NULL);
  2773. /* Ring the phone */
  2774. analog_ring(p);
  2775. } else if (!analog_attempt_transfer(p)) {
  2776. /*
  2777. * Transfer successful. Don't actually hang up at this point.
  2778. * Let our channel legs of the calls die off as the transfer
  2779. * percolates through the core.
  2780. */
  2781. break;
  2782. }
  2783. } else {
  2784. ast_softhangup_nolock(p->subs[ANALOG_SUB_THREEWAY].owner, AST_SOFTHANGUP_DEV);
  2785. ast_channel_unlock(p->subs[ANALOG_SUB_THREEWAY].owner);
  2786. }
  2787. } else {
  2788. /* Swap subs and dis-own channel */
  2789. analog_swap_subs(p, ANALOG_SUB_THREEWAY, ANALOG_SUB_REAL);
  2790. /* Unlock the 3-way call that we swapped to real-call. */
  2791. ast_channel_unlock(p->subs[ANALOG_SUB_REAL].owner);
  2792. analog_set_new_owner(p, NULL);
  2793. /* Ring the phone */
  2794. analog_ring(p);
  2795. }
  2796. }
  2797. } else {
  2798. ast_log(LOG_WARNING, "Got a hangup and my index is %u?\n", idx);
  2799. }
  2800. /* Fall through */
  2801. default:
  2802. analog_set_echocanceller(p, 0);
  2803. return NULL;
  2804. }
  2805. break;
  2806. case ANALOG_EVENT_RINGOFFHOOK:
  2807. if (p->inalarm) {
  2808. break;
  2809. }
  2810. /* for E911, its supposed to wait for offhook then dial
  2811. the second half of the dial string */
  2812. if (((mysig == ANALOG_SIG_E911) || (mysig == ANALOG_SIG_FGC_CAMA) || (mysig == ANALOG_SIG_FGC_CAMAMF)) && (ast_channel_state(ast) == AST_STATE_DIALING_OFFHOOK)) {
  2813. c = strchr(p->dialdest, '/');
  2814. if (c) {
  2815. c++;
  2816. } else {
  2817. c = p->dialdest;
  2818. }
  2819. if (*c) {
  2820. snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*0%s#", c);
  2821. } else {
  2822. ast_copy_string(p->dop.dialstr,"M*2#", sizeof(p->dop.dialstr));
  2823. }
  2824. if (strlen(p->dop.dialstr) > 4) {
  2825. memset(p->echorest, 'w', sizeof(p->echorest) - 1);
  2826. strcpy(p->echorest + (p->echotraining / 401) + 1, p->dop.dialstr + strlen(p->dop.dialstr) - 2);
  2827. p->echorest[sizeof(p->echorest) - 1] = '\0';
  2828. p->echobreak = 1;
  2829. p->dop.dialstr[strlen(p->dop.dialstr)-2] = '\0';
  2830. } else {
  2831. p->echobreak = 0;
  2832. }
  2833. if (analog_dial_digits(p, ANALOG_SUB_REAL, &p->dop)) {
  2834. analog_on_hook(p);
  2835. return NULL;
  2836. }
  2837. analog_set_dialing(p, 1);
  2838. return &p->subs[idx].f;
  2839. }
  2840. switch (p->sig) {
  2841. case ANALOG_SIG_FXOLS:
  2842. case ANALOG_SIG_FXOGS:
  2843. case ANALOG_SIG_FXOKS:
  2844. p->fxsoffhookstate = 1;
  2845. switch (ast_channel_state(ast)) {
  2846. case AST_STATE_RINGING:
  2847. analog_set_echocanceller(p, 1);
  2848. analog_train_echocanceller(p);
  2849. p->subs[idx].f.frametype = AST_FRAME_CONTROL;
  2850. p->subs[idx].f.subclass.integer = AST_CONTROL_ANSWER;
  2851. /* Make sure it stops ringing */
  2852. analog_set_needringing(p, 0);
  2853. analog_off_hook(p);
  2854. ast_debug(1, "channel %d answered\n", p->channel);
  2855. /* Cancel any running CallerID spill */
  2856. analog_cancel_cidspill(p);
  2857. analog_set_dialing(p, 0);
  2858. p->callwaitcas = 0;
  2859. if (analog_check_confirmanswer(p)) {
  2860. /* Ignore answer if "confirm answer" is enabled */
  2861. p->subs[idx].f.frametype = AST_FRAME_NULL;
  2862. p->subs[idx].f.subclass.integer = 0;
  2863. } else if (!ast_strlen_zero(p->dop.dialstr)) {
  2864. /* nick@dccinc.com 4/3/03 - fxo should be able to do deferred dialing */
  2865. res = analog_dial_digits(p, ANALOG_SUB_REAL, &p->dop);
  2866. if (res) {
  2867. p->dop.dialstr[0] = '\0';
  2868. return NULL;
  2869. } else {
  2870. ast_debug(1, "Sent FXO deferred digit string: %s\n", p->dop.dialstr);
  2871. p->subs[idx].f.frametype = AST_FRAME_NULL;
  2872. p->subs[idx].f.subclass.integer = 0;
  2873. analog_set_dialing(p, 1);
  2874. }
  2875. p->dop.dialstr[0] = '\0';
  2876. ast_setstate(ast, AST_STATE_DIALING);
  2877. } else {
  2878. ast_setstate(ast, AST_STATE_UP);
  2879. analog_answer_polarityswitch(p);
  2880. }
  2881. return &p->subs[idx].f;
  2882. case AST_STATE_DOWN:
  2883. ast_setstate(ast, AST_STATE_RING);
  2884. ast_channel_rings_set(ast, 1);
  2885. p->subs[idx].f.frametype = AST_FRAME_CONTROL;
  2886. p->subs[idx].f.subclass.integer = AST_CONTROL_OFFHOOK;
  2887. ast_debug(1, "channel %d picked up\n", p->channel);
  2888. return &p->subs[idx].f;
  2889. case AST_STATE_UP:
  2890. /* Make sure it stops ringing */
  2891. analog_off_hook(p);
  2892. /* Okay -- probably call waiting */
  2893. ast_queue_unhold(p->owner);
  2894. break;
  2895. case AST_STATE_RESERVED:
  2896. /* Start up dialtone */
  2897. if (analog_has_voicemail(p)) {
  2898. res = analog_play_tone(p, ANALOG_SUB_REAL, ANALOG_TONE_STUTTER);
  2899. } else {
  2900. res = analog_play_tone(p, ANALOG_SUB_REAL, ANALOG_TONE_DIALTONE);
  2901. }
  2902. break;
  2903. default:
  2904. ast_log(LOG_WARNING, "FXO phone off hook in weird state %u??\n", ast_channel_state(ast));
  2905. }
  2906. break;
  2907. case ANALOG_SIG_FXSLS:
  2908. case ANALOG_SIG_FXSGS:
  2909. case ANALOG_SIG_FXSKS:
  2910. if (ast_channel_state(ast) == AST_STATE_RING) {
  2911. analog_set_ringtimeout(p, p->ringt_base);
  2912. }
  2913. /* Fall through */
  2914. case ANALOG_SIG_EM:
  2915. case ANALOG_SIG_EM_E1:
  2916. case ANALOG_SIG_EMWINK:
  2917. case ANALOG_SIG_FEATD:
  2918. case ANALOG_SIG_FEATDMF:
  2919. case ANALOG_SIG_FEATDMF_TA:
  2920. case ANALOG_SIG_E911:
  2921. case ANALOG_SIG_FGC_CAMA:
  2922. case ANALOG_SIG_FGC_CAMAMF:
  2923. case ANALOG_SIG_FEATB:
  2924. case ANALOG_SIG_SF:
  2925. case ANALOG_SIG_SFWINK:
  2926. case ANALOG_SIG_SF_FEATD:
  2927. case ANALOG_SIG_SF_FEATDMF:
  2928. case ANALOG_SIG_SF_FEATB:
  2929. switch (ast_channel_state(ast)) {
  2930. case AST_STATE_PRERING:
  2931. ast_setstate(ast, AST_STATE_RING);
  2932. /* Fall through */
  2933. case AST_STATE_DOWN:
  2934. case AST_STATE_RING:
  2935. ast_debug(1, "Ring detected\n");
  2936. p->subs[idx].f.frametype = AST_FRAME_CONTROL;
  2937. p->subs[idx].f.subclass.integer = AST_CONTROL_RING;
  2938. break;
  2939. case AST_STATE_RINGING:
  2940. case AST_STATE_DIALING:
  2941. if (p->outgoing) {
  2942. ast_debug(1, "Line answered\n");
  2943. if (analog_check_confirmanswer(p)) {
  2944. p->subs[idx].f.frametype = AST_FRAME_NULL;
  2945. p->subs[idx].f.subclass.integer = 0;
  2946. } else {
  2947. p->subs[idx].f.frametype = AST_FRAME_CONTROL;
  2948. p->subs[idx].f.subclass.integer = AST_CONTROL_ANSWER;
  2949. ast_setstate(ast, AST_STATE_UP);
  2950. }
  2951. break;
  2952. }
  2953. /* Fall through */
  2954. default:
  2955. ast_log(LOG_WARNING, "Ring/Off-hook in strange state %u on channel %d\n", ast_channel_state(ast), p->channel);
  2956. break;
  2957. }
  2958. break;
  2959. default:
  2960. ast_log(LOG_WARNING, "Don't know how to handle ring/off hook for signalling %d\n", p->sig);
  2961. break;
  2962. }
  2963. break;
  2964. case ANALOG_EVENT_RINGBEGIN:
  2965. switch (p->sig) {
  2966. case ANALOG_SIG_FXSLS:
  2967. case ANALOG_SIG_FXSGS:
  2968. case ANALOG_SIG_FXSKS:
  2969. if (ast_channel_state(ast) == AST_STATE_RING) {
  2970. analog_set_ringtimeout(p, p->ringt_base);
  2971. }
  2972. break;
  2973. default:
  2974. break;
  2975. }
  2976. break;
  2977. case ANALOG_EVENT_RINGEROFF:
  2978. if (p->inalarm) break;
  2979. ast_channel_rings_set(ast, ast_channel_rings(ast) + 1);
  2980. if (ast_channel_rings(ast) == p->cidrings) {
  2981. analog_send_callerid(p, 0, &p->caller);
  2982. }
  2983. if (ast_channel_rings(ast) > p->cidrings) {
  2984. analog_cancel_cidspill(p);
  2985. p->callwaitcas = 0;
  2986. }
  2987. p->subs[idx].f.frametype = AST_FRAME_CONTROL;
  2988. p->subs[idx].f.subclass.integer = AST_CONTROL_RINGING;
  2989. break;
  2990. case ANALOG_EVENT_RINGERON:
  2991. break;
  2992. case ANALOG_EVENT_NOALARM:
  2993. analog_set_alarm(p, 0);
  2994. analog_publish_channel_alarm_clear(p->channel);
  2995. break;
  2996. case ANALOG_EVENT_WINKFLASH:
  2997. if (p->inalarm) {
  2998. break;
  2999. }
  3000. /* Remember last time we got a flash-hook */
  3001. gettimeofday(&p->flashtime, NULL);
  3002. switch (mysig) {
  3003. case ANALOG_SIG_FXOLS:
  3004. case ANALOG_SIG_FXOGS:
  3005. case ANALOG_SIG_FXOKS:
  3006. ast_debug(1, "Winkflash, index: %u, normal: %d, callwait: %d, thirdcall: %d\n",
  3007. idx, analog_get_sub_fd(p, ANALOG_SUB_REAL), analog_get_sub_fd(p, ANALOG_SUB_CALLWAIT), analog_get_sub_fd(p, ANALOG_SUB_THREEWAY));
  3008. /* Cancel any running CallerID spill */
  3009. analog_cancel_cidspill(p);
  3010. p->callwaitcas = 0;
  3011. if (idx != ANALOG_SUB_REAL) {
  3012. ast_log(LOG_WARNING, "Got flash hook with index %u on channel %d?!?\n", idx, p->channel);
  3013. goto winkflashdone;
  3014. }
  3015. if (p->subs[ANALOG_SUB_CALLWAIT].owner) {
  3016. /* Need to hold the lock for real-call, private, and call-waiting call */
  3017. analog_lock_sub_owner(p, ANALOG_SUB_CALLWAIT);
  3018. if (!p->subs[ANALOG_SUB_CALLWAIT].owner) {
  3019. /*
  3020. * The call waiting call dissappeared.
  3021. * Let's just ignore this flash-hook.
  3022. */
  3023. ast_log(LOG_NOTICE, "Whoa, the call-waiting call disappeared.\n");
  3024. goto winkflashdone;
  3025. }
  3026. /* Swap to call-wait */
  3027. analog_swap_subs(p, ANALOG_SUB_REAL, ANALOG_SUB_CALLWAIT);
  3028. analog_play_tone(p, ANALOG_SUB_REAL, -1);
  3029. analog_set_new_owner(p, p->subs[ANALOG_SUB_REAL].owner);
  3030. ast_debug(1, "Making %s the new owner\n", ast_channel_name(p->owner));
  3031. if (ast_channel_state(p->subs[ANALOG_SUB_REAL].owner) == AST_STATE_RINGING) {
  3032. ast_setstate(p->subs[ANALOG_SUB_REAL].owner, AST_STATE_UP);
  3033. ast_queue_control(p->subs[ANALOG_SUB_REAL].owner, AST_CONTROL_ANSWER);
  3034. }
  3035. analog_stop_callwait(p);
  3036. /* Start music on hold if appropriate */
  3037. if (!p->subs[ANALOG_SUB_CALLWAIT].inthreeway) {
  3038. ast_queue_hold(p->subs[ANALOG_SUB_CALLWAIT].owner, p->mohsuggest);
  3039. }
  3040. ast_queue_hold(p->subs[ANALOG_SUB_REAL].owner, p->mohsuggest);
  3041. ast_queue_unhold(p->subs[ANALOG_SUB_REAL].owner);
  3042. /* Unlock the call-waiting call that we swapped to real-call. */
  3043. ast_channel_unlock(p->subs[ANALOG_SUB_REAL].owner);
  3044. } else if (!p->subs[ANALOG_SUB_THREEWAY].owner) {
  3045. if (!p->threewaycalling) {
  3046. /* Just send a flash if no 3-way calling */
  3047. ast_queue_control(p->subs[ANALOG_SUB_REAL].owner, AST_CONTROL_FLASH);
  3048. goto winkflashdone;
  3049. } else if (!analog_check_for_conference(p)) {
  3050. struct ast_callid *callid = NULL;
  3051. int callid_created;
  3052. char cid_num[256];
  3053. char cid_name[256];
  3054. cid_num[0] = '\0';
  3055. cid_name[0] = '\0';
  3056. if (p->dahditrcallerid && p->owner) {
  3057. if (ast_channel_caller(p->owner)->id.number.valid
  3058. && ast_channel_caller(p->owner)->id.number.str) {
  3059. ast_copy_string(cid_num, ast_channel_caller(p->owner)->id.number.str,
  3060. sizeof(cid_num));
  3061. }
  3062. if (ast_channel_caller(p->owner)->id.name.valid
  3063. && ast_channel_caller(p->owner)->id.name.str) {
  3064. ast_copy_string(cid_name, ast_channel_caller(p->owner)->id.name.str,
  3065. sizeof(cid_name));
  3066. }
  3067. }
  3068. /* XXX This section needs much more error checking!!! XXX */
  3069. /* Start a 3-way call if feasible */
  3070. if (!((ast_channel_pbx(ast)) ||
  3071. (ast_channel_state(ast) == AST_STATE_UP) ||
  3072. (ast_channel_state(ast) == AST_STATE_RING))) {
  3073. ast_debug(1, "Flash when call not up or ringing\n");
  3074. goto winkflashdone;
  3075. }
  3076. if (analog_alloc_sub(p, ANALOG_SUB_THREEWAY)) {
  3077. ast_log(LOG_WARNING, "Unable to allocate three-way subchannel\n");
  3078. goto winkflashdone;
  3079. }
  3080. callid_created = ast_callid_threadstorage_auto(&callid);
  3081. /*
  3082. * Make new channel
  3083. *
  3084. * We cannot hold the p or ast locks while creating a new
  3085. * channel.
  3086. */
  3087. analog_unlock_private(p);
  3088. ast_channel_unlock(ast);
  3089. chan = analog_new_ast_channel(p, AST_STATE_RESERVED, 0, ANALOG_SUB_THREEWAY, NULL);
  3090. ast_channel_lock(ast);
  3091. analog_lock_private(p);
  3092. if (!chan) {
  3093. ast_log(LOG_WARNING,
  3094. "Cannot allocate new call structure on channel %d\n",
  3095. p->channel);
  3096. analog_unalloc_sub(p, ANALOG_SUB_THREEWAY);
  3097. ast_callid_threadstorage_auto_clean(callid, callid_created);
  3098. goto winkflashdone;
  3099. }
  3100. if (p->dahditrcallerid) {
  3101. if (!p->origcid_num) {
  3102. p->origcid_num = ast_strdup(p->cid_num);
  3103. }
  3104. if (!p->origcid_name) {
  3105. p->origcid_name = ast_strdup(p->cid_name);
  3106. }
  3107. ast_copy_string(p->cid_num, cid_num, sizeof(p->cid_num));
  3108. ast_copy_string(p->cid_name, cid_name, sizeof(p->cid_name));
  3109. }
  3110. /* Swap things around between the three-way and real call */
  3111. analog_swap_subs(p, ANALOG_SUB_THREEWAY, ANALOG_SUB_REAL);
  3112. /* Disable echo canceller for better dialing */
  3113. analog_set_echocanceller(p, 0);
  3114. res = analog_play_tone(p, ANALOG_SUB_REAL, ANALOG_TONE_DIALRECALL);
  3115. if (res) {
  3116. ast_log(LOG_WARNING, "Unable to start dial recall tone on channel %d\n", p->channel);
  3117. }
  3118. analog_set_new_owner(p, chan);
  3119. p->ss_astchan = chan;
  3120. if (ast_pthread_create_detached(&threadid, NULL, __analog_ss_thread, p)) {
  3121. ast_log(LOG_WARNING, "Unable to start simple switch on channel %d\n", p->channel);
  3122. res = analog_play_tone(p, ANALOG_SUB_REAL, ANALOG_TONE_CONGESTION);
  3123. analog_set_echocanceller(p, 1);
  3124. ast_hangup(chan);
  3125. } else {
  3126. ast_verb(3, "Started three way call on channel %d\n", p->channel);
  3127. /* Start music on hold */
  3128. ast_queue_hold(p->subs[ANALOG_SUB_THREEWAY].owner, p->mohsuggest);
  3129. }
  3130. ast_callid_threadstorage_auto_clean(callid, callid_created);
  3131. }
  3132. } else {
  3133. /* Already have a 3 way call */
  3134. enum analog_sub orig_3way_sub;
  3135. /* Need to hold the lock for real-call, private, and 3-way call */
  3136. analog_lock_sub_owner(p, ANALOG_SUB_THREEWAY);
  3137. if (!p->subs[ANALOG_SUB_THREEWAY].owner) {
  3138. /*
  3139. * The 3-way call dissappeared.
  3140. * Let's just ignore this flash-hook.
  3141. */
  3142. ast_log(LOG_NOTICE, "Whoa, the 3-way call disappeared.\n");
  3143. goto winkflashdone;
  3144. }
  3145. orig_3way_sub = ANALOG_SUB_THREEWAY;
  3146. if (p->subs[ANALOG_SUB_THREEWAY].inthreeway) {
  3147. /* Call is already up, drop the last person */
  3148. ast_debug(1, "Got flash with three way call up, dropping last call on %d\n", p->channel);
  3149. /* If the primary call isn't answered yet, use it */
  3150. if ((ast_channel_state(p->subs[ANALOG_SUB_REAL].owner) != AST_STATE_UP) &&
  3151. (ast_channel_state(p->subs[ANALOG_SUB_THREEWAY].owner) == AST_STATE_UP)) {
  3152. /* Swap back -- we're dropping the real 3-way that isn't finished yet*/
  3153. analog_swap_subs(p, ANALOG_SUB_THREEWAY, ANALOG_SUB_REAL);
  3154. orig_3way_sub = ANALOG_SUB_REAL;
  3155. analog_set_new_owner(p, p->subs[ANALOG_SUB_REAL].owner);
  3156. }
  3157. /* Drop the last call and stop the conference */
  3158. ast_verb(3, "Dropping three-way call on %s\n", ast_channel_name(p->subs[ANALOG_SUB_THREEWAY].owner));
  3159. ast_softhangup_nolock(p->subs[ANALOG_SUB_THREEWAY].owner, AST_SOFTHANGUP_DEV);
  3160. analog_set_inthreeway(p, ANALOG_SUB_REAL, 0);
  3161. analog_set_inthreeway(p, ANALOG_SUB_THREEWAY, 0);
  3162. } else {
  3163. /* Lets see what we're up to */
  3164. if (((ast_channel_pbx(ast)) || (ast_channel_state(ast) == AST_STATE_UP)) &&
  3165. (p->transfertobusy || (ast_channel_state(ast) != AST_STATE_BUSY))) {
  3166. ast_verb(3, "Building conference call with %s and %s\n",
  3167. ast_channel_name(p->subs[ANALOG_SUB_THREEWAY].owner),
  3168. ast_channel_name(p->subs[ANALOG_SUB_REAL].owner));
  3169. /* Put them in the threeway, and flip */
  3170. analog_set_inthreeway(p, ANALOG_SUB_THREEWAY, 1);
  3171. analog_set_inthreeway(p, ANALOG_SUB_REAL, 1);
  3172. if (ast_channel_state(ast) == AST_STATE_UP) {
  3173. analog_swap_subs(p, ANALOG_SUB_THREEWAY, ANALOG_SUB_REAL);
  3174. orig_3way_sub = ANALOG_SUB_REAL;
  3175. }
  3176. ast_queue_unhold(p->subs[orig_3way_sub].owner);
  3177. analog_set_new_owner(p, p->subs[ANALOG_SUB_REAL].owner);
  3178. } else {
  3179. ast_verb(3, "Dumping incomplete call on %s\n", ast_channel_name(p->subs[ANALOG_SUB_THREEWAY].owner));
  3180. analog_swap_subs(p, ANALOG_SUB_THREEWAY, ANALOG_SUB_REAL);
  3181. orig_3way_sub = ANALOG_SUB_REAL;
  3182. ast_softhangup_nolock(p->subs[ANALOG_SUB_THREEWAY].owner, AST_SOFTHANGUP_DEV);
  3183. analog_set_new_owner(p, p->subs[ANALOG_SUB_REAL].owner);
  3184. ast_queue_unhold(p->subs[ANALOG_SUB_REAL].owner);
  3185. analog_set_echocanceller(p, 1);
  3186. }
  3187. }
  3188. ast_channel_unlock(p->subs[orig_3way_sub].owner);
  3189. }
  3190. winkflashdone:
  3191. analog_update_conf(p);
  3192. break;
  3193. case ANALOG_SIG_EM:
  3194. case ANALOG_SIG_EM_E1:
  3195. case ANALOG_SIG_FEATD:
  3196. case ANALOG_SIG_SF:
  3197. case ANALOG_SIG_SFWINK:
  3198. case ANALOG_SIG_SF_FEATD:
  3199. case ANALOG_SIG_FXSLS:
  3200. case ANALOG_SIG_FXSGS:
  3201. if (p->dialing) {
  3202. ast_debug(1, "Ignoring wink on channel %d\n", p->channel);
  3203. } else {
  3204. ast_debug(1, "Got wink in weird state %u on channel %d\n", ast_channel_state(ast), p->channel);
  3205. }
  3206. break;
  3207. case ANALOG_SIG_FEATDMF_TA:
  3208. switch (p->whichwink) {
  3209. case 0:
  3210. ast_debug(1, "ANI2 set to '%d' and ANI is '%s'\n", ast_channel_caller(p->owner)->ani2,
  3211. S_COR(ast_channel_caller(p->owner)->ani.number.valid,
  3212. ast_channel_caller(p->owner)->ani.number.str, ""));
  3213. snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%d%s#",
  3214. ast_channel_caller(p->owner)->ani2,
  3215. S_COR(ast_channel_caller(p->owner)->ani.number.valid,
  3216. ast_channel_caller(p->owner)->ani.number.str, ""));
  3217. break;
  3218. case 1:
  3219. ast_copy_string(p->dop.dialstr, p->finaldial, sizeof(p->dop.dialstr));
  3220. break;
  3221. case 2:
  3222. ast_log(LOG_WARNING, "Received unexpected wink on channel of type ANALOG_SIG_FEATDMF_TA\n");
  3223. return NULL;
  3224. }
  3225. p->whichwink++;
  3226. /* Fall through */
  3227. case ANALOG_SIG_FEATDMF:
  3228. case ANALOG_SIG_E911:
  3229. case ANALOG_SIG_FGC_CAMAMF:
  3230. case ANALOG_SIG_FGC_CAMA:
  3231. case ANALOG_SIG_FEATB:
  3232. case ANALOG_SIG_SF_FEATDMF:
  3233. case ANALOG_SIG_SF_FEATB:
  3234. case ANALOG_SIG_EMWINK:
  3235. /* FGD MF and EMWINK *Must* wait for wink */
  3236. if (!ast_strlen_zero(p->dop.dialstr)) {
  3237. res = analog_dial_digits(p, ANALOG_SUB_REAL, &p->dop);
  3238. if (res) {
  3239. p->dop.dialstr[0] = '\0';
  3240. return NULL;
  3241. } else {
  3242. ast_debug(1, "Sent deferred digit string on channel %d: %s\n", p->channel, p->dop.dialstr);
  3243. }
  3244. }
  3245. p->dop.dialstr[0] = '\0';
  3246. break;
  3247. default:
  3248. ast_log(LOG_WARNING, "Don't know how to handle ring/off hook for signalling %d\n", p->sig);
  3249. }
  3250. break;
  3251. case ANALOG_EVENT_HOOKCOMPLETE:
  3252. if (p->inalarm) break;
  3253. if (analog_check_waitingfordt(p)) {
  3254. break;
  3255. }
  3256. switch (mysig) {
  3257. case ANALOG_SIG_FXSLS: /* only interesting for FXS */
  3258. case ANALOG_SIG_FXSGS:
  3259. case ANALOG_SIG_FXSKS:
  3260. case ANALOG_SIG_EM:
  3261. case ANALOG_SIG_EM_E1:
  3262. case ANALOG_SIG_EMWINK:
  3263. case ANALOG_SIG_FEATD:
  3264. case ANALOG_SIG_SF:
  3265. case ANALOG_SIG_SFWINK:
  3266. case ANALOG_SIG_SF_FEATD:
  3267. if (!ast_strlen_zero(p->dop.dialstr)) {
  3268. res = analog_dial_digits(p, ANALOG_SUB_REAL, &p->dop);
  3269. if (res) {
  3270. p->dop.dialstr[0] = '\0';
  3271. return NULL;
  3272. } else {
  3273. ast_debug(1, "Sent deferred digit string on channel %d: %s\n", p->channel, p->dop.dialstr);
  3274. }
  3275. }
  3276. p->dop.dialstr[0] = '\0';
  3277. p->dop.op = ANALOG_DIAL_OP_REPLACE;
  3278. break;
  3279. case ANALOG_SIG_FEATDMF:
  3280. case ANALOG_SIG_FEATDMF_TA:
  3281. case ANALOG_SIG_E911:
  3282. case ANALOG_SIG_FGC_CAMA:
  3283. case ANALOG_SIG_FGC_CAMAMF:
  3284. case ANALOG_SIG_FEATB:
  3285. case ANALOG_SIG_SF_FEATDMF:
  3286. case ANALOG_SIG_SF_FEATB:
  3287. ast_debug(1, "Got hook complete in MF FGD, waiting for wink now on channel %d\n",p->channel);
  3288. break;
  3289. default:
  3290. break;
  3291. }
  3292. break;
  3293. case ANALOG_EVENT_POLARITY:
  3294. /*
  3295. * If we get a Polarity Switch event, this could be
  3296. * due to line seizure, remote end connect or remote end disconnect.
  3297. *
  3298. * Check to see if we should change the polarity state and
  3299. * mark the channel as UP or if this is an indication
  3300. * of remote end disconnect.
  3301. */
  3302. if (p->polarityonanswerdelay > 0) {
  3303. /* check if event is not too soon after OffHook or Answer */
  3304. if (ast_tvdiff_ms(ast_tvnow(), p->polaritydelaytv) > p->polarityonanswerdelay) {
  3305. switch (ast_channel_state(ast)) {
  3306. case AST_STATE_DIALING: /*!< Digits (or equivalent) have been dialed */
  3307. case AST_STATE_RINGING: /*!< Remote end is ringing */
  3308. if (p->answeronpolarityswitch) {
  3309. ast_debug(1, "Answering on polarity switch! channel %d\n", p->channel);
  3310. ast_setstate(p->owner, AST_STATE_UP);
  3311. p->polarity = POLARITY_REV;
  3312. if (p->hanguponpolarityswitch) {
  3313. p->polaritydelaytv = ast_tvnow();
  3314. }
  3315. } else {
  3316. ast_debug(1, "Ignore Answer on polarity switch, channel %d\n", p->channel);
  3317. }
  3318. break;
  3319. case AST_STATE_UP: /*!< Line is up */
  3320. case AST_STATE_RING: /*!< Line is ringing */
  3321. if (p->hanguponpolarityswitch) {
  3322. ast_debug(1, "HangingUp on polarity switch! channel %d\n", p->channel);
  3323. ast_queue_control_data(ast, AST_CONTROL_PVT_CAUSE_CODE, cause_code, data_size);
  3324. ast_channel_hangupcause_hash_set(ast, cause_code, data_size);
  3325. ast_softhangup(p->owner, AST_SOFTHANGUP_EXPLICIT);
  3326. p->polarity = POLARITY_IDLE;
  3327. } else {
  3328. ast_debug(1, "Ignore Hangup on polarity switch, channel %d\n", p->channel);
  3329. }
  3330. break;
  3331. case AST_STATE_DOWN: /*!< Channel is down and available */
  3332. case AST_STATE_RESERVED: /*!< Channel is down, but reserved */
  3333. case AST_STATE_OFFHOOK: /*!< Channel is off hook */
  3334. case AST_STATE_BUSY: /*!< Line is busy */
  3335. case AST_STATE_DIALING_OFFHOOK: /*!< Digits (or equivalent) have been dialed while offhook */
  3336. case AST_STATE_PRERING: /*!< Channel has detected an incoming call and is waiting for ring */
  3337. default:
  3338. if (p->answeronpolarityswitch || p->hanguponpolarityswitch) {
  3339. ast_debug(1, "Ignoring Polarity switch on channel %d, state %u\n", p->channel, ast_channel_state(ast));
  3340. }
  3341. break;
  3342. }
  3343. } else {
  3344. /* event is too soon after OffHook or Answer */
  3345. switch (ast_channel_state(ast)) {
  3346. case AST_STATE_DIALING: /*!< Digits (or equivalent) have been dialed */
  3347. case AST_STATE_RINGING: /*!< Remote end is ringing */
  3348. if (p->answeronpolarityswitch) {
  3349. ast_debug(1, "Polarity switch detected but NOT answering (too close to OffHook event) on channel %d, state %u\n", p->channel, ast_channel_state(ast));
  3350. }
  3351. break;
  3352. case AST_STATE_UP: /*!< Line is up */
  3353. case AST_STATE_RING: /*!< Line is ringing */
  3354. if (p->hanguponpolarityswitch) {
  3355. ast_debug(1, "Polarity switch detected but NOT hanging up (too close to Answer event) on channel %d, state %u\n", p->channel, ast_channel_state(ast));
  3356. }
  3357. break;
  3358. default:
  3359. if (p->answeronpolarityswitch || p->hanguponpolarityswitch) {
  3360. ast_debug(1, "Polarity switch detected (too close to previous event) on channel %d, state %u\n", p->channel, ast_channel_state(ast));
  3361. }
  3362. break;
  3363. }
  3364. }
  3365. }
  3366. /* Added more log_debug information below to provide a better indication of what is going on */
  3367. ast_debug(1, "Polarity Reversal event occured - DEBUG 2: channel %d, state %u, pol= %d, aonp= %d, honp= %d, pdelay= %d, tv= %" PRIi64 "\n", p->channel, ast_channel_state(ast), p->polarity, p->answeronpolarityswitch, p->hanguponpolarityswitch, p->polarityonanswerdelay, ast_tvdiff_ms(ast_tvnow(), p->polaritydelaytv) );
  3368. break;
  3369. default:
  3370. ast_debug(1, "Dunno what to do with event %d on channel %d\n", res, p->channel);
  3371. }
  3372. return &p->subs[idx].f;
  3373. }
  3374. struct ast_frame *analog_exception(struct analog_pvt *p, struct ast_channel *ast)
  3375. {
  3376. int res;
  3377. int idx;
  3378. struct ast_frame *f;
  3379. ast_debug(1, "%s %d\n", __FUNCTION__, p->channel);
  3380. idx = analog_get_index(ast, p, 1);
  3381. if (idx < 0) {
  3382. idx = ANALOG_SUB_REAL;
  3383. }
  3384. p->subs[idx].f.frametype = AST_FRAME_NULL;
  3385. p->subs[idx].f.datalen = 0;
  3386. p->subs[idx].f.samples = 0;
  3387. p->subs[idx].f.mallocd = 0;
  3388. p->subs[idx].f.offset = 0;
  3389. p->subs[idx].f.subclass.integer = 0;
  3390. p->subs[idx].f.delivery = ast_tv(0,0);
  3391. p->subs[idx].f.src = "dahdi_exception";
  3392. p->subs[idx].f.data.ptr = NULL;
  3393. if (!p->owner) {
  3394. /* If nobody owns us, absorb the event appropriately, otherwise
  3395. we loop indefinitely. This occurs when, during call waiting, the
  3396. other end hangs up our channel so that it no longer exists, but we
  3397. have neither FLASH'd nor ONHOOK'd to signify our desire to
  3398. change to the other channel. */
  3399. res = analog_get_event(p);
  3400. /* Switch to real if there is one and this isn't something really silly... */
  3401. if ((res != ANALOG_EVENT_RINGEROFF) && (res != ANALOG_EVENT_RINGERON) &&
  3402. (res != ANALOG_EVENT_HOOKCOMPLETE)) {
  3403. ast_debug(1, "Restoring owner of channel %d on event %d\n", p->channel, res);
  3404. analog_set_new_owner(p, p->subs[ANALOG_SUB_REAL].owner);
  3405. if (p->owner && ast != p->owner) {
  3406. /*
  3407. * Could this even happen?
  3408. * Possible deadlock because we do not have the real-call lock.
  3409. */
  3410. ast_log(LOG_WARNING, "Event %s on %s is not restored owner %s\n",
  3411. analog_event2str(res), ast_channel_name(ast), ast_channel_name(p->owner));
  3412. }
  3413. if (p->owner) {
  3414. ast_queue_unhold(p->owner);
  3415. }
  3416. }
  3417. switch (res) {
  3418. case ANALOG_EVENT_ONHOOK:
  3419. analog_set_echocanceller(p, 0);
  3420. if (p->owner) {
  3421. ast_verb(3, "Channel %s still has call, ringing phone\n", ast_channel_name(p->owner));
  3422. analog_ring(p);
  3423. analog_stop_callwait(p);
  3424. } else {
  3425. ast_log(LOG_WARNING, "Absorbed %s, but nobody is left!?!?\n",
  3426. analog_event2str(res));
  3427. }
  3428. analog_update_conf(p);
  3429. break;
  3430. case ANALOG_EVENT_RINGOFFHOOK:
  3431. analog_set_echocanceller(p, 1);
  3432. analog_off_hook(p);
  3433. if (p->owner && (ast_channel_state(p->owner) == AST_STATE_RINGING)) {
  3434. ast_queue_control(p->owner, AST_CONTROL_ANSWER);
  3435. analog_set_dialing(p, 0);
  3436. }
  3437. break;
  3438. case ANALOG_EVENT_HOOKCOMPLETE:
  3439. case ANALOG_EVENT_RINGERON:
  3440. case ANALOG_EVENT_RINGEROFF:
  3441. /* Do nothing */
  3442. break;
  3443. case ANALOG_EVENT_WINKFLASH:
  3444. gettimeofday(&p->flashtime, NULL);
  3445. if (p->owner) {
  3446. ast_verb(3, "Channel %d flashed to other channel %s\n", p->channel, ast_channel_name(p->owner));
  3447. if (ast_channel_state(p->owner) != AST_STATE_UP) {
  3448. /* Answer if necessary */
  3449. ast_queue_control(p->owner, AST_CONTROL_ANSWER);
  3450. ast_setstate(p->owner, AST_STATE_UP);
  3451. }
  3452. analog_stop_callwait(p);
  3453. ast_queue_unhold(p->owner);
  3454. } else {
  3455. ast_log(LOG_WARNING, "Absorbed %s, but nobody is left!?!?\n",
  3456. analog_event2str(res));
  3457. }
  3458. analog_update_conf(p);
  3459. break;
  3460. default:
  3461. ast_log(LOG_WARNING, "Don't know how to absorb event %s\n", analog_event2str(res));
  3462. break;
  3463. }
  3464. f = &p->subs[idx].f;
  3465. return f;
  3466. }
  3467. ast_debug(1, "Exception on %d, channel %d\n", ast_channel_fd(ast, 0), p->channel);
  3468. /* If it's not us, return NULL immediately */
  3469. if (ast != p->owner) {
  3470. ast_log(LOG_WARNING, "We're %s, not %s\n", ast_channel_name(ast), ast_channel_name(p->owner));
  3471. f = &p->subs[idx].f;
  3472. return f;
  3473. }
  3474. f = __analog_handle_event(p, ast);
  3475. if (!f) {
  3476. const char *name = ast_strdupa(ast_channel_name(ast));
  3477. /* Tell the CDR this DAHDI device hung up */
  3478. analog_unlock_private(p);
  3479. ast_channel_unlock(ast);
  3480. ast_set_hangupsource(ast, name, 0);
  3481. ast_channel_lock(ast);
  3482. analog_lock_private(p);
  3483. }
  3484. return f;
  3485. }
  3486. void *analog_handle_init_event(struct analog_pvt *i, int event)
  3487. {
  3488. int res;
  3489. pthread_t threadid;
  3490. struct ast_channel *chan;
  3491. struct ast_callid *callid = NULL;
  3492. int callid_created;
  3493. ast_debug(1, "channel (%d) - signaling (%d) - event (%s)\n",
  3494. i->channel, i->sig, analog_event2str(event));
  3495. /* Handle an event on a given channel for the monitor thread. */
  3496. switch (event) {
  3497. case ANALOG_EVENT_WINKFLASH:
  3498. case ANALOG_EVENT_RINGOFFHOOK:
  3499. if (i->inalarm) {
  3500. break;
  3501. }
  3502. /* Got a ring/answer. What kind of channel are we? */
  3503. switch (i->sig) {
  3504. case ANALOG_SIG_FXOLS:
  3505. case ANALOG_SIG_FXOGS:
  3506. case ANALOG_SIG_FXOKS:
  3507. res = analog_off_hook(i);
  3508. i->fxsoffhookstate = 1;
  3509. if (res && (errno == EBUSY)) {
  3510. break;
  3511. }
  3512. callid_created = ast_callid_threadstorage_auto(&callid);
  3513. /* Cancel VMWI spill */
  3514. analog_cancel_cidspill(i);
  3515. if (i->immediate) {
  3516. analog_set_echocanceller(i, 1);
  3517. /* The channel is immediately up. Start right away */
  3518. res = analog_play_tone(i, ANALOG_SUB_REAL, ANALOG_TONE_RINGTONE);
  3519. chan = analog_new_ast_channel(i, AST_STATE_RING, 1, ANALOG_SUB_REAL, NULL);
  3520. if (!chan) {
  3521. ast_log(LOG_WARNING, "Unable to start PBX on channel %d\n", i->channel);
  3522. res = analog_play_tone(i, ANALOG_SUB_REAL, ANALOG_TONE_CONGESTION);
  3523. if (res < 0) {
  3524. ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", i->channel);
  3525. }
  3526. }
  3527. } else {
  3528. /* Check for callerid, digits, etc */
  3529. chan = analog_new_ast_channel(i, AST_STATE_RESERVED, 0, ANALOG_SUB_REAL, NULL);
  3530. i->ss_astchan = chan;
  3531. if (chan) {
  3532. if (analog_has_voicemail(i)) {
  3533. res = analog_play_tone(i, ANALOG_SUB_REAL, ANALOG_TONE_STUTTER);
  3534. } else {
  3535. res = analog_play_tone(i, ANALOG_SUB_REAL, ANALOG_TONE_DIALTONE);
  3536. }
  3537. if (res < 0)
  3538. ast_log(LOG_WARNING, "Unable to play dialtone on channel %d, do you have defaultzone and loadzone defined?\n", i->channel);
  3539. if (ast_pthread_create_detached(&threadid, NULL, __analog_ss_thread, i)) {
  3540. ast_log(LOG_WARNING, "Unable to start simple switch thread on channel %d\n", i->channel);
  3541. res = analog_play_tone(i, ANALOG_SUB_REAL, ANALOG_TONE_CONGESTION);
  3542. if (res < 0) {
  3543. ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", i->channel);
  3544. }
  3545. ast_hangup(chan);
  3546. }
  3547. } else
  3548. ast_log(LOG_WARNING, "Unable to create channel\n");
  3549. }
  3550. ast_callid_threadstorage_auto_clean(callid, callid_created);
  3551. break;
  3552. case ANALOG_SIG_FXSLS:
  3553. case ANALOG_SIG_FXSGS:
  3554. case ANALOG_SIG_FXSKS:
  3555. analog_set_ringtimeout(i, i->ringt_base);
  3556. /* Fall through */
  3557. case ANALOG_SIG_EMWINK:
  3558. case ANALOG_SIG_FEATD:
  3559. case ANALOG_SIG_FEATDMF:
  3560. case ANALOG_SIG_FEATDMF_TA:
  3561. case ANALOG_SIG_E911:
  3562. case ANALOG_SIG_FGC_CAMA:
  3563. case ANALOG_SIG_FGC_CAMAMF:
  3564. case ANALOG_SIG_FEATB:
  3565. case ANALOG_SIG_EM:
  3566. case ANALOG_SIG_EM_E1:
  3567. case ANALOG_SIG_SFWINK:
  3568. case ANALOG_SIG_SF_FEATD:
  3569. case ANALOG_SIG_SF_FEATDMF:
  3570. case ANALOG_SIG_SF_FEATB:
  3571. case ANALOG_SIG_SF:
  3572. callid_created = ast_callid_threadstorage_auto(&callid);
  3573. /* Check for callerid, digits, etc */
  3574. if (i->cid_start == ANALOG_CID_START_POLARITY_IN || i->cid_start == ANALOG_CID_START_DTMF_NOALERT) {
  3575. chan = analog_new_ast_channel(i, AST_STATE_PRERING, 0, ANALOG_SUB_REAL, NULL);
  3576. } else {
  3577. chan = analog_new_ast_channel(i, AST_STATE_RING, 0, ANALOG_SUB_REAL, NULL);
  3578. }
  3579. i->ss_astchan = chan;
  3580. if (!chan) {
  3581. ast_log(LOG_WARNING, "Cannot allocate new structure on channel %d\n", i->channel);
  3582. } else if (ast_pthread_create_detached(&threadid, NULL, __analog_ss_thread, i)) {
  3583. ast_log(LOG_WARNING, "Unable to start simple switch thread on channel %d\n", i->channel);
  3584. res = analog_play_tone(i, ANALOG_SUB_REAL, ANALOG_TONE_CONGESTION);
  3585. if (res < 0) {
  3586. ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", i->channel);
  3587. }
  3588. ast_hangup(chan);
  3589. }
  3590. ast_callid_threadstorage_auto_clean(callid, callid_created);
  3591. break;
  3592. default:
  3593. ast_log(LOG_WARNING, "Don't know how to handle ring/answer with signalling %s on channel %d\n", analog_sigtype_to_str(i->sig), i->channel);
  3594. res = analog_play_tone(i, ANALOG_SUB_REAL, ANALOG_TONE_CONGESTION);
  3595. if (res < 0) {
  3596. ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", i->channel);
  3597. }
  3598. return NULL;
  3599. }
  3600. break;
  3601. case ANALOG_EVENT_NOALARM:
  3602. analog_set_alarm(i, 0);
  3603. analog_publish_channel_alarm_clear(i->channel);
  3604. break;
  3605. case ANALOG_EVENT_ALARM:
  3606. analog_set_alarm(i, 1);
  3607. analog_get_and_handle_alarms(i);
  3608. /* fall thru intentionally */
  3609. case ANALOG_EVENT_ONHOOK:
  3610. /* Back on hook. Hang up. */
  3611. switch (i->sig) {
  3612. case ANALOG_SIG_FXOLS:
  3613. case ANALOG_SIG_FXOGS:
  3614. i->fxsoffhookstate = 0;
  3615. analog_start_polarityswitch(i);
  3616. /* Fall through */
  3617. case ANALOG_SIG_FEATD:
  3618. case ANALOG_SIG_FEATDMF:
  3619. case ANALOG_SIG_FEATDMF_TA:
  3620. case ANALOG_SIG_E911:
  3621. case ANALOG_SIG_FGC_CAMA:
  3622. case ANALOG_SIG_FGC_CAMAMF:
  3623. case ANALOG_SIG_FEATB:
  3624. case ANALOG_SIG_EM:
  3625. case ANALOG_SIG_EM_E1:
  3626. case ANALOG_SIG_EMWINK:
  3627. case ANALOG_SIG_SF_FEATD:
  3628. case ANALOG_SIG_SF_FEATDMF:
  3629. case ANALOG_SIG_SF_FEATB:
  3630. case ANALOG_SIG_SF:
  3631. case ANALOG_SIG_SFWINK:
  3632. case ANALOG_SIG_FXSLS:
  3633. case ANALOG_SIG_FXSGS:
  3634. case ANALOG_SIG_FXSKS:
  3635. analog_set_echocanceller(i, 0);
  3636. res = analog_play_tone(i, ANALOG_SUB_REAL, -1);
  3637. analog_on_hook(i);
  3638. break;
  3639. case ANALOG_SIG_FXOKS:
  3640. i->fxsoffhookstate = 0;
  3641. analog_start_polarityswitch(i);
  3642. analog_set_echocanceller(i, 0);
  3643. /* Diddle the battery for the zhone */
  3644. #ifdef ZHONE_HACK
  3645. analog_off_hook(i);
  3646. usleep(1);
  3647. #endif
  3648. res = analog_play_tone(i, ANALOG_SUB_REAL, -1);
  3649. analog_on_hook(i);
  3650. break;
  3651. default:
  3652. ast_log(LOG_WARNING, "Don't know how to handle on hook with signalling %s on channel %d\n", analog_sigtype_to_str(i->sig), i->channel);
  3653. res = analog_play_tone(i, ANALOG_SUB_REAL, -1);
  3654. return NULL;
  3655. }
  3656. break;
  3657. case ANALOG_EVENT_POLARITY:
  3658. switch (i->sig) {
  3659. case ANALOG_SIG_FXSLS:
  3660. case ANALOG_SIG_FXSKS:
  3661. case ANALOG_SIG_FXSGS:
  3662. callid_created = ast_callid_threadstorage_auto(&callid);
  3663. /* We have already got a PR before the channel was
  3664. created, but it wasn't handled. We need polarity
  3665. to be REV for remote hangup detection to work.
  3666. At least in Spain */
  3667. if (i->hanguponpolarityswitch) {
  3668. i->polarity = POLARITY_REV;
  3669. }
  3670. if (i->cid_start == ANALOG_CID_START_POLARITY || i->cid_start == ANALOG_CID_START_POLARITY_IN) {
  3671. i->polarity = POLARITY_REV;
  3672. ast_verb(2, "Starting post polarity CID detection on channel %d\n",
  3673. i->channel);
  3674. chan = analog_new_ast_channel(i, AST_STATE_PRERING, 0, ANALOG_SUB_REAL, NULL);
  3675. i->ss_astchan = chan;
  3676. if (!chan) {
  3677. ast_log(LOG_WARNING, "Cannot allocate new structure on channel %d\n", i->channel);
  3678. } else if (ast_pthread_create_detached(&threadid, NULL, __analog_ss_thread, i)) {
  3679. ast_log(LOG_WARNING, "Unable to start simple switch thread on channel %d\n", i->channel);
  3680. ast_hangup(chan);
  3681. }
  3682. }
  3683. ast_callid_threadstorage_auto_clean(callid, callid_created);
  3684. break;
  3685. default:
  3686. ast_log(LOG_WARNING,
  3687. "handle_init_event detected polarity reversal on non-FXO (ANALOG_SIG_FXS) interface %d\n",
  3688. i->channel);
  3689. break;
  3690. }
  3691. break;
  3692. case ANALOG_EVENT_DTMFCID:
  3693. switch (i->sig) {
  3694. case ANALOG_SIG_FXSLS:
  3695. case ANALOG_SIG_FXSKS:
  3696. case ANALOG_SIG_FXSGS:
  3697. callid_created = ast_callid_threadstorage_auto(&callid);
  3698. if (i->cid_start == ANALOG_CID_START_DTMF_NOALERT) {
  3699. ast_verb(2, "Starting DTMF CID detection on channel %d\n",
  3700. i->channel);
  3701. chan = analog_new_ast_channel(i, AST_STATE_PRERING, 0, ANALOG_SUB_REAL, NULL);
  3702. i->ss_astchan = chan;
  3703. if (!chan) {
  3704. ast_log(LOG_WARNING, "Cannot allocate new structure on channel %d\n", i->channel);
  3705. } else if (ast_pthread_create_detached(&threadid, NULL, __analog_ss_thread, i)) {
  3706. ast_log(LOG_WARNING, "Unable to start simple switch thread on channel %d\n", i->channel);
  3707. ast_hangup(chan);
  3708. }
  3709. }
  3710. ast_callid_threadstorage_auto_clean(callid, callid_created);
  3711. break;
  3712. default:
  3713. ast_log(LOG_WARNING,
  3714. "handle_init_event detected dtmfcid generation event on non-FXO (ANALOG_SIG_FXS) interface %d\n",
  3715. i->channel);
  3716. break;
  3717. }
  3718. break;
  3719. case ANALOG_EVENT_REMOVED: /* destroy channel, will actually do so in do_monitor */
  3720. ast_log(LOG_NOTICE, "Got ANALOG_EVENT_REMOVED. Destroying channel %d\n",
  3721. i->channel);
  3722. return i->chan_pvt;
  3723. case ANALOG_EVENT_NEONMWI_ACTIVE:
  3724. analog_handle_notify_message(NULL, i, -1, ANALOG_EVENT_NEONMWI_ACTIVE);
  3725. break;
  3726. case ANALOG_EVENT_NEONMWI_INACTIVE:
  3727. analog_handle_notify_message(NULL, i, -1, ANALOG_EVENT_NEONMWI_INACTIVE);
  3728. break;
  3729. }
  3730. return NULL;
  3731. }
  3732. struct analog_pvt *analog_new(enum analog_sigtype signallingtype, void *private_data)
  3733. {
  3734. struct analog_pvt *p;
  3735. p = ast_calloc(1, sizeof(*p));
  3736. if (!p) {
  3737. return p;
  3738. }
  3739. p->outsigmod = ANALOG_SIG_NONE;
  3740. p->sig = signallingtype;
  3741. p->chan_pvt = private_data;
  3742. /* Some defaults for values */
  3743. p->cid_start = ANALOG_CID_START_RING;
  3744. p->cid_signalling = CID_SIG_BELL;
  3745. /* Sub real is assumed to always be alloc'd */
  3746. p->subs[ANALOG_SUB_REAL].allocd = 1;
  3747. return p;
  3748. }
  3749. /*!
  3750. * \brief Delete the analog private structure.
  3751. * \since 1.8
  3752. *
  3753. * \param doomed Analog private structure to delete.
  3754. *
  3755. * \return Nothing
  3756. */
  3757. void analog_delete(struct analog_pvt *doomed)
  3758. {
  3759. ast_free(doomed);
  3760. }
  3761. int analog_config_complete(struct analog_pvt *p)
  3762. {
  3763. /* No call waiting on non FXS channels */
  3764. if ((p->sig != ANALOG_SIG_FXOKS) && (p->sig != ANALOG_SIG_FXOLS) && (p->sig != ANALOG_SIG_FXOGS)) {
  3765. p->permcallwaiting = 0;
  3766. }
  3767. analog_set_callwaiting(p, p->permcallwaiting);
  3768. return 0;
  3769. }
  3770. void analog_free(struct analog_pvt *p)
  3771. {
  3772. ast_free(p);
  3773. }
  3774. /* called while dahdi_pvt is locked in dahdi_fixup */
  3775. int analog_fixup(struct ast_channel *oldchan, struct ast_channel *newchan, void *newp)
  3776. {
  3777. struct analog_pvt *new_pvt = newp;
  3778. int x;
  3779. ast_debug(1, "New owner for channel %d is %s\n", new_pvt->channel, ast_channel_name(newchan));
  3780. if (new_pvt->owner == oldchan) {
  3781. analog_set_new_owner(new_pvt, newchan);
  3782. }
  3783. for (x = 0; x < 3; x++) {
  3784. if (new_pvt->subs[x].owner == oldchan) {
  3785. new_pvt->subs[x].owner = newchan;
  3786. }
  3787. }
  3788. analog_update_conf(new_pvt);
  3789. return 0;
  3790. }
  3791. static void analog_publish_dnd_state(int channel, const char *status)
  3792. {
  3793. RAII_VAR(struct ast_json *, body, NULL, ast_json_unref);
  3794. RAII_VAR(struct ast_str *, dahdichan, ast_str_create(32), ast_free);
  3795. if (!dahdichan) {
  3796. return;
  3797. }
  3798. ast_str_set(&dahdichan, 0, "DAHDI/%d", channel);
  3799. body = ast_json_pack("{s: s, s: s}",
  3800. "Channel", ast_str_buffer(dahdichan),
  3801. "Status", status);
  3802. if (!body) {
  3803. return;
  3804. }
  3805. ast_manager_publish_event("DNDState", EVENT_FLAG_SYSTEM, body);
  3806. }
  3807. int analog_dnd(struct analog_pvt *p, int flag)
  3808. {
  3809. if (flag == -1) {
  3810. return p->dnd;
  3811. }
  3812. p->dnd = flag;
  3813. ast_verb(3, "%s DND on channel %d\n",
  3814. flag ? "Enabled" : "Disabled",
  3815. p->channel);
  3816. analog_publish_dnd_state(p->channel, flag ? "enabled" : "disabled");
  3817. return 0;
  3818. }