5
|
1 /*
|
|
2 sample.c
|
|
3 David Rowe 14 December 2006
|
|
4
|
|
5 User mode side of zaptel echo sampling system.
|
|
6
|
|
7 Compile:
|
|
8
|
|
9 gcc sample.c -o sample -Wall
|
|
10 */
|
|
11
|
|
12 /*
|
|
13 Copyright (C) 2007 David Rowe
|
|
14
|
|
15 All rights reserved.
|
|
16
|
|
17 This program is free software; you can redistribute it and/or modify
|
|
18 it under the terms of the GNU General Public License version 2, as
|
|
19 published by the Free Software Foundation.
|
|
20
|
|
21 This program is distributed in the hope that it will be useful,
|
|
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
24 GNU General Public License for more details.
|
|
25
|
|
26 You should have received a copy of the GNU General Public License
|
|
27 along with this program; if not, write to the Free Software
|
|
28 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
29 */
|
|
30
|
|
31 #include <sys/types.h>
|
|
32 #include <sys/stat.h>
|
|
33 #include <fcntl.h>
|
|
34 #include <sys/ioctl.h>
|
|
35 #include <unistd.h>
|
|
36 #include <stdio.h>
|
|
37 #include <string.h>
|
|
38 #include <stdlib.h>
|
|
39
|
|
40 #define SAMPLE_BUF_SZ 1000
|
|
41 #define FS 8000
|
|
42 #define MAX_STR 256
|
|
43 #define ZT_CHUNKSIZE 8
|
|
44
|
|
45 /* ioctls for sample */
|
|
46
|
|
47 #define SAMPLE_SET_CHANNEL 0
|
|
48 #define SAMPLE_TX_IMPULSE 1
|
|
49
|
|
50 int main(int argc, char *argv[]) {
|
|
51 int fd, len, i, j, frames;
|
|
52 short buf[3*SAMPLE_BUF_SZ];
|
|
53 short txbuf[SAMPLE_BUF_SZ];
|
|
54 short rxbuf[SAMPLE_BUF_SZ];
|
|
55 short ecbuf[SAMPLE_BUF_SZ];
|
|
56 FILE *ftx, *frx, *fec;
|
|
57 float secs;
|
|
58 int sample_ch;
|
|
59 char filename[MAX_STR];
|
|
60 short *pbuf, *ptxbuf, *prxbuf, *pecbuf;
|
|
61
|
|
62 if (argc < 4) {
|
|
63 printf("usage: %s SampleName channel(1|2|.....) length(secs)\n"
|
|
64 " [-i(impulse mode)]",
|
|
65 argv[0]);
|
|
66 exit(0);
|
|
67 }
|
|
68
|
|
69 sprintf(filename, "%s_tx.raw", argv[1]);
|
|
70 ftx = fopen(filename,"wb");
|
|
71 if (ftx == NULL) {
|
|
72 printf("Can't open tx sample file: %s\n", filename);
|
|
73 exit(1);
|
|
74 }
|
|
75
|
|
76 sprintf(filename, "%s_rx.raw", argv[1]);
|
|
77 frx = fopen(filename,"wb");
|
|
78 if (frx == NULL) {
|
|
79 printf("Can't open rx sample file: %s\n", filename);
|
|
80 exit(1);
|
|
81 }
|
|
82
|
|
83 sprintf(filename, "%s_ec.raw", argv[1]);
|
|
84 fec = fopen(filename,"wb");
|
|
85 if (frx == NULL) {
|
|
86 printf("Can't open ec sample file: %s\n", filename);
|
|
87 exit(1);
|
|
88 }
|
|
89
|
|
90 sample_ch = atoi(argv[2]);
|
|
91 if (sample_ch < 1) {
|
|
92 printf("Invalid channel: %d must be > 0\n", sample_ch);
|
|
93 exit(1);
|
|
94 }
|
|
95
|
|
96 secs = atof(argv[3]);
|
|
97 if ((secs < 0.0) || (secs > 100.0)) {
|
|
98 printf("Invalid secs %f, must be between 0 and 100\n", secs);
|
|
99 exit(1);
|
|
100 }
|
|
101 frames = (secs*FS)/SAMPLE_BUF_SZ;
|
|
102
|
|
103 fd = open("/dev/sample", O_RDWR);
|
|
104 if( fd == -1) {
|
|
105 printf("open error...\n");
|
|
106 exit(0);
|
|
107 }
|
|
108
|
|
109 ioctl(fd, SAMPLE_SET_CHANNEL, &sample_ch);
|
|
110
|
|
111 if (argc == 5) {
|
|
112 if(!strcmp(argv[4], "-i")) {
|
|
113 printf("Impulse mode enabled\n");
|
|
114 ioctl(fd, SAMPLE_TX_IMPULSE, &sample_ch);
|
|
115 }
|
|
116 }
|
|
117
|
|
118 printf("sampling Zap/%d...\n", sample_ch);
|
|
119 for(i=0; i<frames; i++) {
|
|
120 len = read(fd, buf, 3*sizeof(short)*SAMPLE_BUF_SZ);
|
|
121 if( len == -1 ) {
|
|
122 printf("read error...\n");
|
|
123 exit(1);
|
|
124 }
|
|
125
|
|
126 /* demultiplex and write to disk */
|
|
127
|
|
128 pbuf = buf;
|
|
129 ptxbuf = txbuf;
|
|
130 prxbuf = rxbuf;
|
|
131 pecbuf = ecbuf;
|
|
132 for(j=0; j<SAMPLE_BUF_SZ; j++) {
|
|
133 *ptxbuf++ = *pbuf++;
|
|
134 *prxbuf++ = *pbuf++;
|
|
135 *pecbuf++ = *pbuf++;
|
|
136 }
|
|
137 fwrite(txbuf, sizeof(short), SAMPLE_BUF_SZ, ftx);
|
|
138 fwrite(rxbuf, sizeof(short), SAMPLE_BUF_SZ, frx);
|
|
139 fwrite(ecbuf, sizeof(short), SAMPLE_BUF_SZ, fec);
|
|
140 }
|
|
141
|
|
142 close(fd);
|
|
143 fclose(ftx);
|
|
144 fclose(frx);
|
|
145 fclose(fec);
|
|
146
|
|
147 return 0;
|
|
148 }
|