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 ast_mutex_t usecnt_lock = AST_MUTEX_INITIALIZER;
76 /* Protect the interface list (of vpb_pvt's) */
77 static ast_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 ast_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
100 static VPB_TONE Dialtone = {440, 440, 440, 0, 0, 0, 5000, 0 };
101 static VPB_TONE Busytone = {440, 0, 0, 0, -100, -100, 500, 500};
102 static VPB_TONE Ringbacktone = {440, 0, 0, 0, -100, -100, 100, 100};
106 #define VPB_MAX_BRIDGES 128
107 static struct vpb_bridge_t {
109 struct ast_channel *c0, *c1, **rc;
110 struct ast_frame **fo;
115 } bridges[VPB_MAX_BRIDGES]; /* Bridges...*/
117 static ast_mutex_t bridge_lock = AST_MUTEX_INITIALIZER;
119 static struct vpb_pvt {
121 struct ast_channel *owner; /* Channel we belong to, possibly NULL */
122 int mode; /* Is this in the */
123 int handle; /* Handle for vpb interface */
127 struct ast_frame f, fr;
128 char buf[VPB_MAX_BUF]; /* Static buffer for reading frames */
129 char obuf[VPB_MAX_BUF];
132 float txgain, rxgain; /* gain control for playing, recording */
134 int wantdtmf; /* Waiting for DTMF. */
135 int silencesupression;
136 char context[AST_MAX_EXTENSION];
138 char ext[AST_MAX_EXTENSION];
139 char language[MAX_LANGUAGE];
140 char callerid[AST_MAX_EXTENSION];
145 struct vpb_bridge_t *bridge;
146 void *timer; /* For call timeout. */
153 int stopreads; /* Stop reading...*/
154 pthread_t readthread; /* For monitoring read channel. One per owned channel. */
156 struct vpb_pvt *next; /* Next channel in list */
159 static char callerid[AST_MAX_EXTENSION];
161 static int vpb_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc)
163 struct vpb_pvt *p0 = (struct vpb_pvt *)c0->pvt->pvt;
164 struct vpb_pvt *p1 = (struct vpb_pvt *)c1->pvt->pvt;
165 int i, len = sizeof bridges/sizeof bridges[0], res;
167 /* Bridge channels, check if we can. I believe we always can, so find a slot.*/
169 ast_mutex_lock(&bridge_lock); {
170 for (i = 0; i < len; i++)
171 if (!bridges[i].inuse)
174 bridges[i].inuse = 1;
175 bridges[i].flags = flags;
180 pthread_mutex_init(&bridges[i].lock, NULL);
181 pthread_cond_init(&bridges[i].cond, NULL);
183 } ast_mutex_unlock(&bridge_lock);
186 ast_log(LOG_WARNING, "Failed to bridge %s and %s!\n", c0->name, c1->name);
190 /* Set bridge pointers. You don't want to take these locks while holding bridge lock.*/
191 ast_mutex_lock(&p0->lock); {
192 p0->bridge = &bridges[i];
193 } ast_mutex_unlock(&p0->lock);
195 ast_mutex_lock(&p1->lock); {
196 p1->bridge = &bridges[i];
197 } ast_mutex_unlock(&p1->lock);
199 if (option_verbose > 4)
200 ast_verbose(VERBOSE_PREFIX_3
201 " Bridging call entered with [%s, %s]\n", c0->name, c1->name);
203 res = vpb_bridge(p0->handle, p1->handle, VPB_BRIDGE_ON, 0);
208 res = pthread_cond_wait(&bridges[i].cond, &bridges[i].lock); /* Wait for condition signal. */
211 done: /* Out of wait. */
213 vpb_bridge(p0->handle, p1->handle, VPB_BRIDGE_OFF, 0);
216 ast_mutex_lock(&bridge_lock); {
217 bridges[i].inuse = 0;
218 pthread_mutex_destroy(&bridges[i].lock);
219 pthread_cond_destroy(&bridges[i].cond);
220 } ast_mutex_unlock(&bridge_lock);
222 ast_mutex_lock(&p0->lock); {
224 } ast_mutex_unlock(&p0->lock);
226 ast_mutex_lock(&p1->lock); {
228 } ast_mutex_unlock(&p1->lock);
231 if (option_verbose > 4)
232 ast_verbose(VERBOSE_PREFIX_3
233 " Bridging call done with [%s, %s] => %d\n", c0->name, c1->name, res);
235 if (res != 0 && res != VPB_OK) /* Don't assume VPB_OK is zero! */
241 static inline int monitor_handle_owned(struct vpb_pvt *p, VPB_EVENT *e)
243 struct ast_frame f = {AST_FRAME_CONTROL}; /* default is control, Clear rest. */
246 if (option_verbose > 4)
247 ast_verbose(VERBOSE_PREFIX_3 " %s handle_owned got event: [%d=>%d]\n",
248 p->dev, e->type, e->data);
253 if (p->mode == MODE_FXO)
254 f.subclass = AST_CONTROL_RING;
256 f.frametype = -1; /* ignore ring on station port. */
259 if (p->calling) { /* This means time to stop calling. */
260 f.subclass = AST_CONTROL_BUSY;
261 vpb_timer_close(p->timer);
263 f.frametype = -1; /* Ignore. */
266 if (p->owner->_state == AST_STATE_UP) {
267 f.frametype = AST_FRAME_DTMF;
268 f.subclass = e->data;
274 if (e->data == VPB_BUSY || e->data == VPB_BUSY_308)
275 f.subclass = AST_CONTROL_BUSY;
276 else if (e->data == VPB_GRUNT) {
277 p->lastgrunt = tcounter;
284 if (e->data == VPB_CALL_CONNECTED)
285 f.subclass = AST_CONTROL_ANSWER;
286 else if (e->data == VPB_CALL_NO_DIAL_TONE ||
287 e->data == VPB_CALL_NO_RING_BACK)
288 f.subclass = AST_CONTROL_CONGESTION;
289 else if (e->data == VPB_CALL_NO_ANSWER ||
290 e->data == VPB_CALL_BUSY)
291 f.subclass = AST_CONTROL_BUSY;
292 else if (e->data == VPB_CALL_DISCONNECTED)
293 f.subclass = AST_CONTROL_HANGUP;
296 case VPB_STATION_OFFHOOK:
297 f.subclass = AST_CONTROL_ANSWER;
300 case VPB_STATION_ONHOOK:
301 f.subclass = AST_CONTROL_HANGUP;
304 case VPB_STATION_FLASH:
305 f.subclass = AST_CONTROL_FLASH;
309 f.subclass = AST_CONTROL_ANSWER;
317 if (option_verbose > 4)
318 ast_verbose(VERBOSE_PREFIX_3 " handle_owned: putting frame: [%d=>%d], bridge=%p\n",
319 f.frametype, f.subclass, (void *)p->bridge);
321 ast_mutex_lock(&p->lock); {
322 if (p->bridge) { /* Check what happened, see if we need to report it. */
323 switch (f.frametype) {
325 if (!(p->bridge->c0 == p->owner &&
326 (p->bridge->flags & AST_BRIDGE_DTMF_CHANNEL_0) ) &&
327 !(p->bridge->c1 == p->owner &&
328 (p->bridge->flags & AST_BRIDGE_DTMF_CHANNEL_1) ))
329 /* Kill bridge, this is interesting. */
333 case AST_FRAME_CONTROL:
334 if (!(p->bridge->flags & AST_BRIDGE_IGNORE_SIGS))
336 if (f.subclass == AST_CONTROL_BUSY ||
337 f.subclass == AST_CONTROL_CONGESTION ||
338 f.subclass == AST_CONTROL_HANGUP ||
339 f.subclass == AST_CONTROL_FLASH)
349 *p->bridge->fo = ast_frisolate(&f);
351 *p->bridge->rc = p->owner;
353 ast_mutex_lock(&p->bridge->lock); {
354 pthread_cond_signal(&p->bridge->cond);
355 } ast_mutex_unlock(&p->bridge->lock);
358 } ast_mutex_unlock(&p->lock);
360 if (endbridge) return 0;
362 if (f.frametype >= 0 && f.frametype != AST_FRAME_NULL)
363 ast_queue_frame(p->owner, &f, 0);
367 static struct ast_channel *vpb_new(struct vpb_pvt *i, int state, char *context);
369 static inline int monitor_handle_notowned(struct vpb_pvt *p, VPB_EVENT *e)
373 if (option_verbose > 4)
374 ast_verbose(VERBOSE_PREFIX_3 " %s: In not owned, mode=%d, [%d=>%d]\n",
375 p->dev, p->mode, e->type, e->data);
379 if (p->mode == MODE_FXO) /* FXO port ring, start * */
380 vpb_new(p, AST_STATE_RING, p->context);
382 case VPB_STATION_OFFHOOK:
383 if (p->mode == MODE_IMMEDIATE)
384 vpb_new(p,AST_STATE_RING, p->context);
386 vpb_playtone_async(p->handle, &Dialtone);
388 p->ext[0] = 0; /* Just to be sure & paranoid.*/
392 case VPB_STATION_ONHOOK: /*, clear ext */
393 vpb_tone_terminate(p->handle);
399 if (p->wantdtmf == 1) {
400 vpb_tone_terminate(p->handle);
406 if (ast_exists_extension(NULL, p->context, p->ext, 1, p->callerid))
407 vpb_new(p,AST_STATE_RING, p->context);
408 else if (!ast_canmatch_extension(NULL, p->context, p->ext, 1, p->callerid))
409 if (ast_exists_extension(NULL, "default", p->ext, 1, p->callerid))
410 vpb_new(p,AST_STATE_RING, "default");
411 else if (!ast_canmatch_extension(NULL, "default", p->ext, 1, p->callerid)) {
414 "%s can't match anything in %s or default\n",
416 vpb_playtone_async(p->handle, &Busytone);
426 if (option_verbose > 4)
427 ast_verbose(VERBOSE_PREFIX_3 " %s: Done not owned, mode=%d, [%d=>%d]\n",
428 p->dev, p->mode, e->type, e->data);
433 static void *do_monitor(void *unused)
436 /* Monitor thread, doesn't die until explicitly killed. */
438 if (option_verbose > 4)
439 ast_verbose(VERBOSE_PREFIX_3 "Starting vpb monitor thread[%ld]\n",
441 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
445 char str[VPB_MAX_STR];
447 int res = vpb_get_event_sync(&e, VPB_WAIT_TIMEOUT);
453 ast_mutex_lock(&monlock),
454 ast_mutex_lock(&iflock); {
455 struct vpb_pvt *p = iflist; /* Find the pvt structure */
457 vpb_translate_event(&e, str);
459 if (e.type == VPB_NULL_EVENT)
460 goto done; /* Nothing to do, just a wakeup call.*/
461 while (p && p->handle != e.handle)
464 if (option_verbose > 2)
465 ast_verbose(VERBOSE_PREFIX_3 " Event [%d=>%s] on %s\n",
466 e.type, str, p ? p->dev : "null");
470 "Got event %s, no matching iface!\n", str);
475 /* Two scenarios: Are you owned or not. */
478 monitor_handle_owned(p, &e);
480 monitor_handle_notowned(p, &e);
483 } ast_mutex_unlock(&iflock);
484 ast_mutex_unlock(&monlock);
487 tcounter += VPB_WAIT_TIMEOUT; /* Ok, not quite but will suffice. */
495 static int restart_monitor(void)
499 /* If we're supposed to be stopped -- stay stopped */
500 if (mthreadactive == -2)
502 ast_mutex_lock(&monlock); {
503 if (monitor_thread == pthread_self()) {
504 ast_log(LOG_WARNING, "Cannot kill myself\n");
508 if (mthreadactive != -1) {
509 /* Why do other drivers kill the thread? No need says I, simply awake thread with event. */
512 e.type = VPB_NULL_EVENT;
517 /* Start a new monitor */
518 int pid = pthread_create(&monitor_thread, NULL, do_monitor, NULL);
520 ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
524 mthreadactive = 0; /* Started the thread!*/
527 if (option_verbose > 4)
528 ast_verbose(VERBOSE_PREFIX_3
529 "Starting vpb restast: thread[%d]\n",
534 } ast_mutex_unlock(&monlock);
539 struct vpb_pvt *mkif(int board, int channel, int mode, float txgain, float rxgain)
544 tmp = (struct vpb_pvt *)calloc(1, sizeof *tmp);
549 tmp->handle = vpb_open(board, channel);
551 if (tmp->handle < 0) {
552 ast_log(LOG_WARNING, "Unable to create channel vpb/%d-%d: %s\n",
553 board, channel, strerror(errno));
559 if (option_verbose > 4)
560 ast_verbose(VERBOSE_PREFIX_3 " vpb turned on echo cancel.\n");
561 vpb_echo_canc_enable();
562 vpb_echo_canc_force_adapt_on();
563 echocancel = 0; /* So we do not initialise twice! */
566 if (option_verbose > 4)
567 ast_verbose(VERBOSE_PREFIX_3 " vpb created channel: [%d:%d]\n",
570 sprintf(tmp->dev, "vpb/%d-%d", board, channel);
574 strcpy(tmp->language, language);
575 strcpy(tmp->context, context);
577 strcpy(tmp->callerid, callerid);
578 tmp->txgain = txgain;
580 tmp->rxgain = rxgain;
583 tmp->lastoutput = -1;
589 pthread_mutex_init(&tmp->lock, NULL);
592 vpb_record_set_gain(tmp->handle, rxgain);
594 vpb_play_set_gain(tmp->handle, txgain);
600 static int vpb_indicate(struct ast_channel *ast, int condition)
602 struct vpb_pvt *p = (struct vpb_pvt *)ast->pvt->pvt;
605 if (option_verbose > 4)
606 ast_verbose(VERBOSE_PREFIX_3 " vpb indicate on %s with %d\n",
610 case AST_CONTROL_BUSY:
611 case AST_CONTROL_CONGESTION:
612 res = vpb_playtone_async(p->handle, &Busytone);
614 case AST_CONTROL_RINGING:
615 res = vpb_playtone_async(p->handle, &Ringbacktone);
617 case AST_CONTROL_ANSWER:
618 case -1: /* -1 means stop playing? */
619 res = vpb_tone_terminate(p->handle);
621 case AST_CONTROL_HANGUP:
622 res = vpb_playtone_async(p->handle, &Busytone);
632 static int vpb_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
634 struct vpb_pvt *p = (struct vpb_pvt *)newchan->pvt->pvt;
637 "New owner for channel %s is %s\n", p->dev, newchan->name);
638 if (p->owner == oldchan)
641 if (newchan->_state == AST_STATE_RINGING)
642 vpb_indicate(newchan, AST_CONTROL_RINGING);
647 static int vpb_digit(struct ast_channel *ast, char digit)
654 return vpb_dial_sync(((struct vpb_pvt *)ast->pvt->pvt)->handle, s);
658 static int vpb_call(struct ast_channel *ast, char *dest, int timeout)
660 struct vpb_pvt *p = (struct vpb_pvt *)ast->pvt->pvt;
662 char *s = strrchr(dest, '/');
669 if (ast->_state != AST_STATE_DOWN && ast->_state != AST_STATE_RESERVED) {
670 ast_log(LOG_WARNING, "vpb_call on %s neither down nor reserved!\n",
674 if (p->mode != MODE_FXO) /* Station port, ring it. */
675 res = vpb_ring_station_async(p->handle, VPB_RING_STATION_ON,'1');
679 vpb_get_call(p->handle, &call);
681 call.dialtone_timeout = VPB_DIALTONE_WAIT;
682 call.answer_timeout = timeout;
683 call.ringback_timeout = VPB_RINGWAIT;
685 vpb_set_call(p->handle, &call);
687 if (option_verbose > 2)
688 ast_verbose(VERBOSE_PREFIX_3 " Calling %s on %s \n",
691 vpb_sethook_sync(p->handle,VPB_OFFHOOK);
693 res = vpb_dial_async(p->handle, s);
696 ast_log(LOG_DEBUG, "Call on %s to %s failed: %s\n",
697 ast->name, dest, vpb_strerror(res));
703 if (option_verbose > 2)
704 ast_verbose(VERBOSE_PREFIX_3
705 " VPB Calling %s [t=%d] on %s returned %d\n",
706 dest, timeout, ast->name, res);
710 vpb_timer_open(&p->timer, p->handle, 0, 100*timeout);
711 vpb_timer_start(p->timer);
714 ast_setstate(ast, AST_STATE_RINGING);
715 ast_queue_control(ast,AST_CONTROL_RINGING, 0);
721 static int vpb_hangup(struct ast_channel *ast)
725 if (option_verbose > 2)
726 ast_verbose(VERBOSE_PREFIX_3 " hangup on vpb (%s)\n", ast->name);
728 if (!ast->pvt || !ast->pvt->pvt) {
729 ast_log(LOG_WARNING, "channel (%s) not connected?\n", ast->name);
733 p = (struct vpb_pvt *)ast->pvt->pvt;
735 vpb_play_terminate(p->handle);
736 vpb_record_terminate(p->handle);
738 if (p->mode != MODE_FXO) { /* station port. */
739 vpb_ring_station_async(p->handle, VPB_RING_STATION_OFF,'1');
740 vpb_playtone_async(p->handle, &Busytone);
743 vpb_sethook_sync(p->handle, VPB_ONHOOK);
745 ast_setstate(ast,AST_STATE_DOWN);
747 ast_mutex_lock(&p->lock); {
748 p->lastinput = p->lastoutput = -1;
753 ast->pvt->pvt = NULL;
754 } ast_mutex_unlock(&p->lock);
756 ast_mutex_lock(&usecnt_lock); {
758 } ast_mutex_unlock(&usecnt_lock);
759 ast_update_use_count();
761 /* Stop thread doing reads. */
763 pthread_join(p->readthread, NULL);
765 if (option_verbose > 2)
766 ast_verbose(VERBOSE_PREFIX_3 " Hungup on %s complete\n", ast->name);
772 static int vpb_answer(struct ast_channel *ast)
774 struct vpb_pvt *p = (struct vpb_pvt *)ast->pvt->pvt;
776 if (p->mode == MODE_FXO)
777 vpb_sethook_sync(p->handle, VPB_OFFHOOK);
780 ast_log(LOG_DEBUG, "vpb answer on %s\n", ast->name);
782 ast_setstate(ast, AST_STATE_UP);
787 static struct ast_frame *vpb_read(struct ast_channel *ast)
789 struct vpb_pvt *p = (struct vpb_pvt *)ast->pvt->pvt;
790 static struct ast_frame f = {AST_FRAME_NULL};
793 ast_log(LOG_NOTICE, "vpb_read should never be called (chan=%s)!\n", p->dev);
798 static inline int ast2vpbformat(int ast_format)
802 case AST_FORMAT_ALAW:
804 case AST_FORMAT_SLINEAR:
806 case AST_FORMAT_ULAW:
808 case AST_FORMAT_ADPCM:
816 static inline int astformatbits(int ast_format)
820 case AST_FORMAT_ALAW:
821 case AST_FORMAT_ULAW:
823 case AST_FORMAT_SLINEAR:
825 case AST_FORMAT_ADPCM:
832 static int vpb_write(struct ast_channel *ast, struct ast_frame *frame)
834 struct vpb_pvt *p = (struct vpb_pvt *)ast->pvt->pvt;
835 int res = 0, fmt = 0;
837 if (frame->frametype != AST_FRAME_VOICE) {
838 ast_log(LOG_WARNING, "Don't know how to handle from type %d\n",
841 } else if (ast->_state != AST_STATE_UP) {
842 if (option_verbose > 4)
843 ast_log(LOG_WARNING, "Writing frame type [%d,%d] on chan %s not up\n",
844 frame->frametype, frame->subclass, ast->name);
850 fmt = ast2vpbformat(frame->subclass);
852 if (option_verbose > 4)
853 ast_verbose(VERBOSE_PREFIX_3
854 " Write chan %s: got frame type = %d\n",
855 p->dev, frame->subclass);
858 ast_log(LOG_WARNING, "vpb_write Cannot handle frames of %d format!\n",
864 if (p->lastoutput != fmt) {
865 vpb_play_buf_start(p->handle, fmt);
869 memcpy(p->obuf, frame->data,
870 (frame->datalen > (int)sizeof p->obuf) ? sizeof p->obuf : frame->datalen);
871 res = vpb_play_buf_sync(p->handle, p->obuf, frame->datalen);
878 /* Read monitor thread function. */
879 static void *do_chanreads(void *pvt)
881 struct vpb_pvt *p = (struct vpb_pvt *)pvt;
882 struct ast_frame *fr = &p->fr;
883 char *readbuf = ((char *)p->buf) + AST_FRIENDLY_OFFSET;
886 fr->frametype = AST_FRAME_VOICE;
890 memset(p->buf, 0, sizeof p->buf);
892 while (!p->stopreads && p->owner) {
894 struct ast_channel *owner = p->owner;
895 int afmt = (owner) ? owner->pvt->rawreadformat : AST_FORMAT_ALAW;
896 int state = (owner) ? owner->_state : AST_STATE_DOWN;
899 fmt = ast2vpbformat(afmt);
906 readlen = VPB_SAMPLES * astformatbits(afmt) / 8;
908 if (p->lastinput != fmt) {
909 if (option_verbose > 2)
910 ast_verbose(" Read_channel ## %s: Setting record mode, bridge = %d\n",
911 p->dev, p->bridge ? 1 : 0);
912 vpb_record_buf_start(p->handle, fmt);
916 ast_mutex_lock(&p->lock); {
918 if (p->bridge->c0 == p->owner &&
919 (p->bridge->flags & AST_BRIDGE_REC_CHANNEL_0))
921 else if (p->bridge->c1 == p->owner &&
922 (p->bridge->flags & AST_BRIDGE_REC_CHANNEL_1))
928 } ast_mutex_unlock(&p->lock);
930 if (state == AST_STATE_UP && bridgerec) {
931 /* Read only if up and not bridged, or a bridge for which we can read. */
932 res = vpb_record_buf_sync(p->handle, readbuf, readlen);
939 fr->samples = VPB_SAMPLES;
941 fr->datalen = readlen;
942 fr->offset = AST_FRIENDLY_OFFSET;
944 ast_mutex_lock(&p->lock); {
945 if (p->owner) ast_queue_frame(p->owner, fr, 0);
946 } ast_mutex_unlock(&p->lock);
951 if (option_verbose > 4)
952 ast_verbose(" Read_channel %s (state=%d), res=%d, bridge=%d\n",
953 p->dev, state, res, bridgerec);
956 /* When stopreads seen, go away! */
957 vpb_record_buf_finish(p->handle);
959 if (option_verbose > 4)
960 ast_verbose(" Read_channel %s terminating, stopreads=%d, owner=%s\n",
961 p->dev, p->stopreads, p->owner? "yes" : "no");
966 static struct ast_channel *vpb_new(struct vpb_pvt *i, int state, char *context)
968 struct ast_channel *tmp;
971 ast_log(LOG_WARNING, "Called vpb_new on owned channel (%s) ?!\n", i->dev);
975 tmp = ast_channel_alloc(1);
977 strncpy(tmp->name, i->dev, sizeof(tmp->name));
980 tmp->nativeformats = prefformat;
981 tmp->pvt->rawreadformat = AST_FORMAT_ALAW;
982 tmp->pvt->rawwriteformat = AST_FORMAT_ALAW;
983 ast_setstate(tmp, state);
984 if (state == AST_STATE_RING)
987 tmp->pvt->send_digit = vpb_digit;
988 tmp->pvt->call = vpb_call;
989 tmp->pvt->hangup = vpb_hangup;
990 tmp->pvt->answer = vpb_answer;
991 tmp->pvt->read = vpb_read;
992 tmp->pvt->write = vpb_write;
993 tmp->pvt->bridge = vpb_bridge;
994 tmp->pvt->indicate = vpb_indicate;
995 tmp->pvt->fixup = vpb_fixup;
997 strncpy(tmp->context, context, sizeof(tmp->context)-1);
999 strncpy(tmp->exten, i->ext, sizeof(tmp->exten)-1);
1001 strncpy(tmp->exten, "s", sizeof(tmp->exten) - 1);
1002 if (strlen(i->language))
1003 strncpy(tmp->language, i->language, sizeof(tmp->language)-1);
1004 if (strlen(i->callerid))
1005 tmp->callerid = strdup(i->callerid);
1009 i->lastinput = i->lastoutput = -1;
1010 i->lastgrunt = tcounter; /* Assume at least one grunt tone seen now. */
1012 ast_mutex_lock(&usecnt_lock);
1014 ast_mutex_unlock(&usecnt_lock);
1015 ast_update_use_count();
1016 if (state != AST_STATE_DOWN)
1017 if (ast_pbx_start(tmp)) {
1018 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
1022 i->stopreads = 0; /* So read thread runs. */
1023 /* Finally start read monitoring thread. */
1024 pthread_create(&i->readthread, NULL, do_chanreads, (void *)i);
1026 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
1030 static struct ast_channel *vpb_request(char *type, int format, void *data)
1034 struct ast_channel *tmp = NULL;
1035 char *name = strdup(data ? (char *)data : "");
1039 format &= prefformat;
1041 ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%d'\n", oldformat);
1046 s = strsep(&sepstr, "/"); /* Handle / issues */
1049 /* Search for an unowned channel */
1050 ast_mutex_lock(&iflock); {
1053 if (strncmp(s, p->dev + 4, sizeof p->dev) == 0)
1055 tmp = vpb_new(p, AST_STATE_DOWN, p->context);
1060 } ast_mutex_unlock(&iflock);
1063 if (option_verbose > 2)
1064 ast_verbose(VERBOSE_PREFIX_3 " %s requested, got: [%s]\n",
1065 name, tmp ? tmp->name : "None");
1073 static float parse_gain_value(char *gain_type, char *value)
1077 /* try to scan number */
1078 if (sscanf(value, "%f", &gain) != 1)
1080 ast_log(LOG_ERROR, "Invalid %s value '%s' in '%s' config\n",
1081 value, gain_type, config);
1082 return DEFAULT_GAIN;
1087 if (value[strlen(value) - 1] == '%')
1088 return gain / (float)100;
1095 struct ast_config *cfg;
1096 struct ast_variable *v;
1097 struct vpb_pvt *tmp;
1098 int board = 0, group = 0;
1099 int mode = MODE_IMMEDIATE;
1100 float txgain = DEFAULT_GAIN, rxgain = DEFAULT_GAIN;
1102 int error = 0; /* Error flag */
1105 setrxgain = settxgain = 0;
1107 cfg = ast_load(config);
1109 /* We *must* have a config file otherwise stop immediately */
1111 ast_log(LOG_ERROR, "Unable to load config %s\n", config);
1115 vpb_seterrormode(VPB_DEVELOPMENT);
1117 ast_mutex_lock(&iflock); {
1118 v = ast_variable_browse(cfg, "interfaces");
1120 /* Create the interface list */
1121 if (strcasecmp(v->name, "board") == 0)
1122 board = atoi(v->value);
1123 else if (strcasecmp(v->name, "group") == 0)
1124 group = atoi(v->value);
1125 else if (strcasecmp(v->name, "channel") == 0) {
1126 int channel = atoi(v->value);
1127 tmp = mkif(board, channel, mode, txgain, rxgain);
1134 "Unable to register channel '%s'\n", v->value);
1139 } else if (strcasecmp(v->name, "silencesupression") == 0)
1140 silencesupression = ast_true(v->value);
1141 else if (strcasecmp(v->name, "language") == 0)
1142 strncpy(language, v->value, sizeof(language)-1);
1143 else if (strcasecmp(v->name, "callerid") == 0)
1144 strncpy(callerid, v->value, sizeof(callerid)-1);
1145 else if (strcasecmp(v->name, "mode") == 0) {
1146 if (strncasecmp(v->value, "di", 2) == 0)
1147 mode = MODE_DIALTONE;
1148 else if (strncasecmp(v->value, "im", 2) == 0)
1149 mode = MODE_IMMEDIATE;
1150 else if (strncasecmp(v->value, "fx", 2) == 0)
1153 ast_log(LOG_WARNING, "Unknown mode: %s\n", v->value);
1154 } else if (!strcasecmp(v->name, "context"))
1155 strncpy(context, v->value, sizeof(context)-1);
1156 else if (!strcasecmp(v->name, "echocancel")) {
1157 if (!strcasecmp(v->value, "off"))
1161 } else if (strcasecmp(v->name, "txgain") == 0) {
1163 txgain = parse_gain_value(v->name, v->value);
1164 } else if (strcasecmp(v->name, "rxgain") == 0) {
1166 rxgain = parse_gain_value(v->name, v->value);
1169 else if (strcasecmp(v->name, "grunttimeout") == 0)
1170 gruntdetect_timeout = 1000*atoi(v->value);
1175 if (gruntdetect_timeout < 1000)
1176 gruntdetect_timeout = 1000;
1179 } ast_mutex_unlock(&iflock);
1185 ast_channel_register(type, tdesc,
1186 prefformat, vpb_request) != 0) {
1187 ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
1196 restart_monitor(); /* And start the monitor for the first time */
1205 /* First, take us out of the channel loop */
1206 ast_channel_unregister(type);
1208 ast_mutex_lock(&iflock); {
1209 /* Hangup all interfaces if they have an owner */
1213 ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
1217 } ast_mutex_unlock(&iflock);
1219 ast_mutex_lock(&monlock); {
1220 if (mthreadactive > -1) {
1221 pthread_cancel(monitor_thread);
1222 pthread_join(monitor_thread, NULL);
1225 } ast_mutex_unlock(&monlock);
1227 ast_mutex_lock(&iflock); {
1228 /* Destroy all the interfaces and free their memory */
1232 pthread_mutex_destroy(&p->lock);
1233 pthread_cancel(p->readthread);
1236 iflist = iflist->next;
1241 } ast_mutex_unlock(&iflock);
1243 ast_mutex_lock(&bridge_lock); {
1244 memset(bridges, 0, sizeof bridges);
1245 } ast_mutex_unlock(&bridge_lock);
1246 pthread_mutex_destroy(&bridge_lock);
1256 ast_mutex_lock(&usecnt_lock);
1258 ast_mutex_unlock(&usecnt_lock);
1269 return ASTERISK_GPL_KEY;
1272 #if defined(__cplusplus) || defined(c_plusplus)