2 * Asterisk -- A telephony toolkit for Linux.
4 * VoiceTronix Interface driver
6 * Copyright (C) 2003, Paul Bagyenda
7 * Paul Bagyenda <bagyenda@dsmagic.com>
8 * Copyright (C) 2004 - 2005, Ben Kramer
9 * Ben Kramer <ben@voicetronix.com.au>
11 * Daniel Bichara <daniel@bichara.com.br> - Brazilian CallerID detection (c)2004
13 * Welber Silveira - welberms@magiclink.com.br - (c)2004
14 * Copying CLID string to propper structure after detection
16 * This program is free software, distributed under the terms of
17 * the GNU General Public License
25 #include <asterisk/lock.h>
26 #include <asterisk/utils.h>
27 #include <asterisk/channel.h>
28 #include <asterisk/config.h>
29 #include <asterisk/logger.h>
30 #include <asterisk/module.h>
31 #include <asterisk/pbx.h>
32 #include <asterisk/options.h>
33 #include <asterisk/callerid.h>
34 #include <asterisk/dsp.h>
35 #include <asterisk/features.h>
39 #include <sys/socket.h>
44 #include <arpa/inet.h>
46 #include <sys/ioctl.h>
56 #define DEFAULT_GAIN 0
57 #define DEFAULT_ECHO_CANCEL 1
59 #define VPB_SAMPLES 160
60 #define VPB_MAX_BUF VPB_SAMPLES*4 + AST_FRIENDLY_OFFSET
62 #define VPB_NULL_EVENT 200
64 #define VPB_WAIT_TIMEOUT 4000
66 #define MAX_VPB_GAIN 12.0
67 #define MIN_VPB_GAIN -12.0
70 #define DTMF_CID_START 'D'
71 #define DTMF_CID_STOP 'C'
74 #if defined(__cplusplus) || defined(c_plusplus)
79 static const char desc[] = "VoiceTronix V6PCI/V12PCI/V4PCI API Support";
80 static const char type[] = "vpb";
81 static const char tdesc[] = "Standard VoiceTronix API Driver";
82 static const char config[] = "vpb.conf";
84 /* Default context for dialtone mode */
85 static char context[AST_MAX_EXTENSION] = "default";
87 /* Default language */
88 static char language[MAX_LANGUAGE] = "";
91 static int gruntdetect_timeout = 3600000; /* Grunt detect timeout is 1hr. */
93 static const int prefformat = AST_FORMAT_SLINEAR;
95 AST_MUTEX_DEFINE_STATIC(usecnt_lock);
97 /* Protect the interface list (of vpb_pvt's) */
98 AST_MUTEX_DEFINE_STATIC(iflock);
100 /* Protect the monitoring thread, so only one process can kill or start it, and not
101 when it's doing something critical. */
102 AST_MUTEX_DEFINE_STATIC(monlock);
104 /* This is the thread for the monitor which checks for input on the channels
105 which are not currently in use. */
106 static pthread_t monitor_thread;
108 static int mthreadactive = -1; /* Flag for monitoring monitorthread.*/
111 static int restart_monitor(void);
113 /* The private structures of the VPB channels are
114 linked for selecting outgoing channels */
116 #define MODE_DIALTONE 1
117 #define MODE_IMMEDIATE 2
120 /* Pick a country or add your own! */
121 /* These are the tones that are played to the user */
123 /* #define TONES_USA */
126 static VPB_TONE Dialtone = {440, 440, 440, -10, -10, -10, 5000, 0 };
127 static VPB_TONE Busytone = {470, 0, 0, -10, -100, -100, 5000, 0 };
128 static VPB_TONE Ringbacktone = {400, 50, 440, -10, -10, -10, 1400, 800 };
131 static VPB_TONE Dialtone = {350, 440, 0, -16, -16, -100, 10000, 0};
132 static VPB_TONE Busytone = {480, 620, 0, -10, -10, -100, 500, 500};
133 static VPB_TONE Ringbacktone = {440, 480, 0, -20, -20, -100, 2000, 4000};
136 /* grunt tone defn's */
137 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 } } };
138 static VPB_DETECT toned_ungrunt = { 2, VPB_GRUNT, 1, 2000, 1, 0, 0, -40, 0, 0, 30, 40, { { 0, 0, 0, 0 } } };
140 /* Use loop drop detection */
141 static int UseLoopDrop=1;
143 /* To use or not to use Native bridging */
144 static int UseNativeBridge=1;
146 /* Use Asterisk Indication or VPB */
147 static int use_ast_ind=0;
149 /* Use Asterisk DTMF detection or VPB */
150 static int use_ast_dtmfdet=0;
152 static int relaxdtmf=0;
154 /* Use Asterisk DTMF play back or VPB */
155 static int use_ast_dtmf=0;
157 /* Break for DTMF on native bridge ? */
158 static int break_for_dtmf=1;
160 /* Set EC suppression threshold */
161 static int ec_supp_threshold=-1;
163 /* Inter Digit Delay for collecting DTMF's */
164 static int dtmf_idd = 3000;
166 #define TIMER_PERIOD_RINGBACK 2000
167 #define TIMER_PERIOD_BUSY 700
168 #define TIMER_PERIOD_RING 4000
170 #define VPB_EVENTS_ALL (VPB_MRING|VPB_MDIGIT|VPB_MDTMF|VPB_MTONEDETECT|VPB_MTIMEREXP|VPB_MPLAY_UNDERFLOW \
171 |VPB_MRECORD_OVERFLOW|VPB_MSTATION_OFFHOOK|VPB_MSTATION_ONHOOK \
172 |VPB_MRING_OFF|VPB_MDROP|VPB_MSTATION_FLASH)
173 #define VPB_EVENTS_NODROP (VPB_MRING|VPB_MDIGIT|VPB_MDTMF|VPB_MTONEDETECT|VPB_MTIMEREXP|VPB_MPLAY_UNDERFLOW \
174 |VPB_MRECORD_OVERFLOW|VPB_MSTATION_OFFHOOK|VPB_MSTATION_ONHOOK \
175 |VPB_MRING_OFF|VPB_MSTATION_FLASH)
176 #define VPB_EVENTS_NODTMF (VPB_MRING|VPB_MDIGIT|VPB_MTONEDETECT|VPB_MTIMEREXP|VPB_MPLAY_UNDERFLOW \
177 |VPB_MRECORD_OVERFLOW|VPB_MSTATION_OFFHOOK|VPB_MSTATION_ONHOOK \
178 |VPB_MRING_OFF|VPB_MDROP|VPB_MSTATION_FLASH)
179 #define VPB_EVENTS_STAT (VPB_MRING|VPB_MDIGIT|VPB_MDTMF|VPB_MTONEDETECT|VPB_MTIMEREXP|VPB_MPLAY_UNDERFLOW \
180 |VPB_MRECORD_OVERFLOW|VPB_MSTATION_OFFHOOK|VPB_MSTATION_ONHOOK \
181 |VPB_MRING_OFF|VPB_MSTATION_FLASH)
184 /* Dialing parameters for Australia */
185 /* #define DIAL_WITH_CALL_PROGRESS */
186 VPB_TONE_MAP DialToneMap[] = { { VPB_BUSY_AUST, VPB_CALL_DISCONNECT, 0 },
187 { VPB_DIAL, VPB_CALL_DIALTONE, 0 },
188 { VPB_RINGBACK_308, VPB_CALL_RINGBACK, 0 },
189 { VPB_BUSY_AUST, VPB_CALL_BUSY, 0 },
190 { VPB_GRUNT, VPB_CALL_GRUNT, 0 },
192 #define VPB_DIALTONE_WAIT 2000 /* Wait up to 2s for a dialtone */
193 #define VPB_RINGWAIT 4000 /* Wait up to 4s for ring tone after dialing */
194 #define VPB_CONNECTED_WAIT 4000 /* If no ring tone detected for 4s then consider call connected */
195 #define TIMER_PERIOD_NOANSWER 120000 /* Let it ring for 120s before deciding theres noone there */
197 #define MAX_BRIDGES_V4PCI 2
198 #define MAX_BRIDGES_V12PCI 128
201 #define VPB_STATE_ONHOOK 0
202 #define VPB_STATE_OFFHOOK 1
203 #define VPB_STATE_DIALLING 2
204 #define VPB_STATE_JOINED 3
205 #define VPB_STATE_GETDTMF 4
206 #define VPB_STATE_PLAYDIAL 5
207 #define VPB_STATE_PLAYBUSY 6
208 #define VPB_STATE_PLAYRING 7
210 #define VPB_GOT_RXHWG 1
211 #define VPB_GOT_TXHWG 2
212 #define VPB_GOT_RXSWG 4
213 #define VPB_GOT_TXSWG 8
217 struct ast_channel *c0, *c1, **rc;
218 struct ast_frame **fo;
225 static vpb_bridge_t * bridges;
226 static int max_bridges = MAX_BRIDGES_V4PCI;
228 AST_MUTEX_DEFINE_STATIC(bridge_lock);
231 vpb_model_unknown = 0,
236 static struct vpb_pvt {
238 ast_mutex_t owner_lock; /* Protect blocks that expect ownership to remain the same */
239 struct ast_channel *owner; /* Channel who owns us, possibly NULL */
241 int golock; /* Got owner lock ? */
243 int mode; /* fxo/imediate/dialtone*/
244 int handle; /* Handle for vpb interface */
246 int state; /* used to keep port state (internal to driver) */
248 int group; /* Which group this port belongs to */
249 ast_group_t callgroup; /* Call group */
250 ast_group_t pickupgroup; /* Pickup group */
253 char dev[256]; /* Device name, eg vpb/1-1 */
254 vpb_model_t vpb_model; /* card model */
256 struct ast_frame f, fr; /* Asterisk frame interface */
257 char buf[VPB_MAX_BUF]; /* Static buffer for reading frames */
259 int dialtone; /* NOT USED */
260 float txgain, rxgain; /* Hardware gain control */
261 float txswgain, rxswgain; /* Software gain control */
263 int wantdtmf; /* Waiting for DTMF. */
264 char context[AST_MAX_EXTENSION]; /* The context for this channel */
266 char ext[AST_MAX_EXTENSION]; /* DTMF buffer for the ext[ens] */
267 char language[MAX_LANGUAGE]; /* language being used */
268 char callerid[AST_MAX_EXTENSION]; /* CallerId used for directly connected phone */
269 int callerid_type; /* Caller ID type: 0=>none 1=>vpb 2=>AstV23 3=>AstBell */
271 int dtmf_caller_pos; /* DTMF CallerID detection (Brazil)*/
273 int lastoutput; /* Holds the last Audio format output'ed */
274 int lastinput; /* Holds the last Audio format input'ed */
275 int last_ignore_dtmf;
277 void *busy_timer; /* Void pointer for busy vpb_timer */
278 int busy_timer_id; /* unique timer ID for busy timer */
280 void *ringback_timer; /* Void pointer for ringback vpb_timer */
281 int ringback_timer_id; /* unique timer ID for ringback timer */
283 void *ring_timer; /* Void pointer for ring vpb_timer */
284 int ring_timer_id; /* unique timer ID for ring timer */
286 void *dtmfidd_timer; /* Void pointer for DTMF IDD vpb_timer */
287 int dtmfidd_timer_id; /* unique timer ID for DTMF IDD timer */
289 struct ast_dsp *vad; /* AST Voice Activation Detection dsp */
291 double lastgrunt; /* time stamp (secs since epoc) of last grunt event */
293 ast_mutex_t lock; /* This one just protects bridge ptr below */
294 vpb_bridge_t *bridge;
296 int stopreads; /* Stop reading...*/
297 int read_state; /* Read state */
298 int chuck_count; /* a count of packets weve chucked away!*/
299 pthread_t readthread; /* For monitoring read channel. One per owned channel. */
301 ast_mutex_t record_lock; /* This one prevents reentering a record_buf block */
302 ast_mutex_t play_lock; /* This one prevents reentering a play_buf block */
303 int play_buf_time; /* How long the last play_buf took */
305 ast_mutex_t play_dtmf_lock;
308 int faxhandled; /* has a fax tone been handled ? */
310 struct vpb_pvt *next; /* Next channel in list */
314 static struct ast_channel *vpb_new(struct vpb_pvt *i, int state, char *context);
315 static void *do_chanreads(void *pvt);
317 static struct ast_channel *vpb_request(const char *type, int format, void *data, int *cause);
318 static int vpb_digit(struct ast_channel *ast, char digit);
319 static int vpb_call(struct ast_channel *ast, char *dest, int timeout);
320 static int vpb_hangup(struct ast_channel *ast);
321 static int vpb_answer(struct ast_channel *ast);
322 static struct ast_frame *vpb_read(struct ast_channel *ast);
323 static int vpb_write(struct ast_channel *ast, struct ast_frame *frame);
324 static int vpb_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc);
325 static int vpb_indicate(struct ast_channel *ast, int condition);
326 static int vpb_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
328 static const struct ast_channel_tech vpb_tech = {
331 capabilities: AST_FORMAT_SLINEAR,
333 requester: vpb_request,
335 send_digit: vpb_digit,
346 indicate: vpb_indicate,
352 bridged_channel: NULL
355 /* Can't get vpb_bridge() working on v4pci without either a horrible
356 * high pitched feedback noise or bad hiss noise depending on gain settings
357 * Get asterisk to do the bridging
359 #define BAD_V4PCI_BRIDGE
361 /* This one enables a half duplex bridge which may be required to prevent high pitched
362 * feedback when getting asterisk to do the bridging and when using certain gain settings.
364 /* #define HALF_DUPLEX_BRIDGE */
366 /* This is the Native bridge code, which Asterisk will try before using its own bridging code */
367 static int vpb_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc)
369 struct vpb_pvt *p0 = (struct vpb_pvt *)c0->tech_pvt;
370 struct vpb_pvt *p1 = (struct vpb_pvt *)c1->tech_pvt;
373 struct ast_channel *cs[3];
374 struct ast_channel *who;
381 #ifdef BAD_V4PCI_BRIDGE
382 if(p0->vpb_model==vpb_model_v4pci)
385 if ( UseNativeBridge != 1){
390 ast_mutex_lock(&p0->lock);
391 ast_mutex_lock(&p1->lock);
394 /* Bridge channels, check if we can. I believe we always can, so find a slot.*/
396 ast_mutex_lock(&bridge_lock); {
397 for (i = 0; i < max_bridges; i++)
398 if (!bridges[i].inuse)
400 if (i < max_bridges) {
401 bridges[i].inuse = 1;
402 bridges[i].endbridge = 0;
403 bridges[i].flags = flags;
409 } ast_mutex_unlock(&bridge_lock);
411 if (i == max_bridges) {
412 ast_log(LOG_WARNING, "%s: vpb_bridge: Failed to bridge %s and %s!\n", p0->dev, c0->name, c1->name);
413 ast_mutex_unlock(&p0->lock);
414 ast_mutex_unlock(&p1->lock);
417 /* Set bridge pointers. You don't want to take these locks while holding bridge lock.*/
418 ast_mutex_lock(&p0->lock); {
419 p0->bridge = &bridges[i];
420 } ast_mutex_unlock(&p0->lock);
422 ast_mutex_lock(&p1->lock); {
423 p1->bridge = &bridges[i];
424 } ast_mutex_unlock(&p1->lock);
426 if (option_verbose>1)
427 ast_verbose(VERBOSE_PREFIX_2 "%s: vpb_bridge: Bridging call entered with [%s, %s]\n",p0->dev, c0->name, c1->name);
430 #ifdef HALF_DUPLEX_BRIDGE
432 if (option_verbose>1)
433 ast_verbose(VERBOSE_PREFIX_2 "%s: vpb_bridge: Starting half-duplex bridge [%s, %s]\n",p0->dev, c0->name, c1->name);
437 memset(p0->buf, 0, sizeof p0->buf);
438 memset(p1->buf, 0, sizeof p1->buf);
440 vpb_record_buf_start(p0->handle, VPB_ALAW);
441 vpb_record_buf_start(p1->handle, VPB_ALAW);
443 vpb_play_buf_start(p0->handle, VPB_ALAW);
444 vpb_play_buf_start(p1->handle, VPB_ALAW);
446 while( !bridges[i].endbridge ) {
447 struct vpb_pvt *from, *to;
455 vpb_record_buf_sync(from->handle, from->buf, VPB_SAMPLES);
456 vpb_play_buf_sync(to->handle, from->buf, VPB_SAMPLES);
459 vpb_record_buf_finish(p0->handle);
460 vpb_record_buf_finish(p1->handle);
462 vpb_play_buf_finish(p0->handle);
463 vpb_play_buf_finish(p1->handle);
465 if (option_verbose>1)
466 ast_verbose(VERBOSE_PREFIX_2 "%s: vpb_bridge: Finished half-duplex bridge [%s, %s]\n",p0->dev, c0->name, c1->name);
472 res = vpb_bridge(p0->handle, p1->handle, VPB_BRIDGE_ON, i+1 /* resource 1 & 2 only for V4PCI*/ );
474 /* pthread_cond_wait(&bridges[i].cond, &bridges[i].lock);*/ /* Wait for condition signal. */
475 while( !bridges[i].endbridge ) {
476 /* Are we really ment to be doing nothing ?!?! */
477 who = ast_waitfor_n(cs, 2, &to);
479 ast_log(LOG_DEBUG, "%s: vpb_bridge: Empty frame read...\n",p0->dev);
480 /* check for hangup / whentohangup */
481 if (ast_check_hangup(c0) || ast_check_hangup(c1))
486 if (!f || ((f->frametype == AST_FRAME_DTMF) &&
487 (((who == c0) && (flags & AST_BRIDGE_DTMF_CHANNEL_0)) ||
488 ((who == c1) && (flags & AST_BRIDGE_DTMF_CHANNEL_1))))) {
491 ast_log(LOG_DEBUG, "%s: vpb_bridge: Got a [%s]\n",p0->dev, f ? "digit" : "hangup");
493 if ((c0->tech_pvt == pvt0) && (!c0->_softhangup)) {
494 if (pr0->set_rtp_peer(c0, NULL, NULL, 0))
495 ast_log(LOG_WARNING, "Channel '%s' failed to revert\n", c0->name);
497 if ((c1->tech_pvt == pvt1) && (!c1->_softhangup)) {
498 if (pr1->set_rtp_peer(c1, NULL, NULL, 0))
499 ast_log(LOG_WARNING, "Channel '%s' failed to revert back\n", c1->name);
502 /* That's all we needed */
504 /* Check if we need to break */
508 else if ((f->frametype == AST_FRAME_DTMF) && ((f->subclass == '#')||(f->subclass == '*'))){
512 if ((f->frametype == AST_FRAME_DTMF) ||
513 (f->frametype == AST_FRAME_VOICE) ||
514 (f->frametype == AST_FRAME_VIDEO))
516 /* Forward voice or DTMF frames if they happen upon us */
517 /* Actually I dont think we want to forward on any frames!
520 } else if (who == c1) {
527 /* Swap priority not that it's a big deal at this point */
532 vpb_bridge(p0->handle, p1->handle, VPB_BRIDGE_OFF, i+1 /* resource 1 & 2 only for V4PCI*/ );
537 ast_mutex_lock(&bridge_lock); {
538 bridges[i].inuse = 0;
539 } ast_mutex_unlock(&bridge_lock);
545 if (option_verbose>1)
546 ast_verbose(VERBOSE_PREFIX_2 "Bridging call done with [%s, %s] => %d\n", c0->name, c1->name, res);
549 ast_mutex_unlock(&p0->lock);
550 ast_mutex_unlock(&p1->lock);
552 return (res==VPB_OK)?0:-1;
555 static double get_time_in_ms()
558 gettimeofday(&tv, NULL);
559 return ((double)tv.tv_sec*1000)+((double)tv.tv_usec/1000);
562 /* Caller ID can be located in different positions between the rings depending on your Telco
563 * Australian (Telstra) callerid starts 700ms after 1st ring and finishes 1.5s after first ring
564 * Use ANALYSE_CID to record rings and determine location of callerid
566 /* #define ANALYSE_CID */
567 #define RING_SKIP 300
568 #define CID_MSECS 2000
570 static void get_callerid(struct vpb_pvt *p)
572 short buf[CID_MSECS*8]; /* 8kHz sampling rate */
573 double cid_record_time;
575 struct ast_channel *owner = p->owner;
577 char * file="cidsams.wav";
580 if( ast_mutex_trylock(&p->record_lock) == 0 ) {
582 char callerid[AST_MAX_EXTENSION] = "";
584 cid_record_time = get_time_in_ms();
585 if (option_verbose>3)
586 ast_verbose(VERBOSE_PREFIX_4 "CID record - start\n");
588 /* Skip any trailing ringtone */
589 vpb_sleep(RING_SKIP);
591 if (option_verbose>3)
592 ast_verbose(VERBOSE_PREFIX_4 "CID record - skipped %fms trailing ring\n",
593 get_time_in_ms() - cid_record_time);
594 cid_record_time = get_time_in_ms();
596 /* Record bit between the rings which contains the callerid */
597 vpb_record_buf_start(p->handle, VPB_LINEAR);
598 rc = vpb_record_buf_sync(p->handle, (char*)buf, sizeof(buf));
599 vpb_record_buf_finish(p->handle);
601 vpb_wave_open_write(&ws, file, VPB_LINEAR);
602 vpb_wave_write(ws,(char*)buf,sizeof(buf));
603 vpb_wave_close_write(ws);
606 if (option_verbose>3)
607 ast_verbose(VERBOSE_PREFIX_4 "CID record - recorded %fms between rings\n",
608 get_time_in_ms() - cid_record_time);
610 ast_mutex_unlock(&p->record_lock);
613 ast_log(LOG_ERROR, "Failed to record caller id sample on %s\n", p->dev );
617 VPB_CID *cli_struct = new VPB_CID;
618 cli_struct->ra_cldn[0]=0;
619 cli_struct->ra_cn[0]=0;
620 /* This decodes FSK 1200baud type callerid */
621 if ((rc=vpb_cid_decode2(cli_struct, buf, CID_MSECS*8)) == VPB_OK ) {
623 if (owner->cid.cid_num)
624 free(owner->cid.cid_num);
625 owner->cid.cid_num=NULL;
626 if (owner->cid.cid_name)
627 free(owner->cid.cid_name);
628 owner->cid.cid_name=NULL;
631 if (cli_struct->ra_cldn[0]=='\0'){
633 owner->cid.cid_num = strdup(cli_struct->cldn);
634 owner->cid.cid_name = strdup(cli_struct->cn);
636 ast_set_callerid(owner, cli_struct->cldn, cli_struct->cn, cli_struct->cldn);
637 if (option_verbose>3)
638 ast_verbose(VERBOSE_PREFIX_4 "CID record - got [%s] [%s]\n",owner->cid.cid_num,owner->cid.cid_name );
639 snprintf(p->callerid,sizeof(p->callerid)-1,"%s %s",cli_struct->cldn,cli_struct->cn);
642 ast_log(LOG_ERROR,"CID record - No caller id avalable on %s \n", p->dev);
646 ast_log(LOG_ERROR, "CID record - Failed to decode caller id on %s - %s\n", p->dev, vpb_strerror(rc) );
647 strncpy(p->callerid,"unknown", sizeof(p->callerid) - 1);
652 ast_log(LOG_ERROR, "CID record - Failed to set record mode for caller id on %s\n", p->dev );
655 static void get_callerid_ast(struct vpb_pvt *p)
657 struct callerid_state *cs;
659 char *name=NULL, *number=NULL;
663 struct ast_channel *owner = p->owner;
667 char * file="cidsams.wav";
669 if(p->callerid_type == 1) {
670 if (option_verbose>3) ast_verbose(VERBOSE_PREFIX_4 "Collected caller ID already\n");
673 else if(p->callerid_type == 2 ) {
674 which_cid=CID_SIG_V23;
675 if (option_verbose>3) ast_verbose(VERBOSE_PREFIX_4 "Collecting Caller ID v23...\n");
677 else if(p->callerid_type == 3) {
678 which_cid=CID_SIG_BELL;
679 if (option_verbose>3) ast_verbose(VERBOSE_PREFIX_4 "Collecting Caller ID bell...\n");
682 if (option_verbose>3)
683 ast_verbose(VERBOSE_PREFIX_4 "Caller ID disabled\n");
686 /* vpb_sleep(RING_SKIP); */
687 /* vpb_record_get_gain(p->handle, &old_gain); */
688 cs = callerid_new(which_cid);
691 vpb_wave_open_write(&ws, file, VPB_MULAW);
692 vpb_record_set_gain(p->handle, 3.0);
693 vpb_record_set_hw_gain(p->handle,12.0);
695 vpb_record_buf_start(p->handle, VPB_MULAW);
696 while((rc == 0)&&(sam_count<8000*3)){
697 vrc = vpb_record_buf_sync(p->handle, (char*)buf, sizeof(buf));
699 ast_log(LOG_ERROR, "%s: Caller ID couldnt read audio buffer!\n",p->dev);
700 rc = callerid_feed(cs,(unsigned char *)buf,sizeof(buf),AST_FORMAT_ULAW);
702 vpb_wave_write(ws,(char*)buf,sizeof(buf));
704 sam_count+=sizeof(buf);
705 if (option_verbose>3) ast_verbose(VERBOSE_PREFIX_4 "Collecting Caller ID samples [%d][%d]...\n",sam_count,rc);
707 vpb_record_buf_finish(p->handle);
709 vpb_wave_close_write(ws);
712 callerid_get(cs, &name, &number, &flags);
713 if (option_verbose>0)
714 ast_verbose(VERBOSE_PREFIX_1 "%s: Caller ID name [%s] number [%s] flags [%d]\n",p->dev,name, number,flags);
717 ast_log(LOG_ERROR, "%s: Failed to decode Caller ID \n", p->dev );
719 /* vpb_record_set_gain(p->handle, old_gain); */
720 /* vpb_record_set_hw_gain(p->handle,6.0); */
723 ast_log(LOG_ERROR, "%s: Failed to create Caller ID struct\n", p->dev );
725 if (owner->cid.cid_num) {
726 free(owner->cid.cid_num);
727 owner->cid.cid_num = NULL;
729 if (owner->cid.cid_name) {
730 free(owner->cid.cid_name);
731 owner->cid.cid_name = NULL;
734 ast_shrink_phone_number(number);
735 if (number && !ast_strlen_zero(number)) {
736 owner->cid.cid_num = strdup(number);
737 owner->cid.cid_ani = strdup(number);
738 if (name && !ast_strlen_zero(name)){
739 owner->cid.cid_name = strdup(name);
740 snprintf(p->callerid,(sizeof(p->callerid)-1),"%s %s",number,name);
743 snprintf(p->callerid,(sizeof(p->callerid)-1),"%s",number);
751 /* Terminate any tones we are presently playing */
752 static void stoptone( int handle)
756 while(vpb_playtone_state(handle)!=VPB_OK){
757 vpb_tone_terminate(handle);
758 ret = vpb_get_event_ch_async(handle,&je);
759 if ((ret == VPB_OK)&&(je.type != VPB_DIALEND)){
760 if (option_verbose > 3){
761 ast_verbose(VERBOSE_PREFIX_4 "Stop tone collected a wrong event!![%d]\n",je.type);
763 /* vpb_put_event(&je); */
769 /* Safe vpb_playtone_async */
770 static int playtone( int handle, VPB_TONE *tone)
774 if (option_verbose > 3)
775 ast_verbose(VERBOSE_PREFIX_4 "[%02d]: Playing tone\n", handle);
776 ret = vpb_playtone_async(handle, tone);
780 static inline int monitor_handle_owned(struct vpb_pvt *p, VPB_EVENT *e)
782 struct ast_frame f = {AST_FRAME_CONTROL}; /* default is control, Clear rest. */
786 if (option_verbose > 3)
787 ast_verbose(VERBOSE_PREFIX_4 "%s: handle_owned: got event: [%d=>%d]\n", p->dev, e->type, e->data);
789 f.src = (char *)type;
792 if (p->mode == MODE_FXO) {
793 f.subclass = AST_CONTROL_RING;
794 vpb_timer_stop(p->ring_timer);
795 vpb_timer_start(p->ring_timer);
797 f.frametype = -1; /* ignore ring on station port. */
805 if (e->data == p->busy_timer_id) {
806 playtone(p->handle,&Busytone);
807 p->state = VPB_STATE_PLAYBUSY;
808 vpb_timer_stop(p->busy_timer);
809 vpb_timer_start(p->busy_timer);
811 } else if (e->data == p->ringback_timer_id) {
812 playtone(p->handle, &Ringbacktone);
813 vpb_timer_stop(p->ringback_timer);
814 vpb_timer_start(p->ringback_timer);
816 } else if (e->data == p->ring_timer_id) {
817 /* We didnt get another ring in time! */
818 if (p->owner->_state != AST_STATE_UP) {
819 /* Assume caller has hung up */
820 vpb_timer_stop(p->ring_timer);
821 f.subclass = AST_CONTROL_HANGUP;
823 vpb_timer_stop(p->ring_timer);
828 f.frametype = -1; /* Ignore. */
834 if (use_ast_dtmfdet){
836 } else if (p->owner->_state == AST_STATE_UP) {
837 f.frametype = AST_FRAME_DTMF;
838 f.subclass = e->data;
844 if (e->data == VPB_BUSY || e->data == VPB_BUSY_308 || e->data == VPB_BUSY_AUST ) {
845 if (option_verbose > 3)
846 ast_verbose(VERBOSE_PREFIX_4 "%s: handle_owned: got event: BUSY\n", p->dev);
847 if (p->owner->_state == AST_STATE_UP) {
848 f.subclass = AST_CONTROL_HANGUP;
851 f.subclass = AST_CONTROL_BUSY;
854 else if (e->data == VPB_FAX){
856 if (strcmp(p->owner->exten, "fax")) {
857 if (ast_exists_extension(p->owner, ast_strlen_zero(p->owner->macrocontext) ? p->owner->context : p->owner->macrocontext, "fax", 1, p->owner->cid.cid_num)) {
858 if (option_verbose > 2)
859 ast_verbose(VERBOSE_PREFIX_3 "Redirecting %s to fax extension\n", p->owner->name);
860 /* Save the DID/DNIS when we transfer the fax call to a "fax" extension */
861 pbx_builtin_setvar_helper(p->owner,"FAXEXTEN",p->owner->exten);
862 if (ast_async_goto(p->owner, p->owner->context, "fax", 1))
863 ast_log(LOG_WARNING, "Failed to async goto '%s' into fax of '%s'\n", p->owner->name, p->owner->context);
865 ast_log(LOG_NOTICE, "Fax detected, but no fax extension\n");
867 ast_log(LOG_DEBUG, "Already in a fax extension, not redirecting\n");
869 ast_log(LOG_DEBUG, "Fax already handled\n");
872 else if (e->data == VPB_GRUNT) {
873 if( ( get_time_in_ms() - p->lastgrunt ) > gruntdetect_timeout ) {
874 /* Nothing heard on line for a very long time
875 * Timeout connection */
876 if (option_verbose > 2)
877 ast_verbose(VERBOSE_PREFIX_3 "grunt timeout\n");
878 ast_log(LOG_NOTICE,"%s: Line hangup due of lack of conversation\n",p->dev);
879 f.subclass = AST_CONTROL_HANGUP;
881 p->lastgrunt = get_time_in_ms();
891 #ifdef DIAL_WITH_CALL_PROGRESS
892 if (e->data == VPB_CALL_CONNECTED)
893 f.subclass = AST_CONTROL_ANSWER;
894 else if (e->data == VPB_CALL_NO_DIAL_TONE || e->data == VPB_CALL_NO_RING_BACK)
895 f.subclass = AST_CONTROL_CONGESTION;
896 else if (e->data == VPB_CALL_NO_ANSWER || e->data == VPB_CALL_BUSY)
897 f.subclass = AST_CONTROL_BUSY;
898 else if (e->data == VPB_CALL_DISCONNECTED)
899 f.subclass = AST_CONTROL_HANGUP;
901 ast_log(LOG_NOTICE,"%s: Got call progress callback but blind dialing \n", p->dev);
906 case VPB_STATION_OFFHOOK:
907 f.subclass = AST_CONTROL_ANSWER;
911 if ((p->mode == MODE_FXO)&&(UseLoopDrop)){ /* ignore loop drop on stations */
912 if (p->owner->_state == AST_STATE_UP)
913 f.subclass = AST_CONTROL_HANGUP;
918 case VPB_STATION_ONHOOK:
919 f.subclass = AST_CONTROL_HANGUP;
922 case VPB_STATION_FLASH:
923 f.subclass = AST_CONTROL_FLASH;
926 /* Called when dialing has finished and ringing starts
927 * No indication that call has really been answered when using blind dialing
931 f.subclass = AST_CONTROL_ANSWER;
932 if (option_verbose > 1)
933 ast_verbose(VERBOSE_PREFIX_2 "%s: Dialend\n", p->dev);
939 case VPB_PLAY_UNDERFLOW:
941 vpb_reset_play_fifo_alarm(p->handle);
944 case VPB_RECORD_OVERFLOW:
946 vpb_reset_record_fifo_alarm(p->handle);
955 if (option_verbose > 3) ast_verbose("%s: LOCKING in handle_owned [%d]\n", p->dev,res);
956 res = ast_mutex_lock(&p->lock);
957 if (option_verbose > 3) ast_verbose("%s: LOCKING count[%d] owner[%d] \n", p->dev, p->lock.__m_count,p->lock.__m_owner);
960 if (p->bridge) { /* Check what happened, see if we need to report it. */
961 switch (f.frametype) {
963 if ( !(p->bridge->c0 == p->owner &&
964 (p->bridge->flags & AST_BRIDGE_DTMF_CHANNEL_0) ) &&
965 !(p->bridge->c1 == p->owner &&
966 (p->bridge->flags & AST_BRIDGE_DTMF_CHANNEL_1) ))
967 /* Kill bridge, this is interesting. */
971 case AST_FRAME_CONTROL:
972 if (!(p->bridge->flags & AST_BRIDGE_IGNORE_SIGS))
974 if (f.subclass == AST_CONTROL_BUSY ||
975 f.subclass == AST_CONTROL_CONGESTION ||
976 f.subclass == AST_CONTROL_HANGUP ||
977 f.subclass == AST_CONTROL_FLASH)
987 *p->bridge->fo = ast_frisolate(&f);
989 *p->bridge->rc = p->owner;
991 ast_mutex_lock(&p->bridge->lock); {
992 p->bridge->endbridge = 1;
993 pthread_cond_signal(&p->bridge->cond);
994 } ast_mutex_unlock(&p->bridge->lock);
1000 res = ast_mutex_unlock(&p->lock);
1002 if (option_verbose > 3) ast_verbose("%s: unLOCKING in handle_owned [%d]\n", p->dev,res);
1007 if (option_verbose > 3)
1008 ast_verbose(VERBOSE_PREFIX_4 "%s: handle_owned: Prepared frame type[%d]subclass[%d], bridge=%p owner=[%s]\n",
1009 p->dev, f.frametype, f.subclass, (void *)p->bridge, p->owner->name);
1011 /* Trylock used here to avoid deadlock that can occur if we
1012 * happen to be in here handling an event when hangup is called
1013 * Problem is that hangup holds p->owner->lock
1015 if ((f.frametype >= 0)&& (f.frametype != AST_FRAME_NULL)&&(p->owner)) {
1016 if (ast_mutex_trylock(&p->owner->lock)==0) {
1017 ast_queue_frame(p->owner, &f);
1018 ast_mutex_unlock(&p->owner->lock);
1019 if (option_verbose > 3)
1020 ast_verbose(VERBOSE_PREFIX_4 "%s: handled_owned: Queued Frame to [%s]\n", p->dev,p->owner->name);
1022 ast_verbose("%s: handled_owned: Missed event %d/%d \n",
1023 p->dev,f.frametype, f.subclass);
1026 res = ast_mutex_unlock(&p->lock);
1028 if (option_verbose > 3) ast_verbose("%s: unLOCKING in handle_owned [%d]\n", p->dev,res);
1034 static inline int monitor_handle_notowned(struct vpb_pvt *p, VPB_EVENT *e)
1037 struct ast_channel *owner = p->owner;
1040 struct ast_channel *c;
1042 if (option_verbose > 3) {
1043 char str[VPB_MAX_STR];
1044 vpb_translate_event(e, str);
1045 ast_verbose(VERBOSE_PREFIX_4 "%s: handle_notowned: mode=%d, event[%d][%s]=[%d]\n",
1046 p->dev, p->mode, e->type,str, e->data);
1051 if (p->mode == MODE_FXO) /* FXO port ring, start * */ {
1052 vpb_new(p, AST_STATE_RING, p->context);
1053 if(p->callerid_type == 1) {
1054 if (option_verbose>3)
1055 ast_verbose(VERBOSE_PREFIX_4 "Using VPB Caller ID\n");
1056 get_callerid(p); /* Australian Caller ID only between 1st and 2nd ring */
1058 get_callerid_ast(p); /* Caller ID using the ast functions */
1059 vpb_timer_stop(p->ring_timer);
1060 vpb_timer_start(p->ring_timer);
1067 case VPB_STATION_OFFHOOK:
1068 if (p->mode == MODE_IMMEDIATE)
1069 vpb_new(p,AST_STATE_RING, p->context);
1071 ast_verbose(VERBOSE_PREFIX_4 "%s: handle_notowned: playing dialtone\n",p->dev);
1072 playtone(p->handle, &Dialtone);
1073 p->state=VPB_STATE_PLAYDIAL;
1075 p->ext[0] = 0; /* Just to be sure & paranoid.*/
1080 if (p->mode == MODE_DIALTONE){
1081 if (p->state == VPB_STATE_PLAYDIAL) {
1082 playtone(p->handle, &Dialtone);
1084 p->ext[0] = 0; /* Just to be sure & paranoid. */
1086 /* These are not needed as they have timers to restart them
1087 else if (p->state == VPB_STATE_PLAYBUSY) {
1088 playtone(p->handle, &Busytone);
1092 else if (p->state == VPB_STATE_PLAYRING) {
1093 playtone(p->handle, &Ringbacktone);
1099 ast_verbose(VERBOSE_PREFIX_4 "%s: handle_notowned: Got a DIALEND when not really expected\n",p->dev);
1103 case VPB_STATION_ONHOOK: /* clear ext */
1104 stoptone(p->handle);
1107 p->state=VPB_STATE_ONHOOK;
1110 if (e->data == p->dtmfidd_timer_id) {
1111 if (ast_exists_extension(NULL, p->context, p->ext, 1, p->callerid)){
1112 if (option_verbose > 3)
1113 ast_verbose(VERBOSE_PREFIX_4 "%s: handle_notowned: DTMF IDD timer out, matching on [%s] in [%s]\n", p->dev,p->ext , p->context);
1115 vpb_new(p,AST_STATE_RING, p->context);
1117 } else if (e->data == p->ring_timer_id) {
1118 /* We didnt get another ring in time! */
1119 if (p->owner->_state != AST_STATE_UP) {
1120 /* Assume caller has hung up */
1121 vpb_timer_stop(p->ring_timer);
1127 if (p->state == VPB_STATE_ONHOOK){
1128 /* DTMF's being passed while on-hook maybe Caller ID */
1129 if ( p->mode == MODE_FXO ) {
1130 if ( e->data == DTMF_CID_START ) { /* CallerID Start signal */
1131 p->dtmf_caller_pos = 0; /* Leaves the first digit out */
1132 memset(p->callerid,0,AST_MAX_EXTENSION);
1134 else if ( e->data == DTMF_CID_STOP ) { /* CallerID End signal */
1135 p->callerid[p->dtmf_caller_pos] = '\0';
1136 if (option_verbose > 2)
1137 ast_verbose(VERBOSE_PREFIX_3 " %s: DTMF CallerID %s\n",p->dev,p->callerid);
1140 if (owner->cid.cid_num)
1141 free(owner->cid.cid_num);
1142 owner->cid.cid_num=NULL;
1143 if (owner->cid.cid_name)
1144 free(owner->cid.cid_name);
1145 owner->cid.cid_name=NULL;
1146 owner->cid.cid_num = strdup(p->callerid);
1150 ast_callerid_split(p->callerid, cid_name, sizeof(cid_name), cid_num, sizeof(cid_num));
1151 ast_set_callerid(owner, cid_num, cid_name, cid_num);
1155 if (option_verbose > 2)
1156 ast_verbose(VERBOSE_PREFIX_3 " %s: DTMF CallerID: no owner to assign CID \n",p->dev);
1158 } else if ( p->dtmf_caller_pos < AST_MAX_EXTENSION ) {
1159 if ( p->dtmf_caller_pos >= 0 )
1160 p->callerid[p->dtmf_caller_pos] = e->data;
1161 p->dtmf_caller_pos++;
1166 if (p->wantdtmf == 1) {
1167 stoptone(p->handle);
1170 p->state=VPB_STATE_GETDTMF;
1172 strncat(p->ext, s, sizeof(p->ext) - strlen(p->ext) - 1);
1174 if (!strcmp(p->ext,ast_pickup_ext())) {
1175 /* Call pickup has been dialled! */
1176 if (ast_pickup_call(c)) {
1177 /* Call pickup wasnt possible */
1182 if (ast_exists_extension(NULL, p->context, p->ext, 1, p->callerid)){
1183 if ( ast_canmatch_extension(NULL, p->context, p->ext, 1, p->callerid)){
1184 if (option_verbose > 3)
1185 ast_verbose(VERBOSE_PREFIX_4 "%s: handle_notowned: Multiple matches on [%s] in [%s]\n", p->dev,p->ext , p->context);
1186 /* Start DTMF IDD timer */
1187 vpb_timer_stop(p->dtmfidd_timer);
1188 vpb_timer_start(p->dtmfidd_timer);
1191 if (option_verbose > 3)
1192 ast_verbose(VERBOSE_PREFIX_4 "%s: handle_notowned: Matched on [%s] in [%s]\n", p->dev,p->ext , p->context);
1193 vpb_new(p,AST_STATE_UP, p->context);
1195 } else if (!ast_canmatch_extension(NULL, p->context, p->ext, 1, p->callerid)){
1196 if (ast_exists_extension(NULL, "default", p->ext, 1, p->callerid)) {
1197 vpb_new(p,AST_STATE_UP, "default");
1198 } else if (!ast_canmatch_extension(NULL, "default", p->ext, 1, p->callerid)) {
1199 if (option_verbose > 3) {
1200 ast_verbose(VERBOSE_PREFIX_4 "%s: handle_notowned: can't match anything in %s or default\n", p->dev, p->context);
1202 playtone(p->handle, &Busytone);
1203 vpb_timer_stop(p->busy_timer);
1204 vpb_timer_start(p->busy_timer);
1205 p->state = VPB_STATE_PLAYBUSY;
1215 if (option_verbose > 3)
1216 ast_verbose(VERBOSE_PREFIX_4 "%s: handle_notowned: mode=%d, [%d=>%d]\n",
1217 p->dev, p->mode, e->type, e->data);
1222 static void *do_monitor(void *unused)
1225 /* Monitor thread, doesn't die until explicitly killed. */
1227 if (option_verbose > 1)
1228 ast_verbose(VERBOSE_PREFIX_2 "Starting vpb monitor thread[%ld]\n",
1231 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
1236 char str[VPB_MAX_STR];
1240 if (option_verbose > 3)
1241 ast_verbose(VERBOSE_PREFIX_4 "Monitor waiting for event\n");
1244 int res = vpb_get_event_sync(&e, VPB_WAIT_TIMEOUT);
1245 if( (res==VPB_NO_EVENTS) || (res==VPB_TIME_OUT) ){
1247 if (option_verbose > 3){
1248 if (res == VPB_NO_EVENTS){
1249 ast_verbose(VERBOSE_PREFIX_4 "No events....\n");
1251 ast_verbose(VERBOSE_PREFIX_4 "No events, timed out....\n");
1258 if (res != VPB_OK) {
1259 ast_log(LOG_ERROR,"Monitor get event error %s\n", vpb_strerror(res) );
1260 ast_verbose("Monitor get event error %s\n", vpb_strerror(res) );
1268 ast_mutex_lock(&monlock); {
1270 if (e.type == VPB_NULL_EVENT) {
1271 if (option_verbose > 3)
1272 ast_verbose(VERBOSE_PREFIX_4 "Monitor got null event\n");
1275 vpb_translate_event(&e, str);
1277 str[(strlen(str)-1)]='\0';
1280 ast_mutex_lock(&iflock); {
1282 while (p && p->handle != e.handle)
1284 } ast_mutex_unlock(&iflock);
1286 if (p && (option_verbose > 3))
1287 ast_verbose(VERBOSE_PREFIX_4 "%s: Event [%d=>%s] \n",
1288 p ? p->dev : "null", e.type, str );
1291 } ast_mutex_unlock(&monlock);
1294 if (e.type != VPB_NULL_EVENT){
1295 ast_log(LOG_WARNING, "Got event [%s][%d], no matching iface!\n", str,e.type);
1296 if (option_verbose > 3){
1297 ast_verbose(VERBOSE_PREFIX_4 "vpb/ERR: No interface for Event [%d=>%s] \n",e.type,str );
1303 /* flush the event from the channel event Q */
1304 vpb_get_event_ch_async(e.handle,&je);
1305 if (option_verbose > 4){
1306 vpb_translate_event(&je, str);
1307 ast_verbose("%s: Flushing event [%d]=>%s\n",p->dev,je.type,str);
1310 /* Check for ownership and locks */
1311 if ((p->owner)&&(!p->golock)){
1312 /* Need to get owner lock */
1313 /* Safely grab both p->lock and p->owner->lock so that there
1314 cannot be a race with something from the other side */
1316 ast_mutex_lock(&p->lock);
1317 while(ast_mutex_trylock(&p->owner->lock)) {
1318 ast_mutex_unlock(&p->lock);
1320 ast_mutex_lock(&p->lock);
1328 /* Two scenarios: Are you owned or not. */
1330 monitor_handle_owned(p, &e);
1332 monitor_handle_notowned(p, &e);
1334 /* if ((!p->owner)&&(p->golock)){
1335 ast_mutex_unlock(&p->owner->lock);
1336 ast_mutex_unlock(&p->lock);
1345 static int restart_monitor(void)
1349 /* If we're supposed to be stopped -- stay stopped */
1350 if (mthreadactive == -2)
1353 if (option_verbose > 3)
1354 ast_verbose(VERBOSE_PREFIX_4 "Restarting monitor\n");
1356 ast_mutex_lock(&monlock); {
1357 if (monitor_thread == pthread_self()) {
1358 ast_log(LOG_WARNING, "Cannot kill myself\n");
1360 if (option_verbose > 3)
1361 ast_verbose(VERBOSE_PREFIX_4 "Monitor trying to kill monitor\n");
1364 if (mthreadactive != -1) {
1365 /* Why do other drivers kill the thread? No need says I, simply awake thread with event. */
1368 e.type = VPB_NULL_EVENT;
1371 if (option_verbose > 3)
1372 ast_verbose(VERBOSE_PREFIX_4 "Trying to reawake monitor\n");
1376 /* Start a new monitor */
1377 int pid = ast_pthread_create(&monitor_thread, NULL, do_monitor, NULL);
1378 if (option_verbose > 3)
1379 ast_verbose(VERBOSE_PREFIX_4 "Created new monitor thread %d\n",pid);
1381 ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
1384 mthreadactive = 0; /* Started the thread!*/
1387 } ast_mutex_unlock(&monlock);
1389 if (option_verbose > 3)
1390 ast_verbose(VERBOSE_PREFIX_4 "Monitor restarted\n");
1395 /* Per board config that must be called after vpb_open() */
1396 static void mkbrd(vpb_model_t model, int echo_cancel)
1399 if(model==vpb_model_v4pci)
1400 max_bridges = MAX_BRIDGES_V4PCI;
1401 bridges = (vpb_bridge_t *)malloc(max_bridges * sizeof(vpb_bridge_t) );
1403 ast_log(LOG_ERROR, "Failed to initialize bridges\n");
1405 memset(bridges,0,max_bridges * sizeof(vpb_bridge_t));
1406 for(int i = 0; i < max_bridges; i++ ) {
1407 ast_mutex_init(&bridges[i].lock);
1408 pthread_cond_init(&bridges[i].cond, NULL);
1413 if (model==vpb_model_v4pci) {
1414 vpb_echo_canc_disable();
1415 ast_log(LOG_NOTICE, "Voicetronix echo cancellation OFF\n");
1418 /* need to it port by port for OpenSwitch*/
1421 if (model==vpb_model_v4pci) {
1422 vpb_echo_canc_enable();
1423 ast_log(LOG_NOTICE, "Voicetronix echo cancellation ON\n");
1424 if (ec_supp_threshold > -1){
1425 vpb_echo_canc_set_sup_thresh((short *)&ec_supp_threshold);
1426 ast_log(LOG_NOTICE, "Voicetronix EC Sup Thres set\n");
1430 /* need to it port by port for OpenSwitch*/
1435 static struct vpb_pvt *mkif(int board, int channel, int mode, int gains, float txgain, float rxgain,
1436 float txswgain, float rxswgain, int bal1, int bal2, int bal3,
1437 char * callerid, int echo_cancel, int group, ast_group_t callgroup, ast_group_t pickupgroup )
1439 struct vpb_pvt *tmp;
1442 tmp = (struct vpb_pvt *)calloc(1, sizeof *tmp);
1447 tmp->handle = vpb_open(board, channel);
1449 if (tmp->handle < 0) {
1450 ast_log(LOG_WARNING, "Unable to create channel vpb/%d-%d: %s\n",
1451 board, channel, strerror(errno));
1456 snprintf(tmp->dev, sizeof(tmp->dev), "vpb/%d-%d", board, channel);
1461 tmp->callgroup = callgroup;
1462 tmp->pickupgroup = pickupgroup;
1464 /* Initilize dtmf caller ID position variable */
1465 tmp->dtmf_caller_pos=0;
1467 strncpy(tmp->language, language, sizeof(tmp->language) - 1);
1468 strncpy(tmp->context, context, sizeof(tmp->context) - 1);
1470 tmp->callerid_type=0;
1472 if (strcasecmp(callerid,"on")==0){
1473 tmp->callerid_type =1;
1474 strncpy(tmp->callerid, "unknown", sizeof(tmp->callerid) - 1);
1476 else if (strcasecmp(callerid,"v23")==0){
1477 tmp->callerid_type =2;
1478 strncpy(tmp->callerid, "unknown", sizeof(tmp->callerid) - 1);
1480 else if (strcasecmp(callerid,"bell")==0){
1481 tmp->callerid_type =3;
1482 strncpy(tmp->callerid, "unknown", sizeof(tmp->callerid) - 1);
1485 strncpy(tmp->callerid, callerid, sizeof(tmp->callerid) - 1);
1488 strncpy(tmp->callerid, "unknown", sizeof(tmp->callerid) - 1);
1491 /* check if codec balances have been set in the config file */
1493 if ((bal1>=0) && !(bal1 & 32)) bal1 |= 32;
1494 vpb_set_codec_reg(tmp->handle, 0x42, bal3);
1496 if(bal1>=0) vpb_set_codec_reg(tmp->handle, 0x32, bal1);
1497 if(bal2>=0) vpb_set_codec_reg(tmp->handle, 0x3a, bal2);
1499 if (gains & VPB_GOT_TXHWG){
1500 if (txgain > MAX_VPB_GAIN){
1501 tmp->txgain = MAX_VPB_GAIN;
1503 else if (txgain < MIN_VPB_GAIN){
1504 tmp->txgain = MIN_VPB_GAIN;
1507 tmp->txgain = txgain;
1510 ast_log(LOG_NOTICE,"VPB setting Tx Hw gain to [%f]\n",tmp->txgain);
1511 vpb_play_set_hw_gain(tmp->handle, tmp->txgain);
1514 if (gains & VPB_GOT_RXHWG){
1515 if (rxgain > MAX_VPB_GAIN){
1516 tmp->rxgain = MAX_VPB_GAIN;
1518 else if (rxgain < MIN_VPB_GAIN){
1519 tmp->rxgain = MIN_VPB_GAIN;
1522 tmp->rxgain = rxgain;
1524 ast_log(LOG_NOTICE,"VPB setting Rx Hw gain to [%f]\n",tmp->rxgain);
1525 vpb_record_set_hw_gain(tmp->handle,tmp->rxgain);
1528 if (gains & VPB_GOT_TXSWG){
1529 tmp->txswgain = txswgain;
1530 ast_log(LOG_NOTICE,"VPB setting Tx Sw gain to [%f]\n",tmp->txswgain);
1531 vpb_play_set_gain(tmp->handle, tmp->txswgain);
1534 if (gains & VPB_GOT_RXSWG){
1535 tmp->rxswgain = rxswgain;
1536 ast_log(LOG_NOTICE,"VPB setting Rx Sw gain to [%f]\n",tmp->rxswgain);
1537 vpb_record_set_gain(tmp->handle, tmp->rxswgain);
1540 tmp->vpb_model = vpb_model_unknown;
1541 if( vpb_get_model(buf) == VPB_OK ) {
1542 if(strcmp(buf,"V12PCI")==0)
1543 tmp->vpb_model = vpb_model_v12pci;
1544 else if(strcmp(buf,"VPB4")==0)
1545 tmp->vpb_model = vpb_model_v4pci;
1548 ast_mutex_init(&tmp->owner_lock);
1549 ast_mutex_init(&tmp->lock);
1550 ast_mutex_init(&tmp->record_lock);
1551 ast_mutex_init(&tmp->play_lock);
1552 ast_mutex_init(&tmp->play_dtmf_lock);
1554 /* set default read state */
1555 tmp->read_state = 0;
1559 tmp->busy_timer_id = vpb_timer_get_unique_timer_id();
1560 vpb_timer_open(&tmp->busy_timer, tmp->handle, tmp->busy_timer_id, TIMER_PERIOD_BUSY);
1562 tmp->ringback_timer_id = vpb_timer_get_unique_timer_id();
1563 vpb_timer_open(&tmp->ringback_timer, tmp->handle, tmp->ringback_timer_id, TIMER_PERIOD_RINGBACK);
1565 tmp->ring_timer_id = vpb_timer_get_unique_timer_id();
1566 vpb_timer_open(&tmp->ring_timer, tmp->handle, tmp->ring_timer_id, TIMER_PERIOD_RING);
1568 tmp->dtmfidd_timer_id = vpb_timer_get_unique_timer_id();
1569 vpb_timer_open(&tmp->dtmfidd_timer, tmp->handle, tmp->dtmfidd_timer_id, dtmf_idd);
1571 if (mode == MODE_FXO){
1572 if (use_ast_dtmfdet)
1573 vpb_set_event_mask(tmp->handle, VPB_EVENTS_NODTMF );
1575 vpb_set_event_mask(tmp->handle, VPB_EVENTS_ALL );
1579 if (use_ast_dtmfdet)
1580 vpb_set_event_mask(tmp->handle, VPB_EVENTS_NODTMF );
1583 vpb_set_event_mask(tmp->handle, VPB_EVENTS_STAT );
1586 if ((tmp->vpb_model == vpb_model_v12pci) && (echo_cancel)){
1587 vpb_hostecho_on(tmp->handle);
1589 if (use_ast_dtmfdet) {
1590 tmp->vad = ast_dsp_new();
1591 ast_dsp_set_features(tmp->vad, DSP_FEATURE_DTMF_DETECT);
1592 ast_dsp_digitmode(tmp->vad, DSP_DIGITMODE_DTMF);
1594 ast_dsp_digitmode(tmp->vad, DSP_DIGITMODE_DTMF|DSP_DIGITMODE_RELAXDTMF);
1600 /* define grunt tone */
1601 vpb_settonedet(tmp->handle,&toned_ungrunt);
1603 ast_log(LOG_NOTICE,"Voicetronix %s channel %s initialized (rxsg=%f/txsg=%f/rxhg=%f/txhg=%f)(0x%x/0x%x/0x%x)\n",
1604 (tmp->vpb_model==vpb_model_v4pci)?"V4PCI": (tmp->vpb_model==vpb_model_v12pci)?"V12PCI":"[Unknown model]",
1605 tmp->dev, tmp->rxswgain, tmp->txswgain, tmp->rxgain, tmp->txgain, bal1, bal2, bal3 );
1610 static int vpb_indicate(struct ast_channel *ast, int condition)
1612 struct vpb_pvt *p = (struct vpb_pvt *)ast->tech_pvt;
1619 if (option_verbose > 3)
1620 ast_verbose(VERBOSE_PREFIX_4 "%s: vpb_indicate [%d] state[%d]\n", p->dev, condition,ast->_state);
1622 if (ast->_state != AST_STATE_UP) {
1623 ast_verbose(VERBOSE_PREFIX_4 "%s: vpb_indicate Not in AST_STATE_UP\n", p->dev, condition,ast->_state);
1629 if (option_verbose > 3) ast_verbose("%s: LOCKING in indicate \n", p->dev);
1630 if (option_verbose > 3) ast_verbose("%s: LOCKING count[%d] owner[%d] \n", p->dev, p->lock.__m_count,p->lock.__m_owner);
1632 ast_mutex_lock(&p->lock);
1634 case AST_CONTROL_BUSY:
1635 case AST_CONTROL_CONGESTION:
1636 if (ast->_state == AST_STATE_UP) {
1637 playtone(p->handle, &Busytone);
1638 p->state = VPB_STATE_PLAYBUSY;
1639 vpb_timer_stop(p->busy_timer);
1640 vpb_timer_start(p->busy_timer);
1643 case AST_CONTROL_RINGING:
1644 if (ast->_state == AST_STATE_UP) {
1645 playtone(p->handle, &Ringbacktone);
1646 p->state = VPB_STATE_PLAYRING;
1647 if (option_verbose > 3)
1648 ast_verbose(VERBOSE_PREFIX_4 "%s: vpb indicate: setting ringback timer [%d]\n", p->dev,p->ringback_timer_id);
1650 vpb_timer_stop(p->ringback_timer);
1651 vpb_timer_start(p->ringback_timer);
1654 case AST_CONTROL_ANSWER:
1655 case -1: /* -1 means stop playing? */
1656 vpb_timer_stop(p->ringback_timer);
1657 vpb_timer_stop(p->busy_timer);
1658 stoptone(p->handle);
1660 case AST_CONTROL_HANGUP:
1661 if (ast->_state == AST_STATE_UP) {
1662 playtone(p->handle, &Busytone);
1663 p->state = VPB_STATE_PLAYBUSY;
1664 vpb_timer_stop(p->busy_timer);
1665 vpb_timer_start(p->busy_timer);
1673 tmp = ast_mutex_unlock(&p->lock);
1675 if (option_verbose > 3) ast_verbose("%s: unLOCKING in indicate [%d]\n", p->dev,tmp);
1680 static int vpb_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
1682 struct vpb_pvt *p = (struct vpb_pvt *)newchan->tech_pvt;
1686 if (option_verbose > 3) ast_verbose("%s: LOCKING in fixup \n", p->dev);
1687 if (option_verbose > 3) ast_verbose("%s: LOCKING count[%d] owner[%d] \n", p->dev, p->lock.__m_count,p->lock.__m_owner);
1689 ast_mutex_lock(&p->lock);
1690 ast_log(LOG_DEBUG, "New owner for channel %s is %s\n", p->dev, newchan->name);
1692 if (p->owner == oldchan) {
1696 if (newchan->_state == AST_STATE_RINGING){
1697 if (use_ast_ind == 1)
1698 ast_indicate(newchan, AST_CONTROL_RINGING);
1700 vpb_indicate(newchan, AST_CONTROL_RINGING);
1703 res= ast_mutex_unlock(&p->lock);
1705 if (option_verbose > 3) ast_verbose("%s: unLOCKING in fixup [%d]\n", p->dev,res);
1710 static int vpb_digit(struct ast_channel *ast, char digit)
1712 struct vpb_pvt *p = (struct vpb_pvt *)ast->tech_pvt;
1720 if (option_verbose > 3) ast_verbose("%s: LOCKING in digit \n", p->dev);
1721 if (option_verbose > 3) ast_verbose("%s: LOCKING count[%d] owner[%d] \n", p->dev, p->lock.__m_count,p->lock.__m_owner);
1723 ast_mutex_lock(&p->lock);
1729 if (option_verbose > 3)
1730 ast_verbose(VERBOSE_PREFIX_4 "%s: vpb_digit: asked to play digit[%s]\n", p->dev, s);
1732 ast_mutex_lock(&p->play_dtmf_lock);
1733 strncat(p->play_dtmf,s,sizeof(*p->play_dtmf));
1734 ast_mutex_unlock(&p->play_dtmf_lock);
1736 res = ast_mutex_unlock(&p->lock);
1738 if (option_verbose > 3) ast_verbose("%s: unLOCKING in digit [%d]\n", p->dev,res);
1743 /* Places a call out of a VPB channel */
1744 static int vpb_call(struct ast_channel *ast, char *dest, int timeout)
1746 struct vpb_pvt *p = (struct vpb_pvt *)ast->tech_pvt;
1748 char *s = strrchr(dest, '/');
1749 char dialstring[254] = "";
1753 if (option_verbose > 3) ast_verbose("%s: LOCKING in call \n", p->dev);
1754 if (option_verbose > 3) ast_verbose("%s: LOCKING count[%d] owner[%d] \n", p->dev, p->lock.__m_count,p->lock.__m_owner);
1756 ast_mutex_lock(&p->lock);
1762 strncpy(dialstring, s, sizeof(dialstring) - 1);
1763 for (i=0; dialstring[i] != '\0' ; i++) {
1764 if ((dialstring[i] == 'w') || (dialstring[i] == 'W'))
1765 dialstring[i] = ',';
1766 else if ((dialstring[i] == 'f') || (dialstring[i] == 'F'))
1767 dialstring[i] = '&';
1769 if (option_verbose > 3)
1770 ast_verbose(VERBOSE_PREFIX_4 "%s: starting call\n", p->dev);
1772 if (ast->_state != AST_STATE_DOWN && ast->_state != AST_STATE_RESERVED) {
1773 ast_log(LOG_WARNING, "vpb_call on %s neither down nor reserved!\n", ast->name);
1774 tmp = ast_mutex_unlock(&p->lock);
1776 if (option_verbose > 3) ast_verbose("%s: unLOCKING in call [%d]\n", p->dev,tmp);
1780 if (p->mode != MODE_FXO) /* Station port, ring it. */
1781 res = vpb_ring_station_async(p->handle, VPB_RING_STATION_ON,0);
1785 /* Dial must timeout or it can leave channels unuseable */
1787 timeout = TIMER_PERIOD_NOANSWER;
1789 timeout = timeout * 1000; /* convert from secs to ms. */
1791 /* These timeouts are only used with call progress dialing */
1792 call.dialtones = 1; /* Number of dialtones to get outside line */
1793 call.dialtone_timeout = VPB_DIALTONE_WAIT; /* Wait this long for dialtone (ms) */
1794 call.ringback_timeout = VPB_RINGWAIT; /* Wait this long for ringing after dialing (ms) */
1795 call.inter_ringback_timeout = VPB_CONNECTED_WAIT; /* If ringing stops for this long consider it connected (ms) */
1796 call.answer_timeout = timeout; /* Time to wait for answer after ringing starts (ms) */
1797 memcpy( &call.tone_map, DialToneMap, sizeof(DialToneMap) );
1798 vpb_set_call(p->handle, &call);
1800 if (option_verbose > 1)
1801 ast_verbose(VERBOSE_PREFIX_2 "%s: Calling %s on %s \n",p->dev, dialstring, ast->name);
1803 if (option_verbose > 2) {
1805 ast_verbose(VERBOSE_PREFIX_2 "%s: Dial parms for %s %d/%dms/%dms/%dms/%dms\n", p->dev
1806 , ast->name, call.dialtones, call.dialtone_timeout
1807 , call.ringback_timeout, call.inter_ringback_timeout
1808 , call.answer_timeout );
1809 for( j=0; !call.tone_map[j].terminate; j++ )
1810 ast_verbose(VERBOSE_PREFIX_2 "%s: Dial parms for %s tone %d->%d\n", p->dev,
1811 ast->name, call.tone_map[j].tone_id, call.tone_map[j].call_id);
1814 if (option_verbose > 3)
1815 ast_verbose("%s: Disabling Loop Drop detection\n",p->dev);
1816 vpb_disable_event(p->handle, VPB_MDROP);
1817 vpb_sethook_sync(p->handle,VPB_OFFHOOK);
1818 p->state=VPB_STATE_OFFHOOK;
1820 #ifndef DIAL_WITH_CALL_PROGRESS
1822 if (option_verbose > 3)
1823 ast_verbose("%s: Enabling Loop Drop detection\n",p->dev);
1824 vpb_enable_event(p->handle, VPB_MDROP);
1825 res = vpb_dial_async(p->handle, dialstring);
1827 if (option_verbose > 3)
1828 ast_verbose("%s: Enabling Loop Drop detection\n",p->dev);
1829 vpb_enable_event(p->handle, VPB_MDROP);
1830 res = vpb_call_async(p->handle, dialstring);
1833 if (res != VPB_OK) {
1834 ast_log(LOG_DEBUG, "Call on %s to %s failed: %s\n", ast->name, s, vpb_strerror(res));
1840 if (option_verbose > 2)
1841 ast_verbose(VERBOSE_PREFIX_3 "%s: VPB Calling %s [t=%d] on %s returned %d\n",p->dev , s, timeout, ast->name, res);
1843 ast_setstate(ast, AST_STATE_RINGING);
1844 ast_queue_control(ast,AST_CONTROL_RINGING);
1847 if (!p->readthread){
1848 ast_pthread_create(&p->readthread, NULL, do_chanreads, (void *)p);
1851 tmp = ast_mutex_unlock(&p->lock);
1853 if (option_verbose > 3) ast_verbose("%s: unLOCKING in call [%d]\n", p->dev,tmp);
1858 static int vpb_hangup(struct ast_channel *ast)
1860 struct vpb_pvt *p = (struct vpb_pvt *)ast->tech_pvt;
1862 char str[VPB_MAX_STR];
1866 if (option_verbose > 3) ast_verbose("%s: LOCKING in hangup \n", p->dev);
1867 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);
1868 if (option_verbose > 3) ast_verbose("%s: LOCKING pthread_self(%d)\n", p->dev,pthread_self());
1869 ast_mutex_lock(&p->lock);
1871 if (option_verbose > 1)
1872 ast_verbose(VERBOSE_PREFIX_2 "%s: Hangup requested\n", ast->name);
1874 if (!ast->tech || !ast->tech_pvt) {
1875 ast_log(LOG_WARNING, "%s: channel not connected?\n", ast->name);
1876 res = ast_mutex_unlock(&p->lock);
1878 if (option_verbose > 3) ast_verbose("%s: unLOCKING in hangup [%d]\n", p->dev,res);
1880 /* Free up ast dsp if we have one */
1881 if ((use_ast_dtmfdet)&&(p->vad)) {
1882 ast_dsp_free(p->vad);
1892 if( p->readthread ){
1893 pthread_join(p->readthread, NULL);
1894 if(option_verbose>3)
1895 ast_verbose( VERBOSE_PREFIX_4 "%s: stopped record thread \n",ast->name);
1899 if (p->lastoutput != -1) {
1900 if(option_verbose>1)
1901 ast_verbose( VERBOSE_PREFIX_2 "%s: Ending play mode \n",ast->name);
1902 vpb_play_terminate(p->handle);
1903 ast_mutex_lock(&p->play_lock); {
1904 vpb_play_buf_finish(p->handle);
1905 } ast_mutex_unlock(&p->play_lock);
1908 if(option_verbose>3)
1909 ast_verbose( VERBOSE_PREFIX_4 "%s: Setting state down\n",ast->name);
1910 ast_setstate(ast,AST_STATE_DOWN);
1914 if (option_verbose > 3) ast_verbose("%s: LOCKING in hangup \n", p->dev);
1915 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);
1916 if (option_verbose > 3) ast_verbose("%s: LOCKING pthread_self(%d)\n", p->dev,pthread_self());
1918 ast_mutex_lock(&p->lock);
1920 if (p->mode != MODE_FXO) {
1922 vpb_ring_station_async(p->handle, VPB_RING_STATION_OFF,0);
1923 if(p->state!=VPB_STATE_ONHOOK){
1924 /* This is causing a "dial end" "play tone" loop
1925 playtone(p->handle, &Busytone);
1926 p->state = VPB_STATE_PLAYBUSY;
1927 if(option_verbose>4)
1928 ast_verbose( VERBOSE_PREFIX_4 "%s: Station offhook[%d], playing busy tone\n",
1929 ast->name,p->state);
1933 stoptone(p->handle);
1936 stoptone(p->handle); /* Terminates any dialing */
1937 vpb_sethook_sync(p->handle, VPB_ONHOOK);
1938 p->state=VPB_STATE_ONHOOK;
1940 while (VPB_OK==vpb_get_event_ch_async(p->handle,&je)){
1941 if(option_verbose>3) {
1942 vpb_translate_event(&je, str);
1943 ast_verbose( VERBOSE_PREFIX_4 "%s: Flushing event [%d]=>%s\n",ast->name,je.type,str);
1950 p->last_ignore_dtmf = 1;
1957 /* Free up ast dsp if we have one */
1958 if ((use_ast_dtmfdet)&&(p->vad)) {
1959 ast_dsp_free(p->vad);
1963 ast_mutex_lock(&usecnt_lock); {
1965 } ast_mutex_unlock(&usecnt_lock);
1966 ast_update_use_count();
1968 if (option_verbose > 1)
1969 ast_verbose(VERBOSE_PREFIX_2 "%s: Hangup complete\n", ast->name);
1973 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);
1975 res = ast_mutex_unlock(&p->lock);
1977 if (option_verbose > 3) ast_verbose("%s: unLOCKING in hangup [%d]\n", p->dev,res);
1978 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);
1983 static int vpb_answer(struct ast_channel *ast)
1985 struct vpb_pvt *p = (struct vpb_pvt *)ast->tech_pvt;
1990 if (option_verbose > 3) ast_verbose("%s: LOCKING in answer \n", p->dev);
1991 if (option_verbose > 3) ast_verbose("%s: LOCKING count[%d] owner[%d] \n", p->dev, p->lock.__m_count,p->lock.__m_owner);
1993 ast_mutex_lock(&p->lock);
1995 if (option_verbose > 3)
1996 ast_verbose(VERBOSE_PREFIX_4 "%s: Answering channel\n",p->dev);
1998 if (p->mode == MODE_FXO){
1999 if (option_verbose > 3)
2000 ast_verbose("%s: Disabling Loop Drop detection\n",p->dev);
2001 vpb_disable_event(p->handle, VPB_MDROP);
2004 if (ast->_state != AST_STATE_UP) {
2005 if (p->mode == MODE_FXO){
2006 vpb_sethook_sync(p->handle, VPB_OFFHOOK);
2007 p->state=VPB_STATE_OFFHOOK;
2009 ret = vpb_get_event_ch_async(p->handle,&je);
2010 if ((ret == VPB_OK)&&((je.type != VPB_DROP)&&(je.type != VPB_RING))){
2011 if (option_verbose > 3){
2012 ast_verbose(VERBOSE_PREFIX_4 "%s: Answer collected a wrong event!!\n",p->dev);
2018 ast_setstate(ast, AST_STATE_UP);
2020 if(option_verbose>1)
2022 ast_verbose( VERBOSE_PREFIX_2 "%s: Answered call from %s on %s [%s]\n", p->dev,
2023 p->owner->callerid, ast->name,(p->mode == MODE_FXO)?"FXO":"FXS");
2025 ast_verbose( VERBOSE_PREFIX_2 "%s: Answered call on %s [%s]\n", p->dev,
2026 ast->name,(p->mode == MODE_FXO)?"FXO":"FXS");
2029 if( !p->readthread ){
2030 /* res = ast_mutex_unlock(&p->lock); */
2031 /* ast_verbose("%s: unLOCKING in answer [%d]\n", p->dev,res); */
2032 ast_pthread_create(&p->readthread, NULL, do_chanreads, (void *)p);
2034 if(option_verbose>3)
2035 ast_verbose(VERBOSE_PREFIX_4 "%s: Record thread already running!!\n",p->dev);
2038 if(option_verbose>3) {
2039 ast_verbose(VERBOSE_PREFIX_4 "%s: Answered state is up\n",p->dev);
2041 /* res = ast_mutex_unlock(&p->lock); */
2042 /* ast_verbose("%s: unLOCKING in answer [%d]\n", p->dev,res); */
2045 if (p->mode == MODE_FXO){
2046 if (option_verbose > 3)
2047 ast_verbose("%s: Re-enabling Loop Drop detection\n",p->dev);
2048 vpb_enable_event(p->handle,VPB_MDROP);
2050 res = ast_mutex_unlock(&p->lock);
2052 if(option_verbose>3) ast_verbose("%s: unLOCKING in answer [%d]\n", p->dev,res);
2057 static struct ast_frame *vpb_read(struct ast_channel *ast)
2059 struct vpb_pvt *p = (struct vpb_pvt *)ast->tech_pvt;
2060 static struct ast_frame f = {AST_FRAME_NULL};
2062 f.src = (char *)type;
2063 ast_log(LOG_NOTICE, "%s: vpb_read: should never be called!\n", p->dev);
2064 ast_verbose("%s: vpb_read: should never be called!\n", p->dev);
2069 static inline int ast2vpbformat(int ast_format)
2071 switch(ast_format) {
2072 case AST_FORMAT_ALAW:
2074 case AST_FORMAT_SLINEAR:
2076 case AST_FORMAT_ULAW:
2078 case AST_FORMAT_ADPCM:
2079 return VPB_OKIADPCM;
2085 static inline char * ast2vpbformatname(int ast_format)
2087 switch(ast_format) {
2088 case AST_FORMAT_ALAW:
2089 return "AST_FORMAT_ALAW:VPB_ALAW";
2090 case AST_FORMAT_SLINEAR:
2091 return "AST_FORMAT_SLINEAR:VPB_LINEAR";
2092 case AST_FORMAT_ULAW:
2093 return "AST_FORMAT_ULAW:VPB_MULAW";
2094 case AST_FORMAT_ADPCM:
2095 return "AST_FORMAT_ADPCM:VPB_OKIADPCM";
2101 static inline int astformatbits(int ast_format)
2103 switch(ast_format) {
2104 case AST_FORMAT_ALAW:
2105 case AST_FORMAT_ULAW:
2107 case AST_FORMAT_SLINEAR:
2109 case AST_FORMAT_ADPCM:
2116 int a_gain_vector(float g, short *v, int n)
2120 for ( i = 0; i<n; i++) {
2131 /* Writes a frame of voice data to a VPB channel */
2132 static int vpb_write(struct ast_channel *ast, struct ast_frame *frame)
2134 struct vpb_pvt *p = (struct vpb_pvt *)ast->tech_pvt;
2135 int res = 0, fmt = 0;
2136 struct timeval play_buf_time_start,play_buf_time_finish;
2137 /* ast_mutex_lock(&p->lock); */
2138 if(option_verbose>5)
2139 ast_verbose("%s: vpb_write: Writing to channel\n", p->dev);
2141 if (frame->frametype != AST_FRAME_VOICE) {
2142 if(option_verbose>3)
2143 ast_verbose("%s: vpb_write: Don't know how to handle from type %d\n", ast->name, frame->frametype);
2144 /* ast_mutex_unlock(&p->lock); */
2146 } else if (ast->_state != AST_STATE_UP) {
2147 if(option_verbose>3)
2148 ast_verbose("%s: vpb_write: Attempt to Write frame type[%d]subclass[%d] on not up chan(state[%d])\n",ast->name, frame->frametype, frame->subclass,ast->_state);
2150 /* ast_mutex_unlock(&p->lock); */
2153 /* ast_log(LOG_DEBUG, "%s: vpb_write: Checked frame type..\n", p->dev); */
2155 fmt = ast2vpbformat(frame->subclass);
2157 ast_log(LOG_WARNING, "%s: vpb_write: Cannot handle frames of %d format!\n",ast->name, frame->subclass);
2161 ast_log(LOG_DEBUG, "%s: vpb_write: Checked frame format..\n", p->dev);
2164 ast_mutex_lock(&p->play_lock);
2167 ast_log(LOG_DEBUG, "%s: vpb_write: Got play lock..\n", p->dev);
2170 /* Check if we have set up the play_buf */
2171 if (p->lastoutput == -1) {
2172 vpb_play_buf_start(p->handle, fmt);
2173 if(option_verbose>1) {
2174 ast_verbose("%s: vpb_write: Starting play mode (codec=%d)[%s]\n",p->dev,fmt,ast2vpbformatname(frame->subclass));
2176 } else if (p->lastoutput != fmt) {
2177 vpb_play_buf_finish(p->handle);
2178 vpb_play_buf_start(p->handle, fmt);
2179 if(option_verbose>1)
2180 ast_verbose("%s: vpb_write: Changed play format (%d=>%d)\n",p->dev,p->lastoutput,fmt);
2182 p->lastoutput = fmt;
2186 /* Apply extra gain ! */
2187 if( p->txswgain > MAX_VPB_GAIN )
2188 a_gain_vector(p->txswgain - MAX_VPB_GAIN , (short*)frame->data, frame->datalen/sizeof(short));
2190 /* ast_log(LOG_DEBUG, "%s: vpb_write: Applied gain..\n", p->dev); */
2192 /* gettimeofday(&tv, NULL); */
2193 /* return ((double)tv.tv_sec*1000)+((double)tv.tv_usec/1000); */
2195 if ((p->read_state == 1)&&(p->play_buf_time<5)){
2196 gettimeofday(&play_buf_time_start,NULL);
2197 res = vpb_play_buf_sync(p->handle, (char*)frame->data, frame->datalen);
2198 if( (res == VPB_OK) && (option_verbose > 5) ) {
2199 short * data = (short*)frame->data;
2200 ast_verbose("%s: vpb_write: Wrote chan (codec=%d) %d %d\n", p->dev, fmt, data[0],data[1]);
2202 gettimeofday(&play_buf_time_finish,NULL);
2203 if (play_buf_time_finish.tv_sec == play_buf_time_start.tv_sec){
2204 p->play_buf_time=(int)((play_buf_time_finish.tv_usec-play_buf_time_start.tv_usec)/1000);
2205 /* 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); */
2208 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);
2210 /* 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); */
2214 ast_log(LOG_DEBUG, "%s: vpb_write: Tossed data away, tooooo much data!![%d]\n", p->dev,p->chuck_count);
2218 ast_mutex_unlock(&p->play_lock);
2219 /* ast_mutex_unlock(&p->lock); */
2220 if(option_verbose>5)
2221 ast_verbose("%s: vpb_write: Done Writing to channel\n", p->dev);
2225 /* Read monitor thread function. */
2226 static void *do_chanreads(void *pvt)
2228 struct vpb_pvt *p = (struct vpb_pvt *)pvt;
2229 struct ast_frame *fr = &p->fr;
2230 char *readbuf = ((char *)p->buf) + AST_FRIENDLY_OFFSET;
2232 int afmt, readlen, res, fmt, trycnt=0;
2234 char * getdtmf_var = NULL;
2236 fr->frametype = AST_FRAME_VOICE;
2237 fr->src = (char *)type;
2239 fr->delivery.tv_sec = 0;
2240 fr->delivery.tv_usec = 0;
2241 fr->samples = VPB_SAMPLES;
2242 fr->offset = AST_FRIENDLY_OFFSET;
2243 memset(p->buf, 0, sizeof p->buf);
2245 if (option_verbose > 2) {
2246 ast_verbose("%s: chanreads: starting thread\n", p->dev);
2248 ast_mutex_lock(&p->record_lock);
2252 while (!p->stopreads && p->owner) {
2254 if (option_verbose > 4)
2255 ast_verbose("%s: chanreads: Starting cycle ...\n", p->dev);
2256 if (option_verbose > 4)
2257 ast_verbose("%s: chanreads: Checking bridge \n", p->dev);
2259 if (p->bridge->c0 == p->owner && (p->bridge->flags & AST_BRIDGE_REC_CHANNEL_0))
2261 else if (p->bridge->c1 == p->owner && (p->bridge->flags & AST_BRIDGE_REC_CHANNEL_1))
2266 if (option_verbose > 4)
2267 ast_verbose("%s: chanreads: No native bridge.\n", p->dev);
2268 if (p->owner->_bridge){
2269 if (option_verbose > 4){
2270 ast_verbose("%s: chanreads: Got Asterisk bridge with [%s].\n", p->dev,p->owner->_bridge->name);
2279 /* if ( (p->owner->_state != AST_STATE_UP) || !bridgerec) */
2280 if ( (p->owner->_state != AST_STATE_UP) )
2282 if (option_verbose > 4) {
2283 if (p->owner->_state != AST_STATE_UP)
2284 ast_verbose("%s: chanreads: Im not up[%d]\n", p->dev,p->owner->_state);
2286 ast_verbose("%s: chanreads: No bridgerec[%d]\n", p->dev,bridgerec);
2292 /* Voicetronix DTMF detection can be triggered off ordinary speech
2293 * This leads to annoying beeps during the conversation
2294 * Avoid this problem by just setting VPB_GETDTMF when you want to listen for DTMF
2296 /* ignore_dtmf = 1; */
2297 ignore_dtmf = 0; /* set this to 1 to turn this feature on */
2298 getdtmf_var = pbx_builtin_getvar_helper(p->owner,"VPB_GETDTMF");
2299 if( getdtmf_var && ( strcasecmp( getdtmf_var, "yes" ) == 0 ) )
2302 if(( ignore_dtmf != p->last_ignore_dtmf ) &&(!use_ast_dtmfdet)){
2303 if(option_verbose>1)
2304 ast_verbose( VERBOSE_PREFIX_2 "%s:Now %s DTMF \n",
2305 p->dev, ignore_dtmf ? "ignoring" : "listening for");
2306 vpb_set_event_mask(p->handle, ignore_dtmf ? VPB_EVENTS_NODTMF : VPB_EVENTS_ALL );
2308 p->last_ignore_dtmf = ignore_dtmf;
2310 /* Play DTMF digits here to avoid problem you get if playing a digit during
2311 * a record operation
2313 if (option_verbose > 5) {
2314 ast_verbose("%s: chanreads: Checking dtmf's \n", p->dev);
2316 ast_mutex_lock(&p->play_dtmf_lock);
2317 if( p->play_dtmf[0] ) {
2318 /* Try to ignore DTMF event we get after playing digit */
2319 /* This DTMF is played by asterisk and leads to an annoying trailing beep on CISCO phones */
2321 vpb_set_event_mask(p->handle, VPB_EVENTS_NODTMF );
2322 if (p->bridge == NULL){
2323 vpb_dial_sync(p->handle,p->play_dtmf);
2324 if(option_verbose>1)
2325 ast_verbose( VERBOSE_PREFIX_2 "%s: chanreads: Played DTMF %s\n",p->dev,p->play_dtmf);
2328 if (option_verbose > 1)
2329 ast_verbose(VERBOSE_PREFIX_2 "%s: chanreads: Not playing DTMF frame on native bridge\n", p->dev);
2331 p->play_dtmf[0] = '\0';
2332 ast_mutex_unlock(&p->play_dtmf_lock);
2333 vpb_sleep(700); /* Long enough to miss echo and DTMF event */
2335 vpb_set_event_mask(p->handle, VPB_EVENTS_ALL );
2338 ast_mutex_unlock(&p->play_dtmf_lock);
2340 /* afmt = (p->owner) ? p->owner->rawreadformat : AST_FORMAT_SLINEAR; */
2342 afmt = p->owner->rawreadformat;
2343 /* ast_log(LOG_DEBUG,"%s: Record using owner format [%s]\n", p->dev, ast2vpbformatname(afmt)); */
2346 afmt = AST_FORMAT_SLINEAR;
2347 /* ast_log(LOG_DEBUG,"%s: Record using default format [%s]\n", p->dev, ast2vpbformatname(afmt)); */
2349 fmt = ast2vpbformat(afmt);
2351 ast_log(LOG_WARNING,"%s: Record failure (unsupported format %d)\n", p->dev, afmt);
2354 readlen = VPB_SAMPLES * astformatbits(afmt) / 8;
2356 if (p->lastinput == -1) {
2357 vpb_record_buf_start(p->handle, fmt);
2358 vpb_reset_record_fifo_alarm(p->handle);
2359 if(option_verbose>1)
2360 ast_verbose( VERBOSE_PREFIX_2 "%s: Starting record mode (codec=%d)[%s]\n",p->dev,fmt,ast2vpbformatname(afmt));
2361 } else if (p->lastinput != fmt) {
2362 vpb_record_buf_finish(p->handle);
2363 vpb_record_buf_start(p->handle, fmt);
2364 if(option_verbose>1)
2365 ast_verbose( VERBOSE_PREFIX_2 "%s: Changed record format (%d=>%d)\n",p->dev,p->lastinput,fmt);
2369 /* Read only if up and not bridged, or a bridge for which we can read. */
2370 if (option_verbose > 5) {
2371 ast_verbose("%s: chanreads: getting buffer!\n", p->dev);
2373 if( (res = vpb_record_buf_sync(p->handle, readbuf, readlen) ) == VPB_OK ) {
2374 if (option_verbose > 5) {
2375 ast_verbose("%s: chanreads: got buffer!\n", p->dev);
2377 /* Apply extra gain ! */
2378 if( p->rxswgain > MAX_VPB_GAIN )
2379 a_gain_vector(p->rxswgain - MAX_VPB_GAIN , (short*)readbuf, readlen/sizeof(short));
2380 if (option_verbose > 5) {
2381 ast_verbose("%s: chanreads: applied gain\n", p->dev);
2384 fr->subclass = afmt;
2386 fr->datalen = readlen;
2387 fr->frametype = AST_FRAME_VOICE;
2389 if ((use_ast_dtmfdet)&&(p->vad)){
2390 fr = ast_dsp_process(p->owner,p->vad,fr);
2391 if (fr && (fr->frametype == AST_FRAME_DTMF))
2392 ast_log(LOG_DEBUG, "%s: chanreads: Detected DTMF '%c'\n",p->dev, fr->subclass);
2393 if (fr->subclass == 'm'){
2394 /* conf mute request */
2395 fr->frametype = AST_FRAME_NULL;
2398 else if (fr->subclass == 'u'){
2400 fr->frametype = AST_FRAME_NULL;
2403 else if (fr->subclass == 'f'){
2406 /* Using trylock here to prevent deadlock when channel is hungup
2407 * (ast_hangup() immediately gets lock)
2409 if (p->owner && !p->stopreads ) {
2410 if (option_verbose > 5) {
2411 ast_verbose("%s: chanreads: queueing buffer on read frame q (state[%d])\n", p->dev,p->owner->_state);
2414 res = ast_mutex_trylock(&p->owner->lock);
2416 } while((res !=0)&&(trycnt<300));
2418 ast_queue_frame(p->owner, fr);
2419 ast_mutex_unlock(&p->owner->lock);
2421 if (option_verbose > 4)
2422 ast_verbose("%s: chanreads: Couldnt get lock after %d tries!\n", p->dev,trycnt);
2427 res = ast_mutex_trylock(&p->owner->lock);
2429 ast_queue_frame(p->owner, fr);
2430 ast_mutex_unlock(&p->owner->lock);
2433 if (option_verbose > 4) ast_verbose("%s: chanreads: try owner->lock gave me EINVAL[%d]\n", p->dev,res);
2434 else if (res == EBUSY )
2435 if (option_verbose > 4) ast_verbose("%s: chanreads: try owner->lock gave me EBUSY[%d]\n", p->dev,res);
2437 res = ast_mutex_trylock(&p->owner->lock);
2440 ast_queue_frame(p->owner, fr);
2441 ast_mutex_unlock(&p->owner->lock);
2445 if (option_verbose > 4) ast_verbose("%s: chanreads: try owner->lock gave me EINVAL[%d]\n", p->dev,res);
2446 else if (res == EBUSY )
2447 if (option_verbose > 4) ast_verbose("%s: chanreads: try owner->lock gave me EBUSY[%d]\n", p->dev,res);
2448 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);
2452 if (option_verbose > 6) {
2453 short * data = (short*)readbuf;
2454 ast_verbose("%s: Read channel (codec=%d) %d %d\n", p->dev, fmt, data[0], data[1] );
2458 if (option_verbose > 4) {
2459 ast_verbose("%s: p->stopreads[%d] p->owner[%p]\n", p->dev, p->stopreads,(void *)p->owner);
2463 ast_log(LOG_WARNING,"%s: Record failure (%s)\n", p->dev, vpb_strerror(res));
2464 vpb_record_buf_finish(p->handle);
2465 vpb_record_buf_start(p->handle, fmt);
2467 if (option_verbose > 4)
2468 ast_verbose("%s: chanreads: Finished cycle...\n", p->dev);
2472 /* When stopreads seen, go away! */
2473 vpb_record_buf_finish(p->handle);
2475 ast_mutex_unlock(&p->record_lock);
2477 if (option_verbose > 1)
2478 ast_verbose(VERBOSE_PREFIX_2 "%s: Ending record mode (%d/%s)\n",
2479 p->dev, p->stopreads, p->owner? "yes" : "no");
2483 static struct ast_channel *vpb_new(struct vpb_pvt *me, int state, char *context)
2485 struct ast_channel *tmp;
2490 ast_log(LOG_WARNING, "Called vpb_new on owned channel (%s) ?!\n", me->dev);
2493 if (option_verbose > 3)
2494 ast_verbose("%s: New call for context [%s]\n",me->dev,context);
2496 tmp = ast_channel_alloc(1);
2498 tmp->tech = &vpb_tech;
2499 strncpy(tmp->name, me->dev, sizeof(tmp->name) - 1);
2502 tmp->callgroup = me->callgroup;
2503 tmp->pickupgroup = me->pickupgroup;
2505 /* Linear is the preferred format. Although Voicetronix supports other formats
2506 * they are all converted to/from linear in the vpb code. Best for us to use
2507 * linear since we can then adjust volume in this modules.
2509 tmp->nativeformats = prefformat;
2510 tmp->rawreadformat = AST_FORMAT_SLINEAR;
2511 tmp->rawwriteformat = AST_FORMAT_SLINEAR;
2512 ast_setstate(tmp, state);
2513 if (state == AST_STATE_RING) {
2517 ast_callerid_split(me->callerid, cid_name, sizeof(cid_name), cid_num, sizeof(cid_num));
2518 ast_set_callerid(tmp, cid_num, cid_name, cid_num);
2522 strncpy(tmp->context, context, sizeof(tmp->context)-1);
2523 if (strlen(me->ext))
2524 strncpy(tmp->exten, me->ext, sizeof(tmp->exten)-1);
2526 strncpy(tmp->exten, "s", sizeof(tmp->exten) - 1);
2527 if (strlen(me->language))
2528 strncpy(tmp->language, me->language, sizeof(tmp->language)-1);
2533 me->lastoutput = -1;
2535 me->last_ignore_dtmf = 1;
2537 me->play_dtmf[0] = '\0';
2540 me->lastgrunt = get_time_in_ms(); /* Assume at least one grunt tone seen now. */
2542 ast_mutex_lock(&usecnt_lock);
2544 ast_mutex_unlock(&usecnt_lock);
2545 ast_update_use_count();
2546 if (state != AST_STATE_DOWN) {
2547 if ((me->mode != MODE_FXO)&&(state != AST_STATE_UP)){
2550 if (ast_pbx_start(tmp)) {
2551 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
2556 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
2561 static struct ast_channel *vpb_request(const char *type, int format, void *data, int *cause)
2565 struct ast_channel *tmp = NULL;
2566 char *name = strdup(data ? (char *)data : "");
2571 format &= prefformat;
2573 ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%d'\n", oldformat);
2578 s = strsep(&sepstr, "/"); /* Handle / issues */
2581 /* Check if we are looking for a group */
2582 if (toupper(name[0]) == 'G' || toupper(name[0])=='R') {
2585 /* Search for an unowned channel */
2586 ast_mutex_lock(&iflock); {
2590 if (strncmp(s, p->dev + 4, sizeof p->dev) == 0) {
2592 tmp = vpb_new(p, AST_STATE_DOWN, p->context);
2598 if ((p->group == group) && (!p->owner)) {
2599 tmp = vpb_new(p, AST_STATE_DOWN, p->context);
2605 } ast_mutex_unlock(&iflock);
2608 if (option_verbose > 1)
2609 ast_verbose(VERBOSE_PREFIX_2 " %s requested, got: [%s]\n",
2610 name, tmp ? tmp->name : "None");
2618 static float parse_gain_value(char *gain_type, char *value)
2622 /* try to scan number */
2623 if (sscanf(value, "%f", &gain) != 1)
2625 ast_log(LOG_ERROR, "Invalid %s value '%s' in '%s' config\n", value, gain_type, config);
2626 return DEFAULT_GAIN;
2631 /*if (value[strlen(value) - 1] == '%') */
2632 /* return gain / (float)100; */
2639 struct ast_config *cfg;
2640 struct ast_variable *v;
2641 struct vpb_pvt *tmp;
2642 int board = 0, group = 0;
2643 ast_group_t callgroup = 0;
2644 ast_group_t pickupgroup = 0;
2645 int mode = MODE_IMMEDIATE;
2646 float txgain = DEFAULT_GAIN, rxgain = DEFAULT_GAIN;
2647 float txswgain = 0, rxswgain = 0;
2649 int first_channel = 1;
2650 int echo_cancel = DEFAULT_ECHO_CANCEL;
2651 int error = 0; /* Error flag */
2652 int bal1 = -1; /* Special value - means do not set */
2655 char * callerid = NULL;
2657 cfg = ast_config_load(config);
2659 /* We *must* have a config file otherwise stop immediately */
2661 ast_log(LOG_ERROR, "Unable to load config %s\n", config);
2665 vpb_seterrormode(VPB_ERROR_CODE);
2667 ast_mutex_lock(&iflock); {
2668 v = ast_variable_browse(cfg, "general");
2670 if (strcasecmp(v->name, "cards") == 0) {
2671 ast_log(LOG_NOTICE,"VPB Driver configured to use [%d] cards\n",atoi(v->value));
2673 else if (strcasecmp(v->name, "indication") == 0) {
2675 ast_log(LOG_NOTICE,"VPB driver using Asterisk Indication functions!\n");
2677 else if (strcasecmp(v->name, "break-for-dtmf") == 0) {
2678 if (ast_true(v->value)){
2683 ast_log(LOG_NOTICE,"VPB driver not stopping for DTMF's in native bridge\n");
2686 else if (strcasecmp(v->name, "ast-dtmf") == 0) {
2688 ast_log(LOG_NOTICE,"VPB driver using Asterisk DTMF play functions!\n");
2690 else if (strcasecmp(v->name, "ast-dtmf-det") == 0) {
2691 use_ast_dtmfdet = 1;
2692 ast_log(LOG_NOTICE,"VPB driver using Asterisk DTMF detection functions!\n");
2694 else if (strcasecmp(v->name, "relaxdtmf") == 0) {
2696 ast_log(LOG_NOTICE,"VPB driver using Relaxed DTMF with Asterisk DTMF detections functions!\n");
2698 else if (strcasecmp(v->name, "ecsuppthres") ==0) {
2699 ec_supp_threshold = atoi(v->value);
2701 else if (strcasecmp(v->name, "dtmfidd") ==0) {
2702 dtmf_idd = atoi(v->value);
2703 ast_log(LOG_NOTICE,"VPB Driver setting DTMF IDD to [%d]ms\n",dtmf_idd);
2708 v = ast_variable_browse(cfg, "interfaces");
2710 /* Create the interface list */
2711 if (strcasecmp(v->name, "board") == 0) {
2712 board = atoi(v->value);
2713 } else if (strcasecmp(v->name, "group") == 0){
2714 group = atoi(v->value);
2715 } else if (strcasecmp(v->name, "callgroup") == 0){
2716 callgroup = ast_get_group(v->value);
2717 } else if (strcasecmp(v->name, "pickupgroup") == 0){
2718 pickupgroup = ast_get_group(v->value);
2719 } else if (strcasecmp(v->name, "useloopdrop") == 0){
2720 UseLoopDrop = atoi(v->value);
2721 } else if (strcasecmp(v->name, "usenativebridge") == 0){
2722 UseNativeBridge = atoi(v->value);
2723 } else if (strcasecmp(v->name, "channel") == 0) {
2724 int channel = atoi(v->value);
2725 tmp = mkif(board, channel, mode, got_gain, txgain, rxgain, txswgain, rxswgain, bal1, bal2, bal3, callerid, echo_cancel,group,callgroup,pickupgroup);
2728 mkbrd( tmp->vpb_model, echo_cancel );
2734 ast_log(LOG_ERROR, "Unable to register channel '%s'\n", v->value);
2738 } else if (strcasecmp(v->name, "language") == 0) {
2739 strncpy(language, v->value, sizeof(language)-1);
2740 } else if (strcasecmp(v->name, "callerid") == 0) {
2741 callerid = strdup(v->value);
2742 } else if (strcasecmp(v->name, "mode") == 0) {
2743 if (strncasecmp(v->value, "di", 2) == 0)
2744 mode = MODE_DIALTONE;
2745 else if (strncasecmp(v->value, "im", 2) == 0)
2746 mode = MODE_IMMEDIATE;
2747 else if (strncasecmp(v->value, "fx", 2) == 0)
2750 ast_log(LOG_WARNING, "Unknown mode: %s\n", v->value);
2751 } else if (!strcasecmp(v->name, "context")) {
2752 strncpy(context, v->value, sizeof(context)-1);
2753 } else if (!strcasecmp(v->name, "echocancel")) {
2754 if (!strcasecmp(v->value, "off"))
2756 } else if (strcasecmp(v->name, "txgain") == 0) {
2757 txswgain = parse_gain_value(v->name, v->value);
2758 got_gain |=VPB_GOT_TXSWG;
2759 } else if (strcasecmp(v->name, "rxgain") == 0) {
2760 rxswgain = parse_gain_value(v->name, v->value);
2761 got_gain |=VPB_GOT_RXSWG;
2762 } else if (strcasecmp(v->name, "txhwgain") == 0) {
2763 txgain = parse_gain_value(v->name, v->value);
2764 got_gain |=VPB_GOT_TXHWG;
2765 } else if (strcasecmp(v->name, "rxhwgain") == 0) {
2766 rxgain = parse_gain_value(v->name, v->value);
2767 got_gain |=VPB_GOT_RXHWG;
2768 } else if (strcasecmp(v->name, "bal1") == 0) {
2769 bal1 = strtol(v->value, NULL, 16);
2770 if(bal1<0 || bal1>255) {
2771 ast_log(LOG_WARNING, "Bad bal1 value: %d\n", bal1);
2774 } else if (strcasecmp(v->name, "bal2") == 0) {
2775 bal2 = strtol(v->value, NULL, 16);
2776 if(bal2<0 || bal2>255) {
2777 ast_log(LOG_WARNING, "Bad bal2 value: %d\n", bal2);
2780 } else if (strcasecmp(v->name, "bal3") == 0) {
2781 bal3 = strtol(v->value, NULL, 16);
2782 if(bal3<0 || bal3>255) {
2783 ast_log(LOG_WARNING, "Bad bal3 value: %d\n", bal3);
2786 } else if (strcasecmp(v->name, "grunttimeout") == 0) {
2787 gruntdetect_timeout = 1000*atoi(v->value);
2792 if (gruntdetect_timeout < 1000)
2793 gruntdetect_timeout = 1000;
2796 } ast_mutex_unlock(&iflock);
2798 ast_config_destroy(cfg);
2800 if (!error && ast_channel_register(&vpb_tech) != 0) {
2801 ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
2809 restart_monitor(); /* And start the monitor for the first time */
2818 /* First, take us out of the channel loop */
2819 ast_channel_unregister(&vpb_tech);
2821 ast_mutex_lock(&iflock); {
2822 /* Hangup all interfaces if they have an owner */
2826 ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
2830 } ast_mutex_unlock(&iflock);
2832 ast_mutex_lock(&monlock); {
2833 if (mthreadactive > -1) {
2834 pthread_cancel(monitor_thread);
2835 pthread_join(monitor_thread, NULL);
2838 } ast_mutex_unlock(&monlock);
2840 ast_mutex_lock(&iflock); {
2841 /* Destroy all the interfaces and free their memory */
2845 ast_mutex_destroy(&p->lock);
2846 pthread_cancel(p->readthread);
2847 ast_mutex_destroy(&p->owner_lock);
2848 ast_mutex_destroy(&p->record_lock);
2849 ast_mutex_destroy(&p->play_lock);
2850 ast_mutex_destroy(&p->play_dtmf_lock);
2853 vpb_close(p->handle);
2855 iflist = iflist->next;
2860 } ast_mutex_unlock(&iflock);
2862 ast_mutex_lock(&bridge_lock); {
2863 memset(bridges, 0, sizeof bridges);
2864 } ast_mutex_unlock(&bridge_lock);
2865 ast_mutex_destroy(&bridge_lock);
2866 for(int i = 0; i < max_bridges; i++ ) {
2867 ast_mutex_destroy(&bridges[i].lock);
2868 pthread_cond_destroy(&bridges[i].cond);
2878 ast_mutex_lock(&usecnt_lock);
2880 ast_mutex_unlock(&usecnt_lock);
2886 return (char *) desc;
2891 return ASTERISK_GPL_KEY;
2895 #if defined(__cplusplus) || defined(c_plusplus)