Mon Jun 21 12:05:46 CEST 2004
--- asterisk-cvs-040621/asterisk/include/asterisk/vmodem.h	2004-06-21 06:29:50.000000000 +0200
+++ asterisk-cvs-040621-isdn/asterisk/include/asterisk/vmodem.h	2004-06-21 12:02:42.000000000 +0200
@@ -115,6 +115,16 @@ struct ast_modem_pvt {
 	unsigned int group;
 	/*! Caller ID if available */
 	char cid[AST_MAX_EXTENSION];	
+	/*! Allow dtmf from network outband */
+	int allowoutbanddtmf;
+	/*! Let other Asterisk code generate inband DTMF in ISDN stream */
+	int generateinbanddtmf;
+	/*! Let asterisk detect DTMF inband from ISDN network */
+	int detectinband;
+	/*! Relaxed dtmf detection */
+	int relaxdtmf;
+	/*! DSP for DTMF detection */
+	struct ast_dsp *dsp;
 	/*! Dialed Number if available */
 	char dnid[AST_MAX_EXTENSION];	
 	/*! Modem initialization String */
--- asterisk-cvs-040621/asterisk/channels/chan_modem.c	2004-06-21 06:29:50.000000000 +0200
+++ asterisk-cvs-040621-isdn/asterisk/channels/chan_modem.c	2004-06-21 11:51:49.000000000 +0200
@@ -63,6 +63,19 @@ static char msn[AST_MAX_EXTENSION]="";
 /* Default Listen */
 static char incomingmsn[AST_MAX_EXTENSION]="";
 
+/* Default skip dtmf */
+static int allowoutbanddtmf = 1;
+
+/* Default generate inband dtmf */
+static int generateinbanddtmf = 0;
+
+/* Default asterisk detection of DTMF inband from ISDN network */
+static int detectinband = 0;
+/* Default Relaxed dtmf detection */
+static int relaxdtmf = 0;
+
+struct ast_dsp *dsp = NULL;
+
 /* Default valid outgoing MSN */
 static char outgoingmsn[AST_MAX_EXTENSION]="";
 
@@ -711,6 +724,10 @@ static struct ast_modem_pvt *mkif(char *
 		strncpy(tmp->language, language, sizeof(tmp->language)-1);
 		strncpy(tmp->msn, msn, sizeof(tmp->msn)-1);
 		strncpy(tmp->incomingmsn, incomingmsn, sizeof(tmp->incomingmsn)-1);
+		tmp->allowoutbanddtmf = allowoutbanddtmf;
+		tmp->generateinbanddtmf = generateinbanddtmf;
+		tmp->detectinband = detectinband;
+		tmp->relaxdtmf = relaxdtmf;
 		snprintf(tmp->outgoingmsn, sizeof(tmp->outgoingmsn), ",%s,", outgoingmsn);
 		strncpy(tmp->dev, iface, sizeof(tmp->dev)-1);
 		/* Maybe in the future we want to allow variable
@@ -972,6 +989,14 @@ int load_module()
 			strncpy(msn, v->value, sizeof(msn)-1);
 		} else if (!strcasecmp(v->name, "incomingmsn")) {
 			strncpy(incomingmsn, v->value, sizeof(incomingmsn)-1);
+		} else if (!strcasecmp(v->name, "allowoutbanddtmf")) {
+			allowoutbanddtmf = atoi(v->value);
+		} else if (!strcasecmp(v->name, "generateinbanddtmf")) {
+			generateinbanddtmf = atoi(v->value);
+		} else if (!strcasecmp(v->name, "detectinband")) {
+			detectinband = atoi(v->value);
+		} else if (!strcasecmp(v->name, "relaxdtmf")) {
+			relaxdtmf = atoi(v->value);
 		} else if (!strcasecmp(v->name, "outgoingmsn")) {
 			strncpy(outgoingmsn, v->value, sizeof(outgoingmsn)-1);
 		} else if (!strcasecmp(v->name, "language")) {
--- asterisk-cvs-040621/asterisk/channels/chan_modem_i4l.c	2004-06-21 06:29:50.000000000 +0200
+++ asterisk-cvs-040621-isdn/asterisk/channels/chan_modem_i4l.c	2004-06-21 12:01:13.000000000 +0200
@@ -24,6 +24,7 @@
 #include <asterisk/frame.h>
 #include <asterisk/logger.h>
 #include <asterisk/options.h>
+#include <asterisk/dsp.h>
 #include "alaw.h"
 
 #define STATE_COMMAND 	0
@@ -94,6 +94,21 @@ static int i4l_startrec(struct ast_modem
 		return -1;
 	}
 	p->ministate = STATE_VOICE;
+	
+	/*  let ast dsp detect dtmf */
+	if (p->detectinband) {
+		if (p->dsp) {
+			ast_log(LOG_DEBUG, "Already have a dsp on %s?\n", p->dev);
+		} else {
+			p->dsp = ast_dsp_new();
+			if (p->dsp) {
+				ast_log(LOG_DEBUG, "Detecting DTMF inband with sw DSP on %s\n",p->dev);
+				ast_dsp_set_features(p->dsp, DSP_FEATURE_DTMF_DETECT);
+				ast_dsp_digitmode(p->dsp, DSP_DIGITMODE_DTMF | p->relaxdtmf);
+			}
+		}
+	}
+
 	return 0;
 }
 
@@ -277,7 +292,7 @@ static struct ast_frame *i4l_handle_esca
 	case '9':
 	case '*':
 	case '#':
-		ast_log(LOG_DEBUG, "DTMF: '%c' (%d)\n", esc, esc);
+		ast_log(LOG_DEBUG, "Detected outband DTMF digit: '%c' (%d)\n", esc, esc);
 		p->fr.frametype=AST_FRAME_DTMF;
 		p->fr.subclass=esc;
 		return &p->fr;
@@ -400,6 +415,13 @@ static struct ast_frame *i4l_read(struct
 						what else is there, the connection is dead. */
 						if (!f)
 							return NULL;
+						/* undo frame if useless due to config */
+						if(! p->allowoutbanddtmf && f->frametype==AST_FRAME_DTMF) {
+							ast_log(LOG_DEBUG, "Skipped outband DTMF digit '%c' (set allowoutbanddtmf in modem.conf)\n",f->subclass);
+							f=0;
+						}
+						  
+
 					} else {
 						*(b++) = ALAW2INT(result[x] & 0xff);
 						p->obuflen += 2;
@@ -421,6 +443,16 @@ static struct ast_frame *i4l_read(struct
 		p->fr.offset = AST_FRIENDLY_OFFSET;
 		p->fr.src = __FUNCTION__;
 		p->obuflen = 0;
+
+		/* process with dsp */
+		if (p->dsp) {
+			f = ast_dsp_process(p->owner, p->dsp, &p->fr);
+			if (f && (f->frametype == AST_FRAME_DTMF)) {
+				ast_log(LOG_DEBUG, "Detected inband DTMF digit: %c on %s\n", f->subclass, p->dev);
+				return f;
+			}
+		}
+		
 		return &p->fr;
 	}
 	return NULL;
@@ -510,6 +542,21 @@ static int i4l_answer(struct ast_modem_p
 		return -1;
 	}
 	p->ministate = STATE_VOICE;
+
+	/*  let ast dsp detect dtmf */
+	if (p->detectinband) {
+		if (p->dsp) {
+			ast_log(LOG_DEBUG, "Already have a dsp on %s?\n", p->dev);
+		} else {
+			p->dsp = ast_dsp_new();
+			if (p->dsp) {
+				ast_log(LOG_DEBUG, "Detecting DTMF inband with sw DSP on %s\n",p->dev);
+				ast_dsp_set_features(p->dsp, DSP_FEATURE_DTMF_DETECT);
+				ast_dsp_digitmode(p->dsp, DSP_DIGITMODE_DTMF | p->relaxdtmf);
+			}
+		}
+	}
+
 	return 0;
 }
 
@@ -520,6 +567,11 @@ static int i4l_dialdigit(struct ast_mode
 		c[0] = CHAR_DLE;
 		c[1] = digit;
 		write(p->fd, c, 2);
+		ast_log(LOG_DEBUG, "Send ISDN out-of-band DTMF %c\n",digit);
+		if(p->generateinbanddtmf) {
+			ast_log(LOG_DEBUG, "Generating inband DTMF as well\n");
+			return -1;
+		}
 	} else
 		ast_log(LOG_DEBUG, "Asked to send digit but call not up on %s\n", p->dev);
 	return 0;
@@ -567,9 +619,14 @@ static int i4l_hangup(struct ast_modem_p
 	char dummy[50];
 	int dtr = TIOCM_DTR;
 
+	/* free the memory used by the DSP */
+	if (p->dsp) {
+		ast_dsp_free(p->dsp);
+		p->dsp = NULL;
+	}
+
 	/* down DTR to hangup modem */
 	ioctl(p->fd, TIOCMBIC, &dtr);
-
 	/* Read anything outstanding */
 	while(read(p->fd, dummy, sizeof(dummy)) > 0);
 
--- asterisk-cvs-040621/asterisk/configs/modem.conf.sample	2004-06-21 06:29:50.000000000 +0200
+++ asterisk-cvs-040621-isdn/asterisk/configs/modem.conf.sample	2004-06-21 12:03:50.000000000 +0200
@@ -69,6 +69,24 @@ mode=immediate
 ; number.
 ;outgoingmsn=50780023,50780024
 ;
+; Allow ISDN4Linux detection of DTMF to reach Asterisk internal
+; (ISDN4Linux sometimes detects digits when no digits are pressed,
+; and if DTMF controlling of asterisk is not used, this can be
+; set to "0".)
+;allowoutbanddtmf=1
+
+; Generate inband DTMF as well as the traditional ISDN outband DTMF
+; when digits are sent out to ISDN4Linux.
+; Is good to use when voip clients only send outband DTMF to Asterisk,
+; but inband is required to control your telephony bank or similar.
+;generateinbanddtmf=0
+
+; If Asterisk internal inband DTMF routines should be used instead 
+; of ISDN4Linux. Set allowoutbanddtmf=0 if this is used.
+;detectinband=0
+
+; Relax the DTMF detection made with detectinband
+;relaxdtmf=0
 ;
 ; two other devices, which are in group '1' and are used when an
 ; outgoing dial used: exten => s,1,Dial,Modem/g1:1234|60|r
