[Asterisk-cvs] zaptel Makefile,1.64,1.65 fxotune.c,1.8,1.9

markster at lists.digium.com markster at lists.digium.com
Fri Jan 14 16:13:38 CST 2005


Update of /usr/cvsroot/zaptel
In directory mongoose.digium.com:/tmp/cvs-serv30336

Modified Files:
	Makefile fxotune.c 
Log Message:
Fix fxotune to find best echo path


Index: Makefile
===================================================================
RCS file: /usr/cvsroot/zaptel/Makefile,v
retrieving revision 1.64
retrieving revision 1.65
diff -u -d -r1.64 -r1.65
--- Makefile	14 Jan 2005 15:18:51 -0000	1.64
+++ Makefile	14 Jan 2005 22:17:26 -0000	1.65
@@ -62,7 +62,7 @@
 MODULESKO=$(shell for x in $(MODULES); do echo "$$x.ko "; done )
 
 ZTTOOL=$(shell if [ -f /usr/include/newt.h ]; then echo zttool; fi)
-BINS=ztcfg torisatool makefw ztmonitor ztspeed $(ZTTOOL) zttest
+BINS=ztcfg torisatool makefw ztmonitor ztspeed $(ZTTOOL) zttest fxotune
 
 #PRIMARY=wcfxsusb
 PRIMARY=torisa

Index: fxotune.c
===================================================================
RCS file: /usr/cvsroot/zaptel/fxotune.c,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -d -r1.8 -r1.9
--- fxotune.c	13 Jan 2005 22:25:01 -0000	1.8
+++ fxotune.c	14 Jan 2005 22:17:26 -0000	1.9
@@ -25,10 +25,8 @@
 #include "fxotune.h"
 #include "zaptel.h"
 
-
-//static int testduration = 100;  /* Test the line for x milliseconds */
-#define TESTDURATION 100  /* Test the line for x milliseconds */
-static int readduration = 100 * 2;
+#define TESTDURATION 64			/* 64 samples of test */
+#define BUFFER_LENGTH 128			/* 128 sample buffers */
 
 static char *zappath = "/dev/zap";
 static char *configfile = "/etc/fxotune.conf";
@@ -40,47 +38,41 @@
 
 #define OUT_OF_BOUNDS(x) ((x) < 0 || (x) > 255)
 
-static short *outbuf = NULL;
-static short obufsize = TESTDURATION * 8 * 2;
+static short outbuf[TESTDURATION];
+static int debug = 0;
 
