chan_vpb.c 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * VoiceTronix Interface driver
  5. *
  6. * Copyright (C) 2003, Paul Bagyenda
  7. * Paul Bagyenda <bagyenda@dsmagic.com>
  8. * Copyright (C) 2004, Ben Kramer
  9. * Ben Kramer <ben@voicetronix.com.au>
  10. *
  11. * Daniel Bichara <daniel@bichara.com.br> - Brazilian CallerID detection (c)2004
  12. *
  13. * Welber Silveira - welberms@magiclink.com.br - (c)2004
  14. * Copying CLID string to propper structure after detection
  15. *
  16. * This program is free software, distributed under the terms of
  17. * the GNU General Public License
  18. */
  19. #include <stdio.h>
  20. #include <string.h>
  21. //#include <asterisk/lock.h>
  22. #include <asterisk/utils.h>
  23. #include <asterisk/channel.h>
  24. #include <asterisk/channel_pvt.h>
  25. #include <asterisk/config.h>
  26. #include <asterisk/logger.h>
  27. #include <asterisk/module.h>
  28. #include <asterisk/pbx.h>
  29. #include <asterisk/options.h>
  30. #include <sys/socket.h>
  31. #include <sys/time.h>
  32. #include <errno.h>
  33. #include <unistd.h>
  34. #include <stdlib.h>
  35. #include <arpa/inet.h>
  36. #include <fcntl.h>
  37. #include <sys/ioctl.h>
  38. #include <ctype.h>
  39. #include <vpbapi.h>
  40. #include <assert.h>
  41. #ifdef pthread_create
  42. #undef pthread_create
  43. #endif
  44. #define DEFAULT_GAIN 0
  45. #define DEFAULT_ECHO_CANCEL 1
  46. #define VPB_SAMPLES 160
  47. #define VPB_MAX_BUF VPB_SAMPLES*4 + AST_FRIENDLY_OFFSET
  48. #define VPB_NULL_EVENT 200
  49. #define VPB_WAIT_TIMEOUT 4000
  50. #define MAX_VPB_GAIN 12.0
  51. /*
  52. #if defined(__cplusplus) || defined(c_plusplus)
  53. extern "C" {
  54. #endif
  55. */
  56. static char *desc = "VoiceTronix V6PCI/V12PCI/V4PCI API Support";
  57. static char *type = "vpb";
  58. static char *tdesc = "Standard VoiceTronix API Driver";
  59. static char *config = "vpb.conf";
  60. /* Default context for dialtone mode */
  61. static char context[AST_MAX_EXTENSION] = "default";
  62. /* Default language */
  63. static char language[MAX_LANGUAGE] = "";
  64. static int usecnt =0;
  65. static int gruntdetect_timeout = 3600000; /* Grunt detect timeout is 1hr. */
  66. static const int prefformat = AST_FORMAT_SLINEAR;
  67. AST_MUTEX_DEFINE_STATIC(usecnt_lock);
  68. /* Protect the interface list (of vpb_pvt's) */
  69. AST_MUTEX_DEFINE_STATIC(iflock);
  70. /* Protect the monitoring thread, so only one process can kill or start it, and not
  71. when it's doing something critical. */
  72. AST_MUTEX_DEFINE_STATIC(monlock);
  73. /* This is the thread for the monitor which checks for input on the channels
  74. which are not currently in use. */
  75. static pthread_t monitor_thread;
  76. static int mthreadactive = -1; /* Flag for monitoring monitorthread.*/
  77. static int restart_monitor(void);
  78. /* The private structures of the VPB channels are
  79. linked for selecting outgoing channels */
  80. #define MODE_DIALTONE 1
  81. #define MODE_IMMEDIATE 2
  82. #define MODE_FXO 3
  83. /* Pick a country or add your own! */
  84. /* These are the tones that are played to the user */
  85. #define TONES_AU
  86. #ifdef TONES_AU
  87. static VPB_TONE Dialtone = {440, 440, 440, -10, -10, -10, 5000, 0 };
  88. static VPB_TONE Busytone = {470, 0, 0, -10, -100, -100, 5000, 0 };
  89. static VPB_TONE Ringbacktone = {400, 50, 440, -10, -10, -10, 1400, 800 };
  90. #endif
  91. /*
  92. #define TONES_USA
  93. #ifdef TONES_USA
  94. static VPB_TONE Dialtone = {425, 0, 0, -16, -100, -100, 10000, 0};
  95. static VPB_TONE Busytone = {425, 0, 0, -10, -100, -100, 500, 500};
  96. static VPB_TONE Ringbacktone = {400, 425, 450, -20, -20, -20, 1000, 1000};
  97. #endif
  98. */
  99. /* grunt tone defn's */
  100. 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 } } };
  101. static VPB_DETECT toned_ungrunt = { 2, VPB_GRUNT, 1, 2000, 1, 0, 0, -40, 0, 0, 30, 40, { { 0, 0, 0, 0 } } };
  102. /* Use loop drop detection */
  103. static int UseLoopDrop=1;
  104. /* To use or not to use Native bridging */
  105. static int UseNativeBridge=1;
  106. #define TIMER_PERIOD_RINGBACK 2000
  107. #define TIMER_PERIOD_BUSY 700
  108. #define VPB_EVENTS_ALL (VPB_MRING|VPB_MDIGIT|VPB_MDTMF|VPB_MTONEDETECT|VPB_MTIMEREXP|VPB_MPLAY_UNDERFLOW \
  109. |VPB_MRECORD_OVERFLOW|VPB_MSTATION_OFFHOOK|VPB_MSTATION_ONHOOK \
  110. |VPB_MRING_OFF|VPB_MDROP|VPB_MSTATION_FLASH)
  111. #define VPB_EVENTS_NODROP (VPB_MRING|VPB_MDIGIT|VPB_MDTMF|VPB_MTONEDETECT|VPB_MTIMEREXP|VPB_MPLAY_UNDERFLOW \
  112. |VPB_MRECORD_OVERFLOW|VPB_MSTATION_OFFHOOK|VPB_MSTATION_ONHOOK \
  113. |VPB_MRING_OFF|VPB_MSTATION_FLASH)
  114. #define VPB_EVENTS_NODTMF (VPB_MRING|VPB_MDIGIT|VPB_MTONEDETECT|VPB_MTIMEREXP|VPB_MPLAY_UNDERFLOW \
  115. |VPB_MRECORD_OVERFLOW|VPB_MSTATION_OFFHOOK|VPB_MSTATION_ONHOOK \
  116. |VPB_MRING_OFF|VPB_MDROP|VPB_MSTATION_FLASH)
  117. #define VPB_EVENTS_STAT (VPB_MRING|VPB_MDIGIT|VPB_MDTMF|VPB_MTONEDETECT|VPB_MTIMEREXP|VPB_MPLAY_UNDERFLOW \
  118. |VPB_MRECORD_OVERFLOW|VPB_MSTATION_OFFHOOK|VPB_MSTATION_ONHOOK \
  119. |VPB_MRING_OFF|VPB_MSTATION_FLASH)
  120. // Dialing parameters for Australia
  121. //#define DIAL_WITH_CALL_PROGRESS
  122. VPB_TONE_MAP DialToneMap[] = { { VPB_BUSY_AUST, VPB_CALL_DISCONNECT, 0 },
  123. { VPB_DIAL, VPB_CALL_DIALTONE, 0 },
  124. { VPB_RINGBACK_308, VPB_CALL_RINGBACK, 0 },
  125. { VPB_BUSY_AUST, VPB_CALL_BUSY, 0 },
  126. { VPB_GRUNT, VPB_CALL_GRUNT, 0 },
  127. { 0, 0, 1 } };
  128. #define VPB_DIALTONE_WAIT 2000 /* Wait up to 2s for a dialtone */
  129. #define VPB_RINGWAIT 4000 /* Wait up to 4s for ring tone after dialing */
  130. #define VPB_CONNECTED_WAIT 4000 /* If no ring tone detected for 4s then consider call connected */
  131. #define TIMER_PERIOD_NOANSWER 120000 /* Let it ring for 120s before deciding theres noone there */
  132. #define MAX_BRIDGES_V4PCI 2
  133. #define MAX_BRIDGES_V12PCI 128
  134. /* port states */
  135. #define VPB_STATE_ONHOOK 0
  136. #define VPB_STATE_OFFHOOK 1
  137. #define VPB_STATE_DIALLING 2
  138. #define VPB_STATE_JOINED 3
  139. #define VPB_STATE_GETDTMF 4
  140. #define VPB_STATE_PLAYDIAL 5
  141. #define VPB_STATE_PLAYBUSY 6
  142. #define VPB_STATE_PLAYRING 7
  143. typedef struct {
  144. int inuse;
  145. struct ast_channel *c0, *c1, **rc;
  146. struct ast_frame **fo;
  147. int flags;
  148. ast_mutex_t lock;
  149. pthread_cond_t cond;
  150. int endbridge;
  151. } vpb_bridge_t;
  152. static vpb_bridge_t * bridges;
  153. static int max_bridges = MAX_BRIDGES_V4PCI;
  154. AST_MUTEX_DEFINE_STATIC(bridge_lock);
  155. typedef enum {
  156. vpb_model_unknown = 0,
  157. vpb_model_v4pci,
  158. vpb_model_v12pci
  159. } vpb_model_t;
  160. static struct vpb_pvt {
  161. ast_mutex_t owner_lock; /* Protect blocks that expect ownership to remain the same */
  162. struct ast_channel *owner; /* Channel who owns us, possibly NULL */
  163. int golock; /* Got owner lock ? */
  164. int mode; /* fxo/imediate/dialtone*/
  165. int handle; /* Handle for vpb interface */
  166. int state; /* used to keep port state (internal to driver) */
  167. int group; /* Which group this port belongs to */
  168. char dev[256]; /* Device name, eg vpb/1-1 */
  169. vpb_model_t vpb_model; /* card model */
  170. struct ast_frame f, fr; /* Asterisk frame interface */
  171. char buf[VPB_MAX_BUF]; /* Static buffer for reading frames */
  172. int dialtone; /* NOT USED */
  173. float txgain, rxgain; /* Hardware gain control */
  174. float txswgain, rxswgain; /* Software gain control */
  175. int wantdtmf; /* Waiting for DTMF. */
  176. char context[AST_MAX_EXTENSION]; /* The context for this channel */
  177. char ext[AST_MAX_EXTENSION]; /* DTMF buffer for the ext[ens] */
  178. char language[MAX_LANGUAGE]; /* language being used */
  179. char callerid[AST_MAX_EXTENSION]; /* CallerId used for directly connected phone */
  180. int brcallerpos; /* Brazilian CallerID detection */
  181. int lastoutput; /* Holds the last Audio format output'ed */
  182. int lastinput; /* Holds the last Audio format input'ed */
  183. int last_ignore_dtmf;
  184. void *busy_timer; /* Void pointer for busy vpb_timer */
  185. int busy_timer_id; /* unique timer ID for busy timer */
  186. void *ringback_timer; /* Void pointer for ringback vpb_timer */
  187. int ringback_timer_id; /* unique timer ID for ringback timer */
  188. double lastgrunt; /* time stamp (secs since epoc) of last grunt event */
  189. ast_mutex_t lock; /* This one just protects bridge ptr below */
  190. vpb_bridge_t *bridge;
  191. int stopreads; /* Stop reading...*/
  192. int read_state; /* Read state */
  193. int chuck_count; /* a count of packets weve chucked away!*/
  194. pthread_t readthread; /* For monitoring read channel. One per owned channel. */
  195. ast_mutex_t record_lock; /* This one prevents reentering a record_buf block */
  196. ast_mutex_t play_lock; /* This one prevents reentering a play_buf block */
  197. int play_buf_time; /* How long the last play_buf took */
  198. ast_mutex_t play_dtmf_lock;
  199. char play_dtmf[16];
  200. struct vpb_pvt *next; /* Next channel in list */
  201. } *iflist = NULL;
  202. static struct ast_channel *vpb_new(struct vpb_pvt *i, int state, char *context);
  203. static void *do_chanreads(void *pvt);
  204. // Can't get vpb_bridge() working on v4pci without either a horrible
  205. // high pitched feedback noise or bad hiss noise depending on gain settings
  206. // Get asterisk to do the bridging
  207. #define BAD_V4PCI_BRIDGE
  208. // This one enables a half duplex bridge which may be required to prevent high pitched
  209. // feedback when getting asterisk to do the bridging and when using certain gain settings.
  210. //#define HALF_DUPLEX_BRIDGE
  211. /* This is the Native bridge code, which Asterisk will try before using its own bridging code */
  212. static int vpb_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc)
  213. {
  214. struct vpb_pvt *p0 = (struct vpb_pvt *)c0->pvt->pvt;
  215. struct vpb_pvt *p1 = (struct vpb_pvt *)c1->pvt->pvt;
  216. int i, res;
  217. #ifdef BAD_V4PCI_BRIDGE
  218. if(p0->vpb_model==vpb_model_v4pci)
  219. return -2;
  220. #endif
  221. if ( UseNativeBridge != 1){
  222. return -2;
  223. }
  224. /*
  225. ast_mutex_lock(&p0->lock);
  226. ast_mutex_lock(&p1->lock);
  227. */
  228. /* Bridge channels, check if we can. I believe we always can, so find a slot.*/
  229. ast_mutex_lock(&bridge_lock); {
  230. for (i = 0; i < max_bridges; i++)
  231. if (!bridges[i].inuse)
  232. break;
  233. if (i < max_bridges) {
  234. bridges[i].inuse = 1;
  235. bridges[i].endbridge = 0;
  236. bridges[i].flags = flags;
  237. bridges[i].rc = rc;
  238. bridges[i].fo = fo;
  239. bridges[i].c0 = c0;
  240. bridges[i].c1 = c1;
  241. }
  242. } ast_mutex_unlock(&bridge_lock);
  243. if (i == max_bridges) {
  244. ast_log(LOG_WARNING, "Failed to bridge %s and %s!\n", c0->name, c1->name);
  245. ast_mutex_unlock(&p0->lock);
  246. ast_mutex_unlock(&p1->lock);
  247. return -2;
  248. } else {
  249. /* Set bridge pointers. You don't want to take these locks while holding bridge lock.*/
  250. ast_mutex_lock(&p0->lock); {
  251. p0->bridge = &bridges[i];
  252. } ast_mutex_unlock(&p0->lock);
  253. ast_mutex_lock(&p1->lock); {
  254. p1->bridge = &bridges[i];
  255. } ast_mutex_unlock(&p1->lock);
  256. if (option_verbose>1)
  257. ast_verbose(VERBOSE_PREFIX_2 "Bridging call entered with [%s, %s]\n", c0->name, c1->name);
  258. }
  259. #ifdef HALF_DUPLEX_BRIDGE
  260. if (option_verbose>1)
  261. ast_verbose(VERBOSE_PREFIX_2 "Starting half-duplex bridge [%s, %s]\n", c0->name, c1->name);
  262. int dir = 0;
  263. memset(p0->buf, 0, sizeof p0->buf);
  264. memset(p1->buf, 0, sizeof p1->buf);
  265. vpb_record_buf_start(p0->handle, VPB_ALAW);
  266. vpb_record_buf_start(p1->handle, VPB_ALAW);
  267. vpb_play_buf_start(p0->handle, VPB_ALAW);
  268. vpb_play_buf_start(p1->handle, VPB_ALAW);
  269. while( !bridges[i].endbridge ) {
  270. struct vpb_pvt *from, *to;
  271. if(++dir%2) {
  272. from = p0;
  273. to = p1;
  274. } else {
  275. from = p1;
  276. to = p0;
  277. }
  278. vpb_record_buf_sync(from->handle, from->buf, VPB_SAMPLES);
  279. vpb_play_buf_sync(to->handle, from->buf, VPB_SAMPLES);
  280. }
  281. vpb_record_buf_finish(p0->handle);
  282. vpb_record_buf_finish(p1->handle);
  283. vpb_play_buf_finish(p0->handle);
  284. vpb_play_buf_finish(p1->handle);
  285. if (option_verbose>1)
  286. ast_verbose(VERBOSE_PREFIX_2 "Finished half-duplex bridge [%s, %s]\n", c0->name, c1->name);
  287. res = VPB_OK;
  288. #else
  289. res = vpb_bridge(p0->handle, p1->handle, VPB_BRIDGE_ON, i+1 /* resource 1 & 2 only for V4PCI*/ );
  290. if (res == VPB_OK) {
  291. //pthread_cond_wait(&bridges[i].cond, &bridges[i].lock); /* Wait for condition signal. */
  292. while( !bridges[i].endbridge ) {};
  293. vpb_bridge(p0->handle, p1->handle, VPB_BRIDGE_OFF, i+1 /* resource 1 & 2 only for V4PCI*/ );
  294. }
  295. #endif
  296. ast_mutex_lock(&bridge_lock); {
  297. bridges[i].inuse = 0;
  298. } ast_mutex_unlock(&bridge_lock);
  299. p0->bridge = NULL;
  300. p1->bridge = NULL;
  301. if (option_verbose>1)
  302. ast_verbose(VERBOSE_PREFIX_2 "Bridging call done with [%s, %s] => %d\n", c0->name, c1->name, res);
  303. /*
  304. ast_mutex_unlock(&p0->lock);
  305. ast_mutex_unlock(&p1->lock);
  306. */
  307. return (res==VPB_OK)?0:-1;
  308. }
  309. static double get_time_in_ms()
  310. {
  311. struct timeval tv;
  312. gettimeofday(&tv, NULL);
  313. return ((double)tv.tv_sec*1000)+((double)tv.tv_usec/1000);
  314. }
  315. // Caller ID can be located in different positions between the rings depending on your Telco
  316. // Australian (Telstra) callerid starts 700ms after 1st ring and finishes 1.5s after first ring
  317. // Use ANALYSE_CID to record rings and determine location of callerid
  318. //#define ANALYSE_CID
  319. #define RING_SKIP 600
  320. #define CID_MSECS 1700
  321. static void get_callerid(struct vpb_pvt *p)
  322. {
  323. short buf[CID_MSECS*8]; // 8kHz sampling rate
  324. double cid_record_time;
  325. int rc;
  326. if(strcasecmp(p->callerid, "on")) {
  327. p->owner->callerid = strdup("unknown");
  328. return;
  329. }
  330. if( ast_mutex_trylock(&p->record_lock) == 0 ) {
  331. char callerid[AST_MAX_EXTENSION] = "";
  332. cid_record_time = get_time_in_ms();
  333. if (option_verbose>3)
  334. ast_verbose(VERBOSE_PREFIX_4 "CID record - start\n");
  335. #ifdef ANALYSE_CID
  336. VPB_RECORD record_parms = { "", 8 * 1000 }; // record a couple of rings
  337. vpb_record_set(p->handle,&record_parms);
  338. ast_verbose(VERBOSE_PREFIX_4 "Recording debug CID sample to /tmp/cid.raw\n");
  339. vpb_record_file_sync(p->handle,"/tmp/cid.raw",VPB_LINEAR);
  340. rc = 1; // don't try to decode_cid
  341. #else
  342. // Skip any trailing ringtone
  343. vpb_sleep(RING_SKIP);
  344. if (option_verbose>3)
  345. ast_verbose(VERBOSE_PREFIX_4 "CID record - skipped %fms trailing ring\n",
  346. get_time_in_ms() - cid_record_time);
  347. cid_record_time = get_time_in_ms();
  348. // Record bit between the rings which contains the callerid
  349. vpb_record_buf_start(p->handle, VPB_LINEAR);
  350. rc = vpb_record_buf_sync(p->handle, (char*)buf, sizeof(buf));
  351. vpb_record_buf_finish(p->handle);
  352. #endif
  353. if (option_verbose>3)
  354. ast_verbose(VERBOSE_PREFIX_4 "CID record - recorded %fms between rings\n",
  355. get_time_in_ms() - cid_record_time);
  356. ast_mutex_unlock(&p->record_lock);
  357. if( rc != VPB_OK ) {
  358. ast_log(LOG_ERROR, "Failed to record caller id sample on %s\n", p->dev );
  359. return;
  360. }
  361. // This decodes FSK 1200baud type callerid
  362. if ((rc=vpb_cid_decode(callerid, buf, CID_MSECS*8)) == VPB_OK ) {
  363. if(!*callerid)
  364. strncpy(callerid,"undisclosed", sizeof(callerid) - 1); // blocked CID (eg caller used 1831)
  365. } else {
  366. ast_log(LOG_ERROR, "Failed to decode caller id on %s - %s\n", p->dev, vpb_strerror(rc) );
  367. strncpy(callerid,"unknown", sizeof(callerid) - 1);
  368. }
  369. p->owner->callerid = strdup(callerid);
  370. } else
  371. ast_log(LOG_ERROR, "Failed to set record mode for caller id on %s\n", p->dev );
  372. }
  373. // Terminate any tones we are presently playing
  374. static void stoptone( int handle)
  375. {
  376. int ret;
  377. VPB_EVENT je;
  378. while(vpb_playtone_state(handle)!=VPB_OK){
  379. vpb_tone_terminate(handle);
  380. ret = vpb_get_event_ch_async(handle,&je);
  381. if ((ret == VPB_OK)&&(je.type != VPB_DIALEND)){
  382. if (option_verbose > 3){
  383. ast_verbose(VERBOSE_PREFIX_4 "Stop tone collected a wrong event!![%d]\n",je.type);
  384. }
  385. // vpb_put_event(&je);
  386. }
  387. vpb_sleep(10);
  388. }
  389. }
  390. /* Safe vpb_playtone_async */
  391. static int playtone( int handle, VPB_TONE *tone)
  392. {
  393. int ret=VPB_OK;
  394. stoptone(handle);
  395. if (option_verbose > 3)
  396. ast_verbose(VERBOSE_PREFIX_4 "[%02d]: Playing tone\n", handle);
  397. ret = vpb_playtone_async(handle, tone);
  398. return ret;
  399. }
  400. static inline int monitor_handle_owned(struct vpb_pvt *p, VPB_EVENT *e)
  401. {
  402. struct ast_frame f = {AST_FRAME_CONTROL}; /* default is control, Clear rest. */
  403. int endbridge = 0;
  404. int res=0;
  405. if (option_verbose > 3)
  406. ast_verbose(VERBOSE_PREFIX_4 "%s: handle_owned: got event: [%d=>%d]\n",
  407. p->dev, e->type, e->data);
  408. f.src = type;
  409. switch (e->type) {
  410. case VPB_RING:
  411. if (p->mode == MODE_FXO) {
  412. f.subclass = AST_CONTROL_RING;
  413. } else
  414. f.frametype = -1; /* ignore ring on station port. */
  415. break;
  416. case VPB_RING_OFF:
  417. f.frametype = -1;
  418. break;
  419. case VPB_TIMEREXP:
  420. if (e->data == p->busy_timer_id) {
  421. playtone(p->handle,&Busytone);
  422. p->state = VPB_STATE_PLAYBUSY;
  423. vpb_timer_stop(p->busy_timer);
  424. vpb_timer_start(p->busy_timer);
  425. f.frametype = -1;
  426. } else if (e->data == p->ringback_timer_id) {
  427. playtone(p->handle, &Ringbacktone);
  428. vpb_timer_stop(p->ringback_timer);
  429. vpb_timer_start(p->ringback_timer);
  430. f.frametype = -1;
  431. } else {
  432. f.frametype = -1; /* Ignore. */
  433. }
  434. break;
  435. case VPB_DTMF:
  436. if (p->owner->_state == AST_STATE_UP) {
  437. f.frametype = AST_FRAME_DTMF;
  438. f.subclass = e->data;
  439. } else
  440. f.frametype = -1;
  441. break;
  442. case VPB_TONEDETECT:
  443. if (e->data == VPB_BUSY || e->data == VPB_BUSY_308 || e->data == VPB_BUSY_AUST ) {
  444. if (option_verbose > 3)
  445. ast_verbose(VERBOSE_PREFIX_4 "%s: handle_owned: got event: BUSY\n", p->dev);
  446. if (p->owner->_state == AST_STATE_UP) {
  447. f.subclass = AST_CONTROL_HANGUP;
  448. }
  449. else {
  450. f.subclass = AST_CONTROL_BUSY;
  451. }
  452. } else if (e->data == VPB_GRUNT) {
  453. if( ( get_time_in_ms() - p->lastgrunt ) > gruntdetect_timeout ) {
  454. // Nothing heard on line for a very long time
  455. // Timeout connection
  456. if (option_verbose > 2)
  457. ast_verbose(VERBOSE_PREFIX_3 "grunt timeout\n");
  458. ast_log(LOG_NOTICE,"%s: Line hangup due of lack of conversation\n",p->dev);
  459. f.subclass = AST_CONTROL_HANGUP;
  460. } else {
  461. p->lastgrunt = get_time_in_ms();
  462. f.frametype = -1;
  463. }
  464. } else
  465. f.frametype = -1;
  466. break;
  467. case VPB_CALLEND:
  468. #ifdef DIAL_WITH_CALL_PROGRESS
  469. if (e->data == VPB_CALL_CONNECTED)
  470. f.subclass = AST_CONTROL_ANSWER;
  471. else if (e->data == VPB_CALL_NO_DIAL_TONE || e->data == VPB_CALL_NO_RING_BACK)
  472. f.subclass = AST_CONTROL_CONGESTION;
  473. else if (e->data == VPB_CALL_NO_ANSWER || e->data == VPB_CALL_BUSY)
  474. f.subclass = AST_CONTROL_BUSY;
  475. else if (e->data == VPB_CALL_DISCONNECTED)
  476. f.subclass = AST_CONTROL_HANGUP;
  477. #else
  478. ast_log(LOG_NOTICE,"%s: Got call progress callback but blind dialing \n", p->dev);
  479. f.frametype = -1;
  480. #endif
  481. break;
  482. case VPB_STATION_OFFHOOK:
  483. f.subclass = AST_CONTROL_ANSWER;
  484. break;
  485. case VPB_DROP:
  486. if ((p->mode == MODE_FXO)&&(UseLoopDrop)){ /* ignore loop drop on stations */
  487. if (p->owner->_state == AST_STATE_UP)
  488. f.subclass = AST_CONTROL_HANGUP;
  489. else
  490. f.frametype = -1;
  491. }
  492. break;
  493. case VPB_STATION_ONHOOK:
  494. f.subclass = AST_CONTROL_HANGUP;
  495. break;
  496. case VPB_STATION_FLASH:
  497. f.subclass = AST_CONTROL_FLASH;
  498. break;
  499. // Called when dialing has finished and ringing starts
  500. // No indication that call has really been answered when using blind dialing
  501. case VPB_DIALEND:
  502. if (p->state < 5){
  503. f.subclass = AST_CONTROL_ANSWER;
  504. if (option_verbose > 1)
  505. ast_verbose(VERBOSE_PREFIX_2 "%s: Dialend\n", p->dev);
  506. } else {
  507. f.frametype = -1;
  508. }
  509. break;
  510. case VPB_PLAY_UNDERFLOW:
  511. f.frametype = -1;
  512. vpb_reset_play_fifo_alarm(p->handle);
  513. break;
  514. case VPB_RECORD_OVERFLOW:
  515. f.frametype = -1;
  516. vpb_reset_record_fifo_alarm(p->handle);
  517. break;
  518. default:
  519. f.frametype = -1;
  520. break;
  521. }
  522. if (option_verbose > 3)
  523. ast_verbose(VERBOSE_PREFIX_4 "%s: handle_owned: putting frame type[%d]subclass[%d], bridge=%p\n",
  524. p->dev, f.frametype, f.subclass, (void *)p->bridge);
  525. /*
  526. if (option_verbose > 3) ast_verbose("%s: LOCKING in handle_owned [%d]\n", p->dev,res);
  527. res = ast_mutex_lock(&p->lock);
  528. if (option_verbose > 3) ast_verbose("%s: LOCKING count[%d] owner[%d] \n", p->dev, p->lock.__m_count,p->lock.__m_owner);
  529. */
  530. {
  531. if (p->bridge) { /* Check what happened, see if we need to report it. */
  532. switch (f.frametype) {
  533. case AST_FRAME_DTMF:
  534. if ( !(p->bridge->c0 == p->owner &&
  535. (p->bridge->flags & AST_BRIDGE_DTMF_CHANNEL_0) ) &&
  536. !(p->bridge->c1 == p->owner &&
  537. (p->bridge->flags & AST_BRIDGE_DTMF_CHANNEL_1) ))
  538. /* Kill bridge, this is interesting. */
  539. endbridge = 1;
  540. break;
  541. case AST_FRAME_CONTROL:
  542. if (!(p->bridge->flags & AST_BRIDGE_IGNORE_SIGS))
  543. #if 0
  544. if (f.subclass == AST_CONTROL_BUSY ||
  545. f.subclass == AST_CONTROL_CONGESTION ||
  546. f.subclass == AST_CONTROL_HANGUP ||
  547. f.subclass == AST_CONTROL_FLASH)
  548. #endif
  549. endbridge = 1;
  550. break;
  551. default:
  552. break;
  553. }
  554. if (endbridge) {
  555. if (p->bridge->fo)
  556. *p->bridge->fo = ast_frisolate(&f);
  557. if (p->bridge->rc)
  558. *p->bridge->rc = p->owner;
  559. ast_mutex_lock(&p->bridge->lock); {
  560. p->bridge->endbridge = 1;
  561. pthread_cond_signal(&p->bridge->cond);
  562. } ast_mutex_unlock(&p->bridge->lock);
  563. }
  564. }
  565. }
  566. if (endbridge){
  567. res = ast_mutex_unlock(&p->lock);
  568. /*
  569. if (option_verbose > 3) ast_verbose("%s: unLOCKING in handle_owned [%d]\n", p->dev,res);
  570. */
  571. return 0;
  572. }
  573. // Trylock used here to avoid deadlock that can occur if we
  574. // happen to be in here handling an event when hangup is called
  575. // Problem is that hangup holds p->owner->lock
  576. if ((f.frametype >= 0)&& (f.frametype != AST_FRAME_NULL)&&(p->owner)) {
  577. if (ast_mutex_trylock(&p->owner->lock)==0) {
  578. ast_queue_frame(p->owner, &f);
  579. ast_mutex_unlock(&p->owner->lock);
  580. } else {
  581. ast_verbose("%s: handled_owned: Missed event %d/%d \n",
  582. p->dev,f.frametype, f.subclass);
  583. }
  584. }
  585. res = ast_mutex_unlock(&p->lock);
  586. /*
  587. if (option_verbose > 3) ast_verbose("%s: unLOCKING in handle_owned [%d]\n", p->dev,res);
  588. */
  589. return 0;
  590. }
  591. static inline int monitor_handle_notowned(struct vpb_pvt *p, VPB_EVENT *e)
  592. {
  593. char s[2] = {0};
  594. if (option_verbose > 3) {
  595. char str[VPB_MAX_STR];
  596. vpb_translate_event(e, str);
  597. ast_verbose(VERBOSE_PREFIX_4 "%s: handle_notowned: mode=%d, event[%d][%s]=[%d]\n",
  598. p->dev, p->mode, e->type,str, e->data);
  599. }
  600. switch(e->type) {
  601. case VPB_RING:
  602. if (p->mode == MODE_FXO) /* FXO port ring, start * */ {
  603. vpb_new(p, AST_STATE_RING, p->context);
  604. get_callerid(p); /* Australian Caller ID only between 1st and 2nd ring */
  605. }
  606. break;
  607. case VPB_RING_OFF:
  608. break;
  609. case VPB_STATION_OFFHOOK:
  610. if (p->mode == MODE_IMMEDIATE)
  611. vpb_new(p,AST_STATE_RING, p->context);
  612. else {
  613. ast_verbose(VERBOSE_PREFIX_4 "%s: handle_notowned: playing dialtone\n",p->dev);
  614. playtone(p->handle, &Dialtone);
  615. p->wantdtmf = 1;
  616. p->ext[0] = 0; /* Just to be sure & paranoid.*/
  617. p->state=VPB_STATE_PLAYDIAL;
  618. }
  619. break;
  620. case VPB_DIALEND:
  621. if (p->mode == MODE_DIALTONE){
  622. if (p->state == VPB_STATE_PLAYDIAL) {
  623. playtone(p->handle, &Dialtone);
  624. p->wantdtmf = 1;
  625. p->ext[0] = 0; // Just to be sure & paranoid.
  626. }
  627. /* These are not needed as they have timers to restart them
  628. else if (p->state == VPB_STATE_PLAYBUSY) {
  629. playtone(p->handle, &Busytone);
  630. p->wantdtmf = 1;
  631. p->ext[0] = 0; // Just to be sure & paranoid.
  632. }
  633. else if (p->state == VPB_STATE_PLAYRING) {
  634. playtone(p->handle, &Ringbacktone);
  635. p->wantdtmf = 1;
  636. p->ext[0] = 0; // Just to be sure & paranoid.
  637. }
  638. */
  639. } else {
  640. ast_verbose(VERBOSE_PREFIX_4 "%s: handle_notowned: Got a DIALEND when not really expected\n",p->dev);
  641. }
  642. break;
  643. case VPB_STATION_ONHOOK: /* clear ext */
  644. stoptone(p->handle);
  645. p->wantdtmf = 1 ;
  646. p->ext[0] = 0;
  647. p->state=VPB_STATE_ONHOOK;
  648. break;
  649. case VPB_DTMF:
  650. if (p->state == VPB_STATE_ONHOOK){
  651. /* DTMF's being passed while on-hook maybe Caller ID */
  652. break;
  653. }
  654. if (p->wantdtmf == 1) {
  655. stoptone(p->handle);
  656. p->wantdtmf = 0;
  657. }
  658. p->state=VPB_STATE_GETDTMF;
  659. s[0] = e->data;
  660. strncat(p->ext, s, sizeof(p->ext) - strlen(p->ext) - 1);
  661. if (ast_exists_extension(NULL, p->context, p->ext, 1, p->callerid)){
  662. vpb_new(p,AST_STATE_RING, p->context);
  663. } else if (!ast_canmatch_extension(NULL, p->context, p->ext, 1, p->callerid)){
  664. if (ast_exists_extension(NULL, "default", p->ext, 1, p->callerid)) {
  665. vpb_new(p,AST_STATE_RING, "default");
  666. } else if (!ast_canmatch_extension(NULL, "default", p->ext, 1, p->callerid)) {
  667. if (option_verbose > 3) {
  668. ast_verbose(VERBOSE_PREFIX_4 "%s: handle_notowned: can't match anything in %s or default\n",
  669. p->dev, p->context);
  670. }
  671. playtone(p->handle, &Busytone);
  672. vpb_timer_stop(p->busy_timer);
  673. vpb_timer_start(p->busy_timer);
  674. p->state = VPB_STATE_PLAYBUSY;
  675. }
  676. }
  677. break;
  678. default:
  679. /* Ignore.*/
  680. break;
  681. }
  682. if (option_verbose > 3)
  683. ast_verbose(VERBOSE_PREFIX_4 "%s: handle_notowned: mode=%d, [%d=>%d]\n",
  684. p->dev, p->mode, e->type, e->data);
  685. return 0;
  686. }
  687. static void *do_monitor(void *unused)
  688. {
  689. /* Monitor thread, doesn't die until explicitly killed. */
  690. if (option_verbose > 1)
  691. ast_verbose(VERBOSE_PREFIX_2 "Starting vpb monitor thread[%ld]\n",
  692. pthread_self());
  693. pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  694. for(;;) {
  695. VPB_EVENT e;
  696. VPB_EVENT je;
  697. char str[VPB_MAX_STR];
  698. struct vpb_pvt *p;
  699. /*
  700. if (option_verbose > 3)
  701. ast_verbose(VERBOSE_PREFIX_4 "Monitor waiting for event\n");
  702. */
  703. int res = vpb_get_event_sync(&e, VPB_WAIT_TIMEOUT);
  704. if( (res==VPB_NO_EVENTS) || (res==VPB_TIME_OUT) ){
  705. /*
  706. if (option_verbose > 3){
  707. if (res == VPB_NO_EVENTS){
  708. ast_verbose(VERBOSE_PREFIX_4 "No events....\n");
  709. } else {
  710. ast_verbose(VERBOSE_PREFIX_4 "No events, timed out....\n");
  711. }
  712. }
  713. */
  714. continue;
  715. }
  716. if (res != VPB_OK) {
  717. ast_log(LOG_ERROR,"Monitor get event error %s\n", vpb_strerror(res) );
  718. ast_verbose("Monitor get event error %s\n", vpb_strerror(res) );
  719. continue;
  720. }
  721. str[0] = 0;
  722. p = NULL;
  723. ast_mutex_lock(&monlock); {
  724. vpb_translate_event(&e, str);
  725. if (e.type == VPB_NULL_EVENT) {
  726. if (option_verbose > 3)
  727. ast_verbose(VERBOSE_PREFIX_4 "Monitor got null event\n");
  728. goto done; /* Nothing to do, just a wakeup call.*/
  729. }
  730. if (strlen(str)>1){
  731. str[(strlen(str)-1)]='\0';
  732. }
  733. ast_mutex_lock(&iflock); {
  734. p = iflist;
  735. while (p && p->handle != e.handle)
  736. p = p->next;
  737. } ast_mutex_unlock(&iflock);
  738. if (p && (option_verbose > 3))
  739. ast_verbose(VERBOSE_PREFIX_4 "%s: Event [%d=>%s] \n",
  740. p ? p->dev : "null", e.type, str );
  741. done: (void)0;
  742. } ast_mutex_unlock(&monlock);
  743. if (!p) {
  744. if (e.type != VPB_NULL_EVENT){
  745. ast_log(LOG_WARNING, "Got event [%s][%d], no matching iface!\n", str,e.type);
  746. if (option_verbose > 3){
  747. ast_verbose(VERBOSE_PREFIX_4 "vpb/ERR: No interface for Event [%d=>%s] \n",e.type,str );
  748. }
  749. }
  750. continue;
  751. }
  752. /* flush the event from the channel event Q */
  753. vpb_get_event_ch_async(e.handle,&je);
  754. if (option_verbose > 4){
  755. vpb_translate_event(&je, str);
  756. ast_verbose("%s: Flushing event [%d]=>%s\n",p->dev,je.type,str);
  757. }
  758. /* Check for ownership and locks */
  759. if ((p->owner)&&(!p->golock)){
  760. /* Need to get owner lock */
  761. /* Safely grab both p->lock and p->owner->lock so that there
  762. cannot be a race with something from the other side */
  763. /*
  764. ast_mutex_lock(&p->lock);
  765. while(ast_mutex_trylock(&p->owner->lock)) {
  766. ast_mutex_unlock(&p->lock);
  767. usleep(1);
  768. ast_mutex_lock(&p->lock);
  769. if (!p->owner)
  770. break;
  771. }
  772. if (p->owner)
  773. p->golock=1;
  774. */
  775. }
  776. /* Two scenarios: Are you owned or not. */
  777. if (p->owner) {
  778. monitor_handle_owned(p, &e);
  779. } else {
  780. monitor_handle_notowned(p, &e);
  781. }
  782. /* if ((!p->owner)&&(p->golock)){
  783. ast_mutex_unlock(&p->owner->lock);
  784. ast_mutex_unlock(&p->lock);
  785. }
  786. */
  787. }
  788. return NULL;
  789. }
  790. static int restart_monitor(void)
  791. {
  792. int error = 0;
  793. /* If we're supposed to be stopped -- stay stopped */
  794. if (mthreadactive == -2)
  795. return 0;
  796. if (option_verbose > 3)
  797. ast_verbose(VERBOSE_PREFIX_4 "Restarting monitor\n");
  798. ast_mutex_lock(&monlock); {
  799. if (monitor_thread == pthread_self()) {
  800. ast_log(LOG_WARNING, "Cannot kill myself\n");
  801. error = -1;
  802. if (option_verbose > 3)
  803. ast_verbose(VERBOSE_PREFIX_4 "Monitor trying to kill monitor\n");
  804. goto done;
  805. }
  806. if (mthreadactive != -1) {
  807. /* Why do other drivers kill the thread? No need says I, simply awake thread with event. */
  808. VPB_EVENT e;
  809. e.handle = 0;
  810. e.type = VPB_NULL_EVENT;
  811. e.data = 0;
  812. if (option_verbose > 3)
  813. ast_verbose(VERBOSE_PREFIX_4 "Trying to reawake monitor\n");
  814. vpb_put_event(&e);
  815. } else {
  816. /* Start a new monitor */
  817. int pid = ast_pthread_create(&monitor_thread, NULL, do_monitor, NULL);
  818. if (option_verbose > 3)
  819. ast_verbose(VERBOSE_PREFIX_4 "Created new monitor thread %d\n",pid);
  820. if (pid < 0) {
  821. ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
  822. error = -1;
  823. goto done;
  824. } else
  825. mthreadactive = 0; /* Started the thread!*/
  826. }
  827. done: (void)0;
  828. } ast_mutex_unlock(&monlock);
  829. if (option_verbose > 3)
  830. ast_verbose(VERBOSE_PREFIX_4 "Monitor restarted\n");
  831. return error;
  832. }
  833. // Per board config that must be called after vpb_open()
  834. static void mkbrd(vpb_model_t model, int echo_cancel)
  835. {
  836. if(!bridges) {
  837. if(model==vpb_model_v4pci)
  838. max_bridges = MAX_BRIDGES_V4PCI;
  839. bridges = (vpb_bridge_t *)malloc(max_bridges * sizeof(vpb_bridge_t) );
  840. if(!bridges)
  841. ast_log(LOG_ERROR, "Failed to initialize bridges\n");
  842. else {
  843. memset(bridges,0,max_bridges * sizeof(vpb_bridge_t));
  844. for(int i = 0; i < max_bridges; i++ ) {
  845. ast_mutex_init(&bridges[i].lock);
  846. pthread_cond_init(&bridges[i].cond, NULL);
  847. }
  848. }
  849. }
  850. if(!echo_cancel) {
  851. if (model==vpb_model_v4pci) {
  852. vpb_echo_canc_disable();
  853. ast_log(LOG_NOTICE, "Voicetronix echo cancellation OFF\n");
  854. }
  855. else {
  856. /* need to it port by port for OpenSwitch*/
  857. }
  858. } else {
  859. if (model==vpb_model_v4pci) {
  860. vpb_echo_canc_enable();
  861. ast_log(LOG_NOTICE, "Voicetronix echo cancellation ON\n");
  862. }
  863. else {
  864. /* need to it port by port for OpenSwitch*/
  865. }
  866. }
  867. }
  868. struct vpb_pvt *mkif(int board, int channel, int mode, float txgain, float rxgain,
  869. float txswgain, float rxswgain, int bal1, int bal2, int bal3,
  870. char * callerid, int echo_cancel, int group )
  871. {
  872. struct vpb_pvt *tmp;
  873. char buf[64];
  874. tmp = (struct vpb_pvt *)calloc(1, sizeof *tmp);
  875. if (!tmp)
  876. return NULL;
  877. tmp->handle = vpb_open(board, channel);
  878. if (tmp->handle < 0) {
  879. ast_log(LOG_WARNING, "Unable to create channel vpb/%d-%d: %s\n",
  880. board, channel, strerror(errno));
  881. free(tmp);
  882. return NULL;
  883. }
  884. snprintf(tmp->dev, sizeof(tmp->dev), "vpb/%d-%d", board, channel);
  885. tmp->mode = mode;
  886. tmp->group = group;
  887. strncpy(tmp->language, language, sizeof(tmp->language) - 1);
  888. strncpy(tmp->context, context, sizeof(tmp->context) - 1);
  889. if(callerid) {
  890. strncpy(tmp->callerid, callerid, sizeof(tmp->callerid) - 1);
  891. free(callerid);
  892. } else {
  893. strncpy(tmp->callerid, "unknown", sizeof(tmp->callerid) - 1);
  894. }
  895. /* check if codec balances have been set in the config file */
  896. if (bal3>=0) {
  897. if ((bal1>=0) && !(bal1 & 32)) bal1 |= 32;
  898. vpb_set_codec_reg(tmp->handle, 0x42, bal3);
  899. }
  900. if(bal1>=0) vpb_set_codec_reg(tmp->handle, 0x32, bal1);
  901. if(bal2>=0) vpb_set_codec_reg(tmp->handle, 0x3a, bal2);
  902. tmp->txgain = txgain;
  903. vpb_play_set_hw_gain(tmp->handle, (txgain > MAX_VPB_GAIN) ? MAX_VPB_GAIN : txgain);
  904. tmp->rxgain = rxgain;
  905. vpb_record_set_hw_gain(tmp->handle, (rxgain > MAX_VPB_GAIN) ? MAX_VPB_GAIN : rxgain);
  906. tmp->txswgain = txswgain;
  907. vpb_play_set_gain(tmp->handle, (txswgain > MAX_VPB_GAIN) ? MAX_VPB_GAIN : txswgain);
  908. tmp->rxswgain = rxswgain;
  909. vpb_record_set_gain(tmp->handle, (rxswgain > MAX_VPB_GAIN) ? MAX_VPB_GAIN : rxswgain);
  910. tmp->vpb_model = vpb_model_unknown;
  911. if( vpb_get_model(buf) == VPB_OK ) {
  912. if(strcmp(buf,"V12PCI")==0)
  913. tmp->vpb_model = vpb_model_v12pci;
  914. else if(strcmp(buf,"VPB4")==0)
  915. tmp->vpb_model = vpb_model_v4pci;
  916. }
  917. ast_mutex_init(&tmp->owner_lock);
  918. ast_mutex_init(&tmp->lock);
  919. ast_mutex_init(&tmp->record_lock);
  920. ast_mutex_init(&tmp->play_lock);
  921. ast_mutex_init(&tmp->play_dtmf_lock);
  922. /* set default read state */
  923. tmp->read_state = 0;
  924. tmp->golock=0;
  925. tmp->busy_timer_id = vpb_timer_get_unique_timer_id();
  926. vpb_timer_open(&tmp->busy_timer, tmp->handle, tmp->busy_timer_id, TIMER_PERIOD_BUSY);
  927. tmp->ringback_timer_id = vpb_timer_get_unique_timer_id();
  928. vpb_timer_open(&tmp->ringback_timer, tmp->handle, tmp->ringback_timer_id, TIMER_PERIOD_RINGBACK);
  929. if (mode == MODE_FXO){
  930. vpb_set_event_mask(tmp->handle, VPB_EVENTS_ALL );
  931. }
  932. else {
  933. vpb_set_event_mask(tmp->handle, VPB_EVENTS_STAT );
  934. }
  935. if ((tmp->vpb_model == vpb_model_v12pci) && (echo_cancel)){
  936. vpb_hostecho_on(tmp->handle);
  937. }
  938. /* define grunt tone */
  939. vpb_settonedet(tmp->handle,&toned_ungrunt);
  940. ast_log(LOG_NOTICE,"Voicetronix %s channel %s initialized (rxgain=%f txgain=%f) (%f/%f/0x%X/0x%X/0x%X)\n",
  941. (tmp->vpb_model==vpb_model_v4pci)?"V4PCI":
  942. (tmp->vpb_model==vpb_model_v12pci)?"V12PCI":"[Unknown model]",
  943. tmp->dev, tmp->rxswgain, tmp->txswgain, tmp->rxgain, tmp->txgain, bal1, bal2, bal3 );
  944. return tmp;
  945. }
  946. static int vpb_indicate(struct ast_channel *ast, int condition)
  947. {
  948. struct vpb_pvt *p = (struct vpb_pvt *)ast->pvt->pvt;
  949. int res = 0;
  950. int tmp = 0;
  951. if (option_verbose > 3)
  952. ast_verbose(VERBOSE_PREFIX_4 "%s: vpb_indicate [%d] state[%d]\n", p->dev, condition,ast->_state);
  953. /*
  954. if (ast->_state != AST_STATE_UP) {
  955. ast_verbose(VERBOSE_PREFIX_4 "%s: vpb_indicate Not in AST_STATE_UP\n", p->dev, condition,ast->_state);
  956. return res;
  957. }
  958. */
  959. /*
  960. if (option_verbose > 3) ast_verbose("%s: LOCKING in indicate \n", p->dev);
  961. if (option_verbose > 3) ast_verbose("%s: LOCKING count[%d] owner[%d] \n", p->dev, p->lock.__m_count,p->lock.__m_owner);
  962. */
  963. ast_mutex_lock(&p->lock);
  964. switch(condition) {
  965. case AST_CONTROL_BUSY:
  966. case AST_CONTROL_CONGESTION:
  967. if (ast->_state == AST_STATE_UP) {
  968. playtone(p->handle, &Busytone);
  969. p->state = VPB_STATE_PLAYBUSY;
  970. vpb_timer_stop(p->busy_timer);
  971. vpb_timer_start(p->busy_timer);
  972. }
  973. break;
  974. case AST_CONTROL_RINGING:
  975. if (ast->_state == AST_STATE_UP) {
  976. playtone(p->handle, &Ringbacktone);
  977. p->state = VPB_STATE_PLAYRING;
  978. if (option_verbose > 3)
  979. ast_verbose(VERBOSE_PREFIX_4 "%s: vpb indicate: setting ringback timer [%d]\n", p->dev,p->ringback_timer_id);
  980. vpb_timer_stop(p->ringback_timer);
  981. vpb_timer_start(p->ringback_timer);
  982. }
  983. break;
  984. case AST_CONTROL_ANSWER:
  985. case -1: /* -1 means stop playing? */
  986. vpb_timer_stop(p->ringback_timer);
  987. vpb_timer_stop(p->busy_timer);
  988. stoptone(p->handle);
  989. break;
  990. case AST_CONTROL_HANGUP:
  991. if (ast->_state == AST_STATE_UP) {
  992. playtone(p->handle, &Busytone);
  993. p->state = VPB_STATE_PLAYBUSY;
  994. vpb_timer_stop(p->busy_timer);
  995. vpb_timer_start(p->busy_timer);
  996. }
  997. break;
  998. default:
  999. res = 0;
  1000. break;
  1001. }
  1002. tmp = ast_mutex_unlock(&p->lock);
  1003. /*
  1004. if (option_verbose > 3) ast_verbose("%s: unLOCKING in indicate [%d]\n", p->dev,tmp);
  1005. */
  1006. return res;
  1007. }
  1008. static int vpb_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
  1009. {
  1010. struct vpb_pvt *p = (struct vpb_pvt *)newchan->pvt->pvt;
  1011. int res = 0;
  1012. /*
  1013. if (option_verbose > 3) ast_verbose("%s: LOCKING in fixup \n", p->dev);
  1014. if (option_verbose > 3) ast_verbose("%s: LOCKING count[%d] owner[%d] \n", p->dev, p->lock.__m_count,p->lock.__m_owner);
  1015. */
  1016. ast_mutex_lock(&p->lock);
  1017. ast_log(LOG_DEBUG, "New owner for channel %s is %s\n", p->dev, newchan->name);
  1018. if (p->owner == oldchan) {
  1019. p->owner = newchan;
  1020. }
  1021. if (newchan->_state == AST_STATE_RINGING)
  1022. vpb_indicate(newchan, AST_CONTROL_RINGING);
  1023. res= ast_mutex_unlock(&p->lock);
  1024. /*
  1025. if (option_verbose > 3) ast_verbose("%s: unLOCKING in fixup [%d]\n", p->dev,res);
  1026. */
  1027. return 0;
  1028. }
  1029. static int vpb_digit(struct ast_channel *ast, char digit)
  1030. {
  1031. struct vpb_pvt *p = (struct vpb_pvt *)ast->pvt->pvt;
  1032. char s[2];
  1033. int res = 0;
  1034. /*
  1035. if (option_verbose > 3) ast_verbose("%s: LOCKING in digit \n", p->dev);
  1036. if (option_verbose > 3) ast_verbose("%s: LOCKING count[%d] owner[%d] \n", p->dev, p->lock.__m_count,p->lock.__m_owner);
  1037. */
  1038. ast_mutex_lock(&p->lock);
  1039. s[0] = digit;
  1040. s[1] = '\0';
  1041. if (option_verbose > 3)
  1042. ast_verbose(VERBOSE_PREFIX_4 "%s: play digit[%s]\n", p->dev, s);
  1043. ast_mutex_lock(&p->play_dtmf_lock);
  1044. strncat(p->play_dtmf,s,sizeof(*p->play_dtmf));
  1045. ast_mutex_unlock(&p->play_dtmf_lock);
  1046. res = ast_mutex_unlock(&p->lock);
  1047. /*
  1048. if (option_verbose > 3) ast_verbose("%s: unLOCKING in digit [%d]\n", p->dev,res);
  1049. */
  1050. return 0;
  1051. }
  1052. /* Places a call out of a VPB channel */
  1053. static int vpb_call(struct ast_channel *ast, char *dest, int timeout)
  1054. {
  1055. struct vpb_pvt *p = (struct vpb_pvt *)ast->pvt->pvt;
  1056. int res = 0,i;
  1057. char *s = strrchr(dest, '/');
  1058. char dialstring[254] = "";
  1059. int tmp = 0;
  1060. /*
  1061. if (option_verbose > 3) ast_verbose("%s: LOCKING in call \n", p->dev);
  1062. if (option_verbose > 3) ast_verbose("%s: LOCKING count[%d] owner[%d] \n", p->dev, p->lock.__m_count,p->lock.__m_owner);
  1063. */
  1064. ast_mutex_lock(&p->lock);
  1065. if (s)
  1066. s = s + 1;
  1067. else
  1068. s = dest;
  1069. strncpy(dialstring, s, sizeof(dialstring) - 1);
  1070. for (i=0; dialstring[i] != '\0' ; i++) {
  1071. if ((dialstring[i] == 'w') || (dialstring[i] == 'W'))
  1072. dialstring[i] = ',';
  1073. else if ((dialstring[i] == 'f') || (dialstring[i] == 'F'))
  1074. dialstring[i] = '&';
  1075. }
  1076. if (option_verbose > 3)
  1077. ast_verbose(VERBOSE_PREFIX_4 "%s: starting call\n", p->dev);
  1078. if (ast->_state != AST_STATE_DOWN && ast->_state != AST_STATE_RESERVED) {
  1079. ast_log(LOG_WARNING, "vpb_call on %s neither down nor reserved!\n", ast->name);
  1080. tmp = ast_mutex_unlock(&p->lock);
  1081. /*
  1082. if (option_verbose > 3) ast_verbose("%s: unLOCKING in call [%d]\n", p->dev,tmp);
  1083. */
  1084. return -1;
  1085. }
  1086. if (p->mode != MODE_FXO) /* Station port, ring it. */
  1087. res = vpb_ring_station_async(p->handle, VPB_RING_STATION_ON,0);
  1088. else {
  1089. VPB_CALL call;
  1090. // Dial must timeout or it can leave channels unuseable
  1091. if( timeout == 0 )
  1092. timeout = TIMER_PERIOD_NOANSWER;
  1093. else
  1094. timeout = timeout * 1000; //convert from secs to ms.
  1095. // These timeouts are only used with call progress dialing
  1096. call.dialtones = 1; // Number of dialtones to get outside line
  1097. call.dialtone_timeout = VPB_DIALTONE_WAIT; // Wait this long for dialtone (ms)
  1098. call.ringback_timeout = VPB_RINGWAIT; // Wait this long for ringing after dialing (ms)
  1099. call.inter_ringback_timeout = VPB_CONNECTED_WAIT; // If ringing stops for this long consider it connected (ms)
  1100. call.answer_timeout = timeout; // Time to wait for answer after ringing starts (ms)
  1101. memcpy( &call.tone_map, DialToneMap, sizeof(DialToneMap) );
  1102. vpb_set_call(p->handle, &call);
  1103. if (option_verbose > 1)
  1104. ast_verbose(VERBOSE_PREFIX_2 "%s: Calling %s on %s \n",p->dev, dialstring, ast->name);
  1105. if (option_verbose > 2) {
  1106. int j;
  1107. ast_verbose(VERBOSE_PREFIX_2 "%s: Dial parms for %s %d/%dms/%dms/%dms/%dms\n", p->dev
  1108. , ast->name, call.dialtones, call.dialtone_timeout
  1109. , call.ringback_timeout, call.inter_ringback_timeout
  1110. , call.answer_timeout );
  1111. for( j=0; !call.tone_map[j].terminate; j++ )
  1112. ast_verbose(VERBOSE_PREFIX_2 "%s: Dial parms for %s tone %d->%d\n", p->dev,
  1113. ast->name, call.tone_map[j].tone_id, call.tone_map[j].call_id);
  1114. }
  1115. if (option_verbose > 4)
  1116. ast_verbose("%s: Disabling Loop Drop detection\n",p->dev);
  1117. vpb_disable_event(p->handle, VPB_MDROP);
  1118. vpb_sethook_sync(p->handle,VPB_OFFHOOK);
  1119. p->state=VPB_STATE_OFFHOOK;
  1120. #ifndef DIAL_WITH_CALL_PROGRESS
  1121. vpb_sleep(300);
  1122. if (option_verbose > 4)
  1123. ast_verbose("%s: Disabling Loop Drop detection\n",p->dev);
  1124. vpb_enable_event(p->handle, VPB_MDROP);
  1125. res = vpb_dial_async(p->handle, dialstring);
  1126. #else
  1127. if (option_verbose > 4)
  1128. ast_verbose("%s: Disabling Loop Drop detection\n",p->dev);
  1129. vpb_enable_event(p->handle, VPB_MDROP);
  1130. res = vpb_call_async(p->handle, dialstring);
  1131. #endif
  1132. if (res != VPB_OK) {
  1133. ast_log(LOG_DEBUG, "Call on %s to %s failed: %s\n", ast->name, s, vpb_strerror(res));
  1134. res = -1;
  1135. } else
  1136. res = 0;
  1137. }
  1138. if (option_verbose > 2)
  1139. ast_verbose(VERBOSE_PREFIX_3 "%s: VPB Calling %s [t=%d] on %s returned %d\n",p->dev , s, timeout, ast->name, res);
  1140. if (res == 0) {
  1141. ast_setstate(ast, AST_STATE_RINGING);
  1142. ast_queue_control(ast,AST_CONTROL_RINGING);
  1143. }
  1144. if (!p->readthread){
  1145. ast_pthread_create(&p->readthread, NULL, do_chanreads, (void *)p);
  1146. }
  1147. tmp = ast_mutex_unlock(&p->lock);
  1148. /*
  1149. if (option_verbose > 3) ast_verbose("%s: unLOCKING in call [%d]\n", p->dev,tmp);
  1150. */
  1151. return res;
  1152. }
  1153. static int vpb_hangup(struct ast_channel *ast)
  1154. {
  1155. struct vpb_pvt *p = (struct vpb_pvt *)ast->pvt->pvt;
  1156. VPB_EVENT je;
  1157. char str[VPB_MAX_STR];
  1158. int res =0 ;
  1159. /*
  1160. if (option_verbose > 3) ast_verbose("%s: LOCKING in hangup \n", p->dev);
  1161. if (option_verbose > 3) ast_verbose("%s: LOCKING in hangup count[%d] owner[%d] \n", p->dev, p->lock.__m_count,p->lock.__m_owner);
  1162. if (option_verbose > 3) ast_verbose("%s: LOCKING pthread_self(%d)\n", p->dev,pthread_self());
  1163. ast_mutex_lock(&p->lock);
  1164. */
  1165. if (option_verbose > 1)
  1166. ast_verbose(VERBOSE_PREFIX_2 "%s: Hangup requested\n", ast->name);
  1167. if (!ast->pvt || !ast->pvt->pvt) {
  1168. ast_log(LOG_WARNING, "%s: channel not connected?\n", ast->name);
  1169. res = ast_mutex_unlock(&p->lock);
  1170. /*
  1171. if (option_verbose > 3) ast_verbose("%s: unLOCKING in hangup [%d]\n", p->dev,res);
  1172. */
  1173. return 0;
  1174. }
  1175. /* Stop record */
  1176. p->stopreads = 1;
  1177. if( p->readthread ){
  1178. pthread_join(p->readthread, NULL);
  1179. if(option_verbose>3)
  1180. ast_verbose( VERBOSE_PREFIX_4 "%s: stopped record thread on %s\n",ast->name,p->dev);
  1181. }
  1182. /* Stop play */
  1183. if (p->lastoutput != -1) {
  1184. if(option_verbose>1)
  1185. ast_verbose( VERBOSE_PREFIX_2 "%s: Ending play mode on %s\n",ast->name,p->dev);
  1186. vpb_play_terminate(p->handle);
  1187. ast_mutex_lock(&p->play_lock); {
  1188. vpb_play_buf_finish(p->handle);
  1189. } ast_mutex_unlock(&p->play_lock);
  1190. }
  1191. if(option_verbose>3)
  1192. ast_verbose( VERBOSE_PREFIX_4 "%s: Setting state down\n",ast->name);
  1193. ast_setstate(ast,AST_STATE_DOWN);
  1194. /*
  1195. if (option_verbose > 3) ast_verbose("%s: LOCKING in hangup \n", p->dev);
  1196. if (option_verbose > 3) ast_verbose("%s: LOCKING in hangup count[%d] owner[%d] \n", p->dev, p->lock.__m_count,p->lock.__m_owner);
  1197. if (option_verbose > 3) ast_verbose("%s: LOCKING pthread_self(%d)\n", p->dev,pthread_self());
  1198. */
  1199. ast_mutex_lock(&p->lock);
  1200. if (p->mode != MODE_FXO) {
  1201. /* station port. */
  1202. vpb_ring_station_async(p->handle, VPB_RING_STATION_OFF,0);
  1203. if(p->state!=VPB_STATE_ONHOOK){
  1204. /* This is causing a "dial end" "play tone" loop
  1205. playtone(p->handle, &Busytone);
  1206. p->state = VPB_STATE_PLAYBUSY;
  1207. if(option_verbose>4)
  1208. ast_verbose( VERBOSE_PREFIX_4 "%s: Station offhook[%d], playing busy tone\n",
  1209. ast->name,p->state);
  1210. */
  1211. }
  1212. else {
  1213. stoptone(p->handle);
  1214. }
  1215. } else {
  1216. stoptone(p->handle); // Terminates any dialing
  1217. vpb_sethook_sync(p->handle, VPB_ONHOOK);
  1218. p->state=VPB_STATE_ONHOOK;
  1219. }
  1220. while (VPB_OK==vpb_get_event_ch_async(p->handle,&je)){
  1221. if(option_verbose>3) {
  1222. vpb_translate_event(&je, str);
  1223. ast_verbose( VERBOSE_PREFIX_4 "%s: Flushing event [%d]=>%s\n",ast->name,je.type,str);
  1224. }
  1225. }
  1226. p->readthread = 0;
  1227. p->lastoutput = -1;
  1228. p->lastinput = -1;
  1229. p->last_ignore_dtmf = 1;
  1230. p->ext[0] = 0;
  1231. p->dialtone = 0;
  1232. p->owner = NULL;
  1233. ast->pvt->pvt=NULL;
  1234. ast_mutex_lock(&usecnt_lock); {
  1235. usecnt--;
  1236. } ast_mutex_unlock(&usecnt_lock);
  1237. ast_update_use_count();
  1238. if (option_verbose > 1)
  1239. ast_verbose(VERBOSE_PREFIX_2 "%s: Hangup complete\n", ast->name);
  1240. restart_monitor();
  1241. /*
  1242. if (option_verbose > 3) ast_verbose("%s: LOCKING in hangup count[%d] owner[%d] \n", p->dev, p->lock.__m_count,p->lock.__m_owner);
  1243. */
  1244. res = ast_mutex_unlock(&p->lock);
  1245. /*
  1246. if (option_verbose > 3) ast_verbose("%s: unLOCKING in hangup [%d]\n", p->dev,res);
  1247. if (option_verbose > 3) ast_verbose("%s: LOCKING in hangup count[%d] owner[%d] \n", p->dev, p->lock.__m_count,p->lock.__m_owner);
  1248. */
  1249. return 0;
  1250. }
  1251. static int vpb_answer(struct ast_channel *ast)
  1252. {
  1253. struct vpb_pvt *p = (struct vpb_pvt *)ast->pvt->pvt;
  1254. VPB_EVENT je;
  1255. int ret;
  1256. int res = 0;
  1257. /*
  1258. if (option_verbose > 3) ast_verbose("%s: LOCKING in answer \n", p->dev);
  1259. if (option_verbose > 3) ast_verbose("%s: LOCKING count[%d] owner[%d] \n", p->dev, p->lock.__m_count,p->lock.__m_owner);
  1260. */
  1261. ast_mutex_lock(&p->lock);
  1262. if (option_verbose > 3)
  1263. ast_verbose(VERBOSE_PREFIX_4 "%s: Answering channel\n",p->dev);
  1264. if (p->mode == MODE_FXO){
  1265. if (option_verbose > 3)
  1266. ast_verbose("%s: Disabling Loop Drop detection\n",p->dev);
  1267. vpb_disable_event(p->handle, VPB_MDROP);
  1268. }
  1269. if (ast->_state != AST_STATE_UP) {
  1270. if (p->mode == MODE_FXO){
  1271. vpb_sethook_sync(p->handle, VPB_OFFHOOK);
  1272. p->state=VPB_STATE_OFFHOOK;
  1273. /* vpb_sleep(500);
  1274. ret = vpb_get_event_ch_async(p->handle,&je);
  1275. if ((ret == VPB_OK)&&((je.type != VPB_DROP)&&(je.type != VPB_RING))){
  1276. if (option_verbose > 3){
  1277. ast_verbose(VERBOSE_PREFIX_4 "%s: Answer collected a wrong event!!\n",p->dev);
  1278. }
  1279. vpb_put_event(&je);
  1280. }
  1281. */
  1282. }
  1283. ast_setstate(ast, AST_STATE_UP);
  1284. if(option_verbose>1)
  1285. ast_verbose( VERBOSE_PREFIX_2 "%s: Answered call from %s on %s [%s]\n", p->dev,
  1286. p->owner->callerid, ast->name,(p->mode == MODE_FXO)?"FXO":"FXS");
  1287. ast->rings = 0;
  1288. if( !p->readthread ){
  1289. // res = ast_mutex_unlock(&p->lock);
  1290. // ast_verbose("%s: unLOCKING in answer [%d]\n", p->dev,res);
  1291. ast_pthread_create(&p->readthread, NULL, do_chanreads, (void *)p);
  1292. } else {
  1293. if(option_verbose>3)
  1294. ast_verbose(VERBOSE_PREFIX_4 "%s: Record thread already running!!\n",p->dev);
  1295. }
  1296. } else {
  1297. if(option_verbose>3) {
  1298. ast_verbose(VERBOSE_PREFIX_4 "%s: Answered state is up\n",p->dev);
  1299. }
  1300. // res = ast_mutex_unlock(&p->lock);
  1301. // ast_verbose("%s: unLOCKING in answer [%d]\n", p->dev,res);
  1302. }
  1303. vpb_sleep(500);
  1304. if (p->mode == MODE_FXO){
  1305. if (option_verbose > 3)
  1306. ast_verbose("%s: Re-enabling Loop Drop detection\n",p->dev);
  1307. vpb_enable_event(p->handle,VPB_MDROP);
  1308. }
  1309. res = ast_mutex_unlock(&p->lock);
  1310. /*
  1311. if(option_verbose>3) ast_verbose("%s: unLOCKING in answer [%d]\n", p->dev,res);
  1312. */
  1313. return 0;
  1314. }
  1315. static struct ast_frame *vpb_read(struct ast_channel *ast)
  1316. {
  1317. struct vpb_pvt *p = (struct vpb_pvt *)ast->pvt->pvt;
  1318. static struct ast_frame f = {AST_FRAME_NULL};
  1319. f.src = type;
  1320. ast_log(LOG_NOTICE, "%s: vpb_read: should never be called!\n", p->dev);
  1321. ast_verbose("%s: vpb_read: should never be called!\n", p->dev);
  1322. return &f;
  1323. }
  1324. static inline int ast2vpbformat(int ast_format)
  1325. {
  1326. switch(ast_format) {
  1327. case AST_FORMAT_ALAW:
  1328. return VPB_ALAW;
  1329. case AST_FORMAT_SLINEAR:
  1330. return VPB_LINEAR;
  1331. case AST_FORMAT_ULAW:
  1332. return VPB_MULAW;
  1333. case AST_FORMAT_ADPCM:
  1334. return VPB_OKIADPCM;
  1335. default:
  1336. return -1;
  1337. }
  1338. }
  1339. static inline char * ast2vpbformatname(int ast_format)
  1340. {
  1341. switch(ast_format) {
  1342. case AST_FORMAT_ALAW:
  1343. return "AST_FORMAT_ALAW:VPB_ALAW";
  1344. case AST_FORMAT_SLINEAR:
  1345. return "AST_FORMAT_SLINEAR:VPB_LINEAR";
  1346. case AST_FORMAT_ULAW:
  1347. return "AST_FORMAT_ULAW:VPB_MULAW";
  1348. case AST_FORMAT_ADPCM:
  1349. return "AST_FORMAT_ADPCM:VPB_OKIADPCM";
  1350. default:
  1351. return "UNKN:UNKN";
  1352. }
  1353. }
  1354. static inline int astformatbits(int ast_format)
  1355. {
  1356. switch(ast_format) {
  1357. case AST_FORMAT_ALAW:
  1358. case AST_FORMAT_ULAW:
  1359. return 8;
  1360. case AST_FORMAT_SLINEAR:
  1361. return 16;
  1362. case AST_FORMAT_ADPCM:
  1363. return 4;
  1364. default:
  1365. return 8;
  1366. }
  1367. }
  1368. int a_gain_vector(float g, short *v, int n)
  1369. {
  1370. int i;
  1371. float tmp;
  1372. for ( i = 0; i<n; i++) {
  1373. tmp = g*v[i];
  1374. if (tmp > 32767.0)
  1375. tmp = 32767.0;
  1376. if (tmp < -32768.0)
  1377. tmp = -32768.0;
  1378. v[i] = (short)tmp;
  1379. }
  1380. return(i);
  1381. }
  1382. /* Writes a frame of voice data to a VPB channel */
  1383. static int vpb_write(struct ast_channel *ast, struct ast_frame *frame)
  1384. {
  1385. struct vpb_pvt *p = (struct vpb_pvt *)ast->pvt->pvt;
  1386. int res = 0, fmt = 0;
  1387. struct timeval play_buf_time_start,play_buf_time_finish;
  1388. // ast_mutex_lock(&p->lock);
  1389. if(option_verbose>5)
  1390. ast_verbose("%s: vpb_write: Writing to channel\n", p->dev);
  1391. if (frame->frametype != AST_FRAME_VOICE) {
  1392. if(option_verbose>3)
  1393. ast_verbose("%s: vpb_write: Don't know how to handle from type %d\n", ast->name, frame->frametype);
  1394. // ast_mutex_unlock(&p->lock);
  1395. return 0;
  1396. } else if (ast->_state != AST_STATE_UP) {
  1397. if(option_verbose>3)
  1398. ast_verbose("%s: vpb_write: Attempt to Write frame type[%d]subclass[%d] on not up chan\n",ast->name, frame->frametype, frame->subclass);
  1399. p->lastoutput = -1;
  1400. // ast_mutex_unlock(&p->lock);
  1401. return 0;
  1402. }
  1403. ast_log(LOG_DEBUG, "%s: vpb_write: Checked frame type..\n", p->dev);
  1404. fmt = ast2vpbformat(frame->subclass);
  1405. if (fmt < 0) {
  1406. ast_log(LOG_WARNING, "%s: vpb_write: Cannot handle frames of %d format!\n",ast->name, frame->subclass);
  1407. return -1;
  1408. }
  1409. ast_log(LOG_DEBUG, "%s: vpb_write: Checked frame format..\n", p->dev);
  1410. ast_mutex_lock(&p->play_lock);
  1411. ast_log(LOG_DEBUG, "%s: vpb_write: Got play lock..\n", p->dev);
  1412. /* Check if we have set up the play_buf */
  1413. if (p->lastoutput == -1) {
  1414. vpb_play_buf_start(p->handle, fmt);
  1415. if(option_verbose>1) {
  1416. ast_verbose("%s: vpb_write: Starting play mode (codec=%d)[%s]\n",p->dev,fmt,ast2vpbformatname(frame->subclass));
  1417. }
  1418. } else if (p->lastoutput != fmt) {
  1419. vpb_play_buf_finish(p->handle);
  1420. vpb_play_buf_start(p->handle, fmt);
  1421. if(option_verbose>1)
  1422. ast_verbose("%s: vpb_write: Changed play format (%d=>%d)\n",p->dev,p->lastoutput,fmt);
  1423. }
  1424. p->lastoutput = fmt;
  1425. // Apply extra gain !
  1426. if( p->txswgain > MAX_VPB_GAIN )
  1427. a_gain_vector(p->txswgain - MAX_VPB_GAIN , (short*)frame->data, frame->datalen/sizeof(short));
  1428. ast_log(LOG_DEBUG, "%s: vpb_write: Applied gain..\n", p->dev);
  1429. // gettimeofday(&tv, NULL);
  1430. // return ((double)tv.tv_sec*1000)+((double)tv.tv_usec/1000);
  1431. if ((p->read_state == 1)&&(p->play_buf_time<5)){
  1432. gettimeofday(&play_buf_time_start,NULL);
  1433. res = vpb_play_buf_sync(p->handle, (char*)frame->data, frame->datalen);
  1434. if( (res == VPB_OK) && (option_verbose > 5) ) {
  1435. short * data = (short*)frame->data;
  1436. ast_verbose("%s: vpb_write: Wrote chan (codec=%d) %d %d\n", p->dev, fmt, data[0],data[1]);
  1437. }
  1438. gettimeofday(&play_buf_time_finish,NULL);
  1439. if (play_buf_time_finish.tv_sec == play_buf_time_start.tv_sec){
  1440. p->play_buf_time=(int)((play_buf_time_finish.tv_usec-play_buf_time_start.tv_usec)/1000);
  1441. // ast_log(LOG_DEBUG, "%s: vpb_write: Timing start(%d) finish(%d)\n", p->dev,play_buf_time_start.tv_usec,play_buf_time_finish.tv_usec);
  1442. }
  1443. else {
  1444. p->play_buf_time=(int)((play_buf_time_finish.tv_sec - play_buf_time_start.tv_sec)*100)+(int)((play_buf_time_finish.tv_usec-play_buf_time_start.tv_usec)/1000);
  1445. }
  1446. // ast_log(LOG_DEBUG, "%s: vpb_write: Wrote data [%d](%d=>%s) to play_buf in [%d]ms..\n", p->dev,frame->datalen,fmt,ast2vpbformatname(frame->subclass),p->play_buf_time);
  1447. }
  1448. else {
  1449. p->chuck_count++;
  1450. ast_log(LOG_DEBUG, "%s: vpb_write: Tossed data away, tooooo much data!![%d]\n", p->dev,p->chuck_count);
  1451. p->play_buf_time=0;
  1452. }
  1453. ast_mutex_unlock(&p->play_lock);
  1454. // ast_mutex_unlock(&p->lock);
  1455. if(option_verbose>5)
  1456. ast_verbose("%s: vpb_write: Done Writing to channel\n", p->dev);
  1457. return 0;
  1458. }
  1459. /* Read monitor thread function. */
  1460. static void *do_chanreads(void *pvt)
  1461. {
  1462. struct vpb_pvt *p = (struct vpb_pvt *)pvt;
  1463. struct ast_frame *fr = &p->fr;
  1464. char *readbuf = ((char *)p->buf) + AST_FRIENDLY_OFFSET;
  1465. int bridgerec = 0;
  1466. int afmt, readlen, res, fmt, trycnt=0;
  1467. int ignore_dtmf;
  1468. char * getdtmf_var = NULL;
  1469. fr->frametype = AST_FRAME_VOICE;
  1470. fr->src = type;
  1471. fr->mallocd = 0;
  1472. fr->delivery.tv_sec = 0;
  1473. fr->delivery.tv_usec = 0;
  1474. fr->samples = VPB_SAMPLES;
  1475. fr->offset = AST_FRIENDLY_OFFSET;
  1476. memset(p->buf, 0, sizeof p->buf);
  1477. if (option_verbose > 2) {
  1478. ast_verbose("%s: chanreads: starting thread\n", p->dev);
  1479. }
  1480. ast_mutex_lock(&p->record_lock);
  1481. p->stopreads = 0;
  1482. p->read_state = 1;
  1483. while (!p->stopreads && p->owner) {
  1484. if (option_verbose > 4)
  1485. ast_verbose("%s: chanreads: Starting cycle ...\n", p->dev);
  1486. if (option_verbose > 4)
  1487. ast_verbose("%s: chanreads: Checking bridge \n", p->dev);
  1488. if (p->bridge) {
  1489. if (p->bridge->c0 == p->owner && (p->bridge->flags & AST_BRIDGE_REC_CHANNEL_0))
  1490. bridgerec = 1;
  1491. else if (p->bridge->c1 == p->owner && (p->bridge->flags & AST_BRIDGE_REC_CHANNEL_1))
  1492. bridgerec = 1;
  1493. else
  1494. bridgerec = 0;
  1495. } else {
  1496. if (option_verbose > 4)
  1497. ast_verbose("%s: chanreads: No native bridge.\n", p->dev);
  1498. if (p->owner->bridge){
  1499. if (option_verbose > 4){
  1500. ast_verbose("%s: chanreads: Got Asterisk bridge with [%s].\n", p->dev,p->owner->bridge->name);
  1501. }
  1502. bridgerec = 1;
  1503. }
  1504. else {
  1505. bridgerec = 0;
  1506. }
  1507. }
  1508. // if ( (p->owner->_state != AST_STATE_UP) || !bridgerec) {
  1509. if ( (p->owner->_state != AST_STATE_UP) ) {
  1510. if (option_verbose > 4) {
  1511. if (p->owner->_state != AST_STATE_UP)
  1512. ast_verbose("%s: chanreads: Im not up[%d]\n", p->dev,p->owner->_state);
  1513. else
  1514. ast_verbose("%s: chanreads: No bridgerec[%d]\n", p->dev,bridgerec);
  1515. }
  1516. vpb_sleep(10);
  1517. continue;
  1518. }
  1519. // Voicetronix DTMF detection can be triggered off ordinary speech
  1520. // This leads to annoying beeps during the conversation
  1521. // Avoid this problem by just setting VPB_GETDTMF when you want to listen for DTMF
  1522. //ignore_dtmf = 1;
  1523. ignore_dtmf = 0; /* set this to 1 to turn this feature on */
  1524. getdtmf_var = pbx_builtin_getvar_helper(p->owner,"VPB_GETDTMF");
  1525. if( getdtmf_var && ( strcasecmp( getdtmf_var, "yes" ) == 0 ) )
  1526. ignore_dtmf = 0;
  1527. if( ignore_dtmf != p->last_ignore_dtmf ) {
  1528. if(option_verbose>1)
  1529. ast_verbose( VERBOSE_PREFIX_2 "%s:Now %s DTMF \n",
  1530. p->dev, ignore_dtmf ? "ignoring" : "listening for");
  1531. vpb_set_event_mask(p->handle, ignore_dtmf ? VPB_EVENTS_NODTMF : VPB_EVENTS_ALL );
  1532. }
  1533. p->last_ignore_dtmf = ignore_dtmf;
  1534. // Play DTMF digits here to avoid problem you get if playing a digit during
  1535. // a record operation
  1536. if (option_verbose > 5) {
  1537. ast_verbose("%s: chanreads: Checking dtmf's \n", p->dev);
  1538. }
  1539. ast_mutex_lock(&p->play_dtmf_lock);
  1540. if( p->play_dtmf[0] ) {
  1541. // Try to ignore DTMF event we get after playing digit
  1542. // This DTMF is played by asterisk and leads to an annoying trailing beep on CISCO phones
  1543. if( !ignore_dtmf)
  1544. vpb_set_event_mask(p->handle, VPB_EVENTS_NODTMF );
  1545. vpb_dial_sync(p->handle,p->play_dtmf);
  1546. if(option_verbose>1)
  1547. ast_verbose( VERBOSE_PREFIX_2 "%s: Played DTMF %s\n",p->dev,p->play_dtmf);
  1548. p->play_dtmf[0] = '\0';
  1549. ast_mutex_unlock(&p->play_dtmf_lock);
  1550. vpb_sleep(700); // Long enough to miss echo and DTMF event
  1551. if( !ignore_dtmf)
  1552. vpb_set_event_mask(p->handle, VPB_EVENTS_ALL );
  1553. continue;
  1554. }
  1555. ast_mutex_unlock(&p->play_dtmf_lock);
  1556. // afmt = (p->owner) ? p->owner->pvt->rawreadformat : AST_FORMAT_SLINEAR;
  1557. if (p->owner){
  1558. afmt = p->owner->pvt->rawreadformat;
  1559. ast_log(LOG_DEBUG,"%s: Record using owner format [%s]\n", p->dev, ast2vpbformatname(afmt));
  1560. }
  1561. else {
  1562. afmt = AST_FORMAT_SLINEAR;
  1563. ast_log(LOG_DEBUG,"%s: Record using default format [%s]\n", p->dev, ast2vpbformatname(afmt));
  1564. }
  1565. fmt = ast2vpbformat(afmt);
  1566. if (fmt < 0) {
  1567. ast_log(LOG_WARNING,"%s: Record failure (unsupported format %d)\n", p->dev, afmt);
  1568. return NULL;
  1569. }
  1570. readlen = VPB_SAMPLES * astformatbits(afmt) / 8;
  1571. if (p->lastinput == -1) {
  1572. vpb_record_buf_start(p->handle, fmt);
  1573. vpb_reset_record_fifo_alarm(p->handle);
  1574. if(option_verbose>1)
  1575. ast_verbose( VERBOSE_PREFIX_2 "%s: Starting record mode (codec=%d)[%s]\n",p->dev,fmt,ast2vpbformatname(afmt));
  1576. } else if (p->lastinput != fmt) {
  1577. vpb_record_buf_finish(p->handle);
  1578. vpb_record_buf_start(p->handle, fmt);
  1579. if(option_verbose>1)
  1580. ast_verbose( VERBOSE_PREFIX_2 "%s: Changed record format (%d=>%d)\n",p->dev,p->lastinput,fmt);
  1581. }
  1582. p->lastinput = fmt;
  1583. /* Read only if up and not bridged, or a bridge for which we can read. */
  1584. if (option_verbose > 5) {
  1585. ast_verbose("%s: chanreads: getting buffer!\n", p->dev);
  1586. }
  1587. if( (res = vpb_record_buf_sync(p->handle, readbuf, readlen) ) == VPB_OK ) {
  1588. if (option_verbose > 5) {
  1589. ast_verbose("%s: chanreads: got buffer!\n", p->dev);
  1590. }
  1591. // Apply extra gain !
  1592. if( p->rxswgain > MAX_VPB_GAIN )
  1593. a_gain_vector(p->rxswgain - MAX_VPB_GAIN , (short*)readbuf, readlen/sizeof(short));
  1594. if (option_verbose > 5) {
  1595. ast_verbose("%s: chanreads: applied gain\n", p->dev);
  1596. }
  1597. fr->subclass = afmt;
  1598. fr->data = readbuf;
  1599. fr->datalen = readlen;
  1600. // Using trylock here to prevent deadlock when channel is hungup
  1601. // (ast_hangup() immediately gets lock)
  1602. if (p->owner && !p->stopreads ) {
  1603. if (option_verbose > 5) {
  1604. ast_verbose("%s: chanreads: queueing buffer on read frame q (state[%d])\n", p->dev,p->owner->_state);
  1605. }
  1606. do {
  1607. res = ast_mutex_trylock(&p->owner->lock);
  1608. trycnt++;
  1609. } while((res !=0)&&(trycnt<300));
  1610. if (res==0) {
  1611. ast_queue_frame(p->owner, fr);
  1612. ast_mutex_unlock(&p->owner->lock);
  1613. } else {
  1614. if (option_verbose > 4)
  1615. ast_verbose("%s: chanreads: Couldnt get lock after %d tries!\n", p->dev,trycnt);
  1616. }
  1617. trycnt=0;
  1618. /*
  1619. res = ast_mutex_trylock(&p->owner->lock);
  1620. // res=0;
  1621. if (res==0) {
  1622. ast_queue_frame(p->owner, fr);
  1623. ast_mutex_unlock(&p->owner->lock);
  1624. } else {
  1625. if (res == EINVAL )
  1626. if (option_verbose > 4) ast_verbose("%s: chanreads: try owner->lock gave me EINVAL[%d]\n", p->dev,res);
  1627. else if (res == EBUSY )
  1628. if (option_verbose > 4) ast_verbose("%s: chanreads: try owner->lock gave me EBUSY[%d]\n", p->dev,res);
  1629. while(res !=0){
  1630. res = ast_mutex_trylock(&p->owner->lock);
  1631. }
  1632. if (res==0) {
  1633. ast_queue_frame(p->owner, fr);
  1634. ast_mutex_unlock(&p->owner->lock);
  1635. }
  1636. else {
  1637. if (res == EINVAL )
  1638. if (option_verbose > 4) ast_verbose("%s: chanreads: try owner->lock gave me EINVAL[%d]\n", p->dev,res);
  1639. else if (res == EBUSY )
  1640. if (option_verbose > 4) ast_verbose("%s: chanreads: try owner->lock gave me EBUSY[%d]\n", p->dev,res);
  1641. if (option_verbose > 4) ast_verbose("%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);
  1642. //assert(p->dev!=p->dev);
  1643. }
  1644. }
  1645. */
  1646. if (option_verbose > 6) {
  1647. short * data = (short*)readbuf;
  1648. ast_verbose("%s: Read channel (codec=%d) %d %d\n", p->dev, fmt, data[0], data[1] );
  1649. }
  1650. }
  1651. else {
  1652. if (option_verbose > 4) {
  1653. ast_verbose("%s: p->stopreads[%d] p->owner[%p]\n", p->dev, p->stopreads,(void *)p->owner);
  1654. }
  1655. }
  1656. } else {
  1657. ast_log(LOG_WARNING,"%s: Record failure (%s)\n", p->dev, vpb_strerror(res));
  1658. vpb_record_buf_finish(p->handle);
  1659. vpb_record_buf_start(p->handle, fmt);
  1660. }
  1661. if (option_verbose > 4)
  1662. ast_verbose("%s: chanreads: Finished cycle...\n", p->dev);
  1663. }
  1664. p->read_state=0;
  1665. /* When stopreads seen, go away! */
  1666. vpb_record_buf_finish(p->handle);
  1667. p->read_state=0;
  1668. ast_mutex_unlock(&p->record_lock);
  1669. if (option_verbose > 1)
  1670. ast_verbose(VERBOSE_PREFIX_2 "%s: Ending record mode (%d/%s)\n",
  1671. p->dev, p->stopreads, p->owner? "yes" : "no");
  1672. return NULL;
  1673. }
  1674. static struct ast_channel *vpb_new(struct vpb_pvt *me, int state, char *context)
  1675. {
  1676. struct ast_channel *tmp;
  1677. if (me->owner) {
  1678. ast_log(LOG_WARNING, "Called vpb_new on owned channel (%s) ?!\n", me->dev);
  1679. return NULL;
  1680. }
  1681. if (option_verbose > 3)
  1682. ast_verbose("%s: New call for context [%s]\n",me->dev,context);
  1683. tmp = ast_channel_alloc(1);
  1684. if (tmp) {
  1685. strncpy(tmp->name, me->dev, sizeof(tmp->name) - 1);
  1686. tmp->type = type;
  1687. // Linear is the preferred format. Although Voicetronix supports other formats
  1688. // they are all converted to/from linear in the vpb code. Best for us to use
  1689. // linear since we can then adjust volume in this modules.
  1690. tmp->nativeformats = prefformat;
  1691. tmp->pvt->rawreadformat = AST_FORMAT_SLINEAR;
  1692. tmp->pvt->rawwriteformat = AST_FORMAT_SLINEAR;
  1693. ast_setstate(tmp, state);
  1694. if (state == AST_STATE_RING)
  1695. tmp->rings = 1;
  1696. tmp->pvt->pvt = me;
  1697. /* set call backs */
  1698. tmp->pvt->send_digit = vpb_digit;
  1699. tmp->pvt->call = vpb_call;
  1700. tmp->pvt->hangup = vpb_hangup;
  1701. tmp->pvt->answer = vpb_answer;
  1702. tmp->pvt->read = vpb_read;
  1703. tmp->pvt->write = vpb_write;
  1704. tmp->pvt->bridge = vpb_bridge;
  1705. tmp->pvt->indicate = vpb_indicate;
  1706. tmp->pvt->fixup = vpb_fixup;
  1707. strncpy(tmp->context, context, sizeof(tmp->context)-1);
  1708. if (strlen(me->ext))
  1709. strncpy(tmp->exten, me->ext, sizeof(tmp->exten)-1);
  1710. else
  1711. strncpy(tmp->exten, "s", sizeof(tmp->exten) - 1);
  1712. if (strlen(me->language))
  1713. strncpy(tmp->language, me->language, sizeof(tmp->language)-1);
  1714. me->owner = tmp;
  1715. me->bridge = NULL;
  1716. me->lastoutput = -1;
  1717. me->lastinput = -1;
  1718. me->last_ignore_dtmf = 1;
  1719. me->readthread = 0;
  1720. me->play_dtmf[0] = '\0';
  1721. me->lastgrunt = get_time_in_ms(); /* Assume at least one grunt tone seen now. */
  1722. ast_mutex_lock(&usecnt_lock);
  1723. usecnt++;
  1724. ast_mutex_unlock(&usecnt_lock);
  1725. ast_update_use_count();
  1726. if (state != AST_STATE_DOWN) {
  1727. if (ast_pbx_start(tmp)) {
  1728. ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
  1729. ast_hangup(tmp);
  1730. }
  1731. }
  1732. } else {
  1733. ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
  1734. }
  1735. return tmp;
  1736. }
  1737. static struct ast_channel *vpb_request(char *type, int format, void *data)
  1738. {
  1739. int oldformat;
  1740. struct vpb_pvt *p;
  1741. struct ast_channel *tmp = NULL;
  1742. char *name = strdup(data ? (char *)data : "");
  1743. char *s, *sepstr;
  1744. int group=-1;
  1745. oldformat = format;
  1746. format &= prefformat;
  1747. if (!format) {
  1748. ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%d'\n", oldformat);
  1749. return NULL;
  1750. }
  1751. sepstr = name;
  1752. s = strsep(&sepstr, "/"); /* Handle / issues */
  1753. if (!s)
  1754. s = "";
  1755. /* Check if we are looking for a group */
  1756. if (toupper(name[0]) == 'G' || toupper(name[0])=='R') {
  1757. group=atoi(name+1);
  1758. }
  1759. /* Search for an unowned channel */
  1760. ast_mutex_lock(&iflock); {
  1761. p = iflist;
  1762. while(p) {
  1763. if (group == -1){
  1764. if (strncmp(s, p->dev + 4, sizeof p->dev) == 0) {
  1765. if (!p->owner) {
  1766. tmp = vpb_new(p, AST_STATE_DOWN, p->context);
  1767. break;
  1768. }
  1769. }
  1770. }
  1771. else {
  1772. if ((p->group == group) && (!p->owner)) {
  1773. tmp = vpb_new(p, AST_STATE_DOWN, p->context);
  1774. break;
  1775. }
  1776. }
  1777. p = p->next;
  1778. }
  1779. } ast_mutex_unlock(&iflock);
  1780. if (option_verbose > 1)
  1781. ast_verbose(VERBOSE_PREFIX_2 " %s requested, got: [%s]\n",
  1782. name, tmp ? tmp->name : "None");
  1783. free(name);
  1784. restart_monitor();
  1785. return tmp;
  1786. }
  1787. static float parse_gain_value(char *gain_type, char *value)
  1788. {
  1789. float gain;
  1790. /* try to scan number */
  1791. if (sscanf(value, "%f", &gain) != 1)
  1792. {
  1793. ast_log(LOG_ERROR, "Invalid %s value '%s' in '%s' config\n", value, gain_type, config);
  1794. return DEFAULT_GAIN;
  1795. }
  1796. /* percentage? */
  1797. //if (value[strlen(value) - 1] == '%')
  1798. // return gain / (float)100;
  1799. return gain;
  1800. }
  1801. int load_module()
  1802. {
  1803. struct ast_config *cfg;
  1804. struct ast_variable *v;
  1805. struct vpb_pvt *tmp;
  1806. int board = 0, group = 0;
  1807. int mode = MODE_IMMEDIATE;
  1808. float txgain = DEFAULT_GAIN, rxgain = DEFAULT_GAIN;
  1809. float txswgain = 0, rxswgain = 0;
  1810. int first_channel = 1;
  1811. int echo_cancel = DEFAULT_ECHO_CANCEL;
  1812. int error = 0; /* Error flag */
  1813. int bal1 = -1; // Special value - means do not set
  1814. int bal2 = -1;
  1815. int bal3 = -1;
  1816. char * callerid = NULL;
  1817. cfg = ast_load(config);
  1818. /* We *must* have a config file otherwise stop immediately */
  1819. if (!cfg) {
  1820. ast_log(LOG_ERROR, "Unable to load config %s\n", config);
  1821. return -1;
  1822. }
  1823. vpb_seterrormode(VPB_ERROR_CODE);
  1824. ast_mutex_lock(&iflock); {
  1825. v = ast_variable_browse(cfg, "interfaces");
  1826. while(v) {
  1827. /* Create the interface list */
  1828. if (strcasecmp(v->name, "board") == 0) {
  1829. board = atoi(v->value);
  1830. } else if (strcasecmp(v->name, "group") == 0){
  1831. group = atoi(v->value);
  1832. } else if (strcasecmp(v->name, "useloopdrop") == 0){
  1833. UseLoopDrop = atoi(v->value);
  1834. } else if (strcasecmp(v->name, "usenativebridge") == 0){
  1835. UseNativeBridge = atoi(v->value);
  1836. } else if (strcasecmp(v->name, "channel") == 0) {
  1837. int channel = atoi(v->value);
  1838. tmp = mkif(board, channel, mode, txgain, rxgain, txswgain, rxswgain, bal1, bal2, bal3, callerid, echo_cancel,group);
  1839. if (tmp) {
  1840. if(first_channel) {
  1841. mkbrd( tmp->vpb_model, echo_cancel );
  1842. first_channel = 0;
  1843. }
  1844. tmp->next = iflist;
  1845. iflist = tmp;
  1846. } else {
  1847. ast_log(LOG_ERROR,
  1848. "Unable to register channel '%s'\n", v->value);
  1849. error = -1;
  1850. goto done;
  1851. }
  1852. callerid = NULL;
  1853. } else if (strcasecmp(v->name, "language") == 0) {
  1854. strncpy(language, v->value, sizeof(language)-1);
  1855. } else if (strcasecmp(v->name, "callerid") == 0) {
  1856. callerid = strdup(v->value);
  1857. } else if (strcasecmp(v->name, "mode") == 0) {
  1858. if (strncasecmp(v->value, "di", 2) == 0)
  1859. mode = MODE_DIALTONE;
  1860. else if (strncasecmp(v->value, "im", 2) == 0)
  1861. mode = MODE_IMMEDIATE;
  1862. else if (strncasecmp(v->value, "fx", 2) == 0)
  1863. mode = MODE_FXO;
  1864. else
  1865. ast_log(LOG_WARNING, "Unknown mode: %s\n", v->value);
  1866. } else if (!strcasecmp(v->name, "context")) {
  1867. strncpy(context, v->value, sizeof(context)-1);
  1868. } else if (!strcasecmp(v->name, "echocancel")) {
  1869. if (!strcasecmp(v->value, "off"))
  1870. echo_cancel = 0;
  1871. } else if (strcasecmp(v->name, "txgain") == 0) {
  1872. txswgain = parse_gain_value(v->name, v->value);
  1873. } else if (strcasecmp(v->name, "rxgain") == 0) {
  1874. rxswgain = parse_gain_value(v->name, v->value);
  1875. } else if (strcasecmp(v->name, "txhwgain") == 0) {
  1876. txgain = parse_gain_value(v->name, v->value);
  1877. } else if (strcasecmp(v->name, "rxhwgain") == 0) {
  1878. rxgain = parse_gain_value(v->name, v->value);
  1879. } else if (strcasecmp(v->name, "bal1") == 0) {
  1880. bal1 = strtol(v->value, NULL, 16);
  1881. if(bal1<0 || bal1>255) {
  1882. ast_log(LOG_WARNING, "Bad bal1 value: %d\n", bal1);
  1883. bal1 = -1;
  1884. }
  1885. } else if (strcasecmp(v->name, "bal2") == 0) {
  1886. bal2 = strtol(v->value, NULL, 16);
  1887. if(bal2<0 || bal2>255) {
  1888. ast_log(LOG_WARNING, "Bad bal2 value: %d\n", bal2);
  1889. bal2 = -1;
  1890. }
  1891. } else if (strcasecmp(v->name, "bal3") == 0) {
  1892. bal3 = strtol(v->value, NULL, 16);
  1893. if(bal3<0 || bal3>255) {
  1894. ast_log(LOG_WARNING, "Bad bal3 value: %d\n", bal3);
  1895. bal3 = -1;
  1896. }
  1897. } else if (strcasecmp(v->name, "grunttimeout") == 0) {
  1898. gruntdetect_timeout = 1000*atoi(v->value);
  1899. }
  1900. v = v->next;
  1901. }
  1902. if (gruntdetect_timeout < 1000)
  1903. gruntdetect_timeout = 1000;
  1904. done: (void)0;
  1905. } ast_mutex_unlock(&iflock);
  1906. ast_destroy(cfg);
  1907. if (!error && ast_channel_register(type, tdesc, prefformat, vpb_request) != 0) {
  1908. ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
  1909. error = -1;
  1910. }
  1911. if (error)
  1912. unload_module();
  1913. else
  1914. restart_monitor(); /* And start the monitor for the first time */
  1915. return error;
  1916. }
  1917. int unload_module()
  1918. {
  1919. struct vpb_pvt *p;
  1920. /* First, take us out of the channel loop */
  1921. ast_channel_unregister(type);
  1922. ast_mutex_lock(&iflock); {
  1923. /* Hangup all interfaces if they have an owner */
  1924. p = iflist;
  1925. while(p) {
  1926. if (p->owner)
  1927. ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
  1928. p = p->next;
  1929. }
  1930. iflist = NULL;
  1931. } ast_mutex_unlock(&iflock);
  1932. ast_mutex_lock(&monlock); {
  1933. if (mthreadactive > -1) {
  1934. pthread_cancel(monitor_thread);
  1935. pthread_join(monitor_thread, NULL);
  1936. }
  1937. mthreadactive = -2;
  1938. } ast_mutex_unlock(&monlock);
  1939. ast_mutex_lock(&iflock); {
  1940. /* Destroy all the interfaces and free their memory */
  1941. while(iflist) {
  1942. p = iflist;
  1943. ast_mutex_destroy(&p->lock);
  1944. pthread_cancel(p->readthread);
  1945. ast_mutex_destroy(&p->owner_lock);
  1946. ast_mutex_destroy(&p->record_lock);
  1947. ast_mutex_destroy(&p->play_lock);
  1948. ast_mutex_destroy(&p->play_dtmf_lock);
  1949. p->readthread = 0;
  1950. vpb_close(p->handle);
  1951. iflist = iflist->next;
  1952. free(p);
  1953. }
  1954. iflist = NULL;
  1955. } ast_mutex_unlock(&iflock);
  1956. ast_mutex_lock(&bridge_lock); {
  1957. memset(bridges, 0, sizeof bridges);
  1958. } ast_mutex_unlock(&bridge_lock);
  1959. ast_mutex_destroy(&bridge_lock);
  1960. for(int i = 0; i < max_bridges; i++ ) {
  1961. ast_mutex_destroy(&bridges[i].lock);
  1962. pthread_cond_destroy(&bridges[i].cond);
  1963. }
  1964. free(bridges);
  1965. return 0;
  1966. }
  1967. int usecount()
  1968. {
  1969. int res;
  1970. ast_mutex_lock(&usecnt_lock);
  1971. res = usecnt;
  1972. ast_mutex_unlock(&usecnt_lock);
  1973. return res;
  1974. }
  1975. char *description()
  1976. {
  1977. return desc;
  1978. }
  1979. char *key()
  1980. {
  1981. return ASTERISK_GPL_KEY;
  1982. }
  1983. /*
  1984. #if defined(__cplusplus) || defined(c_plusplus)
  1985. }
  1986. #endif
  1987. */