summaryrefslogtreecommitdiff
path: root/fxotune.c
diff options
context:
space:
mode:
authormarkster <markster@5390a7c7-147a-4af0-8ec9-7488f05a26cb>2005-01-14 22:17:26 +0000
committermarkster <markster@5390a7c7-147a-4af0-8ec9-7488f05a26cb>2005-01-14 22:17:26 +0000
commit50ff7491ec8c027a9c14b8b43a64cc0d7a68c0b7 (patch)
tree440908c437dd8d030d7c56c2aa6682cabc17b9c5 /fxotune.c
parentb2ccc60ebc6e1d9ccfb4dd5bf20f63504056b0c8 (diff)
Fix fxotune to find best echo path
git-svn-id: http://svn.digium.com/svn/zaptel/trunk@557 5390a7c7-147a-4af0-8ec9-7488f05a26cb
Diffstat (limited to 'fxotune.c')
-rwxr-xr-xfxotune.c150
1 files changed, 81 insertions, 69 deletions
diff --git a/fxotune.c b/fxotune.c
index 029375c..13fa647 100755
--- a/fxotune.c
+++ b/fxotune.c
@@ -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 @@ static char *usage =
#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 @@ int fill_outputdata(void)
* -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 @@ static int echo_tune(int whichzap, const char *dialstr)
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 @@ static int echo_tune(int whichzap, const char *dialstr)
}
/* 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 @@ static int echo_tune(int whichzap, const char *dialstr)
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 @@ static int echo_tune(int whichzap, const char *dialstr)
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 @@ int main (int argc , char **argv)
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]);