chan_vpb.cc 85 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2003, Paul Bagyenda
  5. * Paul Bagyenda <bagyenda@dsmagic.com>
  6. * Copyright (C) 2004 - 2005, Ben Kramer
  7. * Ben Kramer <ben@voicetronix.com.au>
  8. *
  9. * Daniel Bichara <daniel@bichara.com.br> - Brazilian CallerID detection (c)2004
  10. *
  11. * Welber Silveira - welberms@magiclink.com.br - (c)2004
  12. * Copying CLID string to propper structure after detection
  13. *
  14. * See http://www.asterisk.org for more information about
  15. * the Asterisk project. Please do not directly contact
  16. * any of the maintainers of this project for assistance;
  17. * the project provides a web site, mailing lists and IRC
  18. * channels for your use.
  19. *
  20. * This program is free software, distributed under the terms of
  21. * the GNU General Public License Version 2. See the LICENSE file
  22. * at the top of the source tree.
  23. */
  24. /*! \file
  25. *
  26. * \brief VoiceTronix Interface driver
  27. *
  28. * \ingroup channel_drivers
  29. */
  30. /*! \li \ref chan_vpb.cc uses the configuration file \ref vpb.conf
  31. * \addtogroup configuration_file
  32. */
  33. /*! \page vpb.conf vpb.conf
  34. * \verbinclude vpb.conf.sample
  35. */
  36. /*
  37. * XXX chan_vpb needs its native bridge code converted to the
  38. * new bridge technology scheme. The chan_dahdi native bridge
  39. * code can be used as an example. It is unlikely that this
  40. * will ever get done.
  41. *
  42. * The existing native bridge code is marked with the
  43. * VPB_NATIVE_BRIDGING conditional.
  44. */
  45. /*** MODULEINFO
  46. <depend>vpb</depend>
  47. <defaultenabled>no</defaultenabled>
  48. <support_level>extended</support_level>
  49. ***/
  50. #include <vpbapi.h>
  51. extern "C" {
  52. #include "asterisk.h"
  53. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  54. #include "asterisk/lock.h"
  55. #include "asterisk/utils.h"
  56. #include "asterisk/channel.h"
  57. #include "asterisk/config.h"
  58. #include "asterisk/module.h"
  59. #include "asterisk/pbx.h"
  60. #include "asterisk/callerid.h"
  61. #include "asterisk/dsp.h"
  62. #include "asterisk/features.h"
  63. #include "asterisk/musiconhold.h"
  64. #include "asterisk/format_cache.h"
  65. }
  66. #include <sys/socket.h>
  67. #include <sys/time.h>
  68. #include <arpa/inet.h>
  69. #include <fcntl.h>
  70. #include <sys/ioctl.h>
  71. #include <ctype.h>
  72. #include <assert.h>
  73. #ifdef pthread_create
  74. #undef pthread_create
  75. #endif
  76. #define DEFAULT_GAIN 0
  77. #define DEFAULT_ECHO_CANCEL 1
  78. #define VPB_SAMPLES 160
  79. #define VPB_MAX_BUF VPB_SAMPLES*4 + AST_FRIENDLY_OFFSET
  80. #define VPB_NULL_EVENT 200
  81. #define VPB_WAIT_TIMEOUT 4000
  82. #define MAX_VPB_GAIN 12.0
  83. #define MIN_VPB_GAIN -12.0
  84. #define DTMF_CALLERID
  85. #define DTMF_CID_START 'D'
  86. #define DTMF_CID_STOP 'C'
  87. /**/
  88. #if defined(__cplusplus) || defined(c_plusplus)
  89. extern "C" {
  90. #endif
  91. /**/
  92. static const char tdesc[] = "Standard VoiceTronix API Driver";
  93. static const char config[] = "vpb.conf";
  94. /* Default context for dialtone mode */
  95. static char context[AST_MAX_EXTENSION] = "default";
  96. /* Default language */
  97. static char language[MAX_LANGUAGE] = "";
  98. static int gruntdetect_timeout = 3600000; /* Grunt detect timeout is 1hr. */
  99. /* Protect the interface list (of vpb_pvt's) */
  100. AST_MUTEX_DEFINE_STATIC(iflock);
  101. /* Protect the monitoring thread, so only one process can kill or start it, and not
  102. when it's doing something critical. */
  103. AST_MUTEX_DEFINE_STATIC(monlock);
  104. /* This is the thread for the monitor which checks for input on the channels
  105. which are not currently in use. */
  106. static pthread_t monitor_thread;
  107. static int mthreadactive = -1; /* Flag for monitoring monitorthread.*/
  108. static int restart_monitor(void);
  109. /* The private structures of the VPB channels are
  110. linked for selecting outgoing channels */
  111. #define MODE_DIALTONE 1
  112. #define MODE_IMMEDIATE 2
  113. #define MODE_FXO 3
  114. /* Pick a country or add your own! */
  115. /* These are the tones that are played to the user */
  116. #define TONES_AU
  117. /* #define TONES_USA */
  118. #ifdef TONES_AU
  119. static VPB_TONE Dialtone = {440, 440, 440, -10, -10, -10, 5000, 0 };
  120. static VPB_TONE Busytone = {470, 0, 0, -10, -100, -100, 5000, 0 };
  121. static VPB_TONE Ringbacktone = {400, 50, 440, -10, -10, -10, 1400, 800 };
  122. #endif
  123. #ifdef TONES_USA
  124. static VPB_TONE Dialtone = {350, 440, 0, -16, -16, -100, 10000, 0};
  125. static VPB_TONE Busytone = {480, 620, 0, -10, -10, -100, 500, 500};
  126. static VPB_TONE Ringbacktone = {440, 480, 0, -20, -20, -100, 2000, 4000};
  127. #endif
  128. /* grunt tone defn's */
  129. #if 0
  130. static VPB_DETECT toned_grunt = { 3, VPB_GRUNT, 1, 2000, 3000, 0, 0, -40, 0, 0, 0, 40, { { VPB_DELAY, 1000, 0, 0 }, { VPB_RISING, 0, 40, 0 }, { 0, 100, 0, 0 } } };
  131. #endif
  132. static VPB_DETECT toned_ungrunt = { 2, VPB_GRUNT, 1, 2000, 1, 0, 0, -40, 0, 0, 30, 40, { { 0, 0, 0, 0 } } };
  133. /* Use loop polarity detection for CID */
  134. static int UsePolarityCID=0;
  135. /* Use loop drop detection */
  136. static int UseLoopDrop=1;
  137. /* To use or not to use Native bridging */
  138. static int UseNativeBridge=1;
  139. /* Use Asterisk Indication or VPB */
  140. static int use_ast_ind=0;
  141. /* Use Asterisk DTMF detection or VPB */
  142. static int use_ast_dtmfdet=0;
  143. static int relaxdtmf=0;
  144. /* Use Asterisk DTMF play back or VPB */
  145. static int use_ast_dtmf=0;
  146. /* Break for DTMF on native bridge ? */
  147. static int break_for_dtmf=1;
  148. /* Set EC suppression threshold */
  149. static short ec_supp_threshold=-1;
  150. /* Inter Digit Delay for collecting DTMF's */
  151. static int dtmf_idd = 3000;
  152. #define TIMER_PERIOD_RINGBACK 2000
  153. #define TIMER_PERIOD_BUSY 700
  154. #define TIMER_PERIOD_RING 4000
  155. static int timer_period_ring = TIMER_PERIOD_RING;
  156. #define VPB_EVENTS_ALL (VPB_MRING|VPB_MDIGIT|VPB_MDTMF|VPB_MTONEDETECT|VPB_MTIMEREXP \
  157. |VPB_MSTATION_OFFHOOK|VPB_MSTATION_ONHOOK \
  158. |VPB_MRING_OFF|VPB_MDROP|VPB_MSTATION_FLASH)
  159. #define VPB_EVENTS_NODROP (VPB_MRING|VPB_MDIGIT|VPB_MDTMF|VPB_MTONEDETECT|VPB_MTIMEREXP \
  160. |VPB_MSTATION_OFFHOOK|VPB_MSTATION_ONHOOK \
  161. |VPB_MRING_OFF|VPB_MSTATION_FLASH)
  162. #define VPB_EVENTS_NODTMF (VPB_MRING|VPB_MDIGIT|VPB_MTONEDETECT|VPB_MTIMEREXP \
  163. |VPB_MSTATION_OFFHOOK|VPB_MSTATION_ONHOOK \
  164. |VPB_MRING_OFF|VPB_MDROP|VPB_MSTATION_FLASH)
  165. #define VPB_EVENTS_STAT (VPB_MRING|VPB_MDIGIT|VPB_MDTMF|VPB_MTONEDETECT|VPB_MTIMEREXP \
  166. |VPB_MSTATION_OFFHOOK|VPB_MSTATION_ONHOOK \
  167. |VPB_MRING_OFF|VPB_MSTATION_FLASH)
  168. /* Dialing parameters for Australia */
  169. /* #define DIAL_WITH_CALL_PROGRESS */
  170. VPB_TONE_MAP DialToneMap[] = { { VPB_BUSY, VPB_CALL_DISCONNECT, 0 },
  171. { VPB_DIAL, VPB_CALL_DIALTONE, 0 },
  172. { VPB_RINGBACK, VPB_CALL_RINGBACK, 0 },
  173. { VPB_BUSY, VPB_CALL_BUSY, 0 },
  174. { VPB_GRUNT, VPB_CALL_GRUNT, 0 },
  175. { 0, 0, 1 } };
  176. #define VPB_DIALTONE_WAIT 2000 /* Wait up to 2s for a dialtone */
  177. #define VPB_RINGWAIT 4000 /* Wait up to 4s for ring tone after dialing */
  178. #define VPB_CONNECTED_WAIT 4000 /* If no ring tone detected for 4s then consider call connected */
  179. #define TIMER_PERIOD_NOANSWER 120000 /* Let it ring for 120s before deciding theres noone there */
  180. #define MAX_BRIDGES_V4PCI 2
  181. #define MAX_BRIDGES_V12PCI 128
  182. /* port states */
  183. #define VPB_STATE_ONHOOK 0
  184. #define VPB_STATE_OFFHOOK 1
  185. #define VPB_STATE_DIALLING 2
  186. #define VPB_STATE_JOINED 3
  187. #define VPB_STATE_GETDTMF 4
  188. #define VPB_STATE_PLAYDIAL 5
  189. #define VPB_STATE_PLAYBUSY 6
  190. #define VPB_STATE_PLAYRING 7
  191. #define VPB_GOT_RXHWG 1
  192. #define VPB_GOT_TXHWG 2
  193. #define VPB_GOT_RXSWG 4
  194. #define VPB_GOT_TXSWG 8
  195. typedef struct {
  196. int inuse;
  197. struct ast_channel *c0, *c1, **rc;
  198. struct ast_frame **fo;
  199. int flags;
  200. ast_mutex_t lock;
  201. ast_cond_t cond;
  202. int endbridge;
  203. } vpb_bridge_t;
  204. static vpb_bridge_t * bridges;
  205. static int max_bridges = MAX_BRIDGES_V4PCI;
  206. AST_MUTEX_DEFINE_STATIC(bridge_lock);
  207. typedef enum {
  208. vpb_model_unknown = 0,
  209. vpb_model_v4pci,
  210. vpb_model_v12pci
  211. } vpb_model_t;
  212. static struct vpb_pvt {
  213. ast_mutex_t owner_lock; /*!< Protect blocks that expect ownership to remain the same */
  214. struct ast_channel *owner; /*!< Channel who owns us, possibly NULL */
  215. int golock; /*!< Got owner lock ? */
  216. int mode; /*!< fxo/imediate/dialtone */
  217. int handle; /*!< Handle for vpb interface */
  218. int state; /*!< used to keep port state (internal to driver) */
  219. int group; /*!< Which group this port belongs to */
  220. ast_group_t callgroup; /*!< Call group */
  221. ast_group_t pickupgroup; /*!< Pickup group */
  222. char dev[256]; /*!< Device name, eg vpb/1-1 */
  223. vpb_model_t vpb_model; /*!< card model */
  224. struct ast_frame f, fr; /*!< Asterisk frame interface */
  225. char buf[VPB_MAX_BUF]; /*!< Static buffer for reading frames */
  226. int dialtone; /*!< NOT USED */
  227. float txgain, rxgain; /*!< Hardware gain control */
  228. float txswgain, rxswgain; /*!< Software gain control */
  229. int wantdtmf; /*!< Waiting for DTMF. */
  230. char context[AST_MAX_EXTENSION]; /*!< The context for this channel */
  231. char ext[AST_MAX_EXTENSION]; /*!< DTMF buffer for the ext[ens] */
  232. char language[MAX_LANGUAGE]; /*!< language being used */
  233. char callerid[AST_MAX_EXTENSION]; /*!< CallerId used for directly connected phone */
  234. int callerid_type; /*!< Caller ID type: 0=>none 1=>vpb 2=>AstV23 3=>AstBell */
  235. char cid_num[AST_MAX_EXTENSION];
  236. char cid_name[AST_MAX_EXTENSION];
  237. int dtmf_caller_pos; /*!< DTMF CallerID detection (Brazil)*/
  238. int lastoutput; /*!< Holds the last Audio format output'ed */
  239. int lastinput; /*!< Holds the last Audio format input'ed */
  240. int last_ignore_dtmf;
  241. void *busy_timer; /*!< Void pointer for busy vpb_timer */
  242. int busy_timer_id; /*!< unique timer ID for busy timer */
  243. void *ringback_timer; /*!< Void pointer for ringback vpb_timer */
  244. int ringback_timer_id; /*!< unique timer ID for ringback timer */
  245. void *ring_timer; /*!< Void pointer for ring vpb_timer */
  246. int ring_timer_id; /*!< unique timer ID for ring timer */
  247. void *dtmfidd_timer; /*!< Void pointer for DTMF IDD vpb_timer */
  248. int dtmfidd_timer_id; /*!< unique timer ID for DTMF IDD timer */
  249. struct ast_dsp *vad; /*!< AST Voice Activation Detection dsp */
  250. struct timeval lastgrunt; /*!< time stamp of last grunt event */
  251. ast_mutex_t lock; /*!< This one just protects bridge ptr below */
  252. vpb_bridge_t *bridge;
  253. int stopreads; /*!< Stop reading...*/
  254. int read_state; /*!< Read state */
  255. int chuck_count; /*!< a count of packets weve chucked away!*/
  256. pthread_t readthread; /*!< For monitoring read channel. One per owned channel. */
  257. ast_mutex_t record_lock; /*!< This one prevents reentering a record_buf block */
  258. ast_mutex_t play_lock; /*!< This one prevents reentering a play_buf block */
  259. int play_buf_time; /*!< How long the last play_buf took */
  260. struct timeval lastplay; /*!< Last play time */
  261. ast_mutex_t play_dtmf_lock;
  262. char play_dtmf[16];
  263. int faxhandled; /*!< has a fax tone been handled ? */
  264. struct vpb_pvt *next; /*!< Next channel in list */
  265. } *iflist = NULL;
  266. static struct ast_channel *vpb_new(struct vpb_pvt *i, enum ast_channel_state state, const char *context, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor);
  267. static void *do_chanreads(void *pvt);
  268. static struct ast_channel *vpb_request(const char *type, struct ast_format_cap *cap, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *data, int *cause);
  269. static int vpb_digit_begin(struct ast_channel *ast, char digit);
  270. static int vpb_digit_end(struct ast_channel *ast, char digit, unsigned int duration);
  271. static int vpb_call(struct ast_channel *ast, const char *dest, int timeout);
  272. static int vpb_hangup(struct ast_channel *ast);
  273. static int vpb_answer(struct ast_channel *ast);
  274. static struct ast_frame *vpb_read(struct ast_channel *ast);
  275. static int vpb_write(struct ast_channel *ast, struct ast_frame *frame);
  276. static int vpb_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen);
  277. static int vpb_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
  278. static struct ast_channel_tech vpb_tech = {
  279. .type = "vpb",
  280. .description = tdesc,
  281. .capabilities = NULL,
  282. .properties = 0,
  283. .requester = vpb_request,
  284. .devicestate = NULL,
  285. .send_digit_begin = vpb_digit_begin,
  286. .send_digit_end = vpb_digit_end,
  287. .call = vpb_call,
  288. .hangup = vpb_hangup,
  289. .answer = vpb_answer,
  290. .read = vpb_read,
  291. .write = vpb_write,
  292. .send_text = NULL,
  293. .send_image = NULL,
  294. .send_html = NULL,
  295. .exception = NULL,
  296. .early_bridge = NULL,
  297. .indicate = vpb_indicate,
  298. .fixup = vpb_fixup,
  299. .setoption = NULL,
  300. .queryoption = NULL,
  301. .transfer = NULL,
  302. .write_video = NULL,
  303. .write_text = NULL,
  304. .func_channel_read = NULL,
  305. .func_channel_write = NULL,
  306. };
  307. static struct ast_channel_tech vpb_tech_indicate = {
  308. .type = "vpb",
  309. .description = tdesc,
  310. .capabilities = NULL,
  311. .properties = 0,
  312. .requester = vpb_request,
  313. .devicestate = NULL,
  314. .send_digit_begin = vpb_digit_begin,
  315. .send_digit_end = vpb_digit_end,
  316. .call = vpb_call,
  317. .hangup = vpb_hangup,
  318. .answer = vpb_answer,
  319. .read = vpb_read,
  320. .write = vpb_write,
  321. .send_text = NULL,
  322. .send_image = NULL,
  323. .send_html = NULL,
  324. .exception = NULL,
  325. .early_bridge = NULL,
  326. .indicate = NULL,
  327. .fixup = vpb_fixup,
  328. .setoption = NULL,
  329. .queryoption = NULL,
  330. .transfer = NULL,
  331. .write_video = NULL,
  332. .write_text = NULL,
  333. .func_channel_read = NULL,
  334. .func_channel_write = NULL,
  335. };
  336. #if defined(VPB_NATIVE_BRIDGING)
  337. /* Can't get ast_vpb_bridge() working on v4pci without either a horrible
  338. * high pitched feedback noise or bad hiss noise depending on gain settings
  339. * Get asterisk to do the bridging
  340. */
  341. #define BAD_V4PCI_BRIDGE
  342. /* This one enables a half duplex bridge which may be required to prevent high pitched
  343. * feedback when getting asterisk to do the bridging and when using certain gain settings.
  344. */
  345. /* #define HALF_DUPLEX_BRIDGE */
  346. /* This is the Native bridge code, which Asterisk will try before using its own bridging code */
  347. static enum ast_bridge_result ast_vpb_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc, int timeoutms)
  348. {
  349. struct vpb_pvt *p0 = (struct vpb_pvt *)ast_channel_tech_pvt(c0);
  350. struct vpb_pvt *p1 = (struct vpb_pvt *)ast_channel_tech_pvt(c1);
  351. int i;
  352. int res;
  353. struct ast_channel *cs[3];
  354. struct ast_channel *who;
  355. struct ast_frame *f;
  356. cs[0] = c0;
  357. cs[1] = c1;
  358. #ifdef BAD_V4PCI_BRIDGE
  359. if (p0->vpb_model == vpb_model_v4pci)
  360. return AST_BRIDGE_FAILED_NOWARN;
  361. #endif
  362. if (UseNativeBridge != 1) {
  363. return AST_BRIDGE_FAILED_NOWARN;
  364. }
  365. /*
  366. ast_mutex_lock(&p0->lock);
  367. ast_mutex_lock(&p1->lock);
  368. */
  369. /* Bridge channels, check if we can. I believe we always can, so find a slot.*/
  370. ast_mutex_lock(&bridge_lock);
  371. for (i = 0; i < max_bridges; i++)
  372. if (!bridges[i].inuse)
  373. break;
  374. if (i < max_bridges) {
  375. bridges[i].inuse = 1;
  376. bridges[i].endbridge = 0;
  377. bridges[i].flags = flags;
  378. bridges[i].rc = rc;
  379. bridges[i].fo = fo;
  380. bridges[i].c0 = c0;
  381. bridges[i].c1 = c1;
  382. }
  383. ast_mutex_unlock(&bridge_lock);
  384. if (i == max_bridges) {
  385. ast_log(LOG_WARNING, "%s: vpb_bridge: Failed to bridge %s and %s!\n", p0->dev, ast_channel_name(c0), ast_channel_name(c1));
  386. ast_mutex_unlock(&p0->lock);
  387. ast_mutex_unlock(&p1->lock);
  388. return AST_BRIDGE_FAILED_NOWARN;
  389. } else {
  390. /* Set bridge pointers. You don't want to take these locks while holding bridge lock.*/
  391. ast_mutex_lock(&p0->lock);
  392. p0->bridge = &bridges[i];
  393. ast_mutex_unlock(&p0->lock);
  394. ast_mutex_lock(&p1->lock);
  395. p1->bridge = &bridges[i];
  396. ast_mutex_unlock(&p1->lock);
  397. ast_verb(2, "%s: vpb_bridge: Bridging call entered with [%s, %s]\n", p0->dev, ast_channel_name(c0), ast_channel_name(c1));
  398. }
  399. ast_verb(3, "Native bridging %s and %s\n", ast_channel_name(c0), ast_channel_name(c1));
  400. #ifdef HALF_DUPLEX_BRIDGE
  401. ast_debug(2, "%s: vpb_bridge: Starting half-duplex bridge [%s, %s]\n", p0->dev, ast_channel_name(c0), ast_channel_name(c1));
  402. int dir = 0;
  403. memset(p0->buf, 0, sizeof(p0->buf));
  404. memset(p1->buf, 0, sizeof(p1->buf));
  405. vpb_record_buf_start(p0->handle, VPB_ALAW);
  406. vpb_record_buf_start(p1->handle, VPB_ALAW);
  407. vpb_play_buf_start(p0->handle, VPB_ALAW);
  408. vpb_play_buf_start(p1->handle, VPB_ALAW);
  409. while (!bridges[i].endbridge) {
  410. struct vpb_pvt *from, *to;
  411. if (++dir % 2) {
  412. from = p0;
  413. to = p1;
  414. } else {
  415. from = p1;
  416. to = p0;
  417. }
  418. vpb_record_buf_sync(from->handle, from->buf, VPB_SAMPLES);
  419. vpb_play_buf_sync(to->handle, from->buf, VPB_SAMPLES);
  420. }
  421. vpb_record_buf_finish(p0->handle);
  422. vpb_record_buf_finish(p1->handle);
  423. vpb_play_buf_finish(p0->handle);
  424. vpb_play_buf_finish(p1->handle);
  425. ast_debug(2, "%s: vpb_bridge: Finished half-duplex bridge [%s, %s]\n", p0->dev, ast_channel_name(c0), ast_channel_name(c1));
  426. res = VPB_OK;
  427. #else
  428. res = vpb_bridge(p0->handle, p1->handle, VPB_BRIDGE_ON);
  429. if (res == VPB_OK) {
  430. /* pthread_cond_wait(&bridges[i].cond, &bridges[i].lock);*/ /* Wait for condition signal. */
  431. while (!bridges[i].endbridge) {
  432. /* Are we really ment to be doing nothing ?!?! */
  433. who = ast_waitfor_n(cs, 2, &timeoutms);
  434. if (!who) {
  435. if (!timeoutms) {
  436. res = AST_BRIDGE_RETRY;
  437. break;
  438. }
  439. ast_debug(1, "%s: vpb_bridge: Empty frame read...\n", p0->dev);
  440. /* check for hangup / whentohangup */
  441. if (ast_check_hangup(c0) || ast_check_hangup(c1))
  442. break;
  443. continue;
  444. }
  445. f = ast_read(who);
  446. if (!f || ((f->frametype == AST_FRAME_DTMF) &&
  447. (((who == c0) && (flags & AST_BRIDGE_DTMF_CHANNEL_0)) ||
  448. ((who == c1) && (flags & AST_BRIDGE_DTMF_CHANNEL_1))))) {
  449. *fo = f;
  450. *rc = who;
  451. ast_debug(1, "%s: vpb_bridge: Got a [%s]\n", p0->dev, f ? "digit" : "hangup");
  452. #if 0
  453. if ((c0->tech_pvt == pvt0) && (!ast_check_hangup(c0))) {
  454. if (pr0->set_rtp_peer(c0, NULL, NULL, 0))
  455. ast_log(LOG_WARNING, "Channel '%s' failed to revert\n", c0->name);
  456. }
  457. if ((c1->tech_pvt == pvt1) && (!ast_check_hangup(c1))) {
  458. if (pr1->set_rtp_peer(c1, NULL, NULL, 0))
  459. ast_log(LOG_WARNING, "Channel '%s' failed to revert back\n", c1->name);
  460. }
  461. /* That's all we needed */
  462. return 0;
  463. #endif
  464. /* Check if we need to break */
  465. if (break_for_dtmf) {
  466. break;
  467. } else if ((f->frametype == AST_FRAME_DTMF) && ((f->subclass.integer == '#') || (f->subclass.integer == '*'))) {
  468. break;
  469. }
  470. } else {
  471. if ((f->frametype == AST_FRAME_DTMF) ||
  472. (f->frametype == AST_FRAME_VOICE) ||
  473. (f->frametype == AST_FRAME_VIDEO))
  474. {
  475. /* Forward voice or DTMF frames if they happen upon us */
  476. /* Actually I dont think we want to forward on any frames!
  477. if (who == c0) {
  478. ast_write(c1, f);
  479. } else if (who == c1) {
  480. ast_write(c0, f);
  481. }
  482. */
  483. }
  484. ast_frfree(f);
  485. }
  486. /* Swap priority not that it's a big deal at this point */
  487. cs[2] = cs[0];
  488. cs[0] = cs[1];
  489. cs[1] = cs[2];
  490. };
  491. vpb_bridge(p0->handle, p1->handle, VPB_BRIDGE_OFF);
  492. }
  493. #endif
  494. ast_mutex_lock(&bridge_lock);
  495. bridges[i].inuse = 0;
  496. ast_mutex_unlock(&bridge_lock);
  497. p0->bridge = NULL;
  498. p1->bridge = NULL;
  499. ast_verb(2, "Bridging call done with [%s, %s] => %d\n", ast_channel_name(c0), ast_channel_name(c1), res);
  500. /*
  501. ast_mutex_unlock(&p0->lock);
  502. ast_mutex_unlock(&p1->lock);
  503. */
  504. return (res == VPB_OK) ? AST_BRIDGE_COMPLETE : AST_BRIDGE_FAILED;
  505. }
  506. #endif /* defined(VPB_NATIVE_BRIDGING) */
  507. /* Caller ID can be located in different positions between the rings depending on your Telco
  508. * Australian (Telstra) callerid starts 700ms after 1st ring and finishes 1.5s after first ring
  509. * Use ANALYSE_CID to record rings and determine location of callerid
  510. */
  511. /* #define ANALYSE_CID */
  512. #define RING_SKIP 300
  513. #define CID_MSECS 2000
  514. static void get_callerid(struct vpb_pvt *p)
  515. {
  516. short buf[CID_MSECS*8]; /* 8kHz sampling rate */
  517. struct timeval cid_record_time;
  518. int rc;
  519. struct ast_channel *owner = p->owner;
  520. /*
  521. char callerid[AST_MAX_EXTENSION] = "";
  522. */
  523. #ifdef ANALYSE_CID
  524. void * ws;
  525. char * file="cidsams.wav";
  526. #endif
  527. if (ast_mutex_trylock(&p->record_lock) == 0) {
  528. cid_record_time = ast_tvnow();
  529. ast_verb(4, "CID record - start\n");
  530. /* Skip any trailing ringtone */
  531. if (UsePolarityCID != 1){
  532. vpb_sleep(RING_SKIP);
  533. }
  534. ast_verb(4, "CID record - skipped %lldms trailing ring\n",
  535. (long long int) ast_tvdiff_ms(ast_tvnow(), cid_record_time));
  536. cid_record_time = ast_tvnow();
  537. /* Record bit between the rings which contains the callerid */
  538. vpb_record_buf_start(p->handle, VPB_LINEAR);
  539. rc = vpb_record_buf_sync(p->handle, (char*)buf, sizeof(buf));
  540. vpb_record_buf_finish(p->handle);
  541. #ifdef ANALYSE_CID
  542. vpb_wave_open_write(&ws, file, VPB_LINEAR);
  543. vpb_wave_write(ws, (char *)buf, sizeof(buf));
  544. vpb_wave_close_write(ws);
  545. #endif
  546. ast_verb(4, "CID record - recorded %lldms between rings\n",
  547. (long long int) ast_tvdiff_ms(ast_tvnow(), cid_record_time));
  548. ast_mutex_unlock(&p->record_lock);
  549. if (rc != VPB_OK) {
  550. ast_log(LOG_ERROR, "Failed to record caller id sample on %s\n", p->dev);
  551. return;
  552. }
  553. VPB_CID *cli_struct = new VPB_CID;
  554. cli_struct->ra_cldn[0] = 0;
  555. cli_struct->ra_cn[0] = 0;
  556. /* This decodes FSK 1200baud type callerid */
  557. if ((rc = vpb_cid_decode2(cli_struct, buf, CID_MSECS * 8)) == VPB_OK ) {
  558. /*
  559. if (owner->cid.cid_num)
  560. ast_free(owner->cid.cid_num);
  561. owner->cid.cid_num=NULL;
  562. if (owner->cid.cid_name)
  563. ast_free(owner->cid.cid_name);
  564. owner->cid.cid_name=NULL;
  565. */
  566. if (cli_struct->ra_cldn[0] == '\0') {
  567. /*
  568. owner->cid.cid_num = ast_strdup(cli_struct->cldn);
  569. owner->cid.cid_name = ast_strdup(cli_struct->cn);
  570. */
  571. if (owner) {
  572. ast_set_callerid(owner, cli_struct->cldn, cli_struct->cn, cli_struct->cldn);
  573. } else {
  574. strcpy(p->cid_num, cli_struct->cldn);
  575. strcpy(p->cid_name, cli_struct->cn);
  576. }
  577. ast_verb(4, "CID record - got [%s] [%s]\n",
  578. S_COR(ast_channel_caller(owner)->id.number.valid, ast_channel_caller(owner)->id.number.str, ""),
  579. S_COR(ast_channel_caller(owner)->id.name.valid, ast_channel_caller(owner)->id.name.str, ""));
  580. snprintf(p->callerid, sizeof(p->callerid), "%s %s", cli_struct->cldn, cli_struct->cn);
  581. } else {
  582. ast_log(LOG_ERROR, "CID record - No caller id avalable on %s \n", p->dev);
  583. }
  584. } else {
  585. ast_log(LOG_ERROR, "CID record - Failed to decode caller id on %s - %d\n", p->dev, rc);
  586. ast_copy_string(p->callerid, "unknown", sizeof(p->callerid));
  587. }
  588. delete cli_struct;
  589. } else
  590. ast_log(LOG_ERROR, "CID record - Failed to set record mode for caller id on %s\n", p->dev);
  591. }
  592. static void get_callerid_ast(struct vpb_pvt *p)
  593. {
  594. struct callerid_state *cs;
  595. char buf[1024];
  596. char *name = NULL, *number = NULL;
  597. int flags;
  598. int rc = 0, vrc;
  599. int sam_count = 0;
  600. struct ast_channel *owner = p->owner;
  601. int which_cid;
  602. /*
  603. float old_gain;
  604. */
  605. #ifdef ANALYSE_CID
  606. void * ws;
  607. char * file = "cidsams.wav";
  608. #endif
  609. if (p->callerid_type == 1) {
  610. ast_verb(4, "Collected caller ID already\n");
  611. return;
  612. }
  613. else if (p->callerid_type == 2 ) {
  614. which_cid = CID_SIG_V23;
  615. ast_verb(4, "Collecting Caller ID v23...\n");
  616. }
  617. else if (p->callerid_type == 3) {
  618. which_cid = CID_SIG_BELL;
  619. ast_verb(4, "Collecting Caller ID bell...\n");
  620. } else {
  621. ast_verb(4, "Caller ID disabled\n");
  622. return;
  623. }
  624. /* vpb_sleep(RING_SKIP); */
  625. /* vpb_record_get_gain(p->handle, &old_gain); */
  626. cs = callerid_new(which_cid);
  627. if (cs) {
  628. #ifdef ANALYSE_CID
  629. vpb_wave_open_write(&ws, file, VPB_MULAW);
  630. vpb_record_set_gain(p->handle, 3.0);
  631. vpb_record_set_hw_gain(p->handle, 12.0);
  632. #endif
  633. vpb_record_buf_start(p->handle, VPB_MULAW);
  634. while ((rc == 0) && (sam_count < 8000 * 3)) {
  635. vrc = vpb_record_buf_sync(p->handle, (char*)buf, sizeof(buf));
  636. if (vrc != VPB_OK)
  637. ast_log(LOG_ERROR, "%s: Caller ID couldn't read audio buffer!\n", p->dev);
  638. rc = callerid_feed(cs, (unsigned char *)buf, sizeof(buf), ast_format_ulaw);
  639. #ifdef ANALYSE_CID
  640. vpb_wave_write(ws, (char *)buf, sizeof(buf));
  641. #endif
  642. sam_count += sizeof(buf);
  643. ast_verb(4, "Collecting Caller ID samples [%d][%d]...\n", sam_count, rc);
  644. }
  645. vpb_record_buf_finish(p->handle);
  646. #ifdef ANALYSE_CID
  647. vpb_wave_close_write(ws);
  648. #endif
  649. if (rc == 1) {
  650. callerid_get(cs, &name, &number, &flags);
  651. ast_debug(1, "%s: Caller ID name [%s] number [%s] flags [%d]\n", p->dev, name, number, flags);
  652. } else {
  653. ast_log(LOG_ERROR, "%s: Failed to decode Caller ID \n", p->dev);
  654. }
  655. /* vpb_record_set_gain(p->handle, old_gain); */
  656. /* vpb_record_set_hw_gain(p->handle,6.0); */
  657. } else {
  658. ast_log(LOG_ERROR, "%s: Failed to create Caller ID struct\n", p->dev);
  659. }
  660. ast_party_number_free(&ast_channel_caller(owner)->id.number);
  661. ast_party_number_init(&ast_channel_caller(owner)->id.number);
  662. ast_party_name_free(&ast_channel_caller(owner)->id.name);
  663. ast_party_name_init(&ast_channel_caller(owner)->id.name);
  664. if (number)
  665. ast_shrink_phone_number(number);
  666. ast_set_callerid(owner,
  667. number, name,
  668. ast_channel_caller(owner)->ani.number.valid ? NULL : number);
  669. if (!ast_strlen_zero(name)){
  670. snprintf(p->callerid, sizeof(p->callerid), "%s %s", number, name);
  671. } else {
  672. ast_copy_string(p->callerid, number, sizeof(p->callerid));
  673. }
  674. if (cs)
  675. callerid_free(cs);
  676. }
  677. /* Terminate any tones we are presently playing */
  678. static void stoptone(int handle)
  679. {
  680. int ret;
  681. VPB_EVENT je;
  682. while (vpb_playtone_state(handle) != VPB_OK) {
  683. vpb_tone_terminate(handle);
  684. ret = vpb_get_event_ch_async(handle, &je);
  685. if ((ret == VPB_OK) && (je.type != VPB_DIALEND)) {
  686. ast_verb(4, "Stop tone collected a wrong event!![%d]\n", je.type);
  687. /* vpb_put_event(&je); */
  688. }
  689. vpb_sleep(10);
  690. }
  691. }
  692. /* Safe vpb_playtone_async */
  693. static int playtone( int handle, VPB_TONE *tone)
  694. {
  695. int ret = VPB_OK;
  696. stoptone(handle);
  697. ast_verb(4, "[%02d]: Playing tone\n", handle);
  698. ret = vpb_playtone_async(handle, tone);
  699. return ret;
  700. }
  701. static inline int monitor_handle_owned(struct vpb_pvt *p, VPB_EVENT *e)
  702. {
  703. struct ast_frame f = {AST_FRAME_CONTROL}; /* default is control, Clear rest. */
  704. int endbridge = 0;
  705. ast_verb(4, "%s: handle_owned: got event: [%d=>%d]\n", p->dev, e->type, e->data);
  706. f.src = "vpb";
  707. switch (e->type) {
  708. case VPB_RING:
  709. if (p->mode == MODE_FXO) {
  710. f.subclass.integer = AST_CONTROL_RING;
  711. vpb_timer_stop(p->ring_timer);
  712. vpb_timer_start(p->ring_timer);
  713. } else
  714. f.frametype = AST_FRAME_NULL; /* ignore ring on station port. */
  715. break;
  716. case VPB_RING_OFF:
  717. f.frametype = AST_FRAME_NULL;
  718. break;
  719. case VPB_TIMEREXP:
  720. if (e->data == p->busy_timer_id) {
  721. playtone(p->handle, &Busytone);
  722. p->state = VPB_STATE_PLAYBUSY;
  723. vpb_timer_stop(p->busy_timer);
  724. vpb_timer_start(p->busy_timer);
  725. f.frametype = AST_FRAME_NULL;
  726. } else if (e->data == p->ringback_timer_id) {
  727. playtone(p->handle, &Ringbacktone);
  728. vpb_timer_stop(p->ringback_timer);
  729. vpb_timer_start(p->ringback_timer);
  730. f.frametype = AST_FRAME_NULL;
  731. } else if (e->data == p->ring_timer_id) {
  732. /* We didnt get another ring in time! */
  733. if (ast_channel_state(p->owner) != AST_STATE_UP) {
  734. /* Assume caller has hung up */
  735. vpb_timer_stop(p->ring_timer);
  736. f.subclass.integer = AST_CONTROL_HANGUP;
  737. } else {
  738. vpb_timer_stop(p->ring_timer);
  739. f.frametype = AST_FRAME_NULL;
  740. }
  741. } else {
  742. f.frametype = AST_FRAME_NULL; /* Ignore. */
  743. }
  744. break;
  745. case VPB_DTMF_DOWN:
  746. case VPB_DTMF:
  747. if (use_ast_dtmfdet) {
  748. f.frametype = AST_FRAME_NULL;
  749. } else if (ast_channel_state(p->owner) == AST_STATE_UP) {
  750. f.frametype = AST_FRAME_DTMF;
  751. f.subclass.integer = e->data;
  752. } else
  753. f.frametype = AST_FRAME_NULL;
  754. break;
  755. case VPB_TONEDETECT:
  756. if (e->data == VPB_BUSY || e->data == VPB_BUSY_308 || e->data == VPB_BUSY_AUST ) {
  757. ast_debug(4, "%s: handle_owned: got event: BUSY\n", p->dev);
  758. if (ast_channel_state(p->owner) == AST_STATE_UP) {
  759. f.subclass.integer = AST_CONTROL_HANGUP;
  760. } else {
  761. f.subclass.integer = AST_CONTROL_BUSY;
  762. }
  763. } else if (e->data == VPB_FAX) {
  764. if (!p->faxhandled) {
  765. if (strcmp(ast_channel_exten(p->owner), "fax")) {
  766. const char *target_context = S_OR(ast_channel_macrocontext(p->owner), ast_channel_context(p->owner));
  767. if (ast_exists_extension(p->owner, target_context, "fax", 1,
  768. S_COR(ast_channel_caller(p->owner)->id.number.valid, ast_channel_caller(p->owner)->id.number.str, NULL))) {
  769. ast_verb(3, "Redirecting %s to fax extension\n", ast_channel_name(p->owner));
  770. /* Save the DID/DNIS when we transfer the fax call to a "fax" extension */
  771. pbx_builtin_setvar_helper(p->owner, "FAXEXTEN", ast_channel_exten(p->owner));
  772. if (ast_async_goto(p->owner, target_context, "fax", 1)) {
  773. ast_log(LOG_WARNING, "Failed to async goto '%s' into fax of '%s'\n", ast_channel_name(p->owner), target_context);
  774. }
  775. } else {
  776. ast_log(LOG_NOTICE, "Fax detected, but no fax extension\n");
  777. }
  778. } else {
  779. ast_debug(1, "Already in a fax extension, not redirecting\n");
  780. }
  781. } else {
  782. ast_debug(1, "Fax already handled\n");
  783. }
  784. } else if (e->data == VPB_GRUNT) {
  785. if (ast_tvdiff_ms(ast_tvnow(), p->lastgrunt) > gruntdetect_timeout) {
  786. /* Nothing heard on line for a very long time
  787. * Timeout connection */
  788. ast_verb(3, "grunt timeout\n");
  789. ast_log(LOG_NOTICE, "%s: Line hangup due of lack of conversation\n", p->dev);
  790. f.subclass.integer = AST_CONTROL_HANGUP;
  791. } else {
  792. p->lastgrunt = ast_tvnow();
  793. f.frametype = AST_FRAME_NULL;
  794. }
  795. } else {
  796. f.frametype = AST_FRAME_NULL;
  797. }
  798. break;
  799. case VPB_CALLEND:
  800. #ifdef DIAL_WITH_CALL_PROGRESS
  801. if (e->data == VPB_CALL_CONNECTED) {
  802. f.subclass.integer = AST_CONTROL_ANSWER;
  803. } else if (e->data == VPB_CALL_NO_DIAL_TONE || e->data == VPB_CALL_NO_RING_BACK) {
  804. f.subclass.integer = AST_CONTROL_CONGESTION;
  805. } else if (e->data == VPB_CALL_NO_ANSWER || e->data == VPB_CALL_BUSY) {
  806. f.subclass.integer = AST_CONTROL_BUSY;
  807. } else if (e->data == VPB_CALL_DISCONNECTED) {
  808. f.subclass.integer = AST_CONTROL_HANGUP;
  809. }
  810. #else
  811. ast_log(LOG_NOTICE, "%s: Got call progress callback but blind dialing \n", p->dev);
  812. f.frametype = AST_FRAME_NULL;
  813. #endif
  814. break;
  815. case VPB_STATION_OFFHOOK:
  816. f.subclass.integer = AST_CONTROL_ANSWER;
  817. break;
  818. case VPB_DROP:
  819. if ((p->mode == MODE_FXO) && (UseLoopDrop)) { /* ignore loop drop on stations */
  820. if (ast_channel_state(p->owner) == AST_STATE_UP) {
  821. f.subclass.integer = AST_CONTROL_HANGUP;
  822. } else {
  823. f.frametype = AST_FRAME_NULL;
  824. }
  825. }
  826. break;
  827. case VPB_LOOP_ONHOOK:
  828. if (ast_channel_state(p->owner) == AST_STATE_UP) {
  829. f.subclass.integer = AST_CONTROL_HANGUP;
  830. } else {
  831. f.frametype = AST_FRAME_NULL;
  832. }
  833. break;
  834. case VPB_STATION_ONHOOK:
  835. f.subclass.integer = AST_CONTROL_HANGUP;
  836. break;
  837. case VPB_STATION_FLASH:
  838. f.subclass.integer = AST_CONTROL_FLASH;
  839. break;
  840. /* Called when dialing has finished and ringing starts
  841. * No indication that call has really been answered when using blind dialing
  842. */
  843. case VPB_DIALEND:
  844. if (p->state < 5) {
  845. f.subclass.integer = AST_CONTROL_ANSWER;
  846. ast_verb(2, "%s: Dialend\n", p->dev);
  847. } else {
  848. f.frametype = AST_FRAME_NULL;
  849. }
  850. break;
  851. /* case VPB_PLAY_UNDERFLOW:
  852. f.frametype = AST_FRAME_NULL;
  853. vpb_reset_play_fifo_alarm(p->handle);
  854. break;
  855. case VPB_RECORD_OVERFLOW:
  856. f.frametype = AST_FRAME_NULL;
  857. vpb_reset_record_fifo_alarm(p->handle);
  858. break;
  859. */
  860. default:
  861. f.frametype = AST_FRAME_NULL;
  862. break;
  863. }
  864. /*
  865. ast_verb(4, "%s: LOCKING in handle_owned [%d]\n", p->dev,res);
  866. res = ast_mutex_lock(&p->lock);
  867. ast_verb(4, "%s: LOCKING count[%d] owner[%d] \n", p->dev, p->lock.__m_count,p->lock.__m_owner);
  868. */
  869. if (p->bridge) { /* Check what happened, see if we need to report it. */
  870. switch (f.frametype) {
  871. case AST_FRAME_DTMF:
  872. if ( !(p->bridge->c0 == p->owner &&
  873. (p->bridge->flags & AST_BRIDGE_DTMF_CHANNEL_0) ) &&
  874. !(p->bridge->c1 == p->owner &&
  875. (p->bridge->flags & AST_BRIDGE_DTMF_CHANNEL_1) )) {
  876. /* Kill bridge, this is interesting. */
  877. endbridge = 1;
  878. }
  879. break;
  880. case AST_FRAME_CONTROL:
  881. endbridge = 1;
  882. break;
  883. default:
  884. break;
  885. }
  886. if (endbridge) {
  887. if (p->bridge->fo) {
  888. *p->bridge->fo = ast_frisolate(&f);
  889. }
  890. if (p->bridge->rc) {
  891. *p->bridge->rc = p->owner;
  892. }
  893. ast_mutex_lock(&p->bridge->lock);
  894. p->bridge->endbridge = 1;
  895. ast_cond_signal(&p->bridge->cond);
  896. ast_mutex_unlock(&p->bridge->lock);
  897. }
  898. }
  899. if (endbridge) {
  900. ast_mutex_unlock(&p->lock);
  901. /*
  902. ast_verb(4, "%s: unLOCKING in handle_owned [%d]\n", p->dev,res);
  903. */
  904. return 0;
  905. }
  906. ast_verb(4, "%s: handle_owned: Prepared frame type[%d]subclass[%d], bridge=%p owner=[%s]\n",
  907. p->dev, f.frametype, f.subclass.integer, (void *)p->bridge, ast_channel_name(p->owner));
  908. /* Trylock used here to avoid deadlock that can occur if we
  909. * happen to be in here handling an event when hangup is called
  910. * Problem is that hangup holds p->owner->lock
  911. */
  912. if ((f.frametype >= 0) && (f.frametype != AST_FRAME_NULL) && (p->owner)) {
  913. if (ast_channel_trylock(p->owner) == 0) {
  914. ast_queue_frame(p->owner, &f);
  915. ast_channel_unlock(p->owner);
  916. ast_verb(4, "%s: handled_owned: Queued Frame to [%s]\n", p->dev, ast_channel_name(p->owner));
  917. } else {
  918. ast_verbose("%s: handled_owned: Missed event %d/%d \n",
  919. p->dev, f.frametype, f.subclass.integer);
  920. }
  921. }
  922. ast_mutex_unlock(&p->lock);
  923. /*
  924. ast_verb(4, "%s: unLOCKING in handle_owned [%d]\n", p->dev,res);
  925. */
  926. return 0;
  927. }
  928. static inline int monitor_handle_notowned(struct vpb_pvt *p, VPB_EVENT *e)
  929. {
  930. char s[2] = {0};
  931. struct ast_channel *owner = p->owner;
  932. char cid_num[256];
  933. char cid_name[256];
  934. /*
  935. struct ast_channel *c;
  936. */
  937. char str[VPB_MAX_STR];
  938. vpb_translate_event(e, str);
  939. ast_verb(4, "%s: handle_notowned: mode=%d, event[%d][%s]=[%d]\n", p->dev, p->mode, e->type,str, e->data);
  940. switch (e->type) {
  941. case VPB_LOOP_ONHOOK:
  942. case VPB_LOOP_POLARITY:
  943. if (UsePolarityCID == 1) {
  944. ast_verb(4, "Polarity reversal\n");
  945. if (p->callerid_type == 1) {
  946. ast_verb(4, "Using VPB Caller ID\n");
  947. get_callerid(p); /* UK CID before 1st ring*/
  948. }
  949. /* get_callerid_ast(p); */ /* Caller ID using the ast functions */
  950. }
  951. break;
  952. case VPB_RING:
  953. if (p->mode == MODE_FXO) /* FXO port ring, start * */ {
  954. vpb_new(p, AST_STATE_RING, p->context, NULL, NULL);
  955. if (UsePolarityCID != 1) {
  956. if (p->callerid_type == 1) {
  957. ast_verb(4, "Using VPB Caller ID\n");
  958. get_callerid(p); /* Australian CID only between 1st and 2nd ring */
  959. }
  960. get_callerid_ast(p); /* Caller ID using the ast functions */
  961. } else {
  962. ast_log(LOG_ERROR, "Setting caller ID: %s %s\n", p->cid_num, p->cid_name);
  963. ast_set_callerid(p->owner, p->cid_num, p->cid_name, p->cid_num);
  964. p->cid_num[0] = 0;
  965. p->cid_name[0] = 0;
  966. }
  967. vpb_timer_stop(p->ring_timer);
  968. vpb_timer_start(p->ring_timer);
  969. }
  970. break;
  971. case VPB_RING_OFF:
  972. break;
  973. case VPB_STATION_OFFHOOK:
  974. if (p->mode == MODE_IMMEDIATE) {
  975. vpb_new(p,AST_STATE_RING, p->context, NULL, NULL);
  976. } else {
  977. ast_verb(4, "%s: handle_notowned: playing dialtone\n", p->dev);
  978. playtone(p->handle, &Dialtone);
  979. p->state = VPB_STATE_PLAYDIAL;
  980. p->wantdtmf = 1;
  981. p->ext[0] = 0; /* Just to be sure & paranoid.*/
  982. }
  983. break;
  984. case VPB_DIALEND:
  985. if (p->mode == MODE_DIALTONE) {
  986. if (p->state == VPB_STATE_PLAYDIAL) {
  987. playtone(p->handle, &Dialtone);
  988. p->wantdtmf = 1;
  989. p->ext[0] = 0; /* Just to be sure & paranoid. */
  990. }
  991. #if 0
  992. /* These are not needed as they have timers to restart them */
  993. else if (p->state == VPB_STATE_PLAYBUSY) {
  994. playtone(p->handle, &Busytone);
  995. p->wantdtmf = 1;
  996. p->ext[0] = 0;
  997. } else if (p->state == VPB_STATE_PLAYRING) {
  998. playtone(p->handle, &Ringbacktone);
  999. p->wantdtmf = 1;
  1000. p->ext[0] = 0;
  1001. }
  1002. #endif
  1003. } else {
  1004. ast_verb(4, "%s: handle_notowned: Got a DIALEND when not really expected\n",p->dev);
  1005. }
  1006. break;
  1007. case VPB_STATION_ONHOOK: /* clear ext */
  1008. stoptone(p->handle);
  1009. p->wantdtmf = 1 ;
  1010. p->ext[0] = 0;
  1011. p->state = VPB_STATE_ONHOOK;
  1012. break;
  1013. case VPB_TIMEREXP:
  1014. if (e->data == p->dtmfidd_timer_id) {
  1015. if (ast_exists_extension(NULL, p->context, p->ext, 1, p->callerid)){
  1016. ast_verb(4, "%s: handle_notowned: DTMF IDD timer out, matching on [%s] in [%s]\n", p->dev, p->ext, p->context);
  1017. vpb_new(p, AST_STATE_RING, p->context, NULL, NULL);
  1018. }
  1019. } else if (e->data == p->ring_timer_id) {
  1020. /* We didnt get another ring in time! */
  1021. if (p->owner) {
  1022. if (ast_channel_state(p->owner) != AST_STATE_UP) {
  1023. /* Assume caller has hung up */
  1024. vpb_timer_stop(p->ring_timer);
  1025. }
  1026. } else {
  1027. /* No owner any more, Assume caller has hung up */
  1028. vpb_timer_stop(p->ring_timer);
  1029. }
  1030. }
  1031. break;
  1032. case VPB_DTMF:
  1033. if (p->state == VPB_STATE_ONHOOK){
  1034. /* DTMF's being passed while on-hook maybe Caller ID */
  1035. if (p->mode == MODE_FXO) {
  1036. if (e->data == DTMF_CID_START) { /* CallerID Start signal */
  1037. p->dtmf_caller_pos = 0; /* Leaves the first digit out */
  1038. memset(p->callerid, 0, sizeof(p->callerid));
  1039. } else if (e->data == DTMF_CID_STOP) { /* CallerID End signal */
  1040. p->callerid[p->dtmf_caller_pos] = '\0';
  1041. ast_verb(3, " %s: DTMF CallerID %s\n", p->dev, p->callerid);
  1042. if (owner) {
  1043. /*
  1044. if (owner->cid.cid_num)
  1045. ast_free(owner->cid.cid_num);
  1046. owner->cid.cid_num=NULL;
  1047. if (owner->cid.cid_name)
  1048. ast_free(owner->cid.cid_name);
  1049. owner->cid.cid_name=NULL;
  1050. owner->cid.cid_num = strdup(p->callerid);
  1051. */
  1052. cid_name[0] = '\0';
  1053. cid_num[0] = '\0';
  1054. ast_callerid_split(p->callerid, cid_name, sizeof(cid_name), cid_num, sizeof(cid_num));
  1055. ast_set_callerid(owner, cid_num, cid_name, cid_num);
  1056. } else {
  1057. ast_verb(3, " %s: DTMF CallerID: no owner to assign CID \n", p->dev);
  1058. }
  1059. } else if (p->dtmf_caller_pos < AST_MAX_EXTENSION) {
  1060. if (p->dtmf_caller_pos >= 0) {
  1061. p->callerid[p->dtmf_caller_pos] = e->data;
  1062. }
  1063. p->dtmf_caller_pos++;
  1064. }
  1065. }
  1066. break;
  1067. }
  1068. if (p->wantdtmf == 1) {
  1069. stoptone(p->handle);
  1070. p->wantdtmf = 0;
  1071. }
  1072. p->state = VPB_STATE_GETDTMF;
  1073. s[0] = e->data;
  1074. strncat(p->ext, s, sizeof(p->ext) - strlen(p->ext) - 1);
  1075. #if 0
  1076. if (!strcmp(p->ext, ast_pickup_ext())) {
  1077. /* Call pickup has been dialled! */
  1078. if (ast_pickup_call(c)) {
  1079. /* Call pickup wasnt possible */
  1080. }
  1081. } else
  1082. #endif
  1083. if (ast_exists_extension(NULL, p->context, p->ext, 1, p->callerid)) {
  1084. if (ast_canmatch_extension(NULL, p->context, p->ext, 1, p->callerid)) {
  1085. ast_verb(4, "%s: handle_notowned: Multiple matches on [%s] in [%s]\n", p->dev, p->ext, p->context);
  1086. /* Start DTMF IDD timer */
  1087. vpb_timer_stop(p->dtmfidd_timer);
  1088. vpb_timer_start(p->dtmfidd_timer);
  1089. } else {
  1090. ast_verb(4, "%s: handle_notowned: Matched on [%s] in [%s]\n", p->dev, p->ext , p->context);
  1091. vpb_new(p, AST_STATE_UP, p->context, NULL, NULL);
  1092. }
  1093. } else if (!ast_canmatch_extension(NULL, p->context, p->ext, 1, p->callerid)) {
  1094. if (ast_exists_extension(NULL, "default", p->ext, 1, p->callerid)) {
  1095. vpb_new(p, AST_STATE_UP, "default", NULL, NULL);
  1096. } else if (!ast_canmatch_extension(NULL, "default", p->ext, 1, p->callerid)) {
  1097. ast_verb(4, "%s: handle_notowned: can't match anything in %s or default\n", p->dev, p->context);
  1098. playtone(p->handle, &Busytone);
  1099. vpb_timer_stop(p->busy_timer);
  1100. vpb_timer_start(p->busy_timer);
  1101. p->state = VPB_STATE_PLAYBUSY;
  1102. }
  1103. }
  1104. break;
  1105. default:
  1106. /* Ignore.*/
  1107. break;
  1108. }
  1109. ast_verb(4, "%s: handle_notowned: mode=%d, [%d=>%d]\n", p->dev, p->mode, e->type, e->data);
  1110. return 0;
  1111. }
  1112. static void *do_monitor(void *unused)
  1113. {
  1114. /* Monitor thread, doesn't die until explicitly killed. */
  1115. ast_verb(2, "Starting vpb monitor thread[%ld]\n", pthread_self());
  1116. pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  1117. for (;;) {
  1118. VPB_EVENT e;
  1119. VPB_EVENT je;
  1120. char str[VPB_MAX_STR];
  1121. struct vpb_pvt *p;
  1122. /*
  1123. ast_verb(4, "Monitor waiting for event\n");
  1124. */
  1125. int res = vpb_get_event_sync(&e, VPB_WAIT_TIMEOUT);
  1126. if ((res == VPB_NO_EVENTS) || (res == VPB_TIME_OUT)) {
  1127. /*
  1128. if (res == VPB_NO_EVENTS) {
  1129. ast_verb(4, "No events....\n");
  1130. } else {
  1131. ast_verb(4, "No events, timed out....\n");
  1132. }
  1133. */
  1134. continue;
  1135. }
  1136. if (res != VPB_OK) {
  1137. ast_log(LOG_ERROR,"Monitor get event error %d\n", res );
  1138. ast_verbose("Monitor get event error %d\n", res );
  1139. continue;
  1140. }
  1141. str[0] = 0;
  1142. p = NULL;
  1143. ast_mutex_lock(&monlock);
  1144. if (e.type == VPB_NULL_EVENT) {
  1145. ast_verb(4, "Monitor got null event\n");
  1146. } else {
  1147. vpb_translate_event(&e, str);
  1148. if (*str && *(str + 1)) {
  1149. str[strlen(str) - 1] = '\0';
  1150. }
  1151. ast_mutex_lock(&iflock);
  1152. for (p = iflist; p && p->handle != e.handle; p = p->next);
  1153. ast_mutex_unlock(&iflock);
  1154. if (p) {
  1155. ast_verb(4, "%s: Event [%d=>%s]\n",
  1156. p ? p->dev : "null", e.type, str);
  1157. }
  1158. }
  1159. ast_mutex_unlock(&monlock);
  1160. if (!p) {
  1161. if (e.type != VPB_NULL_EVENT) {
  1162. ast_log(LOG_WARNING, "Got event [%s][%d], no matching iface!\n", str, e.type);
  1163. ast_verb(4, "vpb/ERR: No interface for Event [%d=>%s] \n", e.type, str);
  1164. }
  1165. continue;
  1166. }
  1167. /* flush the event from the channel event Q */
  1168. vpb_get_event_ch_async(e.handle, &je);
  1169. vpb_translate_event(&je, str);
  1170. ast_verb(5, "%s: Flushing event [%d]=>%s\n", p->dev, je.type, str);
  1171. /* Check for ownership and locks */
  1172. if ((p->owner) && (!p->golock)) {
  1173. /* Need to get owner lock */
  1174. /* Safely grab both p->lock and p->owner->lock so that there
  1175. cannot be a race with something from the other side */
  1176. /*
  1177. ast_mutex_lock(&p->lock);
  1178. while (ast_mutex_trylock(&p->owner->lock)) {
  1179. ast_mutex_unlock(&p->lock);
  1180. usleep(1);
  1181. ast_mutex_lock(&p->lock);
  1182. if (!p->owner)
  1183. break;
  1184. }
  1185. if (p->owner)
  1186. p->golock = 1;
  1187. */
  1188. }
  1189. /* Two scenarios: Are you owned or not. */
  1190. if (p->owner) {
  1191. monitor_handle_owned(p, &e);
  1192. } else {
  1193. monitor_handle_notowned(p, &e);
  1194. }
  1195. /* if ((!p->owner)&&(p->golock)) {
  1196. ast_mutex_unlock(&p->owner->lock);
  1197. ast_mutex_unlock(&p->lock);
  1198. }
  1199. */
  1200. }
  1201. return NULL;
  1202. }
  1203. static int restart_monitor(void)
  1204. {
  1205. int error = 0;
  1206. /* If we're supposed to be stopped -- stay stopped */
  1207. if (mthreadactive == -2)
  1208. return 0;
  1209. ast_verb(4, "Restarting monitor\n");
  1210. ast_mutex_lock(&monlock);
  1211. if (monitor_thread == pthread_self()) {
  1212. ast_log(LOG_WARNING, "Cannot kill myself\n");
  1213. error = -1;
  1214. ast_verb(4, "Monitor trying to kill monitor\n");
  1215. } else {
  1216. if (mthreadactive != -1) {
  1217. /* Why do other drivers kill the thread? No need says I, simply awake thread with event. */
  1218. VPB_EVENT e;
  1219. e.handle = 0;
  1220. e.type = VPB_EVT_NONE;
  1221. e.data = 0;
  1222. ast_verb(4, "Trying to reawake monitor\n");
  1223. vpb_put_event(&e);
  1224. } else {
  1225. /* Start a new monitor */
  1226. int pid = ast_pthread_create(&monitor_thread, NULL, do_monitor, NULL);
  1227. ast_verb(4, "Created new monitor thread %d\n", pid);
  1228. if (pid < 0) {
  1229. ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
  1230. error = -1;
  1231. } else {
  1232. mthreadactive = 0; /* Started the thread!*/
  1233. }
  1234. }
  1235. }
  1236. ast_mutex_unlock(&monlock);
  1237. ast_verb(4, "Monitor restarted\n");
  1238. return error;
  1239. }
  1240. /* Per board config that must be called after vpb_open() */
  1241. static void mkbrd(vpb_model_t model, int echo_cancel)
  1242. {
  1243. if (!bridges) {
  1244. if (model == vpb_model_v4pci) {
  1245. max_bridges = MAX_BRIDGES_V4PCI;
  1246. }
  1247. bridges = (vpb_bridge_t *)ast_calloc(1, max_bridges * sizeof(vpb_bridge_t));
  1248. if (!bridges) {
  1249. ast_log(LOG_ERROR, "Failed to initialize bridges\n");
  1250. } else {
  1251. int i;
  1252. for (i = 0; i < max_bridges; i++) {
  1253. ast_mutex_init(&bridges[i].lock);
  1254. ast_cond_init(&bridges[i].cond, NULL);
  1255. }
  1256. }
  1257. }
  1258. if (!echo_cancel) {
  1259. if (model == vpb_model_v4pci) {
  1260. vpb_echo_canc_disable();
  1261. ast_log(LOG_NOTICE, "Voicetronix echo cancellation OFF\n");
  1262. } else {
  1263. /* need to do it port by port for OpenSwitch */
  1264. }
  1265. } else {
  1266. if (model == vpb_model_v4pci) {
  1267. vpb_echo_canc_enable();
  1268. ast_log(LOG_NOTICE, "Voicetronix echo cancellation ON\n");
  1269. if (ec_supp_threshold > -1) {
  1270. vpb_echo_canc_set_sup_thresh(0, &ec_supp_threshold);
  1271. ast_log(LOG_NOTICE, "Voicetronix EC Sup Thres set\n");
  1272. }
  1273. } else {
  1274. /* need to do it port by port for OpenSwitch */
  1275. }
  1276. }
  1277. }
  1278. static struct vpb_pvt *mkif(int board, int channel, int mode, int gains, float txgain, float rxgain,
  1279. float txswgain, float rxswgain, int bal1, int bal2, int bal3,
  1280. char * callerid, int echo_cancel, int group, ast_group_t callgroup, ast_group_t pickupgroup )
  1281. {
  1282. struct vpb_pvt *tmp;
  1283. char buf[64];
  1284. tmp = (vpb_pvt *)ast_calloc(1, sizeof(*tmp));
  1285. if (!tmp)
  1286. return NULL;
  1287. tmp->handle = vpb_open(board, channel);
  1288. if (tmp->handle < 0) {
  1289. ast_log(LOG_WARNING, "Unable to create channel vpb/%d-%d: %s\n",
  1290. board, channel, strerror(errno));
  1291. ast_free(tmp);
  1292. return NULL;
  1293. }
  1294. snprintf(tmp->dev, sizeof(tmp->dev), "vpb/%d-%d", board, channel);
  1295. tmp->mode = mode;
  1296. tmp->group = group;
  1297. tmp->callgroup = callgroup;
  1298. tmp->pickupgroup = pickupgroup;
  1299. /* Initialize dtmf caller ID position variable */
  1300. tmp->dtmf_caller_pos = 0;
  1301. ast_copy_string(tmp->language, language, sizeof(tmp->language));
  1302. ast_copy_string(tmp->context, context, sizeof(tmp->context));
  1303. tmp->callerid_type = 0;
  1304. if (callerid) {
  1305. if (strcasecmp(callerid, "on") == 0) {
  1306. tmp->callerid_type = 1;
  1307. ast_copy_string(tmp->callerid, "unknown", sizeof(tmp->callerid));
  1308. } else if (strcasecmp(callerid, "v23") == 0) {
  1309. tmp->callerid_type = 2;
  1310. ast_copy_string(tmp->callerid, "unknown", sizeof(tmp->callerid));
  1311. } else if (strcasecmp(callerid, "bell") == 0) {
  1312. tmp->callerid_type = 3;
  1313. ast_copy_string(tmp->callerid, "unknown", sizeof(tmp->callerid));
  1314. } else {
  1315. ast_copy_string(tmp->callerid, callerid, sizeof(tmp->callerid));
  1316. }
  1317. } else {
  1318. ast_copy_string(tmp->callerid, "unknown", sizeof(tmp->callerid));
  1319. }
  1320. /* check if codec balances have been set in the config file */
  1321. if (bal3 >= 0) {
  1322. if ((bal1>=0) && !(bal1 & 32)) bal1 |= 32;
  1323. vpb_set_codec_reg(tmp->handle, 0x42, bal3);
  1324. }
  1325. if (bal1 >= 0) {
  1326. vpb_set_codec_reg(tmp->handle, 0x32, bal1);
  1327. }
  1328. if (bal2 >= 0) {
  1329. vpb_set_codec_reg(tmp->handle, 0x3a, bal2);
  1330. }
  1331. if (gains & VPB_GOT_TXHWG) {
  1332. if (txgain > MAX_VPB_GAIN) {
  1333. tmp->txgain = MAX_VPB_GAIN;
  1334. } else if (txgain < MIN_VPB_GAIN) {
  1335. tmp->txgain = MIN_VPB_GAIN;
  1336. } else {
  1337. tmp->txgain = txgain;
  1338. }
  1339. ast_log(LOG_NOTICE, "VPB setting Tx Hw gain to [%f]\n", tmp->txgain);
  1340. vpb_play_set_hw_gain(tmp->handle, tmp->txgain);
  1341. }
  1342. if (gains & VPB_GOT_RXHWG) {
  1343. if (rxgain > MAX_VPB_GAIN) {
  1344. tmp->rxgain = MAX_VPB_GAIN;
  1345. } else if (rxgain < MIN_VPB_GAIN) {
  1346. tmp->rxgain = MIN_VPB_GAIN;
  1347. } else {
  1348. tmp->rxgain = rxgain;
  1349. }
  1350. ast_log(LOG_NOTICE, "VPB setting Rx Hw gain to [%f]\n", tmp->rxgain);
  1351. vpb_record_set_hw_gain(tmp->handle, tmp->rxgain);
  1352. }
  1353. if (gains & VPB_GOT_TXSWG) {
  1354. tmp->txswgain = txswgain;
  1355. ast_log(LOG_NOTICE, "VPB setting Tx Sw gain to [%f]\n", tmp->txswgain);
  1356. vpb_play_set_gain(tmp->handle, tmp->txswgain);
  1357. }
  1358. if (gains & VPB_GOT_RXSWG) {
  1359. tmp->rxswgain = rxswgain;
  1360. ast_log(LOG_NOTICE, "VPB setting Rx Sw gain to [%f]\n", tmp->rxswgain);
  1361. vpb_record_set_gain(tmp->handle, tmp->rxswgain);
  1362. }
  1363. tmp->vpb_model = vpb_model_unknown;
  1364. if (vpb_get_model(tmp->handle, buf) == VPB_OK) {
  1365. if (strcmp(buf, "V12PCI") == 0) {
  1366. tmp->vpb_model = vpb_model_v12pci;
  1367. } else if (strcmp(buf, "VPB4") == 0) {
  1368. tmp->vpb_model = vpb_model_v4pci;
  1369. }
  1370. }
  1371. ast_mutex_init(&tmp->owner_lock);
  1372. ast_mutex_init(&tmp->lock);
  1373. ast_mutex_init(&tmp->record_lock);
  1374. ast_mutex_init(&tmp->play_lock);
  1375. ast_mutex_init(&tmp->play_dtmf_lock);
  1376. /* set default read state */
  1377. tmp->read_state = 0;
  1378. tmp->golock = 0;
  1379. tmp->busy_timer_id = vpb_timer_get_unique_timer_id();
  1380. vpb_timer_open(&tmp->busy_timer, tmp->handle, tmp->busy_timer_id, TIMER_PERIOD_BUSY);
  1381. tmp->ringback_timer_id = vpb_timer_get_unique_timer_id();
  1382. vpb_timer_open(&tmp->ringback_timer, tmp->handle, tmp->ringback_timer_id, TIMER_PERIOD_RINGBACK);
  1383. tmp->ring_timer_id = vpb_timer_get_unique_timer_id();
  1384. vpb_timer_open(&tmp->ring_timer, tmp->handle, tmp->ring_timer_id, timer_period_ring);
  1385. tmp->dtmfidd_timer_id = vpb_timer_get_unique_timer_id();
  1386. vpb_timer_open(&tmp->dtmfidd_timer, tmp->handle, tmp->dtmfidd_timer_id, dtmf_idd);
  1387. if (mode == MODE_FXO){
  1388. if (use_ast_dtmfdet)
  1389. vpb_set_event_mask(tmp->handle, VPB_EVENTS_NODTMF);
  1390. else
  1391. vpb_set_event_mask(tmp->handle, VPB_EVENTS_ALL);
  1392. } else {
  1393. /*
  1394. if (use_ast_dtmfdet)
  1395. vpb_set_event_mask(tmp->handle, VPB_EVENTS_NODTMF);
  1396. else
  1397. */
  1398. vpb_set_event_mask(tmp->handle, VPB_EVENTS_STAT);
  1399. }
  1400. if ((tmp->vpb_model == vpb_model_v12pci) && (echo_cancel)) {
  1401. vpb_hostecho_on(tmp->handle);
  1402. }
  1403. if (use_ast_dtmfdet) {
  1404. tmp->vad = ast_dsp_new();
  1405. ast_dsp_set_features(tmp->vad, DSP_FEATURE_DIGIT_DETECT);
  1406. ast_dsp_set_digitmode(tmp->vad, DSP_DIGITMODE_DTMF);
  1407. if (relaxdtmf)
  1408. ast_dsp_set_digitmode(tmp->vad, DSP_DIGITMODE_DTMF|DSP_DIGITMODE_RELAXDTMF);
  1409. } else {
  1410. tmp->vad = NULL;
  1411. }
  1412. /* define grunt tone */
  1413. vpb_settonedet(tmp->handle,&toned_ungrunt);
  1414. ast_log(LOG_NOTICE,"Voicetronix %s channel %s initialized (rxsg=%f/txsg=%f/rxhg=%f/txhg=%f)(0x%x/0x%x/0x%x)\n",
  1415. (tmp->vpb_model == vpb_model_v4pci) ? "V4PCI" :
  1416. (tmp->vpb_model == vpb_model_v12pci) ? "V12PCI" : "[Unknown model]",
  1417. tmp->dev, tmp->rxswgain, tmp->txswgain, tmp->rxgain, tmp->txgain, bal1, bal2, bal3);
  1418. return tmp;
  1419. }
  1420. static int vpb_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen)
  1421. {
  1422. struct vpb_pvt *p = (struct vpb_pvt *)ast_channel_tech_pvt(ast);
  1423. int res = 0;
  1424. if (use_ast_ind == 1) {
  1425. ast_verb(4, "%s: vpb_indicate called when using Ast Indications !?!\n", p->dev);
  1426. return 0;
  1427. }
  1428. ast_verb(4, "%s: vpb_indicate [%d] state[%d]\n", p->dev, condition,ast_channel_state(ast));
  1429. /*
  1430. if (ast->_state != AST_STATE_UP) {
  1431. ast_verb(4, "%s: vpb_indicate Not in AST_STATE_UP\n", p->dev, condition,ast->_state);
  1432. return res;
  1433. }
  1434. */
  1435. /*
  1436. ast_verb(4, "%s: LOCKING in indicate \n", p->dev);
  1437. ast_verb(4, "%s: LOCKING count[%d] owner[%d] \n", p->dev, p->lock.__m_count, p->lock.__m_owner);
  1438. */
  1439. ast_mutex_lock(&p->lock);
  1440. switch (condition) {
  1441. case AST_CONTROL_BUSY:
  1442. case AST_CONTROL_CONGESTION:
  1443. if (ast_channel_state(ast) == AST_STATE_UP) {
  1444. playtone(p->handle, &Busytone);
  1445. p->state = VPB_STATE_PLAYBUSY;
  1446. vpb_timer_stop(p->busy_timer);
  1447. vpb_timer_start(p->busy_timer);
  1448. }
  1449. break;
  1450. case AST_CONTROL_RINGING:
  1451. if (ast_channel_state(ast) == AST_STATE_UP) {
  1452. playtone(p->handle, &Ringbacktone);
  1453. p->state = VPB_STATE_PLAYRING;
  1454. ast_verb(4, "%s: vpb indicate: setting ringback timer [%d]\n", p->dev,p->ringback_timer_id);
  1455. vpb_timer_stop(p->ringback_timer);
  1456. vpb_timer_start(p->ringback_timer);
  1457. }
  1458. break;
  1459. case AST_CONTROL_ANSWER:
  1460. case -1: /* -1 means stop playing? */
  1461. vpb_timer_stop(p->ringback_timer);
  1462. vpb_timer_stop(p->busy_timer);
  1463. stoptone(p->handle);
  1464. break;
  1465. case AST_CONTROL_HANGUP:
  1466. if (ast_channel_state(ast) == AST_STATE_UP) {
  1467. playtone(p->handle, &Busytone);
  1468. p->state = VPB_STATE_PLAYBUSY;
  1469. vpb_timer_stop(p->busy_timer);
  1470. vpb_timer_start(p->busy_timer);
  1471. }
  1472. break;
  1473. case AST_CONTROL_HOLD:
  1474. ast_moh_start(ast, (const char *) data, NULL);
  1475. break;
  1476. case AST_CONTROL_UNHOLD:
  1477. ast_moh_stop(ast);
  1478. break;
  1479. case AST_CONTROL_PVT_CAUSE_CODE:
  1480. res = -1;
  1481. break;
  1482. default:
  1483. res = 0;
  1484. break;
  1485. }
  1486. ast_mutex_unlock(&p->lock);
  1487. return res;
  1488. }
  1489. static int vpb_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
  1490. {
  1491. struct vpb_pvt *p = (struct vpb_pvt *)ast_channel_tech_pvt(newchan);
  1492. /*
  1493. ast_verb(4, "%s: LOCKING in fixup \n", p->dev);
  1494. ast_verb(4, "%s: LOCKING count[%d] owner[%d] \n", p->dev, p->lock.__m_count,p->lock.__m_owner);
  1495. */
  1496. ast_mutex_lock(&p->lock);
  1497. ast_debug(1, "New owner for channel %s is %s\n", p->dev, ast_channel_name(newchan));
  1498. if (p->owner == oldchan) {
  1499. p->owner = newchan;
  1500. }
  1501. if (ast_channel_state(newchan) == AST_STATE_RINGING){
  1502. if (use_ast_ind == 1) {
  1503. ast_verb(4, "%s: vpb_fixup Calling ast_indicate\n", p->dev);
  1504. ast_indicate(newchan, AST_CONTROL_RINGING);
  1505. } else {
  1506. ast_verb(4, "%s: vpb_fixup Calling vpb_indicate\n", p->dev);
  1507. vpb_indicate(newchan, AST_CONTROL_RINGING, NULL, 0);
  1508. }
  1509. }
  1510. ast_mutex_unlock(&p->lock);
  1511. return 0;
  1512. }
  1513. static int vpb_digit_begin(struct ast_channel *ast, char digit)
  1514. {
  1515. /* XXX Modify this callback to let Asterisk control the length of DTMF */
  1516. return 0;
  1517. }
  1518. static int vpb_digit_end(struct ast_channel *ast, char digit, unsigned int duration)
  1519. {
  1520. struct vpb_pvt *p = (struct vpb_pvt *)ast_channel_tech_pvt(ast);
  1521. char s[2];
  1522. if (use_ast_dtmf) {
  1523. ast_verb(4, "%s: vpb_digit: asked to play digit[%c] but we are using asterisk dtmf play back?!\n", p->dev, digit);
  1524. return 0;
  1525. }
  1526. /*
  1527. ast_verb(4, "%s: LOCKING in digit \n", p->dev);
  1528. ast_verb(4, "%s: LOCKING count[%d] owner[%d] \n", p->dev, p->lock.__m_count,p->lock.__m_owner);
  1529. */
  1530. ast_mutex_lock(&p->lock);
  1531. s[0] = digit;
  1532. s[1] = '\0';
  1533. ast_verb(4, "%s: vpb_digit: asked to play digit[%s]\n", p->dev, s);
  1534. ast_mutex_lock(&p->play_dtmf_lock);
  1535. strncat(p->play_dtmf, s, sizeof(p->play_dtmf) - strlen(p->play_dtmf) - 1);
  1536. ast_mutex_unlock(&p->play_dtmf_lock);
  1537. ast_mutex_unlock(&p->lock);
  1538. return 0;
  1539. }
  1540. /* Places a call out of a VPB channel */
  1541. static int vpb_call(struct ast_channel *ast, const char *dest, int timeout)
  1542. {
  1543. struct vpb_pvt *p = (struct vpb_pvt *)ast_channel_tech_pvt(ast);
  1544. int res = 0, i;
  1545. const char *s = strrchr(dest, '/');
  1546. char dialstring[254] = "";
  1547. /*
  1548. ast_verb(4, "%s: LOCKING in call \n", p->dev);
  1549. ast_verb(4, "%s: LOCKING count[%d] owner[%d] \n", p->dev, p->lock.__m_count,p->lock.__m_owner);
  1550. */
  1551. ast_mutex_lock(&p->lock);
  1552. ast_verb(4, "%s: starting call to [%s]\n", p->dev, dest);
  1553. if (s)
  1554. s = s + 1;
  1555. else
  1556. s = dest;
  1557. ast_copy_string(dialstring, s, sizeof(dialstring));
  1558. for (i = 0; dialstring[i] != '\0'; i++) {
  1559. if ((dialstring[i] == 'w') || (dialstring[i] == 'W'))
  1560. dialstring[i] = ',';
  1561. else if ((dialstring[i] == 'f') || (dialstring[i] == 'F'))
  1562. dialstring[i] = '&';
  1563. }
  1564. if (ast_channel_state(ast) != AST_STATE_DOWN && ast_channel_state(ast) != AST_STATE_RESERVED) {
  1565. ast_log(LOG_WARNING, "vpb_call on %s neither down nor reserved!\n", ast_channel_name(ast));
  1566. ast_mutex_unlock(&p->lock);
  1567. return -1;
  1568. }
  1569. if (p->mode != MODE_FXO) /* Station port, ring it. */
  1570. vpb_ring_station_async(p->handle, 2);
  1571. else {
  1572. VPB_CALL call;
  1573. int j;
  1574. /* Dial must timeout or it can leave channels unuseable */
  1575. if (timeout == 0) {
  1576. timeout = TIMER_PERIOD_NOANSWER;
  1577. } else {
  1578. timeout = timeout * 1000; /* convert from secs to ms. */
  1579. }
  1580. /* These timeouts are only used with call progress dialing */
  1581. call.dialtones = 1; /* Number of dialtones to get outside line */
  1582. call.dialtone_timeout = VPB_DIALTONE_WAIT; /* Wait this long for dialtone (ms) */
  1583. call.ringback_timeout = VPB_RINGWAIT; /* Wait this long for ringing after dialing (ms) */
  1584. call.inter_ringback_timeout = VPB_CONNECTED_WAIT; /* If ringing stops for this long consider it connected (ms) */
  1585. call.answer_timeout = timeout; /* Time to wait for answer after ringing starts (ms) */
  1586. memcpy(&call.tone_map, DialToneMap, sizeof(DialToneMap));
  1587. vpb_set_call(p->handle, &call);
  1588. ast_verb(2, "%s: Calling %s on %s \n",p->dev, dialstring, ast_channel_name(ast));
  1589. ast_verb(2, "%s: Dial parms for %s %d/%dms/%dms/%dms/%dms\n", p->dev,
  1590. ast_channel_name(ast), call.dialtones, call.dialtone_timeout,
  1591. call.ringback_timeout, call.inter_ringback_timeout,
  1592. call.answer_timeout);
  1593. for (j = 0; !call.tone_map[j].terminate; j++) {
  1594. ast_verb(2, "%s: Dial parms for %s tone %d->%d\n", p->dev,
  1595. ast_channel_name(ast), call.tone_map[j].tone_id, call.tone_map[j].call_id);
  1596. }
  1597. ast_verb(4, "%s: Disabling Loop Drop detection\n", p->dev);
  1598. vpb_disable_event(p->handle, VPB_MDROP);
  1599. vpb_sethook_sync(p->handle, VPB_OFFHOOK);
  1600. p->state = VPB_STATE_OFFHOOK;
  1601. #ifndef DIAL_WITH_CALL_PROGRESS
  1602. vpb_sleep(300);
  1603. ast_verb(4, "%s: Enabling Loop Drop detection\n", p->dev);
  1604. vpb_enable_event(p->handle, VPB_MDROP);
  1605. res = vpb_dial_async(p->handle, dialstring);
  1606. #else
  1607. ast_verb(4, "%s: Enabling Loop Drop detection\n", p->dev);
  1608. vpb_enable_event(p->handle, VPB_MDROP);
  1609. res = vpb_call_async(p->handle, dialstring);
  1610. #endif
  1611. if (res != VPB_OK) {
  1612. ast_debug(1, "Call on %s to %s failed: %d\n", ast_channel_name(ast), s, res);
  1613. res = -1;
  1614. } else
  1615. res = 0;
  1616. }
  1617. ast_verb(3, "%s: VPB Calling %s [t=%d] on %s returned %d\n", p->dev , s, timeout, ast_channel_name(ast), res);
  1618. if (res == 0) {
  1619. ast_setstate(ast, AST_STATE_RINGING);
  1620. ast_queue_control(ast, AST_CONTROL_RINGING);
  1621. }
  1622. if (!p->readthread) {
  1623. ast_pthread_create(&p->readthread, NULL, do_chanreads, (void *)p);
  1624. }
  1625. ast_mutex_unlock(&p->lock);
  1626. return res;
  1627. }
  1628. static int vpb_hangup(struct ast_channel *ast)
  1629. {
  1630. struct vpb_pvt *p = (struct vpb_pvt *)ast_channel_tech_pvt(ast);
  1631. VPB_EVENT je;
  1632. char str[VPB_MAX_STR];
  1633. /*
  1634. ast_verb(4, "%s: LOCKING in hangup \n", p->dev);
  1635. ast_verb(4, "%s: LOCKING in hangup count[%d] owner[%d] \n", p->dev, p->lock.__m_count,p->lock.__m_owner);
  1636. ast_verb(4, "%s: LOCKING pthread_self(%d)\n", p->dev,pthread_self());
  1637. ast_mutex_lock(&p->lock);
  1638. */
  1639. ast_verb(2, "%s: Hangup requested\n", ast_channel_name(ast));
  1640. if (!ast_channel_tech(ast) || !ast_channel_tech_pvt(ast)) {
  1641. ast_log(LOG_WARNING, "%s: channel not connected?\n", ast_channel_name(ast));
  1642. ast_mutex_unlock(&p->lock);
  1643. /* Free up ast dsp if we have one */
  1644. if (use_ast_dtmfdet && p->vad) {
  1645. ast_dsp_free(p->vad);
  1646. p->vad = NULL;
  1647. }
  1648. return 0;
  1649. }
  1650. /* Stop record */
  1651. p->stopreads = 1;
  1652. if (p->readthread) {
  1653. pthread_join(p->readthread, NULL);
  1654. ast_verb(4, "%s: stopped record thread \n", ast_channel_name(ast));
  1655. }
  1656. /* Stop play */
  1657. if (p->lastoutput != -1) {
  1658. ast_verb(2, "%s: Ending play mode \n", ast_channel_name(ast));
  1659. vpb_play_terminate(p->handle);
  1660. ast_mutex_lock(&p->play_lock);
  1661. vpb_play_buf_finish(p->handle);
  1662. ast_mutex_unlock(&p->play_lock);
  1663. }
  1664. ast_verb(4, "%s: Setting state down\n", ast_channel_name(ast));
  1665. ast_setstate(ast, AST_STATE_DOWN);
  1666. /*
  1667. ast_verb(4, "%s: LOCKING in hangup \n", p->dev);
  1668. ast_verb(4, "%s: LOCKING in hangup count[%d] owner[%d] \n", p->dev, p->lock.__m_count,p->lock.__m_owner);
  1669. ast_verb(4, "%s: LOCKING pthread_self(%d)\n", p->dev,pthread_self());
  1670. */
  1671. ast_mutex_lock(&p->lock);
  1672. if (p->mode != MODE_FXO) {
  1673. /* station port. */
  1674. vpb_ring_station_async(p->handle, 0);
  1675. if (p->state != VPB_STATE_ONHOOK) {
  1676. /* This is causing a "dial end" "play tone" loop
  1677. playtone(p->handle, &Busytone);
  1678. p->state = VPB_STATE_PLAYBUSY;
  1679. ast_verb(5, "%s: Station offhook[%d], playing busy tone\n",
  1680. ast->name,p->state);
  1681. */
  1682. } else {
  1683. stoptone(p->handle);
  1684. }
  1685. #ifdef VPB_PRI
  1686. vpb_setloop_async(p->handle, VPB_OFFHOOK);
  1687. vpb_sleep(100);
  1688. vpb_setloop_async(p->handle, VPB_ONHOOK);
  1689. #endif
  1690. } else {
  1691. stoptone(p->handle); /* Terminates any dialing */
  1692. vpb_sethook_sync(p->handle, VPB_ONHOOK);
  1693. p->state=VPB_STATE_ONHOOK;
  1694. }
  1695. while (VPB_OK == vpb_get_event_ch_async(p->handle, &je)) {
  1696. vpb_translate_event(&je, str);
  1697. ast_verb(4, "%s: Flushing event [%d]=>%s\n", ast_channel_name(ast), je.type, str);
  1698. }
  1699. p->readthread = 0;
  1700. p->lastoutput = -1;
  1701. p->lastinput = -1;
  1702. p->last_ignore_dtmf = 1;
  1703. p->ext[0] = 0;
  1704. p->dialtone = 0;
  1705. p->owner = NULL;
  1706. ast_channel_tech_pvt_set(ast, NULL);
  1707. /* Free up ast dsp if we have one */
  1708. if (use_ast_dtmfdet && p->vad) {
  1709. ast_dsp_free(p->vad);
  1710. p->vad = NULL;
  1711. }
  1712. ast_verb(2, "%s: Hangup complete\n", ast_channel_name(ast));
  1713. restart_monitor();
  1714. ast_mutex_unlock(&p->lock);
  1715. return 0;
  1716. }
  1717. static int vpb_answer(struct ast_channel *ast)
  1718. {
  1719. struct vpb_pvt *p = (struct vpb_pvt *)ast_channel_tech_pvt(ast);
  1720. /*
  1721. VPB_EVENT je;
  1722. int ret;
  1723. ast_verb(4, "%s: LOCKING in answer \n", p->dev);
  1724. ast_verb(4, "%s: LOCKING count[%d] owner[%d] \n", p->dev, p->lock.__m_count,p->lock.__m_owner);
  1725. */
  1726. ast_mutex_lock(&p->lock);
  1727. ast_verb(4, "%s: Answering channel\n", p->dev);
  1728. if (p->mode == MODE_FXO) {
  1729. ast_verb(4, "%s: Disabling Loop Drop detection\n", p->dev);
  1730. vpb_disable_event(p->handle, VPB_MDROP);
  1731. }
  1732. if (ast_channel_state(ast) != AST_STATE_UP) {
  1733. if (p->mode == MODE_FXO) {
  1734. vpb_sethook_sync(p->handle, VPB_OFFHOOK);
  1735. p->state = VPB_STATE_OFFHOOK;
  1736. /* vpb_sleep(500);
  1737. ret = vpb_get_event_ch_async(p->handle, &je);
  1738. if ((ret == VPB_OK) && ((je.type != VPB_DROP)&&(je.type != VPB_RING))){
  1739. ast_verb(4, "%s: Answer collected a wrong event!!\n", p->dev);
  1740. vpb_put_event(&je);
  1741. }
  1742. */
  1743. }
  1744. ast_setstate(ast, AST_STATE_UP);
  1745. ast_verb(2, "%s: Answered call on %s [%s]\n", p->dev,
  1746. ast_channel_name(ast), (p->mode == MODE_FXO) ? "FXO" : "FXS");
  1747. ast_channel_rings_set(ast, 0);
  1748. if (!p->readthread) {
  1749. /* res = ast_mutex_unlock(&p->lock); */
  1750. /* ast_verbose("%s: unLOCKING in answer [%d]\n", p->dev,res); */
  1751. ast_pthread_create(&p->readthread, NULL, do_chanreads, (void *)p);
  1752. } else {
  1753. ast_verb(4, "%s: Record thread already running!!\n", p->dev);
  1754. }
  1755. } else {
  1756. ast_verb(4, "%s: Answered state is up\n", p->dev);
  1757. /* res = ast_mutex_unlock(&p->lock); */
  1758. /* ast_verbose("%s: unLOCKING in answer [%d]\n", p->dev,res); */
  1759. }
  1760. vpb_sleep(500);
  1761. if (p->mode == MODE_FXO) {
  1762. ast_verb(4, "%s: Re-enabling Loop Drop detection\n", p->dev);
  1763. vpb_enable_event(p->handle, VPB_MDROP);
  1764. }
  1765. ast_mutex_unlock(&p->lock);
  1766. return 0;
  1767. }
  1768. static struct ast_frame *vpb_read(struct ast_channel *ast)
  1769. {
  1770. struct vpb_pvt *p = (struct vpb_pvt *)ast_channel_tech_pvt(ast);
  1771. static struct ast_frame f = { AST_FRAME_NULL };
  1772. f.src = "vpb";
  1773. ast_log(LOG_NOTICE, "%s: vpb_read: should never be called!\n", p->dev);
  1774. ast_verbose("%s: vpb_read: should never be called!\n", p->dev);
  1775. return &f;
  1776. }
  1777. static inline AudioCompress ast2vpbformat(struct ast_format *format)
  1778. {
  1779. if (ast_format_cmp(format, ast_format_alaw) == AST_FORMAT_CMP_EQUAL) {
  1780. return VPB_ALAW;
  1781. } else if (ast_format_cmp(format, ast_format_slin) == AST_FORMAT_CMP_EQUAL) {
  1782. return VPB_LINEAR;
  1783. } else if (ast_format_cmp(format, ast_format_ulaw) == AST_FORMAT_CMP_EQUAL) {
  1784. return VPB_MULAW;
  1785. } else if (ast_format_cmp(format, ast_format_adpcm) == AST_FORMAT_CMP_EQUAL) {
  1786. return VPB_OKIADPCM;
  1787. } else {
  1788. return VPB_RAW;
  1789. }
  1790. }
  1791. static inline const char * ast2vpbformatname(struct ast_format *format)
  1792. {
  1793. if (ast_format_cmp(format, ast_format_alaw) == AST_FORMAT_CMP_EQUAL) {
  1794. return "AST_FORMAT_ALAW:VPB_ALAW";
  1795. } else if (ast_format_cmp(format, ast_format_slin) == AST_FORMAT_CMP_EQUAL) {
  1796. return "AST_FORMAT_SLINEAR:VPB_LINEAR";
  1797. } else if (ast_format_cmp(format, ast_format_ulaw) == AST_FORMAT_CMP_EQUAL) {
  1798. return "AST_FORMAT_ULAW:VPB_MULAW";
  1799. } else if (ast_format_cmp(format, ast_format_adpcm) == AST_FORMAT_CMP_EQUAL) {
  1800. return "AST_FORMAT_ADPCM:VPB_OKIADPCM";
  1801. } else {
  1802. return "UNKN:UNKN";
  1803. }
  1804. }
  1805. static inline int astformatbits(struct ast_format *format)
  1806. {
  1807. if (ast_format_cmp(format, ast_format_slin) == AST_FORMAT_CMP_EQUAL) {
  1808. return 16;
  1809. } else if (ast_format_cmp(format, ast_format_adpcm) == AST_FORMAT_CMP_EQUAL) {
  1810. return 4;
  1811. } else {
  1812. return 8;
  1813. }
  1814. }
  1815. int a_gain_vector(float g, short *v, int n)
  1816. {
  1817. int i;
  1818. float tmp;
  1819. for (i = 0; i < n; i++) {
  1820. tmp = g * v[i];
  1821. if (tmp > 32767.0)
  1822. tmp = 32767.0;
  1823. if (tmp < -32768.0)
  1824. tmp = -32768.0;
  1825. v[i] = (short)tmp;
  1826. }
  1827. return i;
  1828. }
  1829. /* Writes a frame of voice data to a VPB channel */
  1830. static int vpb_write(struct ast_channel *ast, struct ast_frame *frame)
  1831. {
  1832. struct vpb_pvt *p = (struct vpb_pvt *)ast_channel_tech_pvt(ast);
  1833. int res = 0;
  1834. AudioCompress fmt = VPB_RAW;
  1835. struct timeval play_buf_time_start;
  1836. int tdiff;
  1837. /* ast_mutex_lock(&p->lock); */
  1838. ast_verb(6, "%s: vpb_write: Writing to channel\n", p->dev);
  1839. if (frame->frametype != AST_FRAME_VOICE) {
  1840. ast_verb(4, "%s: vpb_write: Don't know how to handle from type %d\n", ast_channel_name(ast), frame->frametype);
  1841. /* ast_mutex_unlock(&p->lock); */
  1842. return 0;
  1843. } else if (ast_channel_state(ast) != AST_STATE_UP) {
  1844. ast_verb(4, "%s: vpb_write: Attempt to Write frame type[%d]subclass[%s] on not up chan(state[%d])\n",
  1845. ast_channel_name(ast), frame->frametype, ast_format_get_name(frame->subclass.format), ast_channel_state(ast));
  1846. p->lastoutput = -1;
  1847. /* ast_mutex_unlock(&p->lock); */
  1848. return 0;
  1849. }
  1850. /* ast_debug(1, "%s: vpb_write: Checked frame type..\n", p->dev); */
  1851. fmt = ast2vpbformat(frame->subclass.format);
  1852. if (fmt < 0) {
  1853. ast_log(LOG_WARNING, "%s: vpb_write: Cannot handle frames of %s format!\n", ast_channel_name(ast),
  1854. ast_format_get_name(frame->subclass.format));
  1855. return -1;
  1856. }
  1857. tdiff = ast_tvdiff_ms(ast_tvnow(), p->lastplay);
  1858. ast_debug(1, "%s: vpb_write: time since last play(%d) \n", p->dev, tdiff);
  1859. if (tdiff < (VPB_SAMPLES / 8 - 1)) {
  1860. ast_debug(1, "%s: vpb_write: Asked to play too often (%d) (%d)\n", p->dev, tdiff, frame->datalen);
  1861. /* return 0; */
  1862. }
  1863. p->lastplay = ast_tvnow();
  1864. /*
  1865. ast_debug(1, "%s: vpb_write: Checked frame format..\n", p->dev);
  1866. */
  1867. ast_mutex_lock(&p->play_lock);
  1868. /*
  1869. ast_debug(1, "%s: vpb_write: Got play lock..\n", p->dev);
  1870. */
  1871. /* Check if we have set up the play_buf */
  1872. if (p->lastoutput == -1) {
  1873. vpb_play_buf_start(p->handle, fmt);
  1874. ast_verb(2, "%s: vpb_write: Starting play mode (codec=%d)[%s]\n", p->dev, fmt, ast2vpbformatname(frame->subclass.format));
  1875. p->lastoutput = fmt;
  1876. ast_mutex_unlock(&p->play_lock);
  1877. return 0;
  1878. } else if (p->lastoutput != fmt) {
  1879. vpb_play_buf_finish(p->handle);
  1880. vpb_play_buf_start(p->handle, fmt);
  1881. ast_verb(2, "%s: vpb_write: Changed play format (%d=>%d)\n", p->dev, p->lastoutput, fmt);
  1882. ast_mutex_unlock(&p->play_lock);
  1883. return 0;
  1884. }
  1885. p->lastoutput = fmt;
  1886. /* Apply extra gain ! */
  1887. if( p->txswgain > MAX_VPB_GAIN )
  1888. a_gain_vector(p->txswgain - MAX_VPB_GAIN , (short*)frame->data.ptr, frame->datalen / sizeof(short));
  1889. /* ast_debug(1, "%s: vpb_write: Applied gain..\n", p->dev); */
  1890. /* ast_debug(1, "%s: vpb_write: play_buf_time %d\n", p->dev, p->play_buf_time); */
  1891. if ((p->read_state == 1) && (p->play_buf_time < 5)){
  1892. play_buf_time_start = ast_tvnow();
  1893. /* res = vpb_play_buf_sync(p->handle, (char *)frame->data, tdiff * 8 * 2); */
  1894. res = vpb_play_buf_sync(p->handle, (char *)frame->data.ptr, frame->datalen);
  1895. if(res == VPB_OK) {
  1896. short * data = (short*)frame->data.ptr;
  1897. ast_verb(6, "%s: vpb_write: Wrote chan (codec=%d) %d %d\n", p->dev, fmt, data[0], data[1]);
  1898. }
  1899. p->play_buf_time = ast_tvdiff_ms(ast_tvnow(), play_buf_time_start);
  1900. } else {
  1901. p->chuck_count++;
  1902. ast_debug(1, "%s: vpb_write: Tossed data away, tooooo much data!![%d]\n", p->dev, p->chuck_count);
  1903. p->play_buf_time = 0;
  1904. }
  1905. ast_mutex_unlock(&p->play_lock);
  1906. /* ast_mutex_unlock(&p->lock); */
  1907. ast_verb(6, "%s: vpb_write: Done Writing to channel\n", p->dev);
  1908. return 0;
  1909. }
  1910. /* Read monitor thread function. */
  1911. static void *do_chanreads(void *pvt)
  1912. {
  1913. struct vpb_pvt *p = (struct vpb_pvt *)pvt;
  1914. struct ast_frame *fr = &p->fr;
  1915. char *readbuf = ((char *)p->buf) + AST_FRIENDLY_OFFSET;
  1916. int bridgerec = 0;
  1917. struct ast_format *tmpfmt;
  1918. int readlen, res, trycnt=0;
  1919. AudioCompress fmt;
  1920. int ignore_dtmf;
  1921. const char * getdtmf_var = NULL;
  1922. fr->frametype = AST_FRAME_VOICE;
  1923. fr->src = "vpb";
  1924. fr->mallocd = 0;
  1925. fr->delivery.tv_sec = 0;
  1926. fr->delivery.tv_usec = 0;
  1927. fr->samples = VPB_SAMPLES;
  1928. fr->offset = AST_FRIENDLY_OFFSET;
  1929. memset(p->buf, 0, sizeof p->buf);
  1930. ast_verb(3, "%s: chanreads: starting thread\n", p->dev);
  1931. ast_mutex_lock(&p->record_lock);
  1932. p->stopreads = 0;
  1933. p->read_state = 1;
  1934. while (!p->stopreads && p->owner) {
  1935. ast_verb(5, "%s: chanreads: Starting cycle ...\n", p->dev);
  1936. ast_verb(5, "%s: chanreads: Checking bridge \n", p->dev);
  1937. if (p->bridge) {
  1938. bridgerec = 0;
  1939. } else {
  1940. bridgerec = ast_channel_is_bridged(p->owner) ? 1 : 0;
  1941. }
  1942. /* if ((p->owner->_state != AST_STATE_UP) || !bridgerec) */
  1943. if ((ast_channel_state(p->owner) != AST_STATE_UP)) {
  1944. if (ast_channel_state(p->owner) != AST_STATE_UP) {
  1945. ast_verb(5, "%s: chanreads: Im not up[%d]\n", p->dev, ast_channel_state(p->owner));
  1946. } else {
  1947. ast_verb(5, "%s: chanreads: No bridgerec[%d]\n", p->dev, bridgerec);
  1948. }
  1949. vpb_sleep(10);
  1950. continue;
  1951. }
  1952. /* Voicetronix DTMF detection can be triggered off ordinary speech
  1953. * This leads to annoying beeps during the conversation
  1954. * Avoid this problem by just setting VPB_GETDTMF when you want to listen for DTMF
  1955. */
  1956. /* ignore_dtmf = 1; */
  1957. ignore_dtmf = 0; /* set this to 1 to turn this feature on */
  1958. getdtmf_var = pbx_builtin_getvar_helper(p->owner, "VPB_GETDTMF");
  1959. if (getdtmf_var && strcasecmp(getdtmf_var, "yes") == 0)
  1960. ignore_dtmf = 0;
  1961. if ((ignore_dtmf != p->last_ignore_dtmf) && (!use_ast_dtmfdet)){
  1962. ast_verb(2, "%s:Now %s DTMF \n",
  1963. p->dev, ignore_dtmf ? "ignoring" : "listening for");
  1964. vpb_set_event_mask(p->handle, ignore_dtmf ? VPB_EVENTS_NODTMF : VPB_EVENTS_ALL );
  1965. }
  1966. p->last_ignore_dtmf = ignore_dtmf;
  1967. /* Play DTMF digits here to avoid problem you get if playing a digit during
  1968. * a record operation
  1969. */
  1970. ast_verb(6, "%s: chanreads: Checking dtmf's \n", p->dev);
  1971. ast_mutex_lock(&p->play_dtmf_lock);
  1972. if (p->play_dtmf[0]) {
  1973. /* Try to ignore DTMF event we get after playing digit */
  1974. /* This DTMF is played by asterisk and leads to an annoying trailing beep on CISCO phones */
  1975. if (!ignore_dtmf) {
  1976. vpb_set_event_mask(p->handle, VPB_EVENTS_NODTMF );
  1977. }
  1978. if (p->bridge == NULL) {
  1979. vpb_dial_sync(p->handle, p->play_dtmf);
  1980. ast_verb(2, "%s: chanreads: Played DTMF %s\n", p->dev, p->play_dtmf);
  1981. } else {
  1982. ast_verb(2, "%s: chanreads: Not playing DTMF frame on native bridge\n", p->dev);
  1983. }
  1984. p->play_dtmf[0] = '\0';
  1985. ast_mutex_unlock(&p->play_dtmf_lock);
  1986. vpb_sleep(700); /* Long enough to miss echo and DTMF event */
  1987. if( !ignore_dtmf)
  1988. vpb_set_event_mask(p->handle, VPB_EVENTS_ALL);
  1989. continue;
  1990. }
  1991. ast_mutex_unlock(&p->play_dtmf_lock);
  1992. if (p->owner) {
  1993. tmpfmt = ast_channel_rawreadformat(p->owner);
  1994. } else {
  1995. tmpfmt = ast_format_slin;
  1996. }
  1997. fmt = ast2vpbformat(tmpfmt);
  1998. if (fmt < 0) {
  1999. ast_log(LOG_WARNING, "%s: Record failure (unsupported format %s)\n", p->dev, ast_format_get_name(tmpfmt));
  2000. return NULL;
  2001. }
  2002. readlen = VPB_SAMPLES * astformatbits(tmpfmt) / 8;
  2003. if (p->lastinput == -1) {
  2004. vpb_record_buf_start(p->handle, fmt);
  2005. /* vpb_reset_record_fifo_alarm(p->handle); */
  2006. p->lastinput = fmt;
  2007. ast_verb(2, "%s: Starting record mode (codec=%d)[%s]\n", p->dev, fmt, ast2vpbformatname(tmpfmt));
  2008. continue;
  2009. } else if (p->lastinput != fmt) {
  2010. vpb_record_buf_finish(p->handle);
  2011. vpb_record_buf_start(p->handle, fmt);
  2012. p->lastinput = fmt;
  2013. ast_verb(2, "%s: Changed record format (%d=>%d)\n", p->dev, p->lastinput, fmt);
  2014. continue;
  2015. }
  2016. /* Read only if up and not bridged, or a bridge for which we can read. */
  2017. ast_verb(6, "%s: chanreads: getting buffer!\n", p->dev);
  2018. if( (res = vpb_record_buf_sync(p->handle, readbuf, readlen) ) == VPB_OK ) {
  2019. ast_verb(6, "%s: chanreads: got buffer!\n", p->dev);
  2020. /* Apply extra gain ! */
  2021. if( p->rxswgain > MAX_VPB_GAIN )
  2022. a_gain_vector(p->rxswgain - MAX_VPB_GAIN, (short *)readbuf, readlen / sizeof(short));
  2023. ast_verb(6, "%s: chanreads: applied gain\n", p->dev);
  2024. fr->subclass.format = tmpfmt;
  2025. fr->data.ptr = readbuf;
  2026. fr->datalen = readlen;
  2027. fr->frametype = AST_FRAME_VOICE;
  2028. if ((use_ast_dtmfdet) && (p->vad)) {
  2029. fr = ast_dsp_process(p->owner, p->vad, fr);
  2030. if (fr && (fr->frametype == AST_FRAME_DTMF)) {
  2031. ast_debug(1, "%s: chanreads: Detected DTMF '%c'\n", p->dev, fr->subclass.integer);
  2032. } else if (fr->subclass.integer == 'f') {
  2033. }
  2034. }
  2035. /* Using trylock here to prevent deadlock when channel is hungup
  2036. * (ast_hangup() immediately gets lock)
  2037. */
  2038. if (p->owner && !p->stopreads) {
  2039. ast_verb(6, "%s: chanreads: queueing buffer on read frame q (state[%d])\n", p->dev, ast_channel_state(p->owner));
  2040. do {
  2041. res = ast_channel_trylock(p->owner);
  2042. trycnt++;
  2043. } while ((res !=0 ) && (trycnt < 300));
  2044. if (res == 0) {
  2045. ast_queue_frame(p->owner, fr);
  2046. ast_channel_unlock(p->owner);
  2047. } else {
  2048. ast_verb(5, "%s: chanreads: Couldnt get lock after %d tries!\n", p->dev, trycnt);
  2049. }
  2050. trycnt = 0;
  2051. /*
  2052. res = ast_mutex_trylock(&p->owner->lock);
  2053. if (res == 0) {
  2054. ast_queue_frame(p->owner, fr);
  2055. ast_mutex_unlock(&p->owner->lock);
  2056. } else {
  2057. if (res == EINVAL)
  2058. ast_verb(5, "%s: chanreads: try owner->lock gave me EINVAL[%d]\n", p->dev, res);
  2059. else if (res == EBUSY)
  2060. ast_verb(5, "%s: chanreads: try owner->lock gave me EBUSY[%d]\n", p->dev, res);
  2061. while (res != 0) {
  2062. res = ast_mutex_trylock(&p->owner->lock);
  2063. }
  2064. if (res == 0) {
  2065. ast_queue_frame(p->owner, fr);
  2066. ast_mutex_unlock(&p->owner->lock);
  2067. } else {
  2068. if (res == EINVAL) {
  2069. ast_verb(5, "%s: chanreads: try owner->lock gave me EINVAL[%d]\n", p->dev, res);
  2070. } else if (res == EBUSY) {
  2071. ast_verb(5, "%s: chanreads: try owner->lock gave me EBUSY[%d]\n", p->dev, res);
  2072. }
  2073. ast_verb(5, "%s: chanreads: Couldnt get lock on owner[%s][%d][%d] channel to send frame!\n", p->dev, p->owner->name, (int)p->owner->lock.__m_owner, (int)p->owner->lock.__m_count);
  2074. }
  2075. }
  2076. */
  2077. short *data = (short *)readbuf;
  2078. ast_verb(7, "%s: Read channel (codec=%d) %d %d\n", p->dev, fmt, data[0], data[1]);
  2079. } else {
  2080. ast_verb(5, "%s: p->stopreads[%d] p->owner[%p]\n", p->dev, p->stopreads, (void *)p->owner);
  2081. }
  2082. }
  2083. ast_verb(5, "%s: chanreads: Finished cycle...\n", p->dev);
  2084. }
  2085. p->read_state = 0;
  2086. /* When stopreads seen, go away! */
  2087. vpb_record_buf_finish(p->handle);
  2088. p->read_state = 0;
  2089. ast_mutex_unlock(&p->record_lock);
  2090. ast_verb(2, "%s: Ending record mode (%d/%s)\n",
  2091. p->dev, p->stopreads, p->owner ? "yes" : "no");
  2092. return NULL;
  2093. }
  2094. static struct ast_channel *vpb_new(struct vpb_pvt *me, enum ast_channel_state state, const char *context, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor)
  2095. {
  2096. struct ast_channel *tmp;
  2097. char cid_num[256];
  2098. char cid_name[256];
  2099. if (me->owner) {
  2100. ast_log(LOG_WARNING, "Called vpb_new on owned channel (%s) ?!\n", me->dev);
  2101. return NULL;
  2102. }
  2103. ast_verb(4, "%s: New call for context [%s]\n", me->dev, context);
  2104. tmp = ast_channel_alloc(1, state, 0, 0, "", me->ext, me->context, assignedids, requestor, AST_AMA_NONE, "%s", me->dev);
  2105. if (tmp) {
  2106. if (use_ast_ind == 1){
  2107. ast_channel_tech_set(tmp, &vpb_tech_indicate);
  2108. } else {
  2109. ast_channel_tech_set(tmp, &vpb_tech);
  2110. }
  2111. ast_channel_callgroup_set(tmp, me->callgroup);
  2112. ast_channel_pickupgroup_set(tmp, me->pickupgroup);
  2113. /* Linear is the preferred format. Although Voicetronix supports other formats
  2114. * they are all converted to/from linear in the vpb code. Best for us to use
  2115. * linear since we can then adjust volume in this modules.
  2116. */
  2117. ast_channel_nativeformats_set(tmp, vpb_tech.capabilities);
  2118. ast_channel_set_rawreadformat(tmp, ast_format_slin);
  2119. ast_channel_set_rawwriteformat(tmp, ast_format_slin);
  2120. if (state == AST_STATE_RING) {
  2121. ast_channel_rings_set(tmp, 1);
  2122. cid_name[0] = '\0';
  2123. cid_num[0] = '\0';
  2124. ast_callerid_split(me->callerid, cid_name, sizeof(cid_name), cid_num, sizeof(cid_num));
  2125. ast_set_callerid(tmp, cid_num, cid_name, cid_num);
  2126. }
  2127. ast_channel_tech_pvt_set(tmp, me);
  2128. ast_channel_context_set(tmp, context);
  2129. if (!ast_strlen_zero(me->ext))
  2130. ast_channel_exten_set(tmp, me->ext);
  2131. else
  2132. ast_channel_exten_set(tmp, "s");
  2133. if (!ast_strlen_zero(me->language))
  2134. ast_channel_language_set(tmp, me->language);
  2135. ast_channel_unlock(tmp);
  2136. me->owner = tmp;
  2137. me->bridge = NULL;
  2138. me->lastoutput = -1;
  2139. me->lastinput = -1;
  2140. me->last_ignore_dtmf = 1;
  2141. me->readthread = 0;
  2142. me->play_dtmf[0] = '\0';
  2143. me->faxhandled = 0;
  2144. me->lastgrunt = ast_tvnow(); /* Assume at least one grunt tone seen now. */
  2145. me->lastplay = ast_tvnow(); /* Assume at least one grunt tone seen now. */
  2146. if (state != AST_STATE_DOWN) {
  2147. if ((me->mode != MODE_FXO) && (state != AST_STATE_UP)) {
  2148. vpb_answer(tmp);
  2149. }
  2150. if (ast_pbx_start(tmp)) {
  2151. ast_log(LOG_WARNING, "Unable to start PBX on %s\n", ast_channel_name(tmp));
  2152. ast_hangup(tmp);
  2153. }
  2154. }
  2155. } else {
  2156. ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
  2157. }
  2158. return tmp;
  2159. }
  2160. static struct ast_channel *vpb_request(const char *type, struct ast_format_cap *cap, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *data, int *cause)
  2161. {
  2162. struct vpb_pvt *p;
  2163. struct ast_channel *tmp = NULL;
  2164. char *sepstr, *name;
  2165. const char *s;
  2166. int group = -1;
  2167. if (!(ast_format_cap_iscompatible_format(cap, ast_format_slin))) {
  2168. struct ast_str *buf;
  2169. buf = ast_str_create(AST_FORMAT_CAP_NAMES_LEN);
  2170. if (!buf) {
  2171. return NULL;
  2172. }
  2173. ast_log(LOG_NOTICE, "Asked to create a channel for unsupported formats: %s\n",
  2174. ast_format_cap_get_names(cap, &buf));
  2175. ast_free(buf);
  2176. return NULL;
  2177. }
  2178. name = ast_strdup(S_OR(data, ""));
  2179. sepstr = name;
  2180. s = strsep(&sepstr, "/"); /* Handle / issues */
  2181. if (!s)
  2182. s = "";
  2183. /* Check if we are looking for a group */
  2184. if (toupper(name[0]) == 'G' || toupper(name[0]) == 'R') {
  2185. group = atoi(name + 1);
  2186. }
  2187. /* Search for an unowned channel */
  2188. ast_mutex_lock(&iflock);
  2189. for (p = iflist; p; p = p->next) {
  2190. if (group == -1) {
  2191. if (strncmp(s, p->dev + 4, sizeof p->dev) == 0) {
  2192. if (!p->owner) {
  2193. tmp = vpb_new(p, AST_STATE_DOWN, p->context, assignedids, requestor);
  2194. break;
  2195. }
  2196. }
  2197. } else {
  2198. if ((p->group == group) && (!p->owner)) {
  2199. tmp = vpb_new(p, AST_STATE_DOWN, p->context, assignedids, requestor);
  2200. break;
  2201. }
  2202. }
  2203. }
  2204. ast_mutex_unlock(&iflock);
  2205. ast_verb(2, " %s requested, got: [%s]\n", name, tmp ? ast_channel_name(tmp) : "None");
  2206. ast_free(name);
  2207. restart_monitor();
  2208. return tmp;
  2209. }
  2210. static float parse_gain_value(const char *gain_type, const char *value)
  2211. {
  2212. float gain;
  2213. /* try to scan number */
  2214. if (sscanf(value, "%f", &gain) != 1) {
  2215. ast_log(LOG_ERROR, "Invalid %s value '%s' in '%s' config\n", value, gain_type, config);
  2216. return DEFAULT_GAIN;
  2217. }
  2218. /* percentage? */
  2219. /*if (value[strlen(value) - 1] == '%') */
  2220. /* return gain / (float)100; */
  2221. return gain;
  2222. }
  2223. static int unload_module(void)
  2224. {
  2225. struct vpb_pvt *p;
  2226. /* First, take us out of the channel loop */
  2227. if (use_ast_ind == 1){
  2228. ast_channel_unregister(&vpb_tech_indicate);
  2229. } else {
  2230. ast_channel_unregister(&vpb_tech);
  2231. }
  2232. ast_mutex_lock(&iflock);
  2233. /* Hangup all interfaces if they have an owner */
  2234. for (p = iflist; p; p = p->next) {
  2235. if (p->owner)
  2236. ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
  2237. }
  2238. iflist = NULL;
  2239. ast_mutex_unlock(&iflock);
  2240. ast_mutex_lock(&monlock);
  2241. if (mthreadactive > -1) {
  2242. pthread_cancel(monitor_thread);
  2243. pthread_join(monitor_thread, NULL);
  2244. }
  2245. mthreadactive = -2;
  2246. ast_mutex_unlock(&monlock);
  2247. ast_mutex_lock(&iflock);
  2248. /* Destroy all the interfaces and free their memory */
  2249. while (iflist) {
  2250. p = iflist;
  2251. ast_mutex_destroy(&p->lock);
  2252. pthread_cancel(p->readthread);
  2253. ast_mutex_destroy(&p->owner_lock);
  2254. ast_mutex_destroy(&p->record_lock);
  2255. ast_mutex_destroy(&p->play_lock);
  2256. ast_mutex_destroy(&p->play_dtmf_lock);
  2257. p->readthread = 0;
  2258. vpb_close(p->handle);
  2259. iflist = iflist->next;
  2260. ast_free(p);
  2261. }
  2262. iflist = NULL;
  2263. ast_mutex_unlock(&iflock);
  2264. if (bridges) {
  2265. ast_mutex_lock(&bridge_lock);
  2266. for (int i = 0; i < max_bridges; i++) {
  2267. ast_mutex_destroy(&bridges[i].lock);
  2268. ast_cond_destroy(&bridges[i].cond);
  2269. }
  2270. ast_free(bridges);
  2271. bridges = NULL;
  2272. ast_mutex_unlock(&bridge_lock);
  2273. }
  2274. ao2_cleanup(vpb_tech.capabilities);
  2275. vpb_tech.capabilities = NULL;
  2276. ao2_cleanup(vpb_tech_indicate.capabilities);
  2277. vpb_tech_indicate.capabilities = NULL;
  2278. return 0;
  2279. }
  2280. /*!
  2281. * \brief Load the module
  2282. *
  2283. * Module loading including tests for configuration or dependencies.
  2284. * This function can return AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_DECLINE,
  2285. * or AST_MODULE_LOAD_SUCCESS. If a dependency or environment variable fails
  2286. * tests return AST_MODULE_LOAD_FAILURE. If the module can not load the
  2287. * configuration file or other non-critical problem return
  2288. * AST_MODULE_LOAD_DECLINE. On success return AST_MODULE_LOAD_SUCCESS.
  2289. */
  2290. static enum ast_module_load_result load_module()
  2291. {
  2292. struct ast_config *cfg;
  2293. struct ast_variable *v;
  2294. struct ast_flags config_flags = { 0 };
  2295. struct vpb_pvt *tmp;
  2296. int board = 0, group = 0;
  2297. ast_group_t callgroup = 0;
  2298. ast_group_t pickupgroup = 0;
  2299. int mode = MODE_IMMEDIATE;
  2300. float txgain = DEFAULT_GAIN, rxgain = DEFAULT_GAIN;
  2301. float txswgain = 0, rxswgain = 0;
  2302. int got_gain=0;
  2303. int first_channel = 1;
  2304. int echo_cancel = DEFAULT_ECHO_CANCEL;
  2305. enum ast_module_load_result error = AST_MODULE_LOAD_SUCCESS; /* Error flag */
  2306. int bal1 = -1; /* Special value - means do not set */
  2307. int bal2 = -1;
  2308. int bal3 = -1;
  2309. char * callerid = NULL;
  2310. int num_cards = 0;
  2311. vpb_tech.capabilities = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
  2312. if (!vpb_tech.capabilities) {
  2313. return AST_MODULE_LOAD_DECLINE;
  2314. }
  2315. vpb_tech_indicate.capabilities = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
  2316. if (!vpb_tech_indicate.capabilities) {
  2317. return AST_MODULE_LOAD_DECLINE;
  2318. }
  2319. ast_format_cap_append(vpb_tech.capabilities, ast_format_slin, 0);
  2320. ast_format_cap_append(vpb_tech_indicate.capabilities, ast_format_slin, 0);
  2321. try {
  2322. num_cards = vpb_get_num_cards();
  2323. } catch (std::exception e) {
  2324. ast_log(LOG_ERROR, "No Voicetronix cards detected\n");
  2325. return AST_MODULE_LOAD_DECLINE;
  2326. }
  2327. int ports_per_card[num_cards];
  2328. for (int i = 0; i < num_cards; ++i)
  2329. ports_per_card[i] = vpb_get_ports_per_card(i);
  2330. cfg = ast_config_load(config, config_flags);
  2331. /* We *must* have a config file otherwise stop immediately */
  2332. if (!cfg || cfg == CONFIG_STATUS_FILEINVALID) {
  2333. ast_log(LOG_ERROR, "Unable to load config %s\n", config);
  2334. return AST_MODULE_LOAD_DECLINE;
  2335. }
  2336. ast_mutex_lock(&iflock);
  2337. v = ast_variable_browse(cfg, "general");
  2338. while (v){
  2339. if (strcasecmp(v->name, "cards") == 0) {
  2340. ast_log(LOG_NOTICE, "VPB Driver configured to use [%d] cards\n", atoi(v->value));
  2341. } else if (strcasecmp(v->name, "indication") == 0) {
  2342. use_ast_ind = 1;
  2343. ast_log(LOG_NOTICE, "VPB driver using Asterisk Indication functions!\n");
  2344. } else if (strcasecmp(v->name, "break-for-dtmf") == 0) {
  2345. if (ast_true(v->value)) {
  2346. break_for_dtmf = 1;
  2347. } else {
  2348. break_for_dtmf = 0;
  2349. ast_log(LOG_NOTICE, "VPB driver not stopping for DTMF's in native bridge\n");
  2350. }
  2351. } else if (strcasecmp(v->name, "ast-dtmf") == 0) {
  2352. use_ast_dtmf = 1;
  2353. ast_log(LOG_NOTICE, "VPB driver using Asterisk DTMF play functions!\n");
  2354. } else if (strcasecmp(v->name, "ast-dtmf-det") == 0) {
  2355. use_ast_dtmfdet = 1;
  2356. ast_log(LOG_NOTICE, "VPB driver using Asterisk DTMF detection functions!\n");
  2357. } else if (strcasecmp(v->name, "relaxdtmf") == 0) {
  2358. relaxdtmf = 1;
  2359. ast_log(LOG_NOTICE, "VPB driver using Relaxed DTMF with Asterisk DTMF detections functions!\n");
  2360. } else if (strcasecmp(v->name, "timer_period_ring") == 0) {
  2361. timer_period_ring = atoi(v->value);
  2362. } else if (strcasecmp(v->name, "ecsuppthres") == 0) {
  2363. ec_supp_threshold = (short)atoi(v->value);
  2364. } else if (strcasecmp(v->name, "dtmfidd") == 0) {
  2365. dtmf_idd = atoi(v->value);
  2366. ast_log(LOG_NOTICE, "VPB Driver setting DTMF IDD to [%d]ms\n", dtmf_idd);
  2367. }
  2368. v = v->next;
  2369. }
  2370. v = ast_variable_browse(cfg, "interfaces");
  2371. while (v) {
  2372. /* Create the interface list */
  2373. if (strcasecmp(v->name, "board") == 0) {
  2374. board = atoi(v->value);
  2375. } else if (strcasecmp(v->name, "group") == 0) {
  2376. group = atoi(v->value);
  2377. } else if (strcasecmp(v->name, "callgroup") == 0) {
  2378. callgroup = ast_get_group(v->value);
  2379. } else if (strcasecmp(v->name, "pickupgroup") == 0) {
  2380. pickupgroup = ast_get_group(v->value);
  2381. } else if (strcasecmp(v->name, "usepolaritycid") == 0) {
  2382. UsePolarityCID = atoi(v->value);
  2383. } else if (strcasecmp(v->name, "useloopdrop") == 0) {
  2384. UseLoopDrop = atoi(v->value);
  2385. } else if (strcasecmp(v->name, "usenativebridge") == 0) {
  2386. UseNativeBridge = atoi(v->value);
  2387. } else if (strcasecmp(v->name, "channel") == 0) {
  2388. int channel = atoi(v->value);
  2389. if (board >= num_cards || board < 0 || channel < 0 || channel >= ports_per_card[board]) {
  2390. ast_log(LOG_ERROR, "Invalid board/channel (%d/%d) for channel '%s'\n", board, channel, v->value);
  2391. error = AST_MODULE_LOAD_FAILURE;
  2392. goto done;
  2393. }
  2394. tmp = mkif(board, channel, mode, got_gain, txgain, rxgain, txswgain, rxswgain, bal1, bal2, bal3, callerid, echo_cancel,group,callgroup,pickupgroup);
  2395. if (tmp) {
  2396. if (first_channel) {
  2397. mkbrd(tmp->vpb_model, echo_cancel);
  2398. first_channel = 0;
  2399. }
  2400. tmp->next = iflist;
  2401. iflist = tmp;
  2402. } else {
  2403. ast_log(LOG_ERROR, "Unable to register channel '%s'\n", v->value);
  2404. error = AST_MODULE_LOAD_FAILURE;
  2405. goto done;
  2406. }
  2407. } else if (strcasecmp(v->name, "language") == 0) {
  2408. ast_copy_string(language, v->value, sizeof(language));
  2409. } else if (strcasecmp(v->name, "callerid") == 0) {
  2410. callerid = ast_strdup(v->value);
  2411. } else if (strcasecmp(v->name, "mode") == 0) {
  2412. if (strncasecmp(v->value, "di", 2) == 0) {
  2413. mode = MODE_DIALTONE;
  2414. } else if (strncasecmp(v->value, "im", 2) == 0) {
  2415. mode = MODE_IMMEDIATE;
  2416. } else if (strncasecmp(v->value, "fx", 2) == 0) {
  2417. mode = MODE_FXO;
  2418. } else {
  2419. ast_log(LOG_WARNING, "Unknown mode: %s\n", v->value);
  2420. }
  2421. } else if (!strcasecmp(v->name, "context")) {
  2422. ast_copy_string(context, v->value, sizeof(context));
  2423. } else if (!strcasecmp(v->name, "echocancel")) {
  2424. if (!strcasecmp(v->value, "off")) {
  2425. echo_cancel = 0;
  2426. }
  2427. } else if (strcasecmp(v->name, "txgain") == 0) {
  2428. txswgain = parse_gain_value(v->name, v->value);
  2429. got_gain |= VPB_GOT_TXSWG;
  2430. } else if (strcasecmp(v->name, "rxgain") == 0) {
  2431. rxswgain = parse_gain_value(v->name, v->value);
  2432. got_gain |= VPB_GOT_RXSWG;
  2433. } else if (strcasecmp(v->name, "txhwgain") == 0) {
  2434. txgain = parse_gain_value(v->name, v->value);
  2435. got_gain |= VPB_GOT_TXHWG;
  2436. } else if (strcasecmp(v->name, "rxhwgain") == 0) {
  2437. rxgain = parse_gain_value(v->name, v->value);
  2438. got_gain |= VPB_GOT_RXHWG;
  2439. } else if (strcasecmp(v->name, "bal1") == 0) {
  2440. bal1 = strtol(v->value, NULL, 16);
  2441. if (bal1 < 0 || bal1 > 255) {
  2442. ast_log(LOG_WARNING, "Bad bal1 value: %d\n", bal1);
  2443. bal1 = -1;
  2444. }
  2445. } else if (strcasecmp(v->name, "bal2") == 0) {
  2446. bal2 = strtol(v->value, NULL, 16);
  2447. if (bal2 < 0 || bal2 > 255) {
  2448. ast_log(LOG_WARNING, "Bad bal2 value: %d\n", bal2);
  2449. bal2 = -1;
  2450. }
  2451. } else if (strcasecmp(v->name, "bal3") == 0) {
  2452. bal3 = strtol(v->value, NULL, 16);
  2453. if (bal3 < 0 || bal3 > 255) {
  2454. ast_log(LOG_WARNING, "Bad bal3 value: %d\n", bal3);
  2455. bal3 = -1;
  2456. }
  2457. } else if (strcasecmp(v->name, "grunttimeout") == 0) {
  2458. gruntdetect_timeout = 1000 * atoi(v->value);
  2459. }
  2460. v = v->next;
  2461. }
  2462. if (gruntdetect_timeout < 1000)
  2463. gruntdetect_timeout = 1000;
  2464. done: (void)0;
  2465. ast_mutex_unlock(&iflock);
  2466. ast_config_destroy(cfg);
  2467. if (use_ast_ind == 1) {
  2468. if (error == AST_MODULE_LOAD_SUCCESS && ast_channel_register(&vpb_tech_indicate) != 0) {
  2469. ast_log(LOG_ERROR, "Unable to register channel class 'vpb'\n");
  2470. error = AST_MODULE_LOAD_FAILURE;
  2471. } else {
  2472. ast_log(LOG_NOTICE, "VPB driver Registered (w/AstIndication)\n");
  2473. }
  2474. } else {
  2475. if (error == AST_MODULE_LOAD_SUCCESS && ast_channel_register(&vpb_tech) != 0) {
  2476. ast_log(LOG_ERROR, "Unable to register channel class 'vpb'\n");
  2477. error = AST_MODULE_LOAD_FAILURE;
  2478. } else {
  2479. ast_log(LOG_NOTICE, "VPB driver Registered )\n");
  2480. }
  2481. }
  2482. if (error != AST_MODULE_LOAD_SUCCESS)
  2483. unload_module();
  2484. else
  2485. restart_monitor(); /* And start the monitor for the first time */
  2486. return error;
  2487. }
  2488. /**/
  2489. #if defined(__cplusplus) || defined(c_plusplus)
  2490. }
  2491. #endif
  2492. /**/
  2493. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Voicetronix API driver");