2 * Asterisk -- A telephony toolkit for Linux.
4 * Generic Linux Telephony Interface driver
6 * Copyright (C) 1999, Mark Spencer
8 * Mark Spencer <markster@linux-support.net>
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 <asterisk/utils.h>
26 #include <asterisk/callerid.h>
27 #include <sys/socket.h>
32 #include <arpa/inet.h>
34 #include <sys/ioctl.h>
35 #include <linux/telephony.h>
36 /* Still use some IXJ specific stuff */
37 #include <linux/ixjuser.h>
40 #ifdef QTI_PHONEJACK_TJ_PCI /* check for the newer quicknet driver v.3.1.0 which has this symbol */
48 #define IXJ_PHONE_RING_START(x) ioctl(p->fd, PHONE_RING_START, &x);
49 #else /* FreeBSD and others */
50 #define IXJ_PHONE_RING_START(x) ioctl(p->fd, PHONE_RING_START, x);
51 #endif /* __linux__ */
52 #else /* older driver */
53 #define IXJ_PHONE_RING_START(x) ioctl(p->fd, PHONE_RING_START, &x);
57 #define DEFAULT_CALLER_ID "Unknown"
58 #define PHONE_MAX_BUF 480
59 #define DEFAULT_GAIN 0x100
61 static char *desc = "Linux Telephony API Support";
62 static char *type = "Phone";
63 static char *tdesc = "Standard Linux Telephony API Driver";
64 static char *config = "phone.conf";
66 /* Default context for dialtone mode */
67 static char context[AST_MAX_EXTENSION] = "default";
69 /* Default language */
70 static char language[MAX_LANGUAGE] = "";
73 static int echocancel = AEC_OFF;
75 static int silencesupression = 0;
77 static int prefformat = AST_FORMAT_G723_1 | AST_FORMAT_SLINEAR | AST_FORMAT_ULAW;
79 AST_MUTEX_DEFINE_STATIC(usecnt_lock);
81 /* Protect the interface list (of phone_pvt's) */
82 AST_MUTEX_DEFINE_STATIC(iflock);
84 /* Protect the monitoring thread, so only one process can kill or start it, and not
85 when it's doing something critical. */
86 AST_MUTEX_DEFINE_STATIC(monlock);
88 /* This is the thread for the monitor which checks for input on the channels
89 which are not currently in use. */
90 static pthread_t monitor_thread = AST_PTHREADT_NULL;
92 static int restart_monitor(void);
94 /* The private structures of the Phone Jack channels are linked for
95 selecting outgoing channels */
97 #define MODE_DIALTONE 1
98 #define MODE_IMMEDIATE 2
101 static struct phone_pvt {
102 int fd; /* Raw file descriptor for this device */
103 struct ast_channel *owner; /* Channel we belong to, possibly NULL */
104 int mode; /* Is this in the */
105 int lastformat; /* Last output format */
106 int lastinput; /* Last input format */
107 int ministate; /* Miniature state, for dialtone mode */
108 char dev[256]; /* Device name */
109 struct phone_pvt *next; /* Next channel in list */
110 struct ast_frame fr; /* Frame */
111 char offset[AST_FRIENDLY_OFFSET];
112 char buf[PHONE_MAX_BUF]; /* Static buffer for reading frames */
115 int txgain, rxgain; /* gain control for playing, recording */
116 /* 0x100 - 1.0, 0x200 - 2.0, 0x80 - 0.5 */
117 int cpt; /* Call Progress Tone playing? */
118 int silencesupression;
119 char context[AST_MAX_EXTENSION];
120 char obuf[PHONE_MAX_BUF * 2];
121 char ext[AST_MAX_EXTENSION];
122 char language[MAX_LANGUAGE];
123 char callerid[AST_MAX_EXTENSION];
126 static char callerid[AST_MAX_EXTENSION];
128 static int phone_digit(struct ast_channel *ast, char digit)
133 ast_log(LOG_NOTICE, "Dialed %c\n", digit);
145 outdigit = digit - '0';
155 ioctl(p->fd, IXJCTL_PSTN_SET_STATE, PSTN_ON_HOOK);
157 ioctl(p->fd, IXJCTL_PSTN_SET_STATE, PSTN_OFF_HOOK);
161 ast_log(LOG_WARNING, "Unknown digit '%c'\n", digit);
164 ast_log(LOG_NOTICE, "Dialed %i\n", outdigit);
165 ioctl(p->fd, PHONE_PLAY_TONE, outdigit);
170 static int phone_call(struct ast_channel *ast, char *dest, int timeout)
179 localtime_r(&UtcTime,&tm);
182 sprintf(cid.month, "%02d",(tm.tm_mon + 1));
183 sprintf(cid.day, "%02d", tm.tm_mday);
184 sprintf(cid.hour, "%02d", tm.tm_hour);
185 sprintf(cid.min, "%02d", tm.tm_min);
187 /* the standard format of ast->callerid is: "name" <number>, but not always complete */
188 if (!ast->callerid || ast_strlen_zero(ast->callerid)){
189 strcpy(cid.name, DEFAULT_CALLER_ID);
193 char callerid[256] = "";
194 strncpy(callerid, ast->callerid, sizeof(callerid) - 1);
195 ast_callerid_parse(callerid, &n, &l);
197 ast_shrink_phone_number(l);
198 if (!ast_isphonenumber(l))
202 strncpy(cid.number, l, sizeof(cid.number));
204 strncpy(cid.name, n, sizeof(cid.name));
209 if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
210 ast_log(LOG_WARNING, "phone_call called on %s, neither down nor reserved\n", ast->name);
214 ast_log(LOG_DEBUG, "Ringing %s on %s (%d)\n", dest, ast->name, ast->fds[0]);
216 IXJ_PHONE_RING_START(cid);
217 ast_setstate(ast, AST_STATE_RINGING);
218 ast_queue_control(ast, AST_CONTROL_RINGING);
222 static int phone_hangup(struct ast_channel *ast)
227 ast_log(LOG_DEBUG, "phone_hangup(%s)\n", ast->name);
228 if (!ast->pvt->pvt) {
229 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
232 /* XXX Is there anything we can do to really hang up except stop recording? */
233 ast_setstate(ast, AST_STATE_DOWN);
234 if (ioctl(p->fd, PHONE_REC_STOP))
235 ast_log(LOG_WARNING, "Failed to stop recording\n");
236 if (ioctl(p->fd, PHONE_PLAY_STOP))
237 ast_log(LOG_WARNING, "Failed to stop playing\n");
238 if (ioctl(p->fd, PHONE_RING_STOP))
239 ast_log(LOG_WARNING, "Failed to stop ringing\n");
240 if (ioctl(p->fd, PHONE_CPT_STOP))
241 ast_log(LOG_WARNING, "Failed to stop sounds\n");
243 /* If it's an FXO, hang them up */
244 if (p->mode == MODE_FXO) {
245 if (ioctl(p->fd, PHONE_PSTN_SET_STATE, PSTN_ON_HOOK))
246 ast_log(LOG_DEBUG, "ioctl(PHONE_PSTN_SET_STATE) failed on %s (%s)\n",ast->name, strerror(errno));
249 /* If they're off hook, give a busy signal */
250 if (ioctl(p->fd, PHONE_HOOKSTATE)) {
252 ast_log(LOG_DEBUG, "Got hunghup, giving busy signal\n");
253 ioctl(p->fd, PHONE_BUSY);
261 memset(p->ext, 0, sizeof(p->ext));
262 ((struct phone_pvt *)(ast->pvt->pvt))->owner = NULL;
263 ast_mutex_lock(&usecnt_lock);
266 ast_log(LOG_WARNING, "Usecnt < 0???\n");
267 ast_mutex_unlock(&usecnt_lock);
268 ast_update_use_count();
269 if (option_verbose > 2)
270 ast_verbose( VERBOSE_PREFIX_3 "Hungup '%s'\n", ast->name);
271 ast->pvt->pvt = NULL;
272 ast_setstate(ast, AST_STATE_DOWN);
277 static int phone_setup(struct ast_channel *ast)
281 ioctl(p->fd, PHONE_CPT_STOP);
282 /* Nothing to answering really, just start recording */
283 if (ast->pvt->rawreadformat == AST_FORMAT_G723_1) {
285 ioctl(p->fd, PHONE_REC_STOP);
286 if (p->lastinput != AST_FORMAT_G723_1) {
287 p->lastinput = AST_FORMAT_G723_1;
288 if (ioctl(p->fd, PHONE_REC_CODEC, G723_63)) {
289 ast_log(LOG_WARNING, "Failed to set codec to g723.1\n");
293 } else if (ast->pvt->rawreadformat == AST_FORMAT_SLINEAR) {
294 ioctl(p->fd, PHONE_REC_STOP);
295 if (p->lastinput != AST_FORMAT_SLINEAR) {
296 p->lastinput = AST_FORMAT_SLINEAR;
297 if (ioctl(p->fd, PHONE_REC_CODEC, LINEAR16)) {
298 ast_log(LOG_WARNING, "Failed to set codec to signed linear 16\n");
302 } else if (ast->pvt->rawreadformat == AST_FORMAT_ULAW) {
303 ioctl(p->fd, PHONE_REC_STOP);
304 if (p->lastinput != AST_FORMAT_ULAW) {
305 p->lastinput = AST_FORMAT_ULAW;
306 if (ioctl(p->fd, PHONE_REC_CODEC, ULAW)) {
307 ast_log(LOG_WARNING, "Failed to set codec to uLaw\n");
312 ast_log(LOG_WARNING, "Can't do format %s\n", ast_getformatname(ast->pvt->rawreadformat));
315 if (ioctl(p->fd, PHONE_REC_START)) {
316 ast_log(LOG_WARNING, "Failed to start recording\n");
319 //set the DTMF times (the default is too short)
320 ioctl(p->fd, PHONE_SET_TONE_ON_TIME, 300);
321 ioctl(p->fd, PHONE_SET_TONE_OFF_TIME, 200);
325 static int phone_answer(struct ast_channel *ast)
329 /* In case it's a LineJack, take it off hook */
330 if (p->mode == MODE_FXO) {
331 if (ioctl(p->fd, PHONE_PSTN_SET_STATE, PSTN_OFF_HOOK))
332 ast_log(LOG_DEBUG, "ioctl(PHONE_PSTN_SET_STATE) failed on %s (%s)\n", ast->name, strerror(errno));
334 ast_log(LOG_DEBUG, "Took linejack off hook\n");
338 ast_log(LOG_DEBUG, "phone_answer(%s)\n", ast->name);
340 ast_setstate(ast, AST_STATE_UP);
345 static char phone_2digit(char c)
351 else if ((c < 10) && (c >= 0))
358 static struct ast_frame *phone_exception(struct ast_channel *ast)
361 union telephony_exception phonee;
362 struct phone_pvt *p = ast->pvt->pvt;
365 /* Some nice norms */
373 phonee.bytes = ioctl(p->fd, PHONE_EXCEPTION);
374 if (phonee.bits.dtmf_ready) {
376 ast_log(LOG_DEBUG, "phone_exception(): DTMF\n");
378 /* We've got a digit -- Just handle this nicely and easily */
379 digit = ioctl(p->fd, PHONE_GET_DTMF_ASCII);
380 p->fr.subclass = digit;
381 p->fr.frametype = AST_FRAME_DTMF;
384 if (phonee.bits.hookstate) {
386 ast_log(LOG_DEBUG, "Hookstate changed\n");
387 res = ioctl(p->fd, PHONE_HOOKSTATE);
388 /* See if we've gone on hook, if so, notify by returning NULL */
390 ast_log(LOG_DEBUG, "New hookstate: %d\n", res);
391 if (!res && (p->mode != MODE_FXO))
394 if (ast->_state == AST_STATE_RINGING) {
395 /* They've picked up the phone */
396 p->fr.frametype = AST_FRAME_CONTROL;
397 p->fr.subclass = AST_CONTROL_ANSWER;
399 ast_setstate(ast, AST_STATE_UP);
402 ast_log(LOG_WARNING, "Got off hook in weird state %d\n", ast->_state);
406 if (phonee.bits.pstn_ring)
407 ast_verbose("Unit is ringing\n");
408 if (phonee.bits.caller_id) {
409 ast_verbose("We have caller ID\n");
411 if (phonee.bits.pstn_wink)
412 ast_verbose("Detected Wink\n");
414 /* Strange -- nothing there.. */
415 p->fr.frametype = AST_FRAME_NULL;
420 static struct ast_frame *phone_read(struct ast_channel *ast)
423 struct phone_pvt *p = ast->pvt->pvt;
426 /* Some nice norms */
434 /* Try to read some data... */
436 res = read(p->fd, p->buf, PHONE_MAX_BUF);
440 if (errno == EAGAIN) {
441 ast_log(LOG_WARNING, "Null frame received\n");
442 p->fr.frametype = AST_FRAME_NULL;
447 ast_log(LOG_WARNING, "Error reading: %s\n", strerror(errno));
451 switch(p->buf[0] & 0x3) {
458 /* VAD/CNG, only send two words */
464 p->fr.frametype = AST_FRAME_VOICE;
465 p->fr.subclass = p->lastinput;
466 p->fr.offset = AST_FRIENDLY_OFFSET;
470 static int phone_write_buf(struct phone_pvt *p, char *buf, int len, int frlen)
473 /* Store as much of the buffer as we can, then write fixed frames */
474 int space = sizeof(p->obuf) - p->obuflen;
475 /* Make sure we have enough buffer space to store the frame */
478 memcpy(p->obuf + p->obuflen, buf, len);
480 while(p->obuflen > frlen) {
481 res = write(p->fd, p->obuf, frlen);
485 * Card is in non-blocking mode now and it works well now, but there are
486 * lot of messages like this. So, this message is temporarily disabled.
490 ast_log(LOG_WARNING, "Only wrote %d of %d bytes\n", res, frlen);
494 /* Move memory if necessary */
496 memmove(p->obuf, p->obuf + frlen, p->obuflen);
501 static int phone_write(struct ast_channel *ast, struct ast_frame *frame)
503 struct phone_pvt *p = ast->pvt->pvt;
511 /* Write a frame of (presumably voice) data */
512 if (frame->frametype != AST_FRAME_VOICE) {
513 if (frame->frametype != AST_FRAME_IMAGE)
514 ast_log(LOG_WARNING, "Don't know what to do with frame type '%d'\n", frame->frametype);
517 if (!(frame->subclass &
518 (AST_FORMAT_G723_1 | AST_FORMAT_SLINEAR | AST_FORMAT_ULAW))) {
519 ast_log(LOG_WARNING, "Cannot handle frames in %d format\n", frame->subclass);
523 /* If we're not in up mode, go into up mode now */
524 if (ast->_state != AST_STATE_UP) {
525 ast_setstate(ast, AST_STATE_UP);
529 if (ast->_state != AST_STATE_UP) {
530 /* Don't try tos end audio on-hook */
534 if (frame->subclass == AST_FORMAT_G723_1) {
535 if (p->lastformat != AST_FORMAT_G723_1) {
536 ioctl(p->fd, PHONE_PLAY_STOP);
537 ioctl(p->fd, PHONE_REC_STOP);
538 if (ioctl(p->fd, PHONE_PLAY_CODEC, G723_63)) {
539 ast_log(LOG_WARNING, "Unable to set G723.1 mode\n");
542 if (ioctl(p->fd, PHONE_REC_CODEC, G723_63)) {
543 ast_log(LOG_WARNING, "Unable to set G723.1 mode\n");
546 p->lastformat = AST_FORMAT_G723_1;
547 p->lastinput = AST_FORMAT_G723_1;
548 /* Reset output buffer */
552 if (frame->datalen > 24) {
553 ast_log(LOG_WARNING, "Frame size too large for G.723.1 (%d bytes)\n", frame->datalen);
557 } else if (frame->subclass == AST_FORMAT_SLINEAR) {
558 if (p->lastformat != AST_FORMAT_SLINEAR) {
559 ioctl(p->fd, PHONE_PLAY_STOP);
560 ioctl(p->fd, PHONE_REC_STOP);
561 if (ioctl(p->fd, PHONE_PLAY_CODEC, LINEAR16)) {
562 ast_log(LOG_WARNING, "Unable to set 16-bit linear mode\n");
565 if (ioctl(p->fd, PHONE_REC_CODEC, LINEAR16)) {
566 ast_log(LOG_WARNING, "Unable to set 16-bit linear mode\n");
569 p->lastformat = AST_FORMAT_SLINEAR;
570 p->lastinput = AST_FORMAT_SLINEAR;
572 /* Reset output buffer */
576 } else if (frame->subclass == AST_FORMAT_ULAW) {
577 if (p->lastformat != AST_FORMAT_ULAW) {
578 ioctl(p->fd, PHONE_PLAY_STOP);
579 ioctl(p->fd, PHONE_REC_STOP);
580 if (ioctl(p->fd, PHONE_PLAY_CODEC, ULAW)) {
581 ast_log(LOG_WARNING, "Unable to set uLaw mode\n");
584 if (ioctl(p->fd, PHONE_REC_CODEC, ULAW)) {
585 ast_log(LOG_WARNING, "Unable to set uLaw mode\n");
588 p->lastformat = AST_FORMAT_ULAW;
589 p->lastinput = AST_FORMAT_ULAW;
591 /* Reset output buffer */
597 ioctl(p->fd, PHONE_REC_DEPTH, 3);
598 ioctl(p->fd, PHONE_PLAY_DEPTH, 3);
599 if (ioctl(p->fd, PHONE_PLAY_START)) {
600 ast_log(LOG_WARNING, "Failed to start playback\n");
603 if (ioctl(p->fd, PHONE_REC_START)) {
604 ast_log(LOG_WARNING, "Failed to start recording\n");
608 /* If we get here, we have a voice frame of Appropriate data */
611 while(sofar < frame->datalen) {
612 /* Write in no more than maxfr sized frames */
613 expected = frame->datalen - sofar;
614 if (maxfr < expected)
616 /* XXX Internet Phone Jack does not handle the 4-byte VAD frame properly! XXX
617 we have to pad it to 24 bytes still. */
618 if (frame->datalen == 4) {
619 if (p->silencesupression) {
620 memset(tmpbuf + 4, 0, sizeof(tmpbuf) - 4);
621 memcpy(tmpbuf, frame->data, 4);
623 res = phone_write_buf(p, tmpbuf, expected, maxfr);
628 res = phone_write_buf(p, pos, expected, maxfr);
630 if (res != expected) {
631 if ((errno != EAGAIN) && (errno != EINTR)) {
633 ast_log(LOG_WARNING, "Write returned error (%s)\n", strerror(errno));
635 * Card is in non-blocking mode now and it works well now, but there are
636 * lot of messages like this. So, this message is temporarily disabled.
640 ast_log(LOG_WARNING, "Only wrote %d of %d bytes\n", res, frame->datalen);
643 } else /* Pretend it worked */
652 static struct ast_channel *phone_new(struct phone_pvt *i, int state, char *context)
654 struct ast_channel *tmp;
655 tmp = ast_channel_alloc(1);
657 snprintf(tmp->name, sizeof(tmp->name), "Phone/%s", i->dev + 5);
660 /* XXX Switching formats silently causes kernel panics XXX */
661 tmp->nativeformats = prefformat;
662 tmp->pvt->rawreadformat = prefformat;
663 tmp->pvt->rawwriteformat = prefformat;
664 ast_setstate(tmp, state);
665 if (state == AST_STATE_RING)
668 tmp->pvt->send_digit = phone_digit;
669 tmp->pvt->call = phone_call;
670 tmp->pvt->hangup = phone_hangup;
671 tmp->pvt->answer = phone_answer;
672 tmp->pvt->read = phone_read;
673 tmp->pvt->write = phone_write;
674 tmp->pvt->exception = phone_exception;
675 strncpy(tmp->context, context, sizeof(tmp->context)-1);
677 strncpy(tmp->exten, i->ext, sizeof(tmp->exten)-1);
679 strncpy(tmp->exten, "s", sizeof(tmp->exten) - 1);
680 if (strlen(i->language))
681 strncpy(tmp->language, i->language, sizeof(tmp->language)-1);
682 if (strlen(i->callerid))
683 tmp->callerid = strdup(i->callerid);
685 ast_mutex_lock(&usecnt_lock);
687 ast_mutex_unlock(&usecnt_lock);
688 ast_update_use_count();
689 if (state != AST_STATE_DOWN) {
690 if (state == AST_STATE_RING) {
691 ioctl(tmp->fds[0], PHONE_RINGBACK);
694 if (ast_pbx_start(tmp)) {
695 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
700 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
704 static void phone_mini_packet(struct phone_pvt *i)
708 /* Ignore stuff we read... */
709 res = read(i->fd, buf, sizeof(buf));
711 ast_log(LOG_WARNING, "Read returned %d\n", res);
716 static void phone_check_exception(struct phone_pvt *i)
719 char digit[2] = {0 , 0};
720 union telephony_exception phonee;
721 /* XXX Do something XXX */
723 ast_log(LOG_DEBUG, "Exception!\n");
725 phonee.bytes = ioctl(i->fd, PHONE_EXCEPTION);
726 if (phonee.bits.dtmf_ready) {
727 digit[0] = ioctl(i->fd, PHONE_GET_DTMF_ASCII);
728 if (i->mode == MODE_DIALTONE) {
729 ioctl(i->fd, PHONE_PLAY_STOP);
730 ioctl(i->fd, PHONE_REC_STOP);
731 ioctl(i->fd, PHONE_CPT_STOP);
733 if (strlen(i->ext) < AST_MAX_EXTENSION - 1)
734 strcat(i->ext, digit);
735 if (ast_exists_extension(NULL, i->context, i->ext, 1, i->callerid)) {
736 /* It's a valid extension in its context, get moving! */
737 phone_new(i, AST_STATE_RING, i->context);
738 /* No need to restart monitor, we are the monitor */
740 ast_mutex_lock(&usecnt_lock);
742 ast_mutex_unlock(&usecnt_lock);
743 ast_update_use_count();
745 } else if (!ast_canmatch_extension(NULL, i->context, i->ext, 1, i->callerid)) {
746 /* There is nothing in the specified extension that can match anymore.
748 if (ast_exists_extension(NULL, "default", i->ext, 1, i->callerid)) {
749 /* Check the default, too... */
750 phone_new(i, AST_STATE_RING, "default");
752 ast_mutex_lock(&usecnt_lock);
754 ast_mutex_unlock(&usecnt_lock);
755 ast_update_use_count();
757 /* XXX This should probably be justified better XXX */
758 } else if (!ast_canmatch_extension(NULL, "default", i->ext, 1, i->callerid)) {
759 /* It's not a valid extension, give a busy signal */
761 ast_log(LOG_DEBUG, "%s can't match anything in %s or default\n", i->ext, i->context);
762 ioctl(i->fd, PHONE_BUSY);
767 ast_verbose("Extension is %s\n", i->ext);
771 if (phonee.bits.hookstate) {
772 offhook = ioctl(i->fd, PHONE_HOOKSTATE);
774 if (i->mode == MODE_IMMEDIATE) {
775 phone_new(i, AST_STATE_RING, i->context);
776 } else if (i->mode == MODE_DIALTONE) {
777 ast_mutex_lock(&usecnt_lock);
779 ast_mutex_unlock(&usecnt_lock);
780 ast_update_use_count();
781 /* Reset the extension */
783 /* Play the dialtone */
785 ioctl(i->fd, PHONE_PLAY_STOP);
786 ioctl(i->fd, PHONE_PLAY_CODEC, ULAW);
787 ioctl(i->fd, PHONE_PLAY_START);
792 ast_mutex_lock(&usecnt_lock);
794 ast_mutex_unlock(&usecnt_lock);
795 ast_update_use_count();
797 memset(i->ext, 0, sizeof(i->ext));
800 ioctl(i->fd, PHONE_CPT_STOP);
803 ioctl(i->fd, PHONE_PLAY_STOP);
804 ioctl(i->fd, PHONE_REC_STOP);
809 if (phonee.bits.pstn_ring) {
810 ast_verbose("Unit is ringing\n");
811 phone_new(i, AST_STATE_RING, i->context);
813 if (phonee.bits.caller_id)
814 ast_verbose("We have caller ID\n");
819 static void *do_monitor(void *data)
825 /* The tone we're playing this round */
826 struct timeval tv = {0,0};
828 /* This thread monitors all the frame relay interfaces which are not yet in use
829 (and thus do not have a separate thread) indefinitely */
830 /* From here on out, we die whenever asked */
831 if (pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL)) {
832 ast_log(LOG_WARNING, "Unable to set cancel type to asynchronous\n");
836 /* Don't let anybody kill us right away. Nobody should lock the interface list
837 and wait for the monitor list, but the other way around is okay. */
838 if (ast_mutex_lock(&monlock)) {
839 ast_log(LOG_ERROR, "Unable to grab monitor lock\n");
842 /* Lock the interface list */
843 if (ast_mutex_lock(&iflock)) {
844 ast_log(LOG_ERROR, "Unable to grab interface lock\n");
845 ast_mutex_unlock(&monlock);
848 /* Build the stuff we're going to select on, that is the socket of every
849 phone_pvt that does not have an associated owner channel */
856 if (FD_ISSET(i->fd, &rfds))
857 ast_log(LOG_WARNING, "Descriptor %d appears twice (%s)?\n", i->fd, i->dev);
859 /* This needs to be watched, as it lacks an owner */
860 FD_SET(i->fd, &rfds);
861 FD_SET(i->fd, &efds);
865 /* Remember we're going to have to come back and play
867 if (!tv.tv_usec && !tv.tv_sec) {
868 /* If we're due for a dialtone, play one */
869 if (write(i->fd, DialTone + tonepos, 240) != 240)
870 ast_log(LOG_WARNING, "Dial tone write error\n");
878 /* Okay, now that we know what to do, release the interface lock */
879 ast_mutex_unlock(&iflock);
881 /* And from now on, we're okay to be killed, so release the monitor lock as well */
882 ast_mutex_unlock(&monlock);
883 /* Wait indefinitely for something to happen */
885 /* If we're ready to recycle the time, set it to 30 ms */
887 if (tonepos >= sizeof(DialTone))
889 if (!tv.tv_usec && !tv.tv_sec) {
893 res = ast_select(n + 1, &rfds, NULL, &efds, &tv);
895 res = ast_select(n + 1, &rfds, NULL, &efds, NULL);
900 /* Okay, select has finished. Let's see what happened. */
902 ast_log(LOG_WARNING, "select return %d: %s\n", res, strerror(errno));
905 /* If there are no fd's changed, just continue, it's probably time
906 to play some more dialtones */
909 /* Alright, lock the interface list again, and let's look and see what has
911 if (ast_mutex_lock(&iflock)) {
912 ast_log(LOG_WARNING, "Unable to lock the interface list\n");
917 if (FD_ISSET(i->fd, &rfds)) {
919 ast_log(LOG_WARNING, "Whoa.... I'm owned but found (%d, %s)...\n", i->fd, i->dev);
922 phone_mini_packet(i);
924 if (FD_ISSET(i->fd, &efds)) {
926 ast_log(LOG_WARNING, "Whoa.... I'm owned but found (%d, %s)...\n", i->fd, i->dev);
929 phone_check_exception(i);
933 ast_mutex_unlock(&iflock);
940 static int restart_monitor()
942 /* If we're supposed to be stopped -- stay stopped */
943 if (monitor_thread == AST_PTHREADT_STOP)
945 if (ast_mutex_lock(&monlock)) {
946 ast_log(LOG_WARNING, "Unable to lock monitor\n");
949 if (monitor_thread == pthread_self()) {
950 ast_mutex_unlock(&monlock);
951 ast_log(LOG_WARNING, "Cannot kill myself\n");
954 if (monitor_thread != AST_PTHREADT_NULL) {
955 pthread_cancel(monitor_thread);
957 pthread_join(monitor_thread, NULL);
960 /* Start a new monitor */
961 if (pthread_create(&monitor_thread, NULL, do_monitor, NULL) < 0) {
962 ast_mutex_unlock(&monlock);
963 ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
966 ast_mutex_unlock(&monlock);
970 static struct phone_pvt *mkif(char *iface, int mode, int txgain, int rxgain)
972 /* Make a phone_pvt structure for this interface */
973 struct phone_pvt *tmp;
976 tmp = malloc(sizeof(struct phone_pvt));
978 tmp->fd = open(iface, O_RDWR);
980 ast_log(LOG_WARNING, "Unable to open '%s'\n", iface);
984 if (mode == MODE_FXO) {
985 if (ioctl(tmp->fd, IXJCTL_PORT, PORT_PSTN))
986 ast_log(LOG_DEBUG, "Unable to set port to PSTN\n");
988 if (ioctl(tmp->fd, IXJCTL_PORT, PORT_POTS))
989 ast_log(LOG_DEBUG, "Unable to set port to POTS\n");
991 ioctl(tmp->fd, PHONE_PLAY_STOP);
992 ioctl(tmp->fd, PHONE_REC_STOP);
993 ioctl(tmp->fd, PHONE_RING_STOP);
994 ioctl(tmp->fd, PHONE_CPT_STOP);
995 if (ioctl(tmp->fd, PHONE_PSTN_SET_STATE, PSTN_ON_HOOK))
996 ast_log(LOG_DEBUG, "ioctl(PHONE_PSTN_SET_STATE) failed on %s (%s)\n",iface, strerror(errno));
997 if (echocancel != AEC_OFF)
998 ioctl(tmp->fd, IXJCTL_AEC_START, echocancel);
999 if (silencesupression)
1000 tmp->silencesupression = 1;
1002 ioctl(tmp->fd, PHONE_VAD, tmp->silencesupression);
1005 flags = fcntl(tmp->fd, F_GETFL);
1006 fcntl(tmp->fd, F_SETFL, flags | O_NONBLOCK);
1008 tmp->lastformat = -1;
1009 tmp->lastinput = -1;
1011 memset(tmp->ext, 0, sizeof(tmp->ext));
1012 strncpy(tmp->language, language, sizeof(tmp->language)-1);
1013 strncpy(tmp->dev, iface, sizeof(tmp->dev)-1);
1014 strncpy(tmp->context, context, sizeof(tmp->context)-1);
1019 strncpy(tmp->callerid, callerid, sizeof(tmp->callerid)-1);
1020 tmp->txgain = txgain;
1021 ioctl(tmp->fd, PHONE_PLAY_VOLUME, tmp->txgain);
1022 tmp->rxgain = rxgain;
1023 ioctl(tmp->fd, PHONE_REC_VOLUME, tmp->rxgain);
1028 static struct ast_channel *phone_request(char *type, int format, void *data)
1031 struct phone_pvt *p;
1032 struct ast_channel *tmp = NULL;
1036 format &= (AST_FORMAT_G723_1 | AST_FORMAT_SLINEAR | AST_FORMAT_ULAW);
1038 ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%d'\n", oldformat);
1041 /* Search for an unowned channel */
1042 if (ast_mutex_lock(&iflock)) {
1043 ast_log(LOG_ERROR, "Unable to lock interface list???\n");
1048 if (!strcmp(name, p->dev + 5)) {
1050 tmp = phone_new(p, AST_STATE_DOWN, p->context);
1056 ast_mutex_unlock(&iflock);
1061 /* parse gain value from config file */
1062 static int parse_gain_value(char *gain_type, char *value)
1066 /* try to scan number */
1067 if (sscanf(value, "%f", &gain) != 1)
1069 ast_log(LOG_ERROR, "Invalid %s value '%s' in '%s' config\n",
1070 value, gain_type, config);
1071 return DEFAULT_GAIN;
1074 /* multiplicate gain by 1.0 gain value */
1075 gain = gain * (float)DEFAULT_GAIN;
1078 if (value[strlen(value) - 1] == '%')
1079 return (int)(gain / (float)100);
1084 static int __unload_module(void)
1086 struct phone_pvt *p, *pl;
1087 /* First, take us out of the channel loop */
1088 ast_channel_unregister(type);
1089 if (!ast_mutex_lock(&iflock)) {
1090 /* Hangup all interfaces if they have an owner */
1094 ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
1098 ast_mutex_unlock(&iflock);
1100 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
1103 if (!ast_mutex_lock(&monlock)) {
1104 if (monitor_thread > AST_PTHREADT_NULL) {
1105 pthread_cancel(monitor_thread);
1106 pthread_join(monitor_thread, NULL);
1108 monitor_thread = AST_PTHREADT_STOP;
1109 ast_mutex_unlock(&monlock);
1111 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
1115 if (!ast_mutex_lock(&iflock)) {
1116 /* Destroy all the interfaces and free their memory */
1119 /* Close the socket, assuming it's real */
1124 /* Free associated memory */
1128 ast_mutex_unlock(&iflock);
1130 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
1137 int unload_module(void)
1139 return __unload_module();
1144 struct ast_config *cfg;
1145 struct ast_variable *v;
1146 struct phone_pvt *tmp;
1147 int mode = MODE_IMMEDIATE;
1148 int txgain = DEFAULT_GAIN, rxgain = DEFAULT_GAIN; /* default gain 1.0 */
1149 cfg = ast_load(config);
1151 /* We *must* have a config file otherwise stop immediately */
1153 ast_log(LOG_ERROR, "Unable to load config %s\n", config);
1156 if (ast_mutex_lock(&iflock)) {
1157 /* It's a little silly to lock it, but we mind as well just to be sure */
1158 ast_log(LOG_ERROR, "Unable to lock interface list???\n");
1161 v = ast_variable_browse(cfg, "interfaces");
1163 /* Create the interface list */
1164 if (!strcasecmp(v->name, "device")) {
1165 tmp = mkif(v->value, mode, txgain, rxgain);
1171 ast_log(LOG_ERROR, "Unable to register channel '%s'\n", v->value);
1173 ast_mutex_unlock(&iflock);
1177 } else if (!strcasecmp(v->name, "silencesupression")) {
1178 silencesupression = ast_true(v->value);
1179 } else if (!strcasecmp(v->name, "language")) {
1180 strncpy(language, v->value, sizeof(language)-1);
1181 } else if (!strcasecmp(v->name, "callerid")) {
1182 strncpy(callerid, v->value, sizeof(callerid)-1);
1183 } else if (!strcasecmp(v->name, "mode")) {
1184 if (!strncasecmp(v->value, "di", 2))
1185 mode = MODE_DIALTONE;
1186 else if (!strncasecmp(v->value, "im", 2))
1187 mode = MODE_IMMEDIATE;
1188 else if (!strncasecmp(v->value, "fx", 2))
1191 ast_log(LOG_WARNING, "Unknown mode: %s\n", v->value);
1192 } else if (!strcasecmp(v->name, "context")) {
1193 strncpy(context, v->value, sizeof(context)-1);
1194 } else if (!strcasecmp(v->name, "format")) {
1195 if (!strcasecmp(v->value, "g723.1")) {
1196 prefformat = AST_FORMAT_G723_1;
1197 } else if (!strcasecmp(v->value, "slinear")) {
1198 prefformat = AST_FORMAT_SLINEAR;
1199 } else if (!strcasecmp(v->value, "ulaw")) {
1200 prefformat = AST_FORMAT_ULAW;
1202 ast_log(LOG_WARNING, "Unknown format '%s'\n", v->value);
1203 } else if (!strcasecmp(v->name, "echocancel")) {
1204 if (!strcasecmp(v->value, "off")) {
1205 echocancel = AEC_OFF;
1206 } else if (!strcasecmp(v->value, "low")) {
1207 echocancel = AEC_LOW;
1208 } else if (!strcasecmp(v->value, "medium")) {
1209 echocancel = AEC_MED;
1210 } else if (!strcasecmp(v->value, "high")) {
1211 echocancel = AEC_HIGH;
1213 ast_log(LOG_WARNING, "Unknown echo cancellation '%s'\n", v->value);
1214 } else if (!strcasecmp(v->name, "txgain")) {
1215 txgain = parse_gain_value(v->name, v->value);
1216 } else if (!strcasecmp(v->name, "rxgain")) {
1217 rxgain = parse_gain_value(v->name, v->value);
1221 ast_mutex_unlock(&iflock);
1222 /* Make sure we can register our Adtranphone channel type */
1223 if (ast_channel_register(type, tdesc,
1224 AST_FORMAT_G723_1 | AST_FORMAT_SLINEAR | AST_FORMAT_ULAW, phone_request)) {
1225 ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
1231 /* And start the monitor for the first time */
1239 ast_mutex_lock(&usecnt_lock);
1241 ast_mutex_unlock(&usecnt_lock);
1252 return ASTERISK_GPL_KEY;