2 * Asterisk -- A telephony toolkit for Linux.
4 * VoiceTronix Interface driver
6 * Copyright (C) 2003, Paul Bagyenda
8 * Paul Bagyenda <bagyenda@dsmagic.com>
10 * This program is free software, distributed under the terms of
11 * the GNU General Public License
17 #include <asterisk/lock.h>
18 #include <asterisk/channel.h>
19 #include <asterisk/channel_pvt.h>
20 #include <asterisk/config.h>
21 #include <asterisk/logger.h>
22 #include <asterisk/module.h>
23 #include <asterisk/pbx.h>
24 #include <asterisk/options.h>
25 #include <sys/socket.h>
30 #include <arpa/inet.h>
32 #include <sys/ioctl.h>
37 #define DEFAULT_GAIN 1.0
38 #define VPB_SAMPLES 240
39 #define VPB_MAX_BUF VPB_SAMPLES*4 + AST_FRIENDLY_OFFSET
41 #define VPB_NULL_EVENT 200
43 #define VPB_DIALTONE_WAIT 2000
44 #define VPB_RINGWAIT 2000
45 #define VPB_WAIT_TIMEOUT 40
47 #if defined(__cplusplus) || defined(c_plusplus)
51 static char *desc = "VoiceTronix V6PCI/V12PCI API Support";
52 static char *type = "vpb";
53 static char *tdesc = "Standard VoiceTronix API Driver";
54 static char *config = "vpb.conf";
56 /* Default context for dialtone mode */
57 static char context[AST_MAX_EXTENSION] = "default";
59 /* Default language */
60 static char language[MAX_LANGUAGE] = "";
63 static int echocancel = 1;
64 static int setrxgain = 0, settxgain = 0;
66 static int tcounter = 0;
68 static int gruntdetect_timeout = 5000; /* Grunt detect timeout is 5 seconds. */
70 static int silencesupression = 0;
72 static const int prefformat = AST_FORMAT_ALAW | AST_FORMAT_SLINEAR | AST_FORMAT_ULAW | AST_FORMAT_ADPCM;
74 static pthread_mutex_t usecnt_lock = AST_MUTEX_INITIALIZER;
76 /* Protect the interface list (of vpb_pvt's) */
77 static pthread_mutex_t iflock = AST_MUTEX_INITIALIZER;
79 /* Protect the monitoring thread, so only one process can kill or start it, and not
80 when it's doing something critical. */
81 static pthread_mutex_t monlock = AST_MUTEX_INITIALIZER;
83 /* This is the thread for the monitor which checks for input on the channels
84 which are not currently in use. */
85 static pthread_t monitor_thread;
87 static int mthreadactive = -1; /* Flag for monitoring monitorthread.*/
90 static int restart_monitor(void);
92 /* The private structures of the VPB channels are linked for
93 selecting outgoing channels */
95 #define MODE_DIALTONE 1
96 #define MODE_IMMEDIATE 2
102 static VPB_TONE Dialtone = {450, 425, 400, -10, -10, -10, 10000, 0 };
103 static VPB_TONE Busytone = {425, 0, 0, -10, -100, -100, 500, 500};
104 static VPB_TONE Ringbacktone = {425, 0, 0, -10, -100, -100, 1000, 3000};
107 #define VPB_MAX_BRIDGES 128
108 static struct vpb_bridge_t {
110 struct ast_channel *c0, *c1, **rc;
111 struct ast_frame **fo;
114 pthread_mutex_t lock;
116 } bridges[VPB_MAX_BRIDGES]; /* Bridges...*/
118 static pthread_mutex_t bridge_lock = AST_MUTEX_INITIALIZER;
120 static struct vpb_pvt {
122 struct ast_channel *owner; /* Channel we belong to, possibly NULL */
123 int mode; /* Is this in the */
124 int handle; /* Handle for vpb interface */
128 struct ast_frame f, fr;
129 char buf[VPB_MAX_BUF]; /* Static buffer for reading frames */
130 char obuf[VPB_MAX_BUF];
133 float txgain, rxgain; /* gain control for playing, recording */
135 int wantdtmf; /* Waiting for DTMF. */
136 int silencesupression;
137 char context[AST_MAX_EXTENSION];
139 char ext[AST_MAX_EXTENSION];
140 char language[MAX_LANGUAGE];
141 char callerid[AST_MAX_EXTENSION];
146 struct vpb_bridge_t *bridge;
147 void *timer; /* For call timeout. */
152 pthread_mutex_t lock;
154 int stopreads; /* Stop reading...*/
155 pthread_t readthread; /* For monitoring read channel. One per owned channel. */
157 struct vpb_pvt *next; /* Next channel in list */
160 static char callerid[AST_MAX_EXTENSION];
162 static int vpb_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc)
164 struct vpb_pvt *p0 = (struct vpb_pvt *)c0->pvt->pvt;
165 struct vpb_pvt *p1 = (struct vpb_pvt *)c1->pvt->pvt;
166 int i, len = sizeof bridges/sizeof bridges[0], res;
168 /* Bridge channels, check if we can. I believe we always can, so find a slot.*/
170 ast_pthread_mutex_lock(&bridge_lock); {
171 for (i = 0; i < len; i++)
172 if (!bridges[i].inuse)
175 bridges[i].inuse = 1;
176 bridges[i].flags = flags;
181 pthread_mutex_init(&bridges[i].lock, NULL);
182 pthread_cond_init(&bridges[i].cond, NULL);
184 } ast_pthread_mutex_unlock(&bridge_lock);
187 ast_log(LOG_WARNING, "Failed to bridge %s and %s!\n", c0->name, c1->name);
191 /* Set bridge pointers. You don't want to take these locks while holding bridge lock.*/
192 ast_pthread_mutex_lock(&p0->lock); {
193 p0->bridge = &bridges[i];
194 } ast_pthread_mutex_unlock(&p0->lock);
196 ast_pthread_mutex_lock(&p1->lock); {
197 p1->bridge = &bridges[i];
198 } ast_pthread_mutex_unlock(&p1->lock);
200 if (option_verbose > 4)
201 ast_verbose(VERBOSE_PREFIX_3
202 " Bridging call entered with [%s, %s]\n", c0->name, c1->name);
204 res = vpb_bridge(p0->handle, p1->handle, VPB_BRIDGE_ON, 0);
209 res = pthread_cond_wait(&bridges[i].cond, &bridges[i].lock); /* Wait for condition signal. */
212 done: /* Out of wait. */
214 vpb_bridge(p0->handle, p1->handle, VPB_BRIDGE_OFF, 0);
217 ast_pthread_mutex_lock(&bridge_lock); {
218 bridges[i].inuse = 0;
219 pthread_mutex_destroy(&bridges[i].lock);
220 pthread_cond_destroy(&bridges[i].cond);
221 } ast_pthread_mutex_unlock(&bridge_lock);
223 ast_pthread_mutex_lock(&p0->lock); {
225 } ast_pthread_mutex_unlock(&p0->lock);
227 ast_pthread_mutex_lock(&p1->lock); {
229 } ast_pthread_mutex_unlock(&p1->lock);
232 if (option_verbose > 4)
233 ast_verbose(VERBOSE_PREFIX_3
234 " Bridging call done with [%s, %s] => %d\n", c0->name, c1->name, res);
236 if (res != 0 && res != VPB_OK) /* Don't assume VPB_OK is zero! */
242 static inline int monitor_handle_owned(struct vpb_pvt *p, VPB_EVENT *e)
244 struct ast_frame f = {AST_FRAME_CONTROL}; /* default is control, Clear rest. */
247 if (option_verbose > 4)
248 ast_verbose(VERBOSE_PREFIX_3 " %s handle_owned got event: [%d=>%d]\n",
249 p->dev, e->type, e->data);
254 if (p->mode == MODE_FXO)
255 f.subclass = AST_CONTROL_RING;
257 f.frametype = -1; /* ignore ring on station port. */
260 if (p->calling) { /* This means time to stop calling. */
261 f.subclass = AST_CONTROL_BUSY;
262 vpb_timer_close(p->timer);
264 f.frametype = -1; /* Ignore. */
267 if (p->owner->_state == AST_STATE_UP) {
268 f.frametype = AST_FRAME_DTMF;
269 f.subclass = e->data;
275 if (e->data == VPB_BUSY || e->data == VPB_BUSY_308)
276 f.subclass = AST_CONTROL_BUSY;
277 else if (e->data == VPB_GRUNT) {
278 p->lastgrunt = tcounter;
285 if (e->data == VPB_CALL_CONNECTED)
286 f.subclass = AST_CONTROL_ANSWER;
287 else if (e->data == VPB_CALL_NO_DIAL_TONE ||
288 e->data == VPB_CALL_NO_RING_BACK)
289 f.subclass = AST_CONTROL_CONGESTION;
290 else if (e->data == VPB_CALL_NO_ANSWER ||
291 e->data == VPB_CALL_BUSY)
292 f.subclass = AST_CONTROL_BUSY;
293 else if (e->data == VPB_CALL_DISCONNECTED)
294 f.subclass = AST_CONTROL_HANGUP;
297 case VPB_STATION_OFFHOOK:
298 f.subclass = AST_CONTROL_ANSWER;
301 case VPB_STATION_ONHOOK:
302 f.subclass = AST_CONTROL_HANGUP;
305 case VPB_STATION_FLASH:
306 f.subclass = AST_CONTROL_FLASH;
314 if (option_verbose > 4)
315 ast_verbose(VERBOSE_PREFIX_3 " handle_owned: putting frame: [%d=>%d], bridge=%p\n",
316 f.frametype, f.subclass, (void *)p->bridge);
318 ast_pthread_mutex_lock(&p->lock); {
319 if (p->bridge) { /* Check what happened, see if we need to report it. */
320 switch (f.frametype) {
322 if (!(p->bridge->c0 == p->owner &&
323 (p->bridge->flags & AST_BRIDGE_DTMF_CHANNEL_0) ) &&
324 !(p->bridge->c1 == p->owner &&
325 (p->bridge->flags & AST_BRIDGE_DTMF_CHANNEL_1) ))
326 /* Kill bridge, this is interesting. */
330 case AST_FRAME_CONTROL:
331 if (!(p->bridge->flags & AST_BRIDGE_IGNORE_SIGS))
333 if (f.subclass == AST_CONTROL_BUSY ||
334 f.subclass == AST_CONTROL_CONGESTION ||
335 f.subclass == AST_CONTROL_HANGUP ||
336 f.subclass == AST_CONTROL_FLASH)
346 *p->bridge->fo = ast_frisolate(&f);
348 *p->bridge->rc = p->owner;
350 ast_pthread_mutex_lock(&p->bridge->lock); {
351 pthread_cond_signal(&p->bridge->cond);
352 } ast_pthread_mutex_unlock(&p->bridge->lock);
355 } ast_pthread_mutex_unlock(&p->lock);
357 if (endbridge) return 0;
359 if (f.frametype >= 0 && f.frametype != AST_FRAME_NULL)
360 ast_queue_frame(p->owner, &f, 0);
364 static struct ast_channel *vpb_new(struct vpb_pvt *i, int state, char *context);
366 static inline int monitor_handle_notowned(struct vpb_pvt *p, VPB_EVENT *e)
370 if (option_verbose > 4)
371 ast_verbose(VERBOSE_PREFIX_3 " %s: In not owned, mode=%d, [%d=>%d]\n",
372 p->dev, p->mode, e->type, e->data);
376 if (p->mode == MODE_FXO) /* FXO port ring, start * */
377 vpb_new(p, AST_STATE_RING, p->context);
379 case VPB_STATION_OFFHOOK:
380 if (p->mode == MODE_IMMEDIATE)
381 vpb_new(p,AST_STATE_RING, p->context);
383 vpb_playtone_async(p->handle, &Dialtone);
385 p->ext[0] = 0; /* Just to be sure & paranoid.*/
389 case VPB_STATION_ONHOOK: /*, clear ext */
390 vpb_tone_terminate(p->handle);
396 if (p->wantdtmf == 1) {
397 vpb_tone_terminate(p->handle);
403 if (ast_exists_extension(NULL, p->context, p->ext, 1, p->callerid))
404 vpb_new(p,AST_STATE_RING, p->context);
405 else if (!ast_canmatch_extension(NULL, p->context, p->ext, 1, p->callerid))
406 if (ast_exists_extension(NULL, "default", p->ext, 1, p->callerid))
407 vpb_new(p,AST_STATE_RING, "default");
408 else if (!ast_canmatch_extension(NULL, "default", p->ext, 1, p->callerid)) {
411 "%s can't match anything in %s or default\n",
413 vpb_playtone_async(p->handle, &Busytone);
423 if (option_verbose > 4)
424 ast_verbose(VERBOSE_PREFIX_3 " %s: Done not owned, mode=%d, [%d=>%d]\n",
425 p->dev, p->mode, e->type, e->data);
430 static void *do_monitor(void *unused)
433 /* Monitor thread, doesn't die until explicitly killed. */
435 if (option_verbose > 4)
436 ast_verbose(VERBOSE_PREFIX_3 "Starting vpb monitor thread[%ld]\n",
438 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
442 char str[VPB_MAX_STR];
444 int res = vpb_get_event_sync(&e, VPB_WAIT_TIMEOUT);
450 ast_pthread_mutex_lock(&monlock),
451 ast_pthread_mutex_lock(&iflock); {
452 struct vpb_pvt *p = iflist; /* Find the pvt structure */
454 vpb_translate_event(&e, str);
456 if (e.type == VPB_NULL_EVENT)
457 goto done; /* Nothing to do, just a wakeup call.*/
458 while (p && p->handle != e.handle)
461 if (option_verbose > 2)
462 ast_verbose(VERBOSE_PREFIX_3 " Event [%d=>%s] on %s\n",
463 e.type, str, p ? p->dev : "null");
467 "Got event %s, no matching iface!\n", str);
472 /* Two scenarios: Are you owned or not. */
475 monitor_handle_owned(p, &e);
477 monitor_handle_notowned(p, &e);
480 } ast_pthread_mutex_unlock(&iflock);
481 ast_pthread_mutex_unlock(&monlock);
484 tcounter += VPB_WAIT_TIMEOUT; /* Ok, not quite but will suffice. */
492 static int restart_monitor(void)
496 /* If we're supposed to be stopped -- stay stopped */
497 if (mthreadactive == -2)
499 ast_pthread_mutex_lock(&monlock); {
500 if (monitor_thread == pthread_self()) {
501 ast_log(LOG_WARNING, "Cannot kill myself\n");
505 if (mthreadactive != -1) {
506 /* Why do other drivers kill the thread? No need says I, simply awake thread with event. */
509 e.type = VPB_NULL_EVENT;
514 /* Start a new monitor */
515 int pid = pthread_create(&monitor_thread, NULL, do_monitor, NULL);
517 ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
521 mthreadactive = 0; /* Started the thread!*/
524 if (option_verbose > 4)
525 ast_verbose(VERBOSE_PREFIX_3
526 "Starting vpb restast: thread[%d]\n",
531 } ast_pthread_mutex_unlock(&monlock);
536 struct vpb_pvt *mkif(int board, int channel, int mode, float txgain, float rxgain)
541 tmp = (struct vpb_pvt *)calloc(1, sizeof *tmp);
546 tmp->handle = vpb_open(board, channel);
548 if (tmp->handle < 0) {
549 ast_log(LOG_WARNING, "Unable to create channel vpb/%d-%d: %s\n",
550 board, channel, strerror(errno));
556 if (option_verbose > 4)
557 ast_verbose(VERBOSE_PREFIX_3 " vpb turned on echo cancel.\n");
558 vpb_echo_canc_enable();
559 vpb_echo_canc_force_adapt_on();
560 echocancel = 0; /* So we do not initialise twice! */
563 if (option_verbose > 4)
564 ast_verbose(VERBOSE_PREFIX_3 " vpb created channel: [%d:%d]\n",
567 sprintf(tmp->dev, "vpb/%d-%d", board, channel);
571 strcpy(tmp->language, language);
572 strcpy(tmp->context, context);
574 strcpy(tmp->callerid, callerid);
575 tmp->txgain = txgain;
577 tmp->rxgain = rxgain;
580 tmp->lastoutput = -1;
586 pthread_mutex_init(&tmp->lock, NULL);
589 vpb_record_set_gain(tmp->handle, rxgain);
591 vpb_play_set_gain(tmp->handle, txgain);
597 static int vpb_indicate(struct ast_channel *ast, int condition)
599 struct vpb_pvt *p = (struct vpb_pvt *)ast->pvt->pvt;
602 if (option_verbose > 4)
603 ast_verbose(VERBOSE_PREFIX_3 " vpb indicate on %s with %d\n",
607 case AST_CONTROL_BUSY:
608 case AST_CONTROL_CONGESTION:
609 res = vpb_playtone_async(p->handle, &Busytone);
611 case AST_CONTROL_RINGING:
612 res = vpb_playtone_async(p->handle, &Ringbacktone);
614 case AST_CONTROL_ANSWER:
615 case -1: /* -1 means stop playing? */
616 res = vpb_tone_terminate(p->handle);
618 case AST_CONTROL_HANGUP:
619 res = vpb_playtone_async(p->handle, &Busytone);
629 static int vpb_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
631 struct vpb_pvt *p = (struct vpb_pvt *)newchan->pvt->pvt;
634 "New owner for channel %s is %s\n", p->dev, newchan->name);
635 if (p->owner == oldchan)
638 if (newchan->_state == AST_STATE_RINGING)
639 vpb_indicate(newchan, AST_CONTROL_RINGING);
644 static int vpb_digit(struct ast_channel *ast, char digit)
651 return vpb_dial_sync(((struct vpb_pvt *)ast->pvt->pvt)->handle, s);
655 static int vpb_call(struct ast_channel *ast, char *dest, int timeout)
657 struct vpb_pvt *p = (struct vpb_pvt *)ast->pvt->pvt;
659 char *s = strrchr(dest, '/');
666 if (ast->_state != AST_STATE_DOWN && ast->_state != AST_STATE_RESERVED) {
667 ast_log(LOG_WARNING, "vpb_call on %s neither down nor reserved!\n",
671 if (p->mode != MODE_FXO) /* Station port, ring it. */
672 res = vpb_ring_station_async(p->handle, VPB_RING_STATION_ON);
676 vpb_get_call(p->handle, &call);
678 call.dialtone_timeout = VPB_DIALTONE_WAIT;
679 call.answer_timeout = timeout;
680 call.ringback_timeout = VPB_RINGWAIT;
682 vpb_set_call(p->handle, &call);
684 if (option_verbose > 2)
685 ast_verbose(VERBOSE_PREFIX_3 " Calling %s on %s \n",
688 vpb_sethook_sync(p->handle,VPB_OFFHOOK);
690 res = vpb_dial_async(p->handle, s);
693 ast_log(LOG_DEBUG, "Call on %s to %s failed: %s\n",
694 ast->name, dest, vpb_strerror(res));
700 if (option_verbose > 2)
701 ast_verbose(VERBOSE_PREFIX_3
702 " VPB Calling %s [t=%d] on %s returned %d\n",
703 dest, timeout, ast->name, res);
707 vpb_timer_open(&p->timer, p->handle, 0, 1000*timeout);
708 vpb_timer_start(p->timer);
711 ast_setstate(ast, AST_STATE_RINGING);
712 ast_queue_control(ast,AST_CONTROL_RINGING, 0);
718 static int vpb_hangup(struct ast_channel *ast)
722 if (option_verbose > 2)
723 ast_verbose(VERBOSE_PREFIX_3 " hangup on vpb (%s)\n", ast->name);
725 if (!ast->pvt || !ast->pvt->pvt) {
726 ast_log(LOG_WARNING, "channel (%s) not connected?\n", ast->name);
730 p = (struct vpb_pvt *)ast->pvt->pvt;
732 vpb_play_terminate(p->handle);
733 vpb_record_terminate(p->handle);
735 if (p->mode != MODE_FXO) { /* station port. */
736 vpb_ring_station_async(p->handle, VPB_RING_STATION_OFF);
737 vpb_playtone_async(p->handle, &Busytone);
740 vpb_sethook_sync(p->handle, VPB_ONHOOK);
742 ast_setstate(ast,AST_STATE_DOWN);
744 ast_pthread_mutex_lock(&p->lock); {
745 p->lastinput = p->lastoutput = -1;
750 ast->pvt->pvt = NULL;
751 } ast_pthread_mutex_unlock(&p->lock);
753 ast_pthread_mutex_lock(&usecnt_lock); {
755 } ast_pthread_mutex_unlock(&usecnt_lock);
756 ast_update_use_count();
758 /* Stop thread doing reads. */
760 pthread_join(p->readthread, NULL);
762 if (option_verbose > 2)
763 ast_verbose(VERBOSE_PREFIX_3 " Hungup on %s complete\n", ast->name);
769 static int vpb_answer(struct ast_channel *ast)
771 struct vpb_pvt *p = (struct vpb_pvt *)ast->pvt->pvt;
773 if (p->mode == MODE_FXO)
774 vpb_sethook_sync(p->handle, VPB_OFFHOOK);
777 ast_log(LOG_DEBUG, "vpb answer on %s\n", ast->name);
779 ast_setstate(ast, AST_STATE_UP);
784 static struct ast_frame *vpb_read(struct ast_channel *ast)
786 struct vpb_pvt *p = (struct vpb_pvt *)ast->pvt->pvt;
787 static struct ast_frame f = {AST_FRAME_NULL};
790 ast_log(LOG_NOTICE, "vpb_read should never be called (chan=%s)!\n", p->dev);
795 static inline int ast2vpbformat(int ast_format)
799 case AST_FORMAT_ALAW:
801 case AST_FORMAT_SLINEAR:
803 case AST_FORMAT_ULAW:
805 case AST_FORMAT_ADPCM:
813 static inline int astformatbits(int ast_format)
817 case AST_FORMAT_ALAW:
818 case AST_FORMAT_ULAW:
820 case AST_FORMAT_SLINEAR:
822 case AST_FORMAT_ADPCM:
829 static int vpb_write(struct ast_channel *ast, struct ast_frame *frame)
831 struct vpb_pvt *p = (struct vpb_pvt *)ast->pvt->pvt;
832 int res = 0, fmt = 0;
834 if (frame->frametype != AST_FRAME_VOICE) {
835 ast_log(LOG_WARNING, "Don't know how to handle from type %d\n",
838 } else if (ast->_state != AST_STATE_UP) {
839 if (option_verbose > 4)
840 ast_log(LOG_WARNING, "Writing frame type [%d,%d] on chan %s not up\n",
841 frame->frametype, frame->subclass, ast->name);
847 fmt = ast2vpbformat(frame->subclass);
849 if (option_verbose > 4)
850 ast_verbose(VERBOSE_PREFIX_3
851 " Write chan %s: got frame type = %d\n",
852 p->dev, frame->subclass);
855 ast_log(LOG_WARNING, "vpb_write Cannot handle frames of %d format!\n",
861 if (p->lastoutput != fmt) {
862 vpb_play_buf_start(p->handle, fmt);
866 memcpy(p->obuf, frame->data,
867 (frame->datalen > (int)sizeof p->obuf) ? sizeof p->obuf : frame->datalen);
868 res = vpb_play_buf_sync(p->handle, p->obuf, frame->datalen);
875 /* Read monitor thread function. */
876 static void *do_chanreads(void *pvt)
878 struct vpb_pvt *p = (struct vpb_pvt *)pvt;
879 struct ast_frame *fr = &p->fr;
880 char *readbuf = ((char *)p->buf) + AST_FRIENDLY_OFFSET;
883 fr->frametype = AST_FRAME_VOICE;
887 memset(p->buf, 0, sizeof p->buf);
889 while (!p->stopreads && p->owner) {
891 struct ast_channel *owner = p->owner;
892 int afmt = (owner) ? owner->pvt->rawreadformat : AST_FORMAT_SLINEAR;
893 int state = (owner) ? owner->_state : AST_STATE_DOWN;
896 fmt = ast2vpbformat(afmt);
903 readlen = VPB_SAMPLES * astformatbits(afmt) / 8;
905 if (p->lastinput != fmt) {
906 if (option_verbose > 2)
907 ast_verbose(" Read_channel ## %s: Setting record mode, bridge = %d\n",
908 p->dev, p->bridge ? 1 : 0);
909 vpb_record_buf_start(p->handle, fmt);
913 ast_pthread_mutex_lock(&p->lock); {
915 if (p->bridge->c0 == p->owner &&
916 (p->bridge->flags & AST_BRIDGE_REC_CHANNEL_0))
918 else if (p->bridge->c1 == p->owner &&
919 (p->bridge->flags & AST_BRIDGE_REC_CHANNEL_1))
925 } ast_pthread_mutex_unlock(&p->lock);
927 if (state == AST_STATE_UP && bridgerec) {
928 /* Read only if up and not bridged, or a bridge for which we can read. */
929 res = vpb_record_buf_sync(p->handle, readbuf, readlen);
936 fr->samples = VPB_SAMPLES;
938 fr->datalen = readlen;
939 fr->offset = AST_FRIENDLY_OFFSET;
941 ast_pthread_mutex_lock(&p->lock); {
942 if (p->owner) ast_queue_frame(p->owner, fr, 0);
943 } ast_pthread_mutex_unlock(&p->lock);
948 if (option_verbose > 4)
949 ast_verbose(" Read_channel %s (state=%d), res=%d, bridge=%d\n",
950 p->dev, state, res, bridgerec);
953 /* When stopreads seen, go away! */
954 vpb_record_buf_finish(p->handle);
956 if (option_verbose > 4)
957 ast_verbose(" Read_channel %s terminating, stopreads=%d, owner=%s\n",
958 p->dev, p->stopreads, p->owner? "yes" : "no");
963 static struct ast_channel *vpb_new(struct vpb_pvt *i, int state, char *context)
965 struct ast_channel *tmp;
968 ast_log(LOG_WARNING, "Called vpb_new on owned channel (%s) ?!\n", i->dev);
972 tmp = ast_channel_alloc(1);
974 strncpy(tmp->name, i->dev, sizeof(tmp->name));
977 tmp->nativeformats = prefformat;
978 tmp->pvt->rawreadformat = AST_FORMAT_SLINEAR;
979 tmp->pvt->rawwriteformat = AST_FORMAT_SLINEAR;
980 ast_setstate(tmp, state);
981 if (state == AST_STATE_RING)
984 tmp->pvt->send_digit = vpb_digit;
985 tmp->pvt->call = vpb_call;
986 tmp->pvt->hangup = vpb_hangup;
987 tmp->pvt->answer = vpb_answer;
988 tmp->pvt->read = vpb_read;
989 tmp->pvt->write = vpb_write;
990 tmp->pvt->bridge = vpb_bridge;
991 tmp->pvt->indicate = vpb_indicate;
992 tmp->pvt->fixup = vpb_fixup;
994 strncpy(tmp->context, context, sizeof(tmp->context)-1);
996 strncpy(tmp->exten, i->ext, sizeof(tmp->exten)-1);
998 strncpy(tmp->exten, "s", sizeof(tmp->exten) - 1);
999 if (strlen(i->language))
1000 strncpy(tmp->language, i->language, sizeof(tmp->language)-1);
1001 if (strlen(i->callerid))
1002 tmp->callerid = strdup(i->callerid);
1006 i->lastinput = i->lastoutput = -1;
1007 i->lastgrunt = tcounter; /* Assume at least one grunt tone seen now. */
1009 ast_pthread_mutex_lock(&usecnt_lock);
1011 ast_pthread_mutex_unlock(&usecnt_lock);
1012 ast_update_use_count();
1013 if (state != AST_STATE_DOWN)
1014 if (ast_pbx_start(tmp)) {
1015 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
1019 i->stopreads = 0; /* So read thread runs. */
1020 /* Finally start read monitoring thread. */
1021 pthread_create(&i->readthread, NULL, do_chanreads, (void *)i);
1023 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
1027 static struct ast_channel *vpb_request(char *type, int format, void *data)
1031 struct ast_channel *tmp = NULL;
1032 char *name = strdup(data ? (char *)data : "");
1036 format &= prefformat;
1038 ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%d'\n", oldformat);
1043 s = strsep(&sepstr, "/"); /* Handle / issues */
1046 /* Search for an unowned channel */
1047 ast_pthread_mutex_lock(&iflock); {
1050 if (strncmp(s, p->dev + 4, sizeof p->dev) == 0)
1052 tmp = vpb_new(p, AST_STATE_DOWN, p->context);
1057 } ast_pthread_mutex_unlock(&iflock);
1060 if (option_verbose > 2)
1061 ast_verbose(VERBOSE_PREFIX_3 " %s requested, got: [%s]\n",
1062 name, tmp ? tmp->name : "None");
1070 static float parse_gain_value(char *gain_type, char *value)
1074 /* try to scan number */
1075 if (sscanf(value, "%f", &gain) != 1)
1077 ast_log(LOG_ERROR, "Invalid %s value '%s' in '%s' config\n",
1078 value, gain_type, config);
1079 return DEFAULT_GAIN;
1084 if (value[strlen(value) - 1] == '%')
1085 return gain / (float)100;
1092 struct ast_config *cfg;
1093 struct ast_variable *v;
1094 struct vpb_pvt *tmp;
1095 int board = 0, group = 0;
1096 int mode = MODE_IMMEDIATE;
1097 float txgain = DEFAULT_GAIN, rxgain = DEFAULT_GAIN;
1099 int error = 0; /* Error flag */
1102 setrxgain = settxgain = 0;
1104 cfg = ast_load(config);
1106 /* We *must* have a config file otherwise stop immediately */
1108 ast_log(LOG_ERROR, "Unable to load config %s\n", config);
1112 vpb_seterrormode(VPB_DEVELOPMENT);
1114 ast_pthread_mutex_lock(&iflock); {
1115 v = ast_variable_browse(cfg, "interfaces");
1117 /* Create the interface list */
1118 if (strcasecmp(v->name, "board") == 0)
1119 board = atoi(v->value);
1120 else if (strcasecmp(v->name, "group") == 0)
1121 group = atoi(v->value);
1122 else if (strcasecmp(v->name, "channel") == 0) {
1123 int channel = atoi(v->value);
1124 tmp = mkif(board, channel, mode, txgain, rxgain);
1131 "Unable to register channel '%s'\n", v->value);
1136 } else if (strcasecmp(v->name, "silencesupression") == 0)
1137 silencesupression = ast_true(v->value);
1138 else if (strcasecmp(v->name, "language") == 0)
1139 strncpy(language, v->value, sizeof(language)-1);
1140 else if (strcasecmp(v->name, "callerid") == 0)
1141 strncpy(callerid, v->value, sizeof(callerid)-1);
1142 else if (strcasecmp(v->name, "mode") == 0) {
1143 if (strncasecmp(v->value, "di", 2) == 0)
1144 mode = MODE_DIALTONE;
1145 else if (strncasecmp(v->value, "im", 2) == 0)
1146 mode = MODE_IMMEDIATE;
1147 else if (strncasecmp(v->value, "fx", 2) == 0)
1150 ast_log(LOG_WARNING, "Unknown mode: %s\n", v->value);
1151 } else if (!strcasecmp(v->name, "context"))
1152 strncpy(context, v->value, sizeof(context)-1);
1153 else if (!strcasecmp(v->name, "echocancel")) {
1154 if (!strcasecmp(v->value, "off"))
1158 } else if (strcasecmp(v->name, "txgain") == 0) {
1160 txgain = parse_gain_value(v->name, v->value);
1161 } else if (strcasecmp(v->name, "rxgain") == 0) {
1163 rxgain = parse_gain_value(v->name, v->value);
1166 else if (strcasecmp(v->name, "grunttimeout") == 0)
1167 gruntdetect_timeout = 1000*atoi(v->value);
1172 if (gruntdetect_timeout < 1000)
1173 gruntdetect_timeout = 1000;
1176 } ast_pthread_mutex_unlock(&iflock);
1182 ast_channel_register(type, tdesc,
1183 prefformat, vpb_request) != 0) {
1184 ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
1193 restart_monitor(); /* And start the monitor for the first time */
1202 /* First, take us out of the channel loop */
1203 ast_channel_unregister(type);
1205 ast_pthread_mutex_lock(&iflock); {
1206 /* Hangup all interfaces if they have an owner */
1210 ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
1214 } ast_pthread_mutex_unlock(&iflock);
1216 ast_pthread_mutex_lock(&monlock); {
1217 if (mthreadactive > -1) {
1218 pthread_cancel(monitor_thread);
1219 pthread_join(monitor_thread, NULL);
1222 } ast_pthread_mutex_unlock(&monlock);
1224 ast_pthread_mutex_lock(&iflock); {
1225 /* Destroy all the interfaces and free their memory */
1229 pthread_mutex_destroy(&p->lock);
1230 pthread_cancel(p->readthread);
1233 iflist = iflist->next;
1238 } ast_pthread_mutex_unlock(&iflock);
1240 ast_pthread_mutex_lock(&bridge_lock); {
1241 memset(bridges, 0, sizeof bridges);
1242 } ast_pthread_mutex_unlock(&bridge_lock);
1243 pthread_mutex_destroy(&bridge_lock);
1253 ast_pthread_mutex_lock(&usecnt_lock);
1255 ast_pthread_mutex_unlock(&usecnt_lock);
1266 return ASTERISK_GPL_KEY;
1269 #if defined(__cplusplus) || defined(c_plusplus)