-int process_readbuf(short *ibuf, int isize)
+int process_readbuf(short *ibuf)
 {
-	int i = 0;
-	short *samples = ibuf;
-	short minsample = samples[0];
-
-	for (i = 0; i < isize/2; i++) {
-		if (samples[i] < minsample)
-			minsample = samples[i];
+	int sum=0;
+	int x;
+	for (x=0;x<BUFFER_LENGTH;x++) {
+		sum += abs(ibuf[x]);
 	}
-	return minsample;
+	return sum;
 }
 
 int fill_outputdata(void)
 {
 	int randdev;
 	int cursize = 0;
+	int needlen = TESTDURATION * 2;
 	int res;
 
 	fprintf(stdout, "Getting random impulse data\n");
-	randdev = open("/dev/random", O_RDONLY);
+	randdev = open("/dev/urandom", O_RDONLY);
 	if (randdev < 0) {
 		fprintf(stdout, "Unable to open /dev/random: %s\n", strerror(errno));
 		return -1;
 	}
-	outbuf = malloc(obufsize);
-	if (!outbuf) {
-		fprintf(stdout, "Malloc failed on outbuf.  Bad, bad, bad...\n");
-		exit(-1);
-	}
 	
-	while (cursize < obufsize) {
-		res = read(randdev, &outbuf[cursize], obufsize - cursize);
+	while (needlen) {
+		res = read(randdev, &outbuf[cursize], needlen);
 		if (res <= 0) {
 			fprintf(stdout, "WARNING: could not read from /dev/random: %s\n", strerror(errno));
 			return -1;
 		}
 		cursize += res;
+		needlen -= res;
 	}
 	
 	fprintf(stdout, "Cool, we filled the random data buffer\n");
@@ -94,16 +86,23 @@
  * -1 means the device is not an FXO module or fails */
 static int echo_tune(int whichzap, const char *dialstr)
 {
-	short bestval = 32355;
+	int bestval = -1;
 	int bestindex = -1;
-	int i = 0;
+	int i = 0, j=0;
 	int x;
 	int res = 0;
 	int total = sizeof(echo_trys) / sizeof(struct wctdm_echo_coefs);
-	short inbuf[8192];
+	short inbuf[BUFFER_LENGTH];
 	struct zt_bufferinfo bi;
 	struct zt_dialoperation dop;
-	int ibufsize = readduration * 8 * 2;
+	struct wctdm_echo_coefs coefs;
+
+	/* Set echo settings */
+	memset(&coefs, 0, sizeof(coefs));
+	if (ioctl(whichzap, WCTDM_SET_ECHOTUNE, &coefs)) {
+		fprintf(stdout, "Skipping non-TDM / non-FXO\n");
+		return -1;
+	}
 
 	x = 1;
 	if (ioctl(whichzap, ZT_SETLINEAR, &x)) {
@@ -117,7 +116,7 @@
 		return -1;
 	}
 	bi.numbufs = 2;
-	bi.bufsize = 128;
+	bi.bufsize = BUFFER_LENGTH;
 	bi.txbufpolicy = ZT_POLICY_IMMEDIATE;
 	bi.rxbufpolicy = ZT_POLICY_IMMEDIATE;
 	if (ioctl(whichzap, ZT_SET_BUFINFO, &bi)) {
@@ -126,7 +125,7 @@
 	}
 
 	/* XXX May need to put in a loop XXX */
-	{
+	for (j=0;j<(total + 15)/16;j++) {
 		printf("Going off hook!\n");
 		/* Take off hook */
 		x = ZT_OFFHOOK;
@@ -134,7 +133,8 @@
 			fprintf(stdout, "Unable to set hook state.\n");
 			return -1;
 		}
-		printf("Off hook!\n");
+		sleep(2);
+		printf("Off hook (event = %d)!\n", x);
 		memset(&dop, 0, sizeof(dop));
 		dop.op = ZT_DIAL_OP_REPLACE;
 		dop.dialstr[0] = 'T';
@@ -144,54 +144,65 @@
 			fprintf(stderr, "Unable to dial!\n");
 			return -1;
 		}
+		sleep(2);
 		printf("Finished Dialing...\n");
-		sleep(5);
-		return 0;
-	}
-
-	for (i = 0; i < total; i++) {
-		/* Set echo settings */
-		if (ioctl(whichzap, WCTDM_SET_ECHOTUNE, &echo_trys[i])) {
-			fprintf(stdout, "Unable to set echo params: %s\n", strerror(errno));
-			return -1;
-		}
 
+		for (i = j * 16; (i < total) && (i < (j+1) * 16); i++) {
+			/* Set echo settings */
+			if (ioctl(whichzap, WCTDM_SET_ECHOTUNE, &echo_trys[i])) {
+				fprintf(stderr, "Unable to set echo params: %s\n", strerror(errno));
+				return -1;
+			}
 	
-		/* write samples */
-		res = write(whichzap, outbuf, obufsize);
-		if (res < 0) {
-			fprintf(stdout, "Unable to write: %s\n", strerror(errno));
-			return -1;
-		}
+			x = ZT_FLUSH_READ | ZT_FLUSH_WRITE | ZT_FLUSH_EVENT;
+			if (ioctl(whichzap, ZT_FLUSH, &x)) {
+				fprintf(stderr, "Unable to flush I/O: %s\n", strerror(errno));
+				return -1;
+			}
+	
+			/* write samples */
+			res = write(whichzap, outbuf, TESTDURATION * 2);
+			if (res < 0) {
+				fprintf(stdout, "Unable to write: %s\n", strerror(errno));
+				return -1;
+			}
 
-		if (res != obufsize) {
-			fprintf(stdout, "Only could write %d of %d bytes.\n", res, obufsize);
-			return -1;
-		}
+			if (res != TESTDURATION * 2) {
+				fprintf(stdout, "Only could write %d of %d bytes.\n", res, TESTDURATION * 2);
+				return -1;
+			}
 
-		res = read(whichzap, inbuf, ibufsize);
-		if (res < 0) {
-			fprintf(stdout, "Error in read: %s\n", strerror(errno));
-			return -1;
-		}
+			res = read(whichzap, inbuf, BUFFER_LENGTH * 2);
+			if (res < 0) {
+				fprintf(stdout, "Error in read: %s\n", strerror(errno));
+				return -1;
+			}
 
-		if (res != ibufsize) {
-			fprintf(stdout, "Only could read %d of %d bytes.\n", res, ibufsize);
-			if (res > 0)
-				ibufsize = res;
-			else  {
-				fprintf(stdout, "Cannot read from device\n");
+			if (res != BUFFER_LENGTH * 2) {
+				fprintf(stdout, "Only could read %d of %d bytes.\n", res, BUFFER_LENGTH * 2);
 				return -1;
 			}
-		}
 		
-		res = process_readbuf(inbuf, ibufsize);
-		/* Check to see if the echo values */
-		if (res < bestval) {
-			bestval = res;
-			bestindex = i;
+			res = process_readbuf(inbuf);
+			printf("Test %d: %d\n", i + 1, res);
+			/* Check to see if the echo values */
+			if ((res < bestval) || (bestval < 0)) {
+				bestval = res;
+				bestindex = i;
+			}
+			usleep(100000);
+		}
+		printf("Going on hook!\n");
+		/* Take off hook */
+		x = ZT_ONHOOK;
+		if(ioctl(whichzap, ZT_HOOK, &x)) {
+			fprintf(stdout, "Unable to set hook state.\n");
+			return -1;
 		}
+		sleep(2);
+		printf("On hook (event = %d)!\n", x);
 	}
+	printf("Best is %d from index %d\n", bestval, bestindex);
 
 	return 0;
 
@@ -288,8 +299,9 @@
 
 			fd = open(zapdev, O_RDWR);
 			if (fd < 0) {
-				fprintf(stdout, "open(%s): %s\n", zapdev, strerror(errno));
-				return -1;
+				if (debug)
+					fprintf(stdout, "%s absent: %s\n", zapdev, strerror(errno));
+				continue;
 			}
 
 			res = echo_tune(fd, argv[2]);




More information about the svn-commits mailing list