Skip to content
Snippets Groups Projects
chan_phone.c 39.1 KiB
Newer Older
  • Learn to ignore specific revisions
  • Mark Spencer's avatar
    Mark Spencer committed
    /*
    
     * Asterisk -- An open source telephony toolkit.
    
    Mark Spencer's avatar
    Mark Spencer committed
     *
    
     * Copyright (C) 1999 - 2005, Digium, Inc.
    
    Mark Spencer's avatar
    Mark Spencer committed
     *
    
     * Mark Spencer <markster@digium.com>
    
    Mark Spencer's avatar
    Mark Spencer committed
     *
    
     * See http://www.asterisk.org for more information about
     * the Asterisk project. Please do not directly contact
     * any of the maintainers of this project for assistance;
     * the project provides a web site, mailing lists and IRC
     * channels for your use.
     *
    
    Mark Spencer's avatar
    Mark Spencer committed
     * This program is free software, distributed under the terms of
    
     * the GNU General Public License Version 2. See the LICENSE file
     * at the top of the source tree.
     */
    
    
     * \brief Generic Linux Telephony Interface driver
    
     *
     * \author Mark Spencer <markster@digium.com>
    
    Russell Bryant's avatar
    Russell Bryant committed
     * \ingroup channel_drivers
    
    Mark Spencer's avatar
    Mark Spencer committed
     */
    
    #include <stdio.h>
    #include <string.h>
    
    #include <ctype.h>
    
    Mark Spencer's avatar
    Mark Spencer committed
    #include <sys/socket.h>
    #include <sys/time.h>
    #include <errno.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <arpa/inet.h>
    #include <fcntl.h>
    #include <sys/ioctl.h>
    
    #include <signal.h>
    
    Mark Spencer's avatar
    Mark Spencer committed
    #include <linux/telephony.h>
    /* Still use some IXJ specific stuff */
    
    #include <linux/version.h>
    #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
    # include <linux/compiler.h>
    #endif
    
    Mark Spencer's avatar
    Mark Spencer committed
    #include <linux/ixjuser.h>
    
    Kevin P. Fleming's avatar
    Kevin P. Fleming committed
    ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
    
    
    #include "asterisk/lock.h"
    #include "asterisk/channel.h"
    #include "asterisk/config.h"
    #include "asterisk/logger.h"
    #include "asterisk/module.h"
    #include "asterisk/pbx.h"
    #include "asterisk/options.h"
    #include "asterisk/utils.h"
    #include "asterisk/callerid.h"
    #include "asterisk/causes.h"
    
    #include "asterisk/stringfields.h"
    
    
    Mark Spencer's avatar
    Mark Spencer committed
    #include "DialTone.h"
    
    
    #ifdef QTI_PHONEJACK_TJ_PCI	/* check for the newer quicknet driver v.3.1.0 which has this symbol */
    #define QNDRV_VER 310
    #else
    #define QNDRV_VER 100
    #endif
    
    #if QNDRV_VER > 100
    #ifdef __linux__
    #define IXJ_PHONE_RING_START(x)	ioctl(p->fd, PHONE_RING_START, &x);
    #else /* FreeBSD and others */
    #define IXJ_PHONE_RING_START(x)	ioctl(p->fd, PHONE_RING_START, x);
    #endif /* __linux__ */
    #else	/* older driver */
    #define IXJ_PHONE_RING_START(x)	ioctl(p->fd, PHONE_RING_START, &x);
    #endif
    
    
    #define DEFAULT_CALLER_ID "Unknown"
    
    Mark Spencer's avatar
    Mark Spencer committed
    #define PHONE_MAX_BUF 480
    
    Mark Spencer's avatar
    Mark Spencer committed
    #define DEFAULT_GAIN 0x100
    
    static const char desc[] = "Linux Telephony API Support";
    static const char tdesc[] = "Standard Linux Telephony API Driver";
    static const char config[] = "phone.conf";
    
    Mark Spencer's avatar
    Mark Spencer committed
    
    /* Default context for dialtone mode */
    static char context[AST_MAX_EXTENSION] = "default";
    
    
    Mark Spencer's avatar
    Mark Spencer committed
    /* Default language */
    static char language[MAX_LANGUAGE] = "";
    
    Mark Spencer's avatar
    Mark Spencer committed
    static int usecnt =0;
    
    static int echocancel = AEC_OFF;
    
    static int silencesupression = 0;
    
    
    Mark Spencer's avatar
    Mark Spencer committed
    static int prefformat = AST_FORMAT_G723_1 | AST_FORMAT_SLINEAR | AST_FORMAT_ULAW;
    
    AST_MUTEX_DEFINE_STATIC(usecnt_lock);
    
    Mark Spencer's avatar
    Mark Spencer committed
    
    /* Protect the interface list (of phone_pvt's) */
    
    AST_MUTEX_DEFINE_STATIC(iflock);
    
    Mark Spencer's avatar
    Mark Spencer committed
    
    /* Protect the monitoring thread, so only one process can kill or start it, and not
       when it's doing something critical. */
    
    AST_MUTEX_DEFINE_STATIC(monlock);
    
    
    /* Boolean value whether the monitoring thread shall continue. */
    static unsigned int monitor;
    
    Mark Spencer's avatar
    Mark Spencer committed
    /* This is the thread for the monitor which checks for input on the channels
       which are not currently in use.  */
    
    static pthread_t monitor_thread = AST_PTHREADT_NULL;
    
    Mark Spencer's avatar
    Mark Spencer committed
    
    static int restart_monitor(void);
    
    /* The private structures of the Phone Jack channels are linked for
       selecting outgoing channels */
       
    #define MODE_DIALTONE 	1
    #define MODE_IMMEDIATE	2
    
    Mark Spencer's avatar
    Mark Spencer committed
    #define MODE_FXO		3
    
    #define MODE_FXS        4
    
    
    Mark Spencer's avatar
    Mark Spencer committed
    static struct phone_pvt {
    	int fd;							/* Raw file descriptor for this device */
    	struct ast_channel *owner;		/* Channel we belong to, possibly NULL */
    	int mode;						/* Is this in the  */
    	int lastformat;					/* Last output format */
    	int lastinput;					/* Last input format */
    	int ministate;					/* Miniature state, for dialtone mode */
    	char dev[256];					/* Device name */
    	struct phone_pvt *next;			/* Next channel in list */
    	struct ast_frame fr;			/* Frame */
    	char offset[AST_FRIENDLY_OFFSET];
    
    Mark Spencer's avatar
    Mark Spencer committed
    	char buf[PHONE_MAX_BUF];					/* Static buffer for reading frames */
    
    Mark Spencer's avatar
    Mark Spencer committed
    	int obuflen;
    	int dialtone;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	int txgain, rxgain;             /* gain control for playing, recording  */
    									/* 0x100 - 1.0, 0x200 - 2.0, 0x80 - 0.5 */
    	int cpt;						/* Call Progress Tone playing? */
    
    Mark Spencer's avatar
    Mark Spencer committed
    	int silencesupression;
    	char context[AST_MAX_EXTENSION];
    
    Mark Spencer's avatar
    Mark Spencer committed
    	char obuf[PHONE_MAX_BUF * 2];
    
    Mark Spencer's avatar
    Mark Spencer committed
    	char ext[AST_MAX_EXTENSION];
    
    Mark Spencer's avatar
    Mark Spencer committed
    	char language[MAX_LANGUAGE];
    
    	char cid_num[AST_MAX_EXTENSION];
    	char cid_name[AST_MAX_EXTENSION];
    
    Mark Spencer's avatar
    Mark Spencer committed
    } *iflist = NULL;
    
    
    static char cid_num[AST_MAX_EXTENSION];
    static char cid_name[AST_MAX_EXTENSION];
    
    static struct ast_channel *phone_request(const char *type, int format, void *data, int *cause);
    static int phone_digit(struct ast_channel *ast, char digit);
    static int phone_call(struct ast_channel *ast, char *dest, int timeout);
    static int phone_hangup(struct ast_channel *ast);
    static int phone_answer(struct ast_channel *ast);
    static struct ast_frame *phone_read(struct ast_channel *ast);
    static int phone_write(struct ast_channel *ast, struct ast_frame *frame);
    static struct ast_frame *phone_exception(struct ast_channel *ast);
    
    static int phone_send_text(struct ast_channel *ast, const char *text);
    
    static int phone_fixup(struct ast_channel *old, struct ast_channel *new);
    
    static int phone_indicate(struct ast_channel *chan, int condition);
    
    
    static const struct ast_channel_tech phone_tech = {
    
    	.description = tdesc,
    	.capabilities = AST_FORMAT_G723_1 | AST_FORMAT_SLINEAR | AST_FORMAT_ULAW,
    	.requester = phone_request,
    	.send_digit = phone_digit,
    	.call = phone_call,
    	.hangup = phone_hangup,
    	.answer = phone_answer,
    	.read = phone_read,
    	.write = phone_write,
    	.exception = phone_exception,
    
    	.indicate = phone_indicate,
    
    	.fixup = phone_fixup
    
    };
    
    static struct ast_channel_tech phone_tech_fxs = {
    
    	.description = tdesc,
    	.requester = phone_request,
    	.send_digit = phone_digit,
    	.call = phone_call,
    	.hangup = phone_hangup,
    	.answer = phone_answer,
    	.read = phone_read,
    	.write = phone_write,
    	.exception = phone_exception,
    	.write_video = phone_write,
    	.send_text = phone_send_text,
    
    	.indicate = phone_indicate,
    
    	.fixup = phone_fixup
    
    static int phone_indicate(struct ast_channel *chan, int condition)
    {
    	struct phone_pvt *p = chan->tech_pvt;
    	int res=-1;
    	ast_log(LOG_DEBUG, "Requested indication %d on channel %s\n", condition, chan->name);
    	switch(condition) {
    		case AST_CONTROL_FLASH:
    			ioctl(p->fd, IXJCTL_PSTN_SET_STATE, PSTN_ON_HOOK);
    			usleep(320000);
    			ioctl(p->fd, IXJCTL_PSTN_SET_STATE, PSTN_OFF_HOOK);
    			p->lastformat = -1;
    			res = 0;
    			break;
    		default:
    			ast_log(LOG_WARNING, "Condition %d is not supported on channel %s\n", condition, chan->name);
    	}
    	return res;
    }
    
    
    static int phone_fixup(struct ast_channel *old, struct ast_channel *new)
    {
    	struct phone_pvt *pvt = old->tech_pvt;
    	if (pvt && pvt->owner == old)
    		pvt->owner = new;
    	return 0;
    }
    
    
    Mark Spencer's avatar
    Mark Spencer committed
    static int phone_digit(struct ast_channel *ast, char digit)
    {
    	struct phone_pvt *p;
    	int outdigit;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	ast_log(LOG_NOTICE, "Dialed %c\n", digit);
    
    Mark Spencer's avatar
    Mark Spencer committed
    	switch(digit) {
    	case '0':
    	case '1':
    	case '2':
    	case '3':
    	case '4':
    	case '5':
    	case '6':
    	case '7':
    	case '8':
    	case '9':
    
    Mark Spencer's avatar
    Mark Spencer committed
    		outdigit = digit - '0';
    
    Mark Spencer's avatar
    Mark Spencer committed
    		break;
    	case '*':
    		outdigit = 11;
    		break;
    	case '#':
    		outdigit = 12;
    		break;
    
    	case 'f':	/*flash*/
    
    Mark Spencer's avatar
    Mark Spencer committed
    	case 'F':
    		ioctl(p->fd, IXJCTL_PSTN_SET_STATE, PSTN_ON_HOOK);
    		usleep(320000);
    		ioctl(p->fd, IXJCTL_PSTN_SET_STATE, PSTN_OFF_HOOK);
    		p->lastformat = -1;
    		return 0;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	default:
    		ast_log(LOG_WARNING, "Unknown digit '%c'\n", digit);
    		return -1;
    	}
    
    Mark Spencer's avatar
    Mark Spencer committed
    	ioctl(p->fd, PHONE_PLAY_TONE, outdigit);
    
    Mark Spencer's avatar
    Mark Spencer committed
    	p->lastformat = -1;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	return 0;
    }
    
    static int phone_call(struct ast_channel *ast, char *dest, int timeout)
    {
    	struct phone_pvt *p;
    
    Mark Spencer's avatar
    Mark Spencer committed
    
    	PHONE_CID cid;
    	time_t UtcTime;
    
    	time(&UtcTime);
    	localtime_r(&UtcTime,&tm);
    
    	memset(&cid, 0, sizeof(PHONE_CID));
    
    	if(&tm != NULL) {
    
    		snprintf(cid.month, sizeof(cid.month), "%02d",(tm.tm_mon + 1));
    		snprintf(cid.day, sizeof(cid.day),     "%02d", tm.tm_mday);
    		snprintf(cid.hour, sizeof(cid.hour),   "%02d", tm.tm_hour);
    		snprintf(cid.min, sizeof(cid.min),     "%02d", tm.tm_min);
    
    	}
    	/* the standard format of ast->callerid is:  "name" <number>, but not always complete */
    
    	if (ast_strlen_zero(ast->cid.cid_name))
    
    		strncpy(cid.name, DEFAULT_CALLER_ID, sizeof(cid.name) - 1);
    
    	else
    		strncpy(cid.name, ast->cid.cid_name, sizeof(cid.name) - 1);
    
    	if (ast->cid.cid_num) 
    		strncpy(cid.number, ast->cid.cid_num, sizeof(cid.number) - 1);
    
    Mark Spencer's avatar
    Mark Spencer committed
    	if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
    
    Mark Spencer's avatar
    Mark Spencer committed
    		ast_log(LOG_WARNING, "phone_call called on %s, neither down nor reserved\n", ast->name);
    		return -1;
    	}
    	if (option_debug)
    
    Mark Spencer's avatar
    Mark Spencer committed
    		ast_log(LOG_DEBUG, "Ringing %s on %s (%d)\n", dest, ast->name, ast->fds[0]);
    
    	start = IXJ_PHONE_RING_START(cid);
    
    	if (start == -1)
    		return -1;
    	
    	if (p->mode == MODE_FXS) {
    		char *digit = strchr(dest, '/');
    		if (digit)
    		{
    		  digit++;
    		  while (*digit)
    		    phone_digit(ast, *digit++);
    		}
    	}
     
      	ast_setstate(ast, AST_STATE_RINGING);
    
    	ast_queue_control(ast, AST_CONTROL_RINGING);
    
    Mark Spencer's avatar
    Mark Spencer committed
    	return 0;
    }
    
    static int phone_hangup(struct ast_channel *ast)
    {
    	struct phone_pvt *p;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	if (option_debug)
    		ast_log(LOG_DEBUG, "phone_hangup(%s)\n", ast->name);
    
    Mark Spencer's avatar
    Mark Spencer committed
    		ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
    		return 0;
    	}
    	/* XXX Is there anything we can do to really hang up except stop recording? */
    
    Mark Spencer's avatar
    Mark Spencer committed
    	ast_setstate(ast, AST_STATE_DOWN);
    
    Mark Spencer's avatar
    Mark Spencer committed
    	if (ioctl(p->fd, PHONE_REC_STOP))
    		ast_log(LOG_WARNING, "Failed to stop recording\n");
    	if (ioctl(p->fd, PHONE_PLAY_STOP))
    		ast_log(LOG_WARNING, "Failed to stop playing\n");
    	if (ioctl(p->fd, PHONE_RING_STOP))
    		ast_log(LOG_WARNING, "Failed to stop ringing\n");
    	if (ioctl(p->fd, PHONE_CPT_STOP))
    		ast_log(LOG_WARNING, "Failed to stop sounds\n");
    
    Mark Spencer's avatar
    Mark Spencer committed
    
    	/* If it's an FXO, hang them up */
    	if (p->mode == MODE_FXO) {
    		if (ioctl(p->fd, PHONE_PSTN_SET_STATE, PSTN_ON_HOOK)) 
    			ast_log(LOG_DEBUG, "ioctl(PHONE_PSTN_SET_STATE) failed on %s (%s)\n",ast->name, strerror(errno));
    	}
    
    
    Mark Spencer's avatar
    Mark Spencer committed
    	/* If they're off hook, give a busy signal */
    	if (ioctl(p->fd, PHONE_HOOKSTATE)) {
    		if (option_debug)
    			ast_log(LOG_DEBUG, "Got hunghup, giving busy signal\n");
    		ioctl(p->fd, PHONE_BUSY);
    
    Mark Spencer's avatar
    Mark Spencer committed
    		p->cpt = 1;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	}
    	p->lastformat = -1;
    	p->lastinput = -1;
    	p->ministate = 0;
    	p->obuflen = 0;
    	p->dialtone = 0;
    	memset(p->ext, 0, sizeof(p->ext));
    
    	((struct phone_pvt *)(ast->tech_pvt))->owner = NULL;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	usecnt--;
    	if (usecnt < 0) 
    		ast_log(LOG_WARNING, "Usecnt < 0???\n");
    
    Mark Spencer's avatar
    Mark Spencer committed
    	ast_update_use_count();
    	if (option_verbose > 2) 
    		ast_verbose( VERBOSE_PREFIX_3 "Hungup '%s'\n", ast->name);
    
    Mark Spencer's avatar
    Mark Spencer committed
    	ast_setstate(ast, AST_STATE_DOWN);
    
    Mark Spencer's avatar
    Mark Spencer committed
    	restart_monitor();
    	return 0;
    }
    
    static int phone_setup(struct ast_channel *ast)
    {
    	struct phone_pvt *p;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	ioctl(p->fd, PHONE_CPT_STOP);
    	/* Nothing to answering really, just start recording */
    
    	if (ast->rawreadformat == AST_FORMAT_G723_1) {
    
    Mark Spencer's avatar
    Mark Spencer committed
    		/* Prefer g723 */
    		ioctl(p->fd, PHONE_REC_STOP);
    		if (p->lastinput != AST_FORMAT_G723_1) {
    			p->lastinput = AST_FORMAT_G723_1;
    			if (ioctl(p->fd, PHONE_REC_CODEC, G723_63)) {
    				ast_log(LOG_WARNING, "Failed to set codec to g723.1\n");
    				return -1;
    			}
    		}
    
    	} else if (ast->rawreadformat == AST_FORMAT_SLINEAR) {
    
    Mark Spencer's avatar
    Mark Spencer committed
    		ioctl(p->fd, PHONE_REC_STOP);
    		if (p->lastinput != AST_FORMAT_SLINEAR) {
    			p->lastinput = AST_FORMAT_SLINEAR;
    			if (ioctl(p->fd, PHONE_REC_CODEC, LINEAR16)) {
    				ast_log(LOG_WARNING, "Failed to set codec to signed linear 16\n");
    				return -1;
    			}
    		}
    
    	} else if (ast->rawreadformat == AST_FORMAT_ULAW) {
    
    Mark Spencer's avatar
    Mark Spencer committed
    		ioctl(p->fd, PHONE_REC_STOP);
    		if (p->lastinput != AST_FORMAT_ULAW) {
    			p->lastinput = AST_FORMAT_ULAW;
    			if (ioctl(p->fd, PHONE_REC_CODEC, ULAW)) {
    
    				ast_log(LOG_WARNING, "Failed to set codec to uLaw\n");
    
    Mark Spencer's avatar
    Mark Spencer committed
    				return -1;
    			}
    		}
    
    	} else if (p->mode == MODE_FXS) {
    		ioctl(p->fd, PHONE_REC_STOP);
    
    		if (p->lastinput != ast->rawreadformat) {
    			p->lastinput = ast->rawreadformat;
    			if (ioctl(p->fd, PHONE_REC_CODEC, ast->rawreadformat)) {
    
    				ast_log(LOG_WARNING, "Failed to set codec to %d\n", 
    
    Mark Spencer's avatar
    Mark Spencer committed
    	} else {
    
    		ast_log(LOG_WARNING, "Can't do format %s\n", ast_getformatname(ast->rawreadformat));
    
    Mark Spencer's avatar
    Mark Spencer committed
    		return -1;
    	}
    	if (ioctl(p->fd, PHONE_REC_START)) {
    		ast_log(LOG_WARNING, "Failed to start recording\n");
    		return -1;
    	}
    
    	/* set the DTMF times (the default is too short) */
    
    Mark Spencer's avatar
    Mark Spencer committed
    	ioctl(p->fd, PHONE_SET_TONE_ON_TIME, 300);
    	ioctl(p->fd, PHONE_SET_TONE_OFF_TIME, 200);
    
    Mark Spencer's avatar
    Mark Spencer committed
    	return 0;
    }
    
    static int phone_answer(struct ast_channel *ast)
    {
    
    Mark Spencer's avatar
    Mark Spencer committed
    	struct phone_pvt *p;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	/* In case it's a LineJack, take it off hook */
    	if (p->mode == MODE_FXO) {
    		if (ioctl(p->fd, PHONE_PSTN_SET_STATE, PSTN_OFF_HOOK)) 
    			ast_log(LOG_DEBUG, "ioctl(PHONE_PSTN_SET_STATE) failed on %s (%s)\n", ast->name, strerror(errno));
    		else
    			ast_log(LOG_DEBUG, "Took linejack off hook\n");
    	}
    
    Mark Spencer's avatar
    Mark Spencer committed
    	phone_setup(ast);
    	if (option_debug)
    		ast_log(LOG_DEBUG, "phone_answer(%s)\n", ast->name);
    	ast->rings = 0;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	ast_setstate(ast, AST_STATE_UP);
    
    Mark Spencer's avatar
    Mark Spencer committed
    	return 0;
    }
    
    
    Mark Spencer's avatar
    Mark Spencer committed
    static char phone_2digit(char c)
    {
    	if (c == 12)
    		return '#';
    	else if (c == 11)
    		return '*';
    	else if ((c < 10) && (c >= 0))
    		return '0' + c - 1;
    	else
    		return '?';
    }
    
    Mark Spencer's avatar
    Mark Spencer committed
    static struct ast_frame  *phone_exception(struct ast_channel *ast)
    
    Mark Spencer's avatar
    Mark Spencer committed
    {
    	int res;
    	union telephony_exception phonee;
    
    	struct phone_pvt *p = ast->tech_pvt;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	char digit;
    
    	/* Some nice norms */
    	p->fr.datalen = 0;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	p->fr.samples = 0;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	p->fr.data =  NULL;
    
    	p->fr.src = "Phone";
    
    Mark Spencer's avatar
    Mark Spencer committed
    	p->fr.offset = 0;
    	p->fr.mallocd=0;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	phonee.bytes = ioctl(p->fd, PHONE_EXCEPTION);
    	if (phonee.bits.dtmf_ready)  {
    
    Mark Spencer's avatar
    Mark Spencer committed
    		if (option_debug)
    
    Mark Spencer's avatar
    Mark Spencer committed
    			ast_log(LOG_DEBUG, "phone_exception(): DTMF\n");
    
    Mark Spencer's avatar
    Mark Spencer committed
    		/* We've got a digit -- Just handle this nicely and easily */
    		digit =  ioctl(p->fd, PHONE_GET_DTMF_ASCII);
    		p->fr.subclass = digit;
    		p->fr.frametype = AST_FRAME_DTMF;
    		return &p->fr;
    	}
    	if (phonee.bits.hookstate) {
    
    Mark Spencer's avatar
    Mark Spencer committed
    		if (option_debug)
    			ast_log(LOG_DEBUG, "Hookstate changed\n");
    
    Mark Spencer's avatar
    Mark Spencer committed
    		res = ioctl(p->fd, PHONE_HOOKSTATE);
    		/* See if we've gone on hook, if so, notify by returning NULL */
    
    Mark Spencer's avatar
    Mark Spencer committed
    		if (option_debug)
    			ast_log(LOG_DEBUG, "New hookstate: %d\n", res);
    
    Mark Spencer's avatar
    Mark Spencer committed
    		if (!res && (p->mode != MODE_FXO))
    
    Mark Spencer's avatar
    Mark Spencer committed
    			return NULL;
    		else {
    
    Mark Spencer's avatar
    Mark Spencer committed
    			if (ast->_state == AST_STATE_RINGING) {
    
    Mark Spencer's avatar
    Mark Spencer committed
    				/* They've picked up the phone */
    				p->fr.frametype = AST_FRAME_CONTROL;
    				p->fr.subclass = AST_CONTROL_ANSWER;
    				phone_setup(ast);
    
    Mark Spencer's avatar
    Mark Spencer committed
    				ast_setstate(ast, AST_STATE_UP);
    
    Mark Spencer's avatar
    Mark Spencer committed
    				return &p->fr;
    			}  else 
    
    Mark Spencer's avatar
    Mark Spencer committed
    				ast_log(LOG_WARNING, "Got off hook in weird state %d\n", ast->_state);
    
    Mark Spencer's avatar
    Mark Spencer committed
    #if 1
    
    Mark Spencer's avatar
    Mark Spencer committed
    	if (phonee.bits.pstn_ring)
    		ast_verbose("Unit is ringing\n");
    	if (phonee.bits.caller_id) {
    
    Mark Spencer's avatar
    Mark Spencer committed
    		ast_verbose("We have caller ID\n");
    
    Mark Spencer's avatar
    Mark Spencer committed
    	}
    
    Mark Spencer's avatar
    Mark Spencer committed
    	if (phonee.bits.pstn_wink)
    		ast_verbose("Detected Wink\n");
    
    Mark Spencer's avatar
    Mark Spencer committed
    #endif
    
    Mark Spencer's avatar
    Mark Spencer committed
    	/* Strange -- nothing there.. */
    	p->fr.frametype = AST_FRAME_NULL;
    	p->fr.subclass = 0;
    	return &p->fr;
    }
    
    static struct ast_frame  *phone_read(struct ast_channel *ast)
    {
    	int res;
    
    	struct phone_pvt *p = ast->tech_pvt;
    
    Mark Spencer's avatar
    Mark Spencer committed
    
    	/* Some nice norms */
    	p->fr.datalen = 0;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	p->fr.samples = 0;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	p->fr.data =  NULL;
    
    	p->fr.src = "Phone";
    
    Mark Spencer's avatar
    Mark Spencer committed
    	p->fr.offset = 0;
    	p->fr.mallocd=0;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	/* Try to read some data... */
    	CHECK_BLOCKING(ast);
    
    Mark Spencer's avatar
    Mark Spencer committed
    	res = read(p->fd, p->buf, PHONE_MAX_BUF);
    
    	ast_clear_flag(ast, AST_FLAG_BLOCKING);
    
    Mark Spencer's avatar
    Mark Spencer committed
    	if (res < 0) {
    #if 0
    		if (errno == EAGAIN) {
    			ast_log(LOG_WARNING, "Null frame received\n");
    			p->fr.frametype = AST_FRAME_NULL;
    			p->fr.subclass = 0;
    			return &p->fr;
    		}
    #endif
    		ast_log(LOG_WARNING, "Error reading: %s\n", strerror(errno));
    		return NULL;
    	}
    	p->fr.data = p->buf;
    
    	if (p->mode != MODE_FXS)
    
    Mark Spencer's avatar
    Mark Spencer committed
    	switch(p->buf[0] & 0x3) {
    	case '0':
    	case '1':
    		/* Normal */
    		break;
    	case '2':
    	case '3':
    		/* VAD/CNG, only send two words */
    		res = 4;
    		break;
    	}
    
    Mark Spencer's avatar
    Mark Spencer committed
    	p->fr.samples = 240;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	p->fr.datalen = res;
    
    	p->fr.frametype = p->lastinput <= AST_FORMAT_MAX_AUDIO ?
                              AST_FRAME_VOICE : 
    			  p->lastinput <= AST_FORMAT_PNG ? AST_FRAME_IMAGE 
    			  : AST_FRAME_VIDEO;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	p->fr.subclass = p->lastinput;
    	p->fr.offset = AST_FRIENDLY_OFFSET;
    
    	/* Byteswap from little-endian to native-endian */
    	if (p->fr.subclass == AST_FORMAT_SLINEAR)
    		ast_frame_byteswap_le(&p->fr);
    
    Mark Spencer's avatar
    Mark Spencer committed
    	return &p->fr;
    }
    
    
    static int phone_write_buf(struct phone_pvt *p, const char *buf, int len, int frlen, int swap)
    
    Mark Spencer's avatar
    Mark Spencer committed
    {
    	int res;
    	/* Store as much of the buffer as we can, then write fixed frames */
    	int space = sizeof(p->obuf) - p->obuflen;
    	/* Make sure we have enough buffer space to store the frame */
    	if (space < len)
    		len = space;
    
    		ast_swapcopy_samples(p->obuf+p->obuflen, buf, len/2);
    
    Mark Spencer's avatar
    Mark Spencer committed
    	p->obuflen += len;
    	while(p->obuflen > frlen) {
    		res = write(p->fd, p->obuf, frlen);
    		if (res != frlen) {
    			if (res < 1) {
    
    Mark Spencer's avatar
    Mark Spencer committed
    /*
     * Card is in non-blocking mode now and it works well now, but there are
     * lot of messages like this. So, this message is temporarily disabled.
     */
    				return 0;
    
    Mark Spencer's avatar
    Mark Spencer committed
    			} else {
    				ast_log(LOG_WARNING, "Only wrote %d of %d bytes\n", res, frlen);
    			}
    		}
    		p->obuflen -= frlen;
    		/* Move memory if necessary */
    		if (p->obuflen) 
    			memmove(p->obuf, p->obuf + frlen, p->obuflen);
    	}
    	return len;
    }
    
    
    static int phone_send_text(struct ast_channel *ast, const char *text)
    
    {
        int length = strlen(text);
    
        return phone_write_buf(ast->tech_pvt, text, length, length, 0) == 
    
               length ? 0 : -1;
    
    Mark Spencer's avatar
    Mark Spencer committed
    static int phone_write(struct ast_channel *ast, struct ast_frame *frame)
    {
    
    	struct phone_pvt *p = ast->tech_pvt;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	int res;
    	int maxfr=0;
    	char *pos;
    	int sofar;
    	int expected;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	int codecset = 0;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	char tmpbuf[4];
    	/* Write a frame of (presumably voice) data */
    
    	if (frame->frametype != AST_FRAME_VOICE && p->mode != MODE_FXS) {
    
    Mark Spencer's avatar
    Mark Spencer committed
    		if (frame->frametype != AST_FRAME_IMAGE)
    			ast_log(LOG_WARNING, "Don't know what to do with  frame type '%d'\n", frame->frametype);
    		return 0;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	}
    
    Mark Spencer's avatar
    Mark Spencer committed
    	if (!(frame->subclass &
    
    		(AST_FORMAT_G723_1 | AST_FORMAT_SLINEAR | AST_FORMAT_ULAW)) && 
    	    p->mode != MODE_FXS) {
    
    Mark Spencer's avatar
    Mark Spencer committed
    		ast_log(LOG_WARNING, "Cannot handle frames in %d format\n", frame->subclass);
    		return -1;
    	}
    
    Mark Spencer's avatar
    Mark Spencer committed
    #if 0
    
    Mark Spencer's avatar
    Mark Spencer committed
    	/* If we're not in up mode, go into up mode now */
    
    Mark Spencer's avatar
    Mark Spencer committed
    	if (ast->_state != AST_STATE_UP) {
    		ast_setstate(ast, AST_STATE_UP);
    
    Mark Spencer's avatar
    Mark Spencer committed
    		phone_setup(ast);
    	}
    
    Mark Spencer's avatar
    Mark Spencer committed
    #else
    	if (ast->_state != AST_STATE_UP) {
    		/* Don't try tos end audio on-hook */
    		return 0;
    	}
    #endif	
    
    Mark Spencer's avatar
    Mark Spencer committed
    	if (frame->subclass == AST_FORMAT_G723_1) {
    		if (p->lastformat != AST_FORMAT_G723_1) {
    			ioctl(p->fd, PHONE_PLAY_STOP);
    			ioctl(p->fd, PHONE_REC_STOP);
    			if (ioctl(p->fd, PHONE_PLAY_CODEC, G723_63)) {
    				ast_log(LOG_WARNING, "Unable to set G723.1 mode\n");
    				return -1;
    			}
    			if (ioctl(p->fd, PHONE_REC_CODEC, G723_63)) {
    				ast_log(LOG_WARNING, "Unable to set G723.1 mode\n");
    				return -1;
    			}
    			p->lastformat = AST_FORMAT_G723_1;
    			p->lastinput = AST_FORMAT_G723_1;
    			/* Reset output buffer */
    			p->obuflen = 0;
    
    Mark Spencer's avatar
    Mark Spencer committed
    			codecset = 1;
    
    Mark Spencer's avatar
    Mark Spencer committed
    		}
    		if (frame->datalen > 24) {
    			ast_log(LOG_WARNING, "Frame size too large for G.723.1 (%d bytes)\n", frame->datalen);
    			return -1;
    		}
    		maxfr = 24;
    	} else if (frame->subclass == AST_FORMAT_SLINEAR) {
    		if (p->lastformat != AST_FORMAT_SLINEAR) {
    			ioctl(p->fd, PHONE_PLAY_STOP);
    			ioctl(p->fd, PHONE_REC_STOP);
    			if (ioctl(p->fd, PHONE_PLAY_CODEC, LINEAR16)) {
    				ast_log(LOG_WARNING, "Unable to set 16-bit linear mode\n");
    				return -1;
    			}
    			if (ioctl(p->fd, PHONE_REC_CODEC, LINEAR16)) {
    				ast_log(LOG_WARNING, "Unable to set 16-bit linear mode\n");
    				return -1;
    			}
    			p->lastformat = AST_FORMAT_SLINEAR;
    			p->lastinput = AST_FORMAT_SLINEAR;
    
    Mark Spencer's avatar
    Mark Spencer committed
    			codecset = 1;
    
    Mark Spencer's avatar
    Mark Spencer committed
    			/* Reset output buffer */
    			p->obuflen = 0;
    		}
    		maxfr = 480;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	} else if (frame->subclass == AST_FORMAT_ULAW) {
    		if (p->lastformat != AST_FORMAT_ULAW) {
    			ioctl(p->fd, PHONE_PLAY_STOP);
    			ioctl(p->fd, PHONE_REC_STOP);
    			if (ioctl(p->fd, PHONE_PLAY_CODEC, ULAW)) {
    				ast_log(LOG_WARNING, "Unable to set uLaw mode\n");
    				return -1;
    			}
    			if (ioctl(p->fd, PHONE_REC_CODEC, ULAW)) {
    				ast_log(LOG_WARNING, "Unable to set uLaw mode\n");
    				return -1;
    			}
    			p->lastformat = AST_FORMAT_ULAW;
    			p->lastinput = AST_FORMAT_ULAW;
    			codecset = 1;
    			/* Reset output buffer */
    			p->obuflen = 0;
    		}
    		maxfr = 240;
    
    	} else {
    		if (p->lastformat != frame->subclass) {
    			ioctl(p->fd, PHONE_PLAY_STOP);
    			ioctl(p->fd, PHONE_REC_STOP);
    			if (ioctl(p->fd, PHONE_PLAY_CODEC, frame->subclass)) {
    				ast_log(LOG_WARNING, "Unable to set %d mode\n",
    					frame->subclass);
    				return -1;
    			}
    			if (ioctl(p->fd, PHONE_REC_CODEC, frame->subclass)) {
    				ast_log(LOG_WARNING, "Unable to set %d mode\n",
    					frame->subclass);
    				return -1;
    			}
    			p->lastformat = frame->subclass;
    			p->lastinput = frame->subclass;
    			codecset = 1;
    			/* Reset output buffer */
    			p->obuflen = 0;
    		}
    		maxfr = 480;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	}
    
     	if (codecset) {
    
    Mark Spencer's avatar
    Mark Spencer committed
    		ioctl(p->fd, PHONE_REC_DEPTH, 3);
    		ioctl(p->fd, PHONE_PLAY_DEPTH, 3);
    		if (ioctl(p->fd, PHONE_PLAY_START)) {
    			ast_log(LOG_WARNING, "Failed to start playback\n");
    			return -1;
    		}
    		if (ioctl(p->fd, PHONE_REC_START)) {
    			ast_log(LOG_WARNING, "Failed to start recording\n");
    			return -1;
    		}
    
    Mark Spencer's avatar
    Mark Spencer committed
    	}
    
    	/* If we get here, we have a frame of Appropriate data */
    
    Mark Spencer's avatar
    Mark Spencer committed
    	sofar = 0;
    	pos = frame->data;
    	while(sofar < frame->datalen) {
    		/* Write in no more than maxfr sized frames */
    		expected = frame->datalen - sofar;
    		if (maxfr < expected)
    			expected = maxfr;
    		/* XXX Internet Phone Jack does not handle the 4-byte VAD frame properly! XXX 
    		   we have to pad it to 24 bytes still.  */
    		if (frame->datalen == 4) {
    			if (p->silencesupression) {
    				memset(tmpbuf + 4, 0, sizeof(tmpbuf) - 4);
    				memcpy(tmpbuf, frame->data, 4);
    				expected = 24;
    
    				res = phone_write_buf(p, tmpbuf, expected, maxfr, 0);
    
    Mark Spencer's avatar
    Mark Spencer committed
    			}
    			res = 4;
    			expected=4;
    		} else {
    
    			int swap = 0;
    #if __BYTE_ORDER == __BIG_ENDIAN
    			if (frame->subclass == AST_FORMAT_SLINEAR)
    				swap = 1; /* Swap big-endian samples to little-endian as we copy */
    #endif
    			res = phone_write_buf(p, pos, expected, maxfr, swap);
    
    Mark Spencer's avatar
    Mark Spencer committed
    		}
    		if (res != expected) {
    
    Mark Spencer's avatar
    Mark Spencer committed
    			if ((errno != EAGAIN) && (errno != EINTR)) {
    
    Mark Spencer's avatar
    Mark Spencer committed
    				if (res < 0) 
    					ast_log(LOG_WARNING, "Write returned error (%s)\n", strerror(errno));
    	/*
    	 * Card is in non-blocking mode now and it works well now, but there are
    	 * lot of messages like this. So, this message is temporarily disabled.
    	 */
    
    Mark Spencer's avatar
    Mark Spencer committed
    #if 0
    
    Mark Spencer's avatar
    Mark Spencer committed
    				else
    					ast_log(LOG_WARNING, "Only wrote %d of %d bytes\n", res, frame->datalen);
    
    Mark Spencer's avatar
    Mark Spencer committed
    #endif
    
    Mark Spencer's avatar
    Mark Spencer committed
    				return -1;
    			} else /* Pretend it worked */
    				res = expected;
    
    Mark Spencer's avatar
    Mark Spencer committed
    		}
    		sofar += res;
    		pos += res;
    	}
    	return 0;
    }
    
    static struct ast_channel *phone_new(struct phone_pvt *i, int state, char *context)
    {
    	struct ast_channel *tmp;
    
    	struct phone_codec_data codec;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	tmp = ast_channel_alloc(1);
    
    Mark Spencer's avatar
    Mark Spencer committed
    	if (tmp) {
    
    		ast_string_field_build(tmp, name, "Phone/%s", i->dev + 5);
    
    Mark Spencer's avatar
    Mark Spencer committed
    		tmp->fds[0] = i->fd;
    
    Mark Spencer's avatar
    Mark Spencer committed
    		/* XXX Switching formats silently causes kernel panics XXX */
    
    		if (i->mode == MODE_FXS &&
    		    ioctl(i->fd, PHONE_QUERY_CODEC, &codec) == 0) {
    			if (codec.type == LINEAR16)
    				tmp->nativeformats =
    
    				tmp->rawreadformat =
    				tmp->rawwriteformat =
    
    				AST_FORMAT_SLINEAR;
    			else {
    				tmp->nativeformats =
    
    				tmp->rawreadformat =
    				tmp->rawwriteformat =
    
    				prefformat & ~AST_FORMAT_SLINEAR;
    			}
    		}
    		else {
    			tmp->nativeformats = prefformat;
    
    			tmp->rawreadformat = prefformat;
    			tmp->rawwriteformat = prefformat;
    
    Mark Spencer's avatar
    Mark Spencer committed
    		ast_setstate(tmp, state);
    
    Mark Spencer's avatar
    Mark Spencer committed
    		if (state == AST_STATE_RING)
    			tmp->rings = 1;
    
    Mark Spencer's avatar
    Mark Spencer committed
    		strncpy(tmp->context, context, sizeof(tmp->context)-1);
    
    Mark Spencer's avatar
    Mark Spencer committed
    			strncpy(tmp->exten, i->ext, sizeof(tmp->exten)-1);
    
    Mark Spencer's avatar
    Mark Spencer committed
    		else
    			strncpy(tmp->exten, "s",  sizeof(tmp->exten) - 1);
    
    			ast_string_field_set(tmp, language, i->language);
    
    		if (!ast_strlen_zero(i->cid_num))
    			tmp->cid.cid_num = strdup(i->cid_num);
    		if (!ast_strlen_zero(i->cid_name))
    			tmp->cid.cid_name = strdup(i->cid_name);
    
    Mark Spencer's avatar
    Mark Spencer committed
    		i->owner = tmp;
    
    Mark Spencer's avatar
    Mark Spencer committed
    		usecnt++;
    
    Mark Spencer's avatar
    Mark Spencer committed
    		ast_update_use_count();
    		if (state != AST_STATE_DOWN) {
    			if (state == AST_STATE_RING) {
    
    Mark Spencer's avatar
    Mark Spencer committed
    				ioctl(tmp->fds[0], PHONE_RINGBACK);
    
    Mark Spencer's avatar
    Mark Spencer committed
    				i->cpt = 1;
    
    Mark Spencer's avatar
    Mark Spencer committed
    			}
    			if (ast_pbx_start(tmp)) {
    				ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
    				ast_hangup(tmp);
    			}
    		}
    	} else
    		ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
    	return tmp;
    }
    
    static void phone_mini_packet(struct phone_pvt *i)
    {
    	int res;
    	char buf[1024];
    	/* Ignore stuff we read... */
    	res = read(i->fd, buf, sizeof(buf));
    	if (res < 1) {
    
    		ast_log(LOG_WARNING, "Read returned %d: %s\n", res, strerror(errno));
    
    Mark Spencer's avatar
    Mark Spencer committed
    		return;
    	}
    }
    
    static void phone_check_exception(struct phone_pvt *i)
    {
    	int offhook=0;
    	char digit[2] = {0 , 0};
    	union telephony_exception phonee;
    	/* XXX Do something XXX */
    #if 0
    	ast_log(LOG_DEBUG, "Exception!\n");
    #endif
    	phonee.bytes = ioctl(i->fd, PHONE_EXCEPTION);
    	if (phonee.bits.dtmf_ready)  {
    		digit[0] = ioctl(i->fd, PHONE_GET_DTMF_ASCII);
    
    		if (i->mode == MODE_DIALTONE || i->mode == MODE_FXS) {
    
    Mark Spencer's avatar
    Mark Spencer committed
    			ioctl(i->fd, PHONE_PLAY_STOP);
    			ioctl(i->fd, PHONE_REC_STOP);
    			ioctl(i->fd, PHONE_CPT_STOP);
    			i->dialtone = 0;
    			if (strlen(i->ext) < AST_MAX_EXTENSION - 1)
    
    				strncat(i->ext, digit, sizeof(i->ext) - strlen(i->ext) - 1);
    
    			if ((i->mode != MODE_FXS ||
    			     !(phonee.bytes = ioctl(i->fd, PHONE_EXCEPTION)) ||
    			     !phonee.bits.dtmf_ready) &&
    			    ast_exists_extension(NULL, i->context, i->ext, 1, i->cid_num)) {
    
    Mark Spencer's avatar
    Mark Spencer committed
    				/* It's a valid extension in its context, get moving! */
    				phone_new(i, AST_STATE_RING, i->context);
    				/* No need to restart monitor, we are the monitor */
    
    			} else if (!ast_canmatch_extension(NULL, i->context, i->ext, 1, i->cid_num)) {
    
    Mark Spencer's avatar
    Mark Spencer committed
    				/* There is nothing in the specified extension that can match anymore.
    				   Try the default */
    
    				if (ast_exists_extension(NULL, "default", i->ext, 1, i->cid_num)) {
    
    Mark Spencer's avatar
    Mark Spencer committed
    					/* Check the default, too... */
    					phone_new(i, AST_STATE_RING, "default");
    					/* XXX This should probably be justified better XXX */
    
    				}  else if (!ast_canmatch_extension(NULL, "default", i->ext, 1, i->cid_num)) {
    
    Mark Spencer's avatar
    Mark Spencer committed
    					/* It's not a valid extension, give a busy signal */
    					if (option_debug)
    						ast_log(LOG_DEBUG, "%s can't match anything in %s or default\n", i->ext, i->context);
    					ioctl(i->fd, PHONE_BUSY);
    
    Mark Spencer's avatar
    Mark Spencer committed
    					i->cpt = 1;
    
    Mark Spencer's avatar
    Mark Spencer committed
    				}
    			}
    #if 0
    			ast_verbose("Extension is %s\n", i->ext);
    #endif
    		}
    	}
    	if (phonee.bits.hookstate) {
    		offhook = ioctl(i->fd, PHONE_HOOKSTATE);
    		if (offhook) {
    			if (i->mode == MODE_IMMEDIATE) {
    				phone_new(i, AST_STATE_RING, i->context);
    			} else if (i->mode == MODE_DIALTONE) {
    
    Mark Spencer's avatar
    Mark Spencer committed
    				usecnt++;
    
    Mark Spencer's avatar
    Mark Spencer committed
    				ast_update_use_count();
    				/* Reset the extension */
    				i->ext[0] = '\0';
    				/* Play the dialtone */
    				i->dialtone++;
    				ioctl(i->fd, PHONE_PLAY_STOP);
    				ioctl(i->fd, PHONE_PLAY_CODEC, ULAW);
    				ioctl(i->fd, PHONE_PLAY_START);
    
    Mark Spencer's avatar
    Mark Spencer committed
    				i->lastformat = -1;
    
    Mark Spencer's avatar
    Mark Spencer committed
    			}
    		} else {
    			if (i->dialtone) {
    
    Mark Spencer's avatar
    Mark Spencer committed
    				usecnt--;
    
    Mark Spencer's avatar
    Mark Spencer committed
    				ast_update_use_count();
    			}
    			memset(i->ext, 0, sizeof(i->ext));
    
    Mark Spencer's avatar
    Mark Spencer committed
    			if (i->cpt)
    			{
    				ioctl(i->fd, PHONE_CPT_STOP);
    				i->cpt = 0;
    			}
    
    Mark Spencer's avatar
    Mark Spencer committed
    			ioctl(i->fd, PHONE_PLAY_STOP);
    			ioctl(i->fd, PHONE_REC_STOP);
    			i->dialtone = 0;
    
    Mark Spencer's avatar
    Mark Spencer committed
    			i->lastformat = -1;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	if (phonee.bits.pstn_ring) {
    
    Mark Spencer's avatar
    Mark Spencer committed
    		ast_verbose("Unit is ringing\n");
    
    Mark Spencer's avatar
    Mark Spencer committed
    		phone_new(i, AST_STATE_RING, i->context);
    	}
    
    Mark Spencer's avatar
    Mark Spencer committed
    	if (phonee.bits.caller_id)
    		ast_verbose("We have caller ID\n");
    	
    	
    }
    
    static void *do_monitor(void *data)
    {
    	fd_set rfds, efds;
    	int n, res;
    	struct phone_pvt *i;
    	int tonepos = 0;
    	/* The tone we're playing this round */
    	struct timeval tv = {0,0};
    	int dotone;
    	/* This thread monitors all the frame relay interfaces which are not yet in use
    	   (and thus do not have a separate thread) indefinitely */
    
    	while (monitor) {
    
    Mark Spencer's avatar
    Mark Spencer committed
    		/* Don't let anybody kill us right away.  Nobody should lock the interface list
    		   and wait for the monitor list, but the other way around is okay. */
    		/* Lock the interface list */