chan_vpb.cc 86 KB

